From c80ce332791c94b83a03c9f7c3cbb937f474cc5d Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Sat, 27 Aug 2022 17:05:11 +0200
Subject: allow selecting a map in editor view

---
 web/pw-server/src/lib/components/SubmitPane.svelte | 54 ++++++++++++++--------
 1 file changed, 36 insertions(+), 18 deletions(-)

(limited to 'web/pw-server/src/lib/components/SubmitPane.svelte')

diff --git a/web/pw-server/src/lib/components/SubmitPane.svelte b/web/pw-server/src/lib/components/SubmitPane.svelte
index 9fd2380..b0f86c8 100644
--- a/web/pw-server/src/lib/components/SubmitPane.svelte
+++ b/web/pw-server/src/lib/components/SubmitPane.svelte
@@ -1,9 +1,11 @@
 <script lang="ts">
+  import { ApiClient } from "$lib/api_client";
+
   import { get_session_token } from "$lib/auth";
   import { getBotName, saveBotName } from "$lib/bot_code";
 
   import { currentUser } from "$lib/stores/current_user";
-  import { selectedOpponent } from "$lib/stores/editor_state";
+  import { selectedOpponent, selectedMap } from "$lib/stores/editor_state";
 
   import { createEventDispatcher, onMount } from "svelte";
   import Select from "svelte-select";
@@ -11,6 +13,8 @@
   export let editSession;
 
   let availableBots: object[] = [];
+  let maps: object[] = [];
+
   let botName: string | undefined = undefined;
   // whether to show the "save succesful" message
   let saveSuccesful = false;
@@ -19,26 +23,28 @@
 
   onMount(async () => {
     botName = getBotName();
+    const apiClient = new ApiClient();
 
-    const res = await fetch("/api/bots", {
-      headers: {
-        "Content-Type": "application/json",
-      },
-    });
+    const [_bots, _maps] = await Promise.all([
+      apiClient.get("/api/bots"),
+      apiClient.get("/api/maps"),
+    ]);
 
-    if (res.ok) {
-      availableBots = await res.json();
-      if (!$selectedOpponent) {
-        selectedOpponent.set(availableBots.find((b) => b["name"] === "simplebot"));
-      }
+    availableBots = _bots;
+    maps = _maps;
+
+    if (!$selectedOpponent) {
+      selectedOpponent.set(availableBots.find((b) => b["name"] === "simplebot"));
+    }
+
+    if (!$selectedMap) {
+      selectedMap.set(maps.find((m) => m["name"] === "hex"));
     }
   });
 
   const dispatch = createEventDispatcher();
 
   async function submitBot() {
-    const opponentName = $selectedOpponent["name"];
-
     let response = await fetch("/api/submit_bot", {
       method: "POST",
       headers: {
@@ -46,7 +52,8 @@
       },
       body: JSON.stringify({
         code: editSession.getDocument().getValue(),
-        opponent_name: opponentName,
+        opponent_name: $selectedOpponent["name"],
+        map_name: $selectedMap["name"],
       }),
     });
 
@@ -103,8 +110,8 @@
 <div class="submit-pane">
   <div class="match-form">
     <h4>Play a match</h4>
-    <div class="play-text">Select an opponent to test your bot</div>
-    <div class="opponentSelect">
+    <div class="play-text">Opponent</div>
+    <div class="opponent-select">
       <Select
         optionIdentifier="name"
         labelIdentifier="name"
@@ -113,6 +120,16 @@
         isClearable={false}
       />
     </div>
+    <span>Map</span>
+    <div class="map-select">
+      <Select
+        optionIdentifier="name"
+        labelIdentifier="name"
+        items={maps}
+        bind:value={$selectedMap}
+        isClearable={false}
+      />
+    </div>
     <button class="submit-button play-button" on:click={submitBot}>Play</button>
   </div>
   <div class="save-form">
@@ -148,8 +165,9 @@
     margin-bottom: 0.3em;
   }
 
-  .opponentSelect {
-    margin: 20px 0;
+  .opponent-select,
+  .map-select {
+    margin: 8px 0;
   }
 
   .save-form {
-- 
cgit v1.2.3

