aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib
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
parent67c8a2780c92d247b7343b2107f3d69fc9763797 (diff)
downloadplanetwars.dev-00d31df58d0ea68b11600d98ebf53150a2a0cb88.tar.xz
planetwars.dev-00d31df58d0ea68b11600d98ebf53150a2a0cb88.zip
design new BotMatch view
Diffstat (limited to 'web/pw-server/src/lib')
-rw-r--r--web/pw-server/src/lib/api_types.ts20
-rw-r--r--web/pw-server/src/lib/components/PlayerLog.svelte3
-rw-r--r--web/pw-server/src/lib/components/SubmitPane.svelte8
-rw-r--r--web/pw-server/src/lib/components/matches/BotMatchCard.svelte104
-rw-r--r--web/pw-server/src/lib/components/matches/BotMatchList.svelte19
-rw-r--r--web/pw-server/src/lib/components/navbar/UserControls.svelte1
-rw-r--r--web/pw-server/src/lib/matches.ts55
7 files changed, 202 insertions, 8 deletions
diff --git a/web/pw-server/src/lib/api_types.ts b/web/pw-server/src/lib/api_types.ts
new file mode 100644
index 0000000..03190cb
--- /dev/null
+++ b/web/pw-server/src/lib/api_types.ts
@@ -0,0 +1,20 @@
+export type Match = {
+ id: number;
+ timestamp: string;
+ state: string;
+ players: MatchPlayer[];
+ winner: number;
+ map: Map;
+};
+
+export type MatchPlayer = {
+ bot_id: number;
+ bot_version_id: number;
+ bot_name: string;
+ owner_id?: number;
+ had_errors?: boolean;
+};
+
+export type Map = {
+ name: string;
+};
diff --git a/web/pw-server/src/lib/components/PlayerLog.svelte b/web/pw-server/src/lib/components/PlayerLog.svelte
index 52cae8e..60097d8 100644
--- a/web/pw-server/src/lib/components/PlayerLog.svelte
+++ b/web/pw-server/src/lib/components/PlayerLog.svelte
@@ -3,6 +3,7 @@
export let matchLog: string;
export let playerId: number;
+ export let showStdErr: boolean = true;
let playerLog: PlayerLog;
@@ -66,7 +67,7 @@
<div class="bad-command-error">Parse error: {logTurn.action.error}</div>
</div>
{/if}
- {#if logTurn.stderr.length > 0}
+ {#if showStdErr && logTurn.stderr.length > 0}
<div class="stderr-header">stderr</div>
<div class="stderr-text-box">
{#each logTurn.stderr as stdErrMsg}
diff --git a/web/pw-server/src/lib/components/SubmitPane.svelte b/web/pw-server/src/lib/components/SubmitPane.svelte
index e46166c..b1aaf9e 100644
--- a/web/pw-server/src/lib/components/SubmitPane.svelte
+++ b/web/pw-server/src/lib/components/SubmitPane.svelte
@@ -122,13 +122,7 @@
</div>
<span>Map</span>
<div class="map-select">
- <Select
- itemId="name"
- label="name"
- items={maps}
- bind:value={$selectedMap}
- clearable={false}
- />
+ <Select itemId="name" label="name" items={maps} bind:value={$selectedMap} clearable={false} />
</div>
<button class="submit-button play-button" on:click={submitBot}>Play</button>
</div>
diff --git a/web/pw-server/src/lib/components/matches/BotMatchCard.svelte b/web/pw-server/src/lib/components/matches/BotMatchCard.svelte
new file mode 100644
index 0000000..2f50173
--- /dev/null
+++ b/web/pw-server/src/lib/components/matches/BotMatchCard.svelte
@@ -0,0 +1,104 @@
+<script lang="ts">
+ import type { BotMatch } from "$lib/matches";
+ import dayjs from "dayjs";
+
+ export let botMatch: BotMatch;
+</script>
+
+<a class="bot-match-card-wrapper" href={`/matches/${botMatch.id}`}>
+ <div class="bot-match-card">
+ <div class="bot-match-outcome">
+ {botMatch.outcome}
+ </div>
+ <div class="bot-match-card-main">
+ <div class="opponent-name">
+ <a class="bot-link" href={`/bots/${botMatch.opponent.bot_name}`}
+ >{botMatch.opponent.bot_name}</a
+ >
+ </div>
+ <div class="map-name">
+ {botMatch.map.name}
+ </div>
+ </div>
+ <div class="bot-card-right">
+ <div class="match-timestamp">
+ {dayjs(botMatch.timestamp).format("YYYY-MM-DD HH:mm")}
+ </div>
+ <div class="match-errors">
+ {#if botMatch.hadErrors}
+ ! Had errors
+ {/if}
+ </div>
+ </div>
+ </div>
+</a>
+
+<style lang="scss">
+ @use "src/styles/variables";
+
+ .bot-match-card {
+ margin: 4px;
+ padding: 12px;
+ display: flex;
+ border: 1px solid variables.$light-grey;
+ }
+
+ .bot-match-outcome {
+ font-size: 24pt;
+ font-weight: 600;
+ font-family: "Open Sans", sans-serif;
+ text-transform: uppercase;
+ color: #333;
+ width: 75px;
+ display: flex;
+ justify-content: center;
+ margin-top: -4px;
+ }
+
+ .bot-match-card-main {
+ flex-grow: 1;
+ padding: 0 25px;
+ }
+
+ .bot-card-right {
+ display: flex;
+ flex-direction: column;
+ justify-content: space-between;
+ align-items: flex-end;
+ }
+
+ .opponent-name {
+ font-size: 18pt;
+ color: variables.$blue-primary;
+ padding-bottom: 4px;
+ }
+
+ .match-errors {
+ color: red;
+ font-weight: 600;
+ text-align: right;
+ }
+
+ .match-timestamp {
+ text-align: right;
+ }
+
+ .bot-link {
+ color: variables.$blue-primary;
+ text-decoration: none;
+ }
+
+ .bot-link:hover {
+ text-decoration: underline;
+ }
+
+ .bot-match-card:hover {
+ background-color: rgb(246, 248, 250);
+ }
+
+ .bot-match-card-wrapper {
+ text-decoration: none;
+ color: black;
+ display: block;
+ }
+</style>
diff --git a/web/pw-server/src/lib/components/matches/BotMatchList.svelte b/web/pw-server/src/lib/components/matches/BotMatchList.svelte
new file mode 100644
index 0000000..959cd07
--- /dev/null
+++ b/web/pw-server/src/lib/components/matches/BotMatchList.svelte
@@ -0,0 +1,19 @@
+<script lang="ts">
+ import type { BotMatch } from "$lib/matches";
+ import BotMatchCard from "./BotMatchCard.svelte";
+
+ export let botMatches: BotMatch[];
+
+ function match_url(match: object) {
+ return `/matches/${match["id"]}`;
+ }
+</script>
+
+<div>
+ {#each botMatches as botMatch}
+ <BotMatchCard {botMatch} />
+ {/each}
+</div>
+
+<style lang="scss">
+</style>
diff --git a/web/pw-server/src/lib/components/navbar/UserControls.svelte b/web/pw-server/src/lib/components/navbar/UserControls.svelte
index 334b3b4..fa920ad 100644
--- a/web/pw-server/src/lib/components/navbar/UserControls.svelte
+++ b/web/pw-server/src/lib/components/navbar/UserControls.svelte
@@ -39,6 +39,7 @@
<a class="current-user-name" href="/users/{$currentUser['username']}">
{$currentUser["username"]}
</a>
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="sign-out" on:click={signOut}>Sign out</div>
{:else}
<a class="account-href" href="/login">Sign in</a>
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,
+ };
+}