diff options
Diffstat (limited to 'frontend/app/states')
| -rw-r--r-- | frontend/app/states/watch.ts | 32 |
1 files changed, 32 insertions, 0 deletions
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"; + } +} |
