-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHome.svelte
More file actions
273 lines (249 loc) · 7.15 KB
/
Copy pathHome.svelte
File metadata and controls
273 lines (249 loc) · 7.15 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<script lang="ts">
/** @import * from '../../bindings/gui' */
import toast from "svelte-5-french-toast";
// @ts-ignore
import {
App,
BotInfo,
type StartMatchOptions,
} from "../../bindings/gui/index.js";
import arenaImages from "../arena-images";
import { MAPS_STANDARD } from "../arena-names";
import reloadIcon from "../assets/reload.svg";
import { BASE_PLAYERS } from "../base-players";
import BotList from "../components/BotList.svelte";
// @ts-ignore
import MatchSettings from "../components/MatchSettings/Main.svelte";
import PathsViewer from "../components/PathsViewer.svelte";
// @ts-ignore
import Teams from "../components/Teams/Main.svelte";
import { type DraggablePlayer, draggablePlayerToPlayerJs } from "../index";
import { mapStore } from "../settings";
const backgroundImage =
arenaImages[Math.floor(Math.random() * arenaImages.length)];
let paths: {
tagName: string | null;
repo: string | null;
installPath: string;
}[] = $state(
JSON.parse(window.localStorage.getItem("BOT_SEARCH_PATHS") || "[]"),
);
let launcherOptionsVisible = $state(false);
let players: DraggablePlayer[] = $state([...BASE_PLAYERS]);
let selectedTeam = $state(null);
let loadingPlayers = $state(false);
let latestBotUpdateTime = null;
let showPathsViewer = $state(false);
async function updateBots() {
loadingPlayers = true;
let internalUpdateTime = new Date();
latestBotUpdateTime = internalUpdateTime;
const result = await App.GetBots(paths.map((x) => x.installPath));
if (latestBotUpdateTime !== internalUpdateTime) {
return; // if newer "search" already started, dont write old data
}
players = result.map((x: BotInfo) => {
// @ts-ignore
const n: typeof DraggablePlayer = {
displayName: x.config.settings.name,
icon: x.config.settings.logoFile,
player: new BotInfo(x),
id: Math.random(),
tags: x.config.details.tags,
};
return n;
});
players = [...BASE_PLAYERS, ...players];
loadingPlayers = false;
console.log("Loaded bots:", result);
}
$effect(() => {
window.localStorage.setItem("BOT_SEARCH_PATHS", JSON.stringify(paths));
updateBots();
});
let bluePlayers: DraggablePlayer[] = $state([]);
let orangePlayers: DraggablePlayer[] = $state([]);
let showHuman = $state(true);
$effect(() => {
showHuman = !(
bluePlayers.some((x) => x.tags.includes("human")) ||
orangePlayers.some((x) => x.tags.includes("human"))
);
});
let mode = $state(localStorage.getItem("MS_MODE") || "Soccer");
$effect(() => {
localStorage.setItem("MS_MODE", mode);
});
let extraOptions = $state(
JSON.parse(
localStorage.getItem("MS_EXTRAOPTIONS") ||
'{"enableStateSetting": true, "existingMatchBehavior": 0}',
),
);
$effect(() => {
localStorage.setItem("MS_EXTRAOPTIONS", JSON.stringify(extraOptions));
});
let mutatorSettings = $state(
JSON.parse(localStorage.getItem("MS_MUTATORS") || "{}"),
);
$effect(() => {
localStorage.setItem("MS_MUTATORS", JSON.stringify(mutatorSettings));
});
async function onMatchStart(randomizeMap: boolean) {
const launcher = localStorage.getItem("MS_LAUNCHER");
if (!launcher) {
toast.error("Please select a launcher first", {
position: "bottom-right",
duration: 5000,
});
launcherOptionsVisible = true;
return;
}
if (randomizeMap) {
$mapStore =
Object.values(MAPS_STANDARD)[
Math.floor(Math.random() * Object.keys(MAPS_STANDARD).length)
];
}
const options: StartMatchOptions = {
map: $mapStore,
gameMode: mode,
bluePlayers: bluePlayers.map((x: DraggablePlayer) => {
// @ts-ignore
return draggablePlayerToPlayerJs(x);
}),
orangePlayers: orangePlayers.map((x: DraggablePlayer) => {
// @ts-ignore
return draggablePlayerToPlayerJs(x);
}),
launcher,
launcherArg: localStorage.getItem("MS_LAUNCHER_ARG") || "",
mutatorSettings,
extraOptions,
};
toast("Starting match...", {
position: "bottom-right",
});
const response = await App.StartMatch(options);
if (response.success) {
toast.success("Sent start match command", {
position: "bottom-right",
duration: 5000,
});
} else {
toast.error(`Match start failed\n${response.message}`, {
position: "bottom-right",
duration: 5000,
});
}
}
async function onMatchStop() {
toast("Stopping match...", {
position: "bottom-right",
});
const response = await App.StopMatch(false);
if (response.success) {
toast.success("Sent stop match command", {
position: "bottom-right",
duration: 5000,
});
} else {
toast.error(`Match stop failed\n${response.message}`, {
position: "bottom-right",
duration: 5000,
});
}
}
let searchQuery = $state("");
function handleSearch(event: Event) {
searchQuery = (event.target as HTMLInputElement).value;
}
</script>
<div class="page" style={`background-image: url("${backgroundImage}")`}>
<div class="availableBots box">
<header>
<h1>Bots</h1>
<div class="dropdown">
<button onclick={() => { showPathsViewer = true }}>Add/Remove</button>
</div>
{#if loadingPlayers}
<h3>Searching...</h3>
{:else}
<button class="reloadButton" onclick={updateBots}
><img src={reloadIcon} alt="reload" /></button
>
{/if}
<div style="flex:1"></div>
<input type="text" class="botSearch" placeholder="Search..." oninput={handleSearch}/>
</header>
<BotList
bind:bluePlayers
bind:orangePlayers
bind:showHuman
items={players}
searchQuery={searchQuery}
selectedTeam={selectedTeam}
/>
</div>
<div class="teams"><Teams bind:bluePlayers bind:orangePlayers bind:selectedTeam /></div>
<div class="box">
<MatchSettings
onStart={onMatchStart}
onStop={onMatchStop}
bind:map={$mapStore}
bind:mode
bind:mutators={mutatorSettings}
bind:extraOptions
bind:launcherOptionsVisible
/>
</div>
</div>
<PathsViewer bind:visible={showPathsViewer} bind:paths={paths} />
<style>
.page {
padding: 1rem;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
overflow: auto;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
}
.page * {
user-select: none;
-webkit-user-select: none;
}
.box {
border-radius: 0.4rem;
background-color: var(--background);
padding: 0.6rem;
}
.page > div:not(:first-child) {
margin-top: 1rem;
}
.availableBots {
padding-bottom: 0.6rem;
display: flex;
flex-direction: column;
}
.availableBots header {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 0.6rem;
}
.reloadButton {
padding: 0px;
}
.reloadButton img {
filter: invert();
}
.teams {
overflow: auto;
display: flex;
flex-direction: column;
}
</style>