Skip to content

Commit 4c4729f

Browse files
committed
wip incremental updates
1 parent 9abd725 commit 4c4729f

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/main/client/src/Game.jsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const Game = () => {
4747
let stompClient = useContext(StompContext)
4848
let auth = useAuthStore(state => state.auth)
4949
let setGameState = useGameStore(state => state.setGameState)
50+
let addMove = useGameStore(state => state.addMove)
5051
let { board, currentColor, currentPlayer, counting, forbidden } = useGameStore(state => state.gameState)
5152
let [forbidden_x, forbidden_y] = forbidden
5253
let initialized = useRef()
@@ -192,13 +193,9 @@ export const Game = () => {
192193
return
193194
}
194195
initialized.current = true
195-
let sub1 = stompClient.subscribe("/topic/game/" + gameId, (message) => {
196-
let game = JSON.parse(message.body)
197-
setGameState(game)
198-
})
199196
let sub2 = stompClient.subscribe("/topic/move/" + gameId, (message) => {
200197
let move = JSON.parse(message.body)
201-
console.log(move) // TODO
198+
addMove(move)
202199
})
203200
doTry(async () => {
204201
let game = await tfetch("/api/game/" + gameId, {
@@ -209,10 +206,9 @@ export const Game = () => {
209206
setGameState(game)
210207
})
211208
return () => {
212-
sub1.unsubscribe()
213209
sub2.unsubscribe()
214210
}
215-
}, [setGameState, initialized, stompClient, gameId, auth])
211+
}, [setGameState, addMove, initialized, stompClient, gameId, auth])
216212
if (!board.length) {
217213
return <div>Loading...</div>
218214
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function Panel({zoom, setZoom}) {
4646
let { gameId } = useParams()
4747
let stompClient = useContext(StompContext)
4848
let auth = useAuthStore(state => state.auth)
49-
let { black, white} = useGameStore(state => state)
49+
let black = useGameStore(state => state.black)
50+
let white = useGameStore(state => state.white)
5051
let { board, currentPlayer, counting } = useGameStore(state => state.gameState)
5152
let navigate = useNavigate()
5253
let onExit = useCallback(() => {

src/main/client/src/model/board.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ export function isForbidden(board, groupInfo, currentColor) {
166166
}
167167
return true
168168
}
169+
170+
export function updateBoard(board, {color, pass, x: xx, y: yy}) {
171+
let result = []
172+
for (let y = 0; y < board.length; y++) {
173+
result[y] = board[y].slice()
174+
}
175+
if (!pass) {
176+
result[yy][xx] = color
177+
}
178+
return result
179+
}

src/main/client/src/store.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
} from "immer"
77
import {
88
BLACK,
9+
WHITE,
910
} from "./util.js"
1011
import {
1112
rehydrate,
13+
updateBoard,
1214
} from "./model/board.js"
1315

1416
export const useAuthStore = create((set) => ({
@@ -31,7 +33,10 @@ export const useAuthStore = create((set) => ({
3133
},
3234
}))
3335

34-
export const useGameStore = create((set) => ({
36+
export const useGameStore = create((set, get) => ({
37+
moves: [],
38+
baseBoard: [],
39+
queueStatus: "up_to_date",
3540
editMode: false,
3641
black: {
3742
name: "",
@@ -52,11 +57,28 @@ export const useGameStore = create((set) => ({
5257
counting: false,
5358
forbidden: [-1, -1],
5459
},
60+
addMove: (move) => {
61+
set(produce(state => {
62+
if (get().moves.length < move.n) {
63+
state.queueStatus = "behind"
64+
return
65+
}
66+
state.queueStatus = "up_to_date"
67+
state.moves.push(move)
68+
let updated = updateBoard(get().baseBoard, move)
69+
state.baseBoard = updated
70+
state.gameState.board = rehydrate(updated)
71+
state.gameState.currentColor = get().gameState.currentColor ^ (BLACK | WHITE)
72+
state.gameState.currentPlayer = get().gameState.currentPlayer === get().black.name ? get().white.name : get().black.name
73+
}))
74+
},
5575
setGameState: (game) => {
5676
set(produce(state => {
5777
state.black = game.black
5878
state.white = game.white
5979
state.editMode = game.editMode
80+
state.baseBoard = game.board
81+
state.moves = game.moves
6082
state.gameState.board = rehydrate(game.board)
6183
state.gameState.currentPlayer = game.currentPlayer
6284
state.gameState.currentColor = game.currentColor

0 commit comments

Comments
 (0)