add save bot form

This commit is contained in:
Ilion Beyst 2022-03-04 20:23:58 +01:00
parent a8986d7a28
commit af04709fe1
2 changed files with 58 additions and 5 deletions

View file

@ -4,6 +4,7 @@
let availableBots: object[] = []; let availableBots: object[] = [];
let selectedOpponent = "simplebot"; let selectedOpponent = "simplebot";
let botName: string | undefined = undefined;
const optionIdentifier = "name"; const optionIdentifier = "name";
const labelIdentifier = "name"; const labelIdentifier = "name";
@ -23,8 +24,14 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
function submit() { function submitBot() {
dispatch("submit"); dispatch("submitBot");
}
function saveBot() {
dispatch("saveBot", {
botName: botName,
});
} }
</script> </script>
@ -39,7 +46,12 @@
bind:value={selectedOpponent} bind:value={selectedOpponent}
/> />
</div> </div>
<button class="play-button" on:click={submit}>Play</button> <button class="submit-button play-button" on:click={submitBot}>Play</button>
</div>
<div class="save-form">
<h4>Save your bot</h4>
<input type="text" class="bot-name-input" placeholder="bot name" bind:value={botName} />
<button class="submit-button save-button" on:click={saveBot}>Save</button>
</div> </div>
</div> </div>
@ -47,12 +59,29 @@
.submit-pane { .submit-pane {
margin: 20px; margin: 20px;
flex: 1; flex: 1;
display: flex;
flex-direction: column;
} }
.opponentSelect { .opponentSelect {
margin: 20px 0; margin: 20px 0;
} }
.save-form {
margin-top: 8em;
}
.submit-button {
padding: 8px 16px;
border-radius: 8px;
border: 0;
font-size: 18pt;
display: block;
margin: 10px auto;
background-color: lightgreen;
cursor: pointer;
}
.play-button { .play-button {
padding: 8px 16px; padding: 8px 16px;
border-radius: 8px; border-radius: 8px;
@ -63,4 +92,15 @@
background-color: lightgreen; background-color: lightgreen;
cursor: pointer; cursor: pointer;
} }
.bot-name-input {
width: 100%;
}
.save-button {
background-color: lightgreen;
cursor: pointer;
padding: 8px 16px;
border: 0;
}
</style> </style>

View file

@ -38,7 +38,7 @@
(editSession as any).on("change", debounce(saveCode, 2000)); (editSession as any).on("change", debounce(saveCode, 2000));
} }
async function submitCode() { async function submitBot() {
let response = await fetch("/api/submit_bot", { let response = await fetch("/api/submit_bot", {
method: "POST", method: "POST",
headers: { headers: {
@ -62,6 +62,19 @@
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;
@ -168,7 +181,7 @@
{#if selectedMatchId} {#if selectedMatchId}
<OutputPane matchLog={selectedMatchLog} /> <OutputPane matchLog={selectedMatchLog} />
{:else} {:else}
<SubmitPane on:submit={submitCode} /> <SubmitPane on:submitBot={submitBot} on:saveBot={saveBot} />
{/if} {/if}
</div> </div>
</div> </div>