aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/matches.ts
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-10-30 14:37:38 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-10-30 16:23:35 +0100
commit00d31df58d0ea68b11600d98ebf53150a2a0cb88 (patch)
tree523abfde1b55f4a808095a440b88376a63e8d6f3 /web/pw-server/src/lib/matches.ts
parent67c8a2780c92d247b7343b2107f3d69fc9763797 (diff)
downloadplanetwars.dev-00d31df58d0ea68b11600d98ebf53150a2a0cb88.tar.xz
planetwars.dev-00d31df58d0ea68b11600d98ebf53150a2a0cb88.zip
design new BotMatch view
Diffstat (limited to 'web/pw-server/src/lib/matches.ts')
-rw-r--r--web/pw-server/src/lib/matches.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/web/pw-server/src/lib/matches.ts b/web/pw-server/src/lib/matches.ts
new file mode 100644
index 0000000..d889a0d
--- /dev/null
+++ b/web/pw-server/src/lib/matches.ts
@@ -0,0 +1,55 @@
+import type { Match as ApiMatch, MatchPlayer as ApiMatchPlayer, Map } from "./api_types";
+
+// match from the perspective of a bot
+export type BotMatch = {
+ id: number;
+ opponent: BotMatchOpponent;
+ outcome: BotMatchOutcome;
+ timestamp: string;
+ map: Map;
+ hadErrors?: boolean;
+};
+
+export type BotMatchOutcome = "win" | "loss" | "tie";
+
+export type BotMatchOpponent = {
+ bot_id: number;
+ bot_name: string;
+ owner_id?: number;
+};
+
+export function apiMatchtoBotMatch(bot_name: string, apiMatch: ApiMatch): BotMatch {
+ let player: ApiMatchPlayer;
+ let playerIndex: number;
+ let opponent: ApiMatchPlayer;
+ apiMatch.players.forEach((matchPlayer, index) => {
+ if (matchPlayer.bot_name === bot_name) {
+ player = matchPlayer;
+ playerIndex = index;
+ } else {
+ opponent = matchPlayer;
+ }
+ });
+
+ if (player === undefined || opponent === undefined || playerIndex === undefined) {
+ throw "could not assign player and opponent";
+ }
+
+ let outcome: BotMatchOutcome;
+ if (apiMatch.winner === playerIndex) {
+ outcome = "win";
+ } else if (apiMatch.winner) {
+ outcome = "loss";
+ } else {
+ outcome = "tie";
+ }
+
+ return {
+ id: apiMatch.id,
+ opponent,
+ outcome,
+ timestamp: apiMatch.timestamp,
+ map: apiMatch.map,
+ hadErrors: player.had_errors,
+ };
+}