1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
<script lang="ts">
import { createEventDispatcher, onMount } from "svelte";
import Select from "svelte-select";
export let editSession;
let availableBots: object[] = [];
let selectedOpponent = undefined;
let botName: string | undefined = undefined;
let saveErrorText = undefined;
onMount(async () => {
const res = await fetch("/api/bots", {
headers: {
"Content-Type": "application/json",
},
});
if (res.ok) {
availableBots = await res.json();
selectedOpponent = availableBots.find((b) => b["name"] === "simplebot");
}
});
const dispatch = createEventDispatcher();
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;
}
}
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);
// clear errors
saveErrorText = undefined;
} else {
if (responseData["error"] === "BotNameTaken") {
saveErrorText = "Bot name is already taken";
} else {
// unexpected error
throw responseData;
}
}
}
</script>
<div class="submit-pane">
<div class="match-form">
<div class="play-text">Select an opponent to test your bot</div>
<div class="opponentSelect">
<Select
optionIdentifier="name"
labelIdentifier="name"
items={availableBots}
bind:value={selectedOpponent}
/>
</div>
<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} />
{#if saveErrorText}
<div class="error-text">{saveErrorText}</div>
{/if}
<button class="submit-button save-button" on:click={saveBot}>Save</button>
</div>
</div>
<style lang="scss">
.submit-pane {
margin: 20px;
flex: 1;
display: flex;
flex-direction: column;
}
.opponentSelect {
margin: 20px 0;
}
.save-form {
margin-top: 8em;
}
.error-text {
color: red;
}
.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 {
padding: 8px 16px;
border-radius: 8px;
border: 0;
font-size: 18pt;
display: block;
margin: 10px auto;
background-color: lightgreen;
cursor: pointer;
}
.bot-name-input {
width: 100%;
}
.save-button {
background-color: lightgreen;
cursor: pointer;
padding: 8px 16px;
border: 0;
}
</style>
|