Skip to content

Commit ac39d63

Browse files
committed
review: teleport when ctrl click
1 parent 2bf2d16 commit ac39d63

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
isKibitz,
5757
createGameState,
5858
isReviewing,
59+
teleport,
5960
} from "./state.js"
6061
import {
6162
BoardSettings,
@@ -84,8 +85,6 @@ function Board({gameState, setGameState}) {
8485
let navigate = useNavigate()
8586
let stompClient = useContext(StompContext)
8687
let auth = useAuthStore(state => state.auth)
87-
let ctrlKeyDownRef = useRef()
88-
ctrlKeyDownRef.current = ctrlKeyDown
8988
let cursorXref = useRef()
9089
cursorXref.current = cursor_x
9190
let cursorYref = useRef()
@@ -166,7 +165,7 @@ function Board({gameState, setGameState}) {
166165
return has
167166
}, [gameState, counting, board, isCursorInBounds])
168167

169-
let showMoveNumbers = ctrlKeyDownRef.current && (isKibitz(gameState, auth) || gameHasEnded(gameState))
168+
let showMoveNumbers = ctrlKeyDown && (isKibitz(gameState, auth) || gameHasEnded(gameState))
170169

171170
let context = useMemo(() => {
172171
let dim = board.length
@@ -249,6 +248,13 @@ function Board({gameState, setGameState}) {
249248
let onClick = useCallback(() => {
250249
let cursor_x = cursorXref.current
251250
let cursor_y = cursorYref.current
251+
if (showMoveNumbers) {
252+
let historyEntry = board[cursor_y][cursor_x].historyEntry
253+
if (historyEntry.n !== -1) {
254+
setGameState(teleport(gameState, historyEntry.n + 1))
255+
}
256+
return
257+
}
252258
if (gameHasEnded(gameState)) {
253259
return
254260
}
@@ -278,15 +284,15 @@ function Board({gameState, setGameState}) {
278284
x: cursor_x,
279285
y: cursor_y,
280286
}
281-
if (!isSelfPlay(gameState)) { // myColor is 0 in self play
282-
setGameState(addMove(gameState, {...move, color: myColor}))
287+
if (!isSelfPlay(gameState)) { // can't add early in self play; myColor is 0
288+
setGameState(addMove(gameState, {...move, color: myColor})) // early add move
283289
}
284290
playClickSound()
285291
stompClient.publish({
286292
destination: "/app/game/move",
287293
body: JSON.stringify(move),
288294
})
289-
}, [gameState, setGameState, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound, isCursorInBounds])
295+
}, [gameState, setGameState, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound, isCursorInBounds, showMoveNumbers])
290296

291297
useEffect(() => {
292298
if (!board.length) {

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,21 @@ export function isReviewing(baseState) {
109109
return baseState.viewPos < baseState.queueLength
110110
}
111111

112-
export function moveBack(baseState) {
113-
let viewPos = baseState.viewPos - 1
114-
if (viewPos < 0) {
115-
return baseState
112+
export function teleport(baseState, targetPos) {
113+
let viewPos = baseState.viewPos
114+
if (targetPos >= baseState.queueLength) {
115+
return goToEnd(baseState)
116116
}
117+
let diff = targetPos - viewPos
118+
let direction = Math.sign(diff)
117119
let moves = baseState.moves
118-
let move = moves[viewPos]
119-
let baseBoard = unApply(baseState.baseBoard, move)
120+
let baseBoard = baseState.baseBoard
121+
let action = diff < 0 ? unApply : updateBoard
122+
for (let i = 0; i < Math.abs(diff); i++) {
123+
let [, updated] = action(baseBoard, diff < 0 ? moves[viewPos - 1] : moves[viewPos])
124+
baseBoard = updated
125+
viewPos += direction
126+
}
120127
let historyBoard = baseState.historyBoard
121128
return produce(baseState, (draft) => {
122129
draft.baseBoard = baseBoard
@@ -131,21 +138,14 @@ export function moveBack(baseState) {
131138
})
132139
}
133140

141+
export function moveBack(baseState) {
142+
let viewPos = baseState.viewPos
143+
return teleport(baseState, viewPos - 1)
144+
}
145+
134146
export function moveForward(baseState) {
135147
let viewPos = baseState.viewPos
136-
if (viewPos - baseState.queueLength >= 0) {
137-
return goToEnd(baseState)
138-
}
139-
let moves = baseState.moves
140-
let move = moves[viewPos]
141-
let [, updated] = updateBoard(baseState.baseBoard, move)
142-
let historyBoard = baseState.historyBoard
143-
return produce(baseState, (draft) => {
144-
draft.baseBoard = updated
145-
draft.board = cheapRehydrate(updated, historyBoard)
146-
draft.viewPos = viewPos + 1
147-
draft.lastMove = move.action === "pass" ? undefined : move
148-
})
148+
return teleport(baseState, viewPos + 1)
149149
}
150150

151151
function goToEnd(baseState) {
@@ -161,6 +161,7 @@ function goToEnd(baseState) {
161161
}
162162
return produce(baseState, (draft) => {
163163
draft.board = rehydrate(baseBoard, historyBoard)
164+
draft.viewPos = baseState.queueLength
164165
})
165166
}
166167

@@ -320,7 +321,7 @@ function unApply(board, move) {
320321
result[y][x] = (move.color ^ COLORS)
321322
})
322323
}
323-
return result
324+
return [1, result]
324325
}
325326

326327
function cheapRehydrate(baseBoard, historyBoard) {

0 commit comments

Comments
 (0)