add game state to zustand store
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { create } from "zustand"
|
||||
import type { RoomState, Player } from "@celebrate-esc/shared"
|
||||
import type { RoomState, Player, GameState, Dish } from "@celebrate-esc/shared"
|
||||
|
||||
interface RoomStore {
|
||||
room: RoomState | null
|
||||
@@ -13,6 +13,12 @@ interface RoomStore {
|
||||
addPlayer: (player: Player) => void
|
||||
setAct: (act: RoomState["currentAct"]) => void
|
||||
reset: () => void
|
||||
gameState: GameState | null
|
||||
setGameState: (gameState: GameState) => void
|
||||
addDish: (dish: Dish) => void
|
||||
recordDishGuess: (dishId: string, guessedCountry: string) => void
|
||||
lockPredictions: () => void
|
||||
setDishResults: (results: GameState["dishResults"]) => void
|
||||
}
|
||||
|
||||
export const useRoomStore = create<RoomStore>((set) => ({
|
||||
@@ -54,5 +60,47 @@ export const useRoomStore = create<RoomStore>((set) => ({
|
||||
return { room: { ...state.room, currentAct: act } }
|
||||
}),
|
||||
|
||||
reset: () => set({ room: null, mySessionId: null, connectionStatus: "disconnected" }),
|
||||
reset: () => set({ room: null, mySessionId: null, connectionStatus: "disconnected", gameState: null }),
|
||||
|
||||
gameState: null,
|
||||
setGameState: (gameState) => set({ gameState }),
|
||||
addDish: (dish) =>
|
||||
set((state) => {
|
||||
if (!state.gameState) return state
|
||||
return {
|
||||
gameState: {
|
||||
...state.gameState,
|
||||
dishes: [...state.gameState.dishes, dish],
|
||||
},
|
||||
}
|
||||
}),
|
||||
recordDishGuess: (dishId, guessedCountry) =>
|
||||
set((state) => {
|
||||
if (!state.gameState) return state
|
||||
const existing = state.gameState.myDishGuesses.filter((g) => g.dishId !== dishId)
|
||||
return {
|
||||
gameState: {
|
||||
...state.gameState,
|
||||
myDishGuesses: [...existing, { dishId, playerId: "", guessedCountry }],
|
||||
},
|
||||
}
|
||||
}),
|
||||
lockPredictions: () =>
|
||||
set((state) => {
|
||||
if (!state.gameState) return state
|
||||
return {
|
||||
gameState: { ...state.gameState, predictionsLocked: true },
|
||||
}
|
||||
}),
|
||||
setDishResults: (results) =>
|
||||
set((state) => {
|
||||
if (!state.gameState) return state
|
||||
return {
|
||||
gameState: {
|
||||
...state.gameState,
|
||||
dishResults: results,
|
||||
dishes: state.gameState.dishes.map((d) => ({ ...d, revealed: true })),
|
||||
},
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user