simplify room store, remove dish message handlers from websocket hook

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 17:38:53 +01:00
parent aaee0f6b0d
commit f9e01f18fd
2 changed files with 8 additions and 56 deletions

View File

@@ -29,10 +29,7 @@ export function useWebSocket(roomCode: string) {
setAct,
reset,
setGameState,
addDish,
recordDishGuess,
lockPredictions,
setDishResults,
} = useRoomStore()
const send = useCallback((message: ClientMessage) => {
@@ -69,7 +66,6 @@ export function useWebSocket(roomCode: string) {
setMySessionId(msg.sessionId)
storeSession(roomCode, msg.sessionId)
} else if (sessionId) {
// Reconnected with stored session
setMySessionId(sessionId)
}
break
@@ -95,15 +91,6 @@ export function useWebSocket(roomCode: string) {
case "predictions_locked":
lockPredictions()
break
case "dish_added":
addDish(msg.dish)
break
case "dish_guess_recorded":
recordDishGuess(msg.dishId, msg.guessedCountry)
break
case "dishes_revealed":
setDishResults(msg.results)
break
case "error":
console.error("Server error:", msg.message)
break
@@ -118,7 +105,7 @@ export function useWebSocket(roomCode: string) {
ws.close()
reset()
}
}, [roomCode, setRoom, setMySessionId, setConnectionStatus, updatePlayerConnected, addPlayer, setAct, reset, setGameState, addDish, recordDishGuess, lockPredictions, setDishResults])
}, [roomCode, setRoom, setMySessionId, setConnectionStatus, updatePlayerConnected, addPlayer, setAct, reset, setGameState, lockPredictions])
return { send }
}

View File

@@ -1,10 +1,11 @@
import { create } from "zustand"
import type { RoomState, Player, GameState, Dish } from "@celebrate-esc/shared"
import type { RoomState, Player, GameState } from "@celebrate-esc/shared"
interface RoomStore {
room: RoomState | null
mySessionId: string | null
connectionStatus: "disconnected" | "connecting" | "connected"
gameState: GameState | null
setRoom: (room: RoomState) => void
setMySessionId: (sessionId: string) => void
@@ -12,19 +13,16 @@ interface RoomStore {
updatePlayerConnected: (playerId: string, connected: boolean) => void
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
reset: () => void
}
export const useRoomStore = create<RoomStore>((set) => ({
room: null,
mySessionId: null,
connectionStatus: "disconnected",
gameState: null,
setRoom: (room) => set({ room }),
setMySessionId: (sessionId) => set({ mySessionId: sessionId }),
@@ -44,7 +42,6 @@ export const useRoomStore = create<RoomStore>((set) => ({
addPlayer: (player) =>
set((state) => {
if (!state.room) return state
// Avoid duplicates
if (state.room.players.some((p) => p.id === player.id)) return state
return {
room: {
@@ -60,31 +57,8 @@ export const useRoomStore = create<RoomStore>((set) => ({
return { room: { ...state.room, currentAct: act } }
}),
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
@@ -92,15 +66,6 @@ export const useRoomStore = create<RoomStore>((set) => ({
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 })),
},
}
}),
reset: () => set({ room: null, mySessionId: null, connectionStatus: "disconnected", gameState: null }),
}))