Skip to content

Commit 35866ae

Browse files
committed
wip review
1 parent d6627c7 commit 35866ae

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

src/main/client/src/feature/game/Game.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function Game() {
6969
className="h-full">
7070
<MuteIcon />
7171
<Board gameState={gameState} setGameState={setGameState} />
72-
<GamePanel gameState={gameState} />
72+
<GamePanel gameState={gameState} setGameState={setGameState} />
7373
</div>
7474
)
7575
}

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ import {
4343
countingComplete,
4444
currentPlayer,
4545
isSelfPlay,
46+
isKibitz,
47+
moveBack,
4648
countingAgreed,
4749
gameHasEnded,
4850
} from "./state.js"
4951

50-
export const GamePanel = ({gameState}) => {
52+
export const GamePanel = ({gameState, setGameState}) => {
5153
return (
5254
<SideBar page="game">
5355
<div className="pr-3 pt-4 pl-2 h-full flex flex-col">
54-
<Panel gameState={gameState} />
56+
<Panel gameState={gameState} setGameState={setGameState} />
5557
</div>
5658
</SideBar>
5759
)
5860
}
5961

60-
function Panel({gameState}) {
62+
function Panel({gameState, setGameState}) {
6163
let {gameId} = useParams()
6264
let zoom = useViewStateStore(state => state.zoom)
6365
let setZoom = useViewStateStore(state => state.setZoom)
@@ -146,15 +148,17 @@ function Panel({gameState}) {
146148
<div className="flex-none">
147149
Move {queueLength}
148150
</div>
149-
<div className="flex-none">
150-
<Button
151-
onClick={onPass}
152-
className="py-1 px-4"
153-
disabled={gameHasEnded(gameState) || counting || currentPlayer(gameState) !== auth.name}>
154-
Pass
155-
</Button>
156-
</div>
157-
{counting && <>
151+
{!isKibitz(gameState, auth) && (
152+
<div className="flex-none">
153+
<Button
154+
onClick={onPass}
155+
className="py-1 px-4"
156+
disabled={gameHasEnded(gameState) || counting || currentPlayer(gameState) !== auth.name}>
157+
Pass
158+
</Button>
159+
</div>
160+
)}
161+
{!isKibitz(gameState, auth) && counting && <>
158162
<div className="flex-none">
159163
<Button
160164
className="py-1 px-4"
@@ -172,6 +176,15 @@ function Panel({gameState}) {
172176
</Button>
173177
</div>
174178
</>}
179+
{(gameHasEnded(gameState) || isKibitz(gameState, auth)) && (
180+
<div className="flex-none">
181+
<Button
182+
onClick={() => setGameState(moveBack(gameState))}
183+
className="py-1 px-2">
184+
Back
185+
</Button>
186+
</div>
187+
)}
175188
{result && (
176189
<div className="flex-none">
177190
{(result.w > result.b ? "W+" : "B+") + Math.abs(result.b - result.w)}

src/main/client/src/feature/game/state.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function initialState() {
2626
id: "",
2727
moves: [],
2828
baseBoard: [],
29+
viewPos: 0,
2930
dim: 0,
3031
handicap: 0,
3132
queueStatus: "behind",
@@ -68,6 +69,10 @@ export function isSelfPlay({black, white}) {
6869
return black === white
6970
}
7071

72+
export function isKibitz({black, white}, auth) {
73+
return black !== auth.name && white !== auth.name
74+
}
75+
7176
export function currentColor({moves, handicap}) {
7277
if (handicap > moves.length) {
7378
return BLACK
@@ -78,7 +83,7 @@ export function currentColor({moves, handicap}) {
7883
return moves[moves.length - 1].color ^ COLORS
7984
}
8085

81-
export function countingAgreed ({moves, myColor}) {
86+
export function countingAgreed({moves, myColor}) {
8287
if (!moves.length) {
8388
return false
8489
}
@@ -94,6 +99,43 @@ export function gameHasEnded({moves}) {
9499
return move.action === "end"
95100
}
96101

102+
export function moveBack(baseState) {
103+
return produce(baseState, (draft) => {
104+
let moves = baseState.moves
105+
let queueLength = baseState.queueLength
106+
if (!queueLength) {
107+
return
108+
}
109+
let move = moves[queueLength - 1]
110+
let baseBoard = unApply(baseState.baseBoard, move)
111+
draft.baseBoard = baseBoard
112+
draft.board = rehydrate(baseBoard)
113+
})
114+
}
115+
116+
function unApply(board, move) {
117+
let dim = board.length
118+
let result = Array(dim)
119+
for (let i = 0; i < board.length; i++) {
120+
result[i] = Array(dim)
121+
}
122+
for (let y = 0; y < board.length; y++) {
123+
for (let x = 0; x < board[y].length; x++) {
124+
if (move.x === x && move.y === y) {
125+
result[y][x] = 0
126+
} else {
127+
result[y][x] = board[y][x]
128+
}
129+
}
130+
}
131+
if (move.dead) {
132+
move.dead.forEach((x, y) => {
133+
result[y][x] = move.color ^ COLORS
134+
})
135+
}
136+
return result
137+
}
138+
97139
export function addMove(baseState, move) {
98140
return produce(baseState, (draft) => {
99141
let {action, n} = move
@@ -199,7 +241,7 @@ function createMoveData(baseBoard, moves, move, counting) {
199241
return [move, count(updated), [-1, -1]]
200242
}
201243
let [dead, updated] = updateBoard(baseBoard, move)
202-
let storedMove = {...move, dead}
244+
let storedMove = {...move, dead: dead}
203245
let forbidden = getForbidden(baseBoard, updated, storedMove)
204246
return [storedMove, updated, forbidden]
205247
}

0 commit comments

Comments
 (0)