From 48da968cd65a5b89003fee4c7da0d4d17fadc889 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 30 Jan 2026 04:57:35 +0000 Subject: [PATCH] fix: Fix idle workers display showing player1's workers when spectating - Fix gameStore.syncWithGameSetup() to properly handle spectator mode by setting playerId to empty string when localPlayerId is null - Add syncWithGameSetup() call when game loading completes to ensure playerId is synced with the actual game setup state - Reset idle worker count to 0 in IdleWorkerButton when transitioning to spectator mode to prevent stale data display When spectating all-AI games, the playerId in gameStore was staying as 'player1' (the default) because syncWithGameSetup() only updated when localPlayerId was truthy. This caused the IdleWorkerButton to show player 1's workers even though the user was spectating. https://claude.ai/code/session_012UE6oa6ZDLCFVLJYwVW3b5 --- src/components/game/IdleWorkerButton.tsx | 7 +++++-- src/components/game/WebGPUGameCanvas.tsx | 2 ++ src/store/gameStore.ts | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/game/IdleWorkerButton.tsx b/src/components/game/IdleWorkerButton.tsx index d085e029..0b229c39 100644 --- a/src/components/game/IdleWorkerButton.tsx +++ b/src/components/game/IdleWorkerButton.tsx @@ -18,8 +18,11 @@ export function IdleWorkerButton() { // Update idle worker count via event-driven approach + throttled polling // Note: Hooks must be called unconditionally, so spectator check is after all hooks useEffect(() => { - // Skip for spectators - if (isSpectator) return; + // Skip for spectators and reset count + if (isSpectator) { + setIdleWorkerCount(0); + return; + } const bridge = getWorkerBridge(); if (!bridge) return; diff --git a/src/components/game/WebGPUGameCanvas.tsx b/src/components/game/WebGPUGameCanvas.tsx index 04040c58..538f8374 100644 --- a/src/components/game/WebGPUGameCanvas.tsx +++ b/src/components/game/WebGPUGameCanvas.tsx @@ -232,6 +232,8 @@ export function WebGPUGameCanvas() { const handleLoadingComplete = useCallback(() => { setIsLoading(false); useGameStore.getState().setGameReady(true); + // Sync playerId with game setup (handles spectator mode where localPlayerId is null) + useGameStore.getState().syncWithGameSetup(); setTimeout(() => { const eventBus = workerBridgeRef.current?.eventBus; diff --git a/src/store/gameStore.ts b/src/store/gameStore.ts index 9fac69fd..96a5120d 100644 --- a/src/store/gameStore.ts +++ b/src/store/gameStore.ts @@ -447,9 +447,9 @@ export const useGameStore = create((set, get) => ({ syncWithGameSetup: () => { const localPlayerId = getLocalPlayerId(); - if (localPlayerId) { - set({ playerId: localPlayerId }); - } + // Set playerId to local player, or empty string if spectating + // This ensures we don't keep showing player1's data when spectating + set({ playerId: localPlayerId ?? '' }); }, reset: () => set(initialState),