aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-03-06 16:37:11 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-03-06 16:37:11 +0100
commitbd5dd17eb91f6501fb1944040e9d547c148a60aa (patch)
tree246f05a958ca4ab99e68e7f1a01f3b955ecdd8ce /web/pw-server/src/lib
parent47ec2c6721c53ab047c43d75b16fbec9f8db5750 (diff)
downloadplanetwars.dev-bd5dd17eb91f6501fb1944040e9d547c148a60aa.tar.xz
planetwars.dev-bd5dd17eb91f6501fb1944040e9d547c148a60aa.zip
refactor: move bot api calls to submitpane
Diffstat (limited to 'web/pw-server/src/lib')
-rw-r--r--web/pw-server/src/lib/components/SubmitPane.svelte46
1 files changed, 40 insertions, 6 deletions
diff --git a/web/pw-server/src/lib/components/SubmitPane.svelte b/web/pw-server/src/lib/components/SubmitPane.svelte
index 588a5e7..3c9fe22 100644
--- a/web/pw-server/src/lib/components/SubmitPane.svelte
+++ b/web/pw-server/src/lib/components/SubmitPane.svelte
@@ -2,6 +2,8 @@
import { createEventDispatcher, onMount } from "svelte";
import Select from "svelte-select";
+ export let editSession;
+
let availableBots: object[] = [];
let selectedOpponent = undefined;
let botName: string | undefined = undefined;
@@ -21,16 +23,48 @@
const dispatch = createEventDispatcher();
- function submitBot() {
- dispatch("submitBot", {
- opponentName: selectedOpponent["name"],
+ async function submitBot() {
+ const opponentName = selectedOpponent["name"];
+
+ let response = await fetch("/api/submit_bot", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ code: editSession.getDocument().getValue(),
+ opponent_name: opponentName,
+ }),
});
+
+ let responseData = await response.json();
+
+ if (response.ok) {
+ // object has a "match" key containing the match data
+ dispatch("matchCreated", responseData);
+ } else {
+ throw responseData;
+ }
}
- function saveBot() {
- dispatch("saveBot", {
- botName: botName,
+ async function saveBot() {
+ let response = await fetch("/api/save_bot", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ bot_name: botName,
+ code: editSession.getDocument().getValue(),
+ }),
});
+
+ let responseData = await response.json();
+ if (response.ok) {
+ dispatch("botSaved", responseData);
+ } else {
+ throw responseData;
+ }
}
</script>