From 80ee46c81dda5331f66aa401435447f22ff187cd Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 21 Mar 2025 09:23:01 +0900 Subject: feat(frontend): show game result in 1v1 watch --- frontend/app/states/watch.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'frontend/app/states/watch.ts') diff --git a/frontend/app/states/watch.ts b/frontend/app/states/watch.ts index 8c7faa7..2c255f4 100644 --- a/frontend/app/states/watch.ts +++ b/frontend/app/states/watch.ts @@ -93,3 +93,35 @@ export function calcCodeSize(code: string): number { const utf8Encoded = new TextEncoder().encode(trimmed); return utf8Encoded.length; } + +export type GameResultKind = "winA" | "winB" | "draw"; + +export function checkGameResultKind( + gameStateKind: GameStateKind, + stateA: LatestGameState | null, + stateB: LatestGameState | null, +): GameResultKind | null { + if (gameStateKind !== "finished") { + return null; + } + + const scoreA = stateA?.score; + const scoreB = stateB?.score; + if (scoreA == null && scoreB == null) { + return "draw"; + } + if (scoreA == null) { + return "winB"; + } + if (scoreB == null) { + return "winA"; + } + if (scoreA === scoreB) { + // If score is non-null, state and best_score_submitted_at should also be non-null. + const submittedAtA = stateA!.best_score_submitted_at!; + const submittedAtB = stateB!.best_score_submitted_at!; + return submittedAtA < submittedAtB ? "winA" : "winB"; + } else { + return scoreA < scoreB ? "winA" : "winB"; + } +} -- cgit v1.2.3-70-g09d2