diff --git a/backend/src/realtime/ws/hub.ts b/backend/src/realtime/ws/hub.ts index f393c029..538abba5 100644 --- a/backend/src/realtime/ws/hub.ts +++ b/backend/src/realtime/ws/hub.ts @@ -30,7 +30,15 @@ function enrichRoundPayload( const short = Number(pools.short ?? 0); const long = Number(pools.long ?? 0); const total = short + long; - if (total <= 0) return round; + if (total <= 0) { + // Empty pools must clear previous client state (do not omit zero amounts). + return { + ...round, + shortPoolPercent: Number(round.shortPoolPercent ?? 0), + longPoolPercent: Number(round.longPoolPercent ?? 0), + ...(includePoolAmounts ? { shortPool: 0, longPool: 0 } : {}), + }; + } const shortPoolPercent = Math.round((short / total) * 1000) / 10; const longPoolPercent = Math.round((long / total) * 1000) / 10; return { diff --git a/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts b/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts index bc107853..035d20f0 100644 --- a/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts +++ b/prediqo_web/src/features/candle-predict/hooks/useGameSocket.ts @@ -26,24 +26,42 @@ function mergeRound( ): GameState['round'] { if (!incoming) return prev ?? null; const merged = { ...(prev ?? {}), ...incoming } as NonNullable; + const incomingRec = incoming as Record; + const roundChanged = + prev?.id != null && + incomingRec.id != null && + String(prev.id) !== String(incomingRec.id); - const inShort = Number((incoming as { shortPool?: number }).shortPool ?? NaN); - const inLong = Number((incoming as { longPool?: number }).longPool ?? NaN); - const inPoolTotal = - (Number.isFinite(inShort) ? inShort : 0) + (Number.isFinite(inLong) ? inLong : 0); - if (inPoolTotal > 0) { - merged.shortPool = inShort; - merged.longPool = inLong; + const hasPoolAmounts = + Object.prototype.hasOwnProperty.call(incomingRec, 'shortPool') || + Object.prototype.hasOwnProperty.call(incomingRec, 'longPool'); + const inShort = Number(incomingRec.shortPool ?? NaN); + const inLong = Number(incomingRec.longPool ?? NaN); + + if (hasPoolAmounts) { + // Explicit values win — including zeros for a fresh empty round. + merged.shortPool = Number.isFinite(inShort) ? inShort : 0; + merged.longPool = Number.isFinite(inLong) ? inLong : 0; + } else if (roundChanged) { + merged.shortPool = 0; + merged.longPool = 0; } else if (prev) { if (merged.shortPool == null && prev.shortPool != null) merged.shortPool = prev.shortPool; if (merged.longPool == null && prev.longPool != null) merged.longPool = prev.longPool; } - const inShortPct = Number((incoming as { shortPoolPercent?: number }).shortPoolPercent ?? NaN); - const inLongPct = Number((incoming as { longPoolPercent?: number }).longPoolPercent ?? NaN); - if (Number.isFinite(inShortPct) && Number.isFinite(inLongPct) && inShortPct + inLongPct > 0) { - merged.shortPoolPercent = inShortPct; - merged.longPoolPercent = inLongPct; + const hasPoolPercents = + Object.prototype.hasOwnProperty.call(incomingRec, 'shortPoolPercent') || + Object.prototype.hasOwnProperty.call(incomingRec, 'longPoolPercent'); + const inShortPct = Number(incomingRec.shortPoolPercent ?? NaN); + const inLongPct = Number(incomingRec.longPoolPercent ?? NaN); + + if (hasPoolPercents) { + merged.shortPoolPercent = Number.isFinite(inShortPct) ? inShortPct : 0; + merged.longPoolPercent = Number.isFinite(inLongPct) ? inLongPct : 0; + } else if (roundChanged) { + merged.shortPoolPercent = 0; + merged.longPoolPercent = 0; } merged.status = effectiveRoundStatus(merged);