create editor store

This commit is contained in:
Ilion Beyst 2022-08-22 21:58:13 +02:00
parent 82ab9cef78
commit fa4c684475
3 changed files with 38 additions and 14 deletions

View file

@ -3,13 +3,14 @@
import { getBotName, saveBotName } from "$lib/bot_code"; import { getBotName, saveBotName } from "$lib/bot_code";
import { currentUser } from "$lib/stores/current_user"; import { currentUser } from "$lib/stores/current_user";
import { selectedOpponent } from "$lib/stores/editor_state";
import { createEventDispatcher, onMount } from "svelte"; import { createEventDispatcher, onMount } from "svelte";
import Select from "svelte-select"; import Select from "svelte-select";
export let editSession; export let editSession;
let availableBots: object[] = []; let availableBots: object[] = [];
let selectedOpponent = undefined;
let botName: string | undefined = undefined; let botName: string | undefined = undefined;
// whether to show the "save succesful" message // whether to show the "save succesful" message
let saveSuccesful = false; let saveSuccesful = false;
@ -27,14 +28,16 @@
if (res.ok) { if (res.ok) {
availableBots = await res.json(); availableBots = await res.json();
selectedOpponent = availableBots.find((b) => b["name"] === "simplebot"); if (!$selectedOpponent) {
selectedOpponent.set(availableBots.find((b) => b["name"] === "simplebot"));
}
} }
}); });
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
async function submitBot() { async function submitBot() {
const opponentName = selectedOpponent["name"]; const opponentName = $selectedOpponent["name"];
let response = await fetch("/api/submit_bot", { let response = await fetch("/api/submit_bot", {
method: "POST", method: "POST",
@ -106,7 +109,7 @@
optionIdentifier="name" optionIdentifier="name"
labelIdentifier="name" labelIdentifier="name"
items={availableBots} items={availableBots}
bind:value={selectedOpponent} bind:value={$selectedOpponent}
isClearable={false} isClearable={false}
/> />
</div> </div>

View file

@ -0,0 +1,26 @@
import { writable } from "svelte/store";
const MAX_MATCHES = 100;
function createMatchHistory() {
const { subscribe, update } = writable([]);
function pushMatch(match: object) {
update((matches) => {
if (matches.length == MAX_MATCHES) {
matches.pop();
}
matches.unshift(match);
return matches;
});
}
return {
subscribe,
pushMatch,
};
}
export const matchHistory = createMatchHistory();
export const selectedOpponent = writable(null);

View file

@ -2,26 +2,22 @@
import Visualizer from "$lib/components/Visualizer.svelte"; import Visualizer from "$lib/components/Visualizer.svelte";
import EditorView from "$lib/components/EditorView.svelte"; import EditorView from "$lib/components/EditorView.svelte";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { DateTime } from "luxon"; import { DateTime } from "luxon";
import type { Ace } from "ace-builds"; import type { Ace } from "ace-builds";
import ace from "ace-builds/src-noconflict/ace?client"; import ace from "ace-builds/src-noconflict/ace?client";
import * as AcePythonMode from "ace-builds/src-noconflict/mode-python?client"; import * as AcePythonMode from "ace-builds/src-noconflict/mode-python?client";
import { getBotCode, saveBotCode, hasBotCode } from "$lib/bot_code"; import { getBotCode, saveBotCode } from "$lib/bot_code";
import { matchHistory } from "$lib/stores/editor_state";
import { debounce } from "$lib/utils"; import { debounce } from "$lib/utils";
import SubmitPane from "$lib/components/SubmitPane.svelte"; import SubmitPane from "$lib/components/SubmitPane.svelte";
import OutputPane from "$lib/components/OutputPane.svelte"; import OutputPane from "$lib/components/OutputPane.svelte";
import RulesView from "$lib/components/RulesView.svelte";
import Leaderboard from "$lib/components/Leaderboard.svelte";
enum ViewMode { enum ViewMode {
Editor, Editor,
MatchVisualizer, MatchVisualizer,
} }
let matches = [];
let viewMode = ViewMode.Editor; let viewMode = ViewMode.Editor;
let selectedMatchId: string | undefined = undefined; let selectedMatchId: string | undefined = undefined;
let selectedMatchLog: string | undefined = undefined; let selectedMatchLog: string | undefined = undefined;
@ -47,8 +43,7 @@
async function onMatchCreated(e: CustomEvent) { async function onMatchCreated(e: CustomEvent) {
const matchData = e.detail["match"]; const matchData = e.detail["match"];
matches.unshift(matchData); matchHistory.pushMatch(matchData);
matches = matches;
await selectMatch(matchData["id"]); await selectMatch(matchData["id"]);
} }
@ -123,7 +118,7 @@
} }
} }
$: selectedMatch = matches.find((m) => m["id"] === selectedMatchId); $: selectedMatch = $matchHistory.find((m) => m["id"] === selectedMatchId);
</script> </script>
<div class="container"> <div class="container">
@ -137,7 +132,7 @@
</div> </div>
<div class="sidebar-header">match history</div> <div class="sidebar-header">match history</div>
<ul class="match-list"> <ul class="match-list">
{#each matches as match} {#each $matchHistory as match}
<li <li
class="match-card sidebar-item" class="match-card sidebar-item"
on:click={() => selectMatch(match.id)} on:click={() => selectMatch(match.id)}