aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/states
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-03-21 09:23:01 +0900
committernsfisis <nsfisis@gmail.com>2025-03-21 09:50:56 +0900
commit80ee46c81dda5331f66aa401435447f22ff187cd (patch)
tree10830e1e882808b0ecbebd2164fd805160558ca2 /frontend/app/states
parentd379ce3309e5241359b9849fd0170909a140169c (diff)
downloadphperkaigi-2025-albatross-80ee46c81dda5331f66aa401435447f22ff187cd.tar.gz
phperkaigi-2025-albatross-80ee46c81dda5331f66aa401435447f22ff187cd.tar.zst
phperkaigi-2025-albatross-80ee46c81dda5331f66aa401435447f22ff187cd.zip
feat(frontend): show game result in 1v1 watch
Diffstat (limited to 'frontend/app/states')
-rw-r--r--frontend/app/states/watch.ts32
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";
+ }
+}