refactor: move bot api calls to submitpane
This commit is contained in:
parent
47ec2c6721
commit
bd5dd17eb9
2 changed files with 43 additions and 41 deletions
|
@ -2,6 +2,8 @@
|
||||||
import { createEventDispatcher, onMount } from "svelte";
|
import { createEventDispatcher, onMount } from "svelte";
|
||||||
import Select from "svelte-select";
|
import Select from "svelte-select";
|
||||||
|
|
||||||
|
export let editSession;
|
||||||
|
|
||||||
let availableBots: object[] = [];
|
let availableBots: object[] = [];
|
||||||
let selectedOpponent = undefined;
|
let selectedOpponent = undefined;
|
||||||
let botName: string | undefined = undefined;
|
let botName: string | undefined = undefined;
|
||||||
|
@ -21,16 +23,48 @@
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
function submitBot() {
|
async function submitBot() {
|
||||||
dispatch("submitBot", {
|
const opponentName = selectedOpponent["name"];
|
||||||
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() {
|
async function saveBot() {
|
||||||
dispatch("saveBot", {
|
let response = await fetch("/api/save_bot", {
|
||||||
botName: botName,
|
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>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -38,45 +38,13 @@
|
||||||
(editSession as any).on("change", debounce(saveCode, 2000));
|
(editSession as any).on("change", debounce(saveCode, 2000));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submitBot(e: CustomEvent) {
|
async function onMatchCreated(e: CustomEvent) {
|
||||||
console.log(e.detail);
|
const matchData = e.detail["match"];
|
||||||
let response = await fetch("/api/submit_bot", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
code: editSession.getDocument().getValue(),
|
|
||||||
opponent_name: e.detail["opponentName"],
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw Error(response.statusText);
|
|
||||||
}
|
|
||||||
|
|
||||||
let responseData = await response.json();
|
|
||||||
|
|
||||||
let matchData = responseData["match"];
|
|
||||||
|
|
||||||
matches.unshift(matchData);
|
matches.unshift(matchData);
|
||||||
matches = matches;
|
matches = matches;
|
||||||
await selectMatch(matchData["id"]);
|
await selectMatch(matchData["id"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveBot(e: CustomEvent) {
|
|
||||||
let response = await fetch("/api/save_bot", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
bot_name: e.detail["botName"],
|
|
||||||
code: editSession.getDocument().getValue(),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function selectMatch(matchId: string) {
|
async function selectMatch(matchId: string) {
|
||||||
selectedMatchId = matchId;
|
selectedMatchId = matchId;
|
||||||
selectedMatchLog = null;
|
selectedMatchLog = null;
|
||||||
|
@ -183,7 +151,7 @@
|
||||||
{#if selectedMatchId}
|
{#if selectedMatchId}
|
||||||
<OutputPane matchLog={selectedMatchLog} />
|
<OutputPane matchLog={selectedMatchLog} />
|
||||||
{:else}
|
{:else}
|
||||||
<SubmitPane on:submitBot={submitBot} on:saveBot={saveBot} />
|
<SubmitPane {editSession} on:matchCreated={onMatchCreated} />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue