aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/editor.svelte
blob: ff8232c6d29b75437967cfde9f2f417c88a38617 (plain)
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
<script lang="ts">
  import Visualizer from "$lib/components/Visualizer.svelte";
  import EditorView from "$lib/components/EditorView.svelte";
  import { onMount } from "svelte";
  import { DateTime } from "luxon";

  import type { Ace } from "ace-builds";
  import ace from "ace-builds/src-noconflict/ace?client";
  import * as AcePythonMode from "ace-builds/src-noconflict/mode-python?client";
  import { getBotCode, saveBotCode } from "$lib/bot_code";
  import { matchHistory } from "$lib/stores/editor_state";
  import { debounce } from "$lib/utils";
  import SubmitPane from "$lib/components/SubmitPane.svelte";
  import OutputPane from "$lib/components/OutputPane.svelte";
  import BotName from "./bots/[bot_name].svelte";

  enum ViewMode {
    Editor,
    MatchVisualizer,
  }

  let viewMode = ViewMode.Editor;
  let selectedMatchId: string | undefined = undefined;
  let selectedMatchLog: string | undefined = undefined;

  let editSession: Ace.EditSession;

  onMount(() => {
    init_editor();
  });

  function init_editor() {
    editSession = new ace.EditSession(getBotCode());
    editSession.setMode(new AcePythonMode.Mode());

    const saveCode = () => {
      const code = editSession.getDocument().getValue();
      saveBotCode(code);
    };

    // cast to any because the type annotations are wrong here
    (editSession as any).on("change", debounce(saveCode, 2000));
  }

  async function onMatchCreated(e: CustomEvent) {
    const matchData = e.detail["match"];
    matchHistory.pushMatch(matchData);
    await selectMatch(matchData["id"]);
  }

  async function selectMatch(matchId: string) {
    selectedMatchId = matchId;
    selectedMatchLog = null;
    fetchSelectedMatchLog(matchId);

    viewMode = ViewMode.MatchVisualizer;
  }

  async function fetchSelectedMatchLog(matchId: string) {
    if (matchId !== selectedMatchId) {
      return;
    }

    let matchLog = await getMatchLog(matchId);

    if (matchLog) {
      selectedMatchLog = matchLog;
    } else {
      // try again in 1 second
      setTimeout(fetchSelectedMatchLog, 1000, matchId);
    }
  }

  async function getMatchData(matchId: string) {
    let response = await fetch(`/api/matches/${matchId}`, {
      headers: {
        "Content-Type": "application/json",
      },
    });

    if (!response.ok) {
      throw Error(response.statusText);
    }

    let matchData = await response.json();
    return matchData;
  }

  async function getMatchLog(matchId: string) {
    const matchData = await getMatchData(matchId);
    console.log(matchData);
    if (matchData["state"] !== "Finished") {
      // log is not available yet
      return null;
    }

    const res = await fetch(`/api/matches/${matchId}/log`, {
      headers: {
        "Content-Type": "application/json",
      },
    });

    let log = await res.text();
    return log;
  }

  function setViewMode(viewMode_: ViewMode) {
    selectedMatchId = undefined;
    selectedMatchLog = undefined;
    viewMode = viewMode_;
  }

  function formatMatchTimestamp(timestampString: string): string {
    let timestamp = DateTime.fromISO(timestampString, { zone: "utc" }).toLocal();
    if (timestamp.startOf("day").equals(DateTime.now().startOf("day"))) {
      return timestamp.toFormat("HH:mm");
    } else {
      return timestamp.toFormat("dd/MM");
    }
  }

  $: selectedMatch = $matchHistory.find((m) => m["id"] === selectedMatchId);
</script>

<div class="container">
  <div class="sidebar-left">
    <div
      class="editor-button sidebar-item"
      class:selected={viewMode === ViewMode.Editor}
      on:click={() => setViewMode(ViewMode.Editor)}
    >
      Code
    </div>
    <div class="sidebar-header">match history</div>
    <ul class="match-list">
      {#each $matchHistory as match}
        <li
          class="match-card sidebar-item"
          on:click={() => selectMatch(match.id)}
          class:selected={match.id === selectedMatchId}
        >
          <div class="match-timestamp">{formatMatchTimestamp(match.timestamp)}</div>
          <div class="match-card-body">
            <!-- ugly temporary hardcode -->
            <div class="match-opponent">{match["players"][1]["bot_name"]}</div>
            <div class="match-map">{match["map"]?.name}</div>
          </div>
        </li>
      {/each}
    </ul>
  </div>
  <div class="editor-container">
    {#if viewMode === ViewMode.MatchVisualizer}
      <Visualizer matchData={selectedMatch} matchLog={selectedMatchLog} />
    {:else if viewMode === ViewMode.Editor}
      <EditorView {editSession} />
    {/if}
  </div>
  <div class="sidebar-right">
    {#if viewMode === ViewMode.MatchVisualizer}
      <OutputPane matchLog={selectedMatchLog} />
    {:else if viewMode === ViewMode.Editor}
      <SubmitPane {editSession} on:matchCreated={onMatchCreated} />
    {/if}
  </div>
</div>

<style lang="scss">
  @import "src/styles/variables.scss";

  .container {
    display: flex;
    flex-grow: 1;
    min-height: 0;
  }

  .sidebar-left {
    width: 240px;
    background-color: $bg-color;
    display: flex;
    flex-direction: column;
  }
  .sidebar-right {
    width: 400px;
    background-color: white;
    border-left: 1px solid;
    padding: 0;
    display: flex;
    overflow: hidden;
  }
  .editor-container {
    flex-grow: 1;
    flex-shrink: 1;
    overflow: hidden;
    background-color: white;
  }

  .editor-container {
    height: 100%;
  }

  .sidebar-item {
    color: #eee;
    padding: 15px;
  }

  .sidebar-item:hover {
    background-color: #333;
  }

  .sidebar-item.selected {
    background-color: #333;
  }

  .match-list {
    list-style: none;
    color: #eee;
    padding-top: 15px;
    overflow-y: scroll;
    padding-left: 0px;
  }

  .match-card {
    padding: 10px 15px;
    font-size: 11pt;
    display: flex;
  }

  .match-timestamp {
    color: #ccc;
  }

  .match-card-body {
    margin: 0 8px;
  }

  .match-opponent {
    font-weight: 600;
    color: #eee;
  }

  .match-map {
    color: #ccc;
  }

  .sidebar-header {
    margin-top: 2em;
    text-transform: uppercase;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    font-family: "Open Sans", sans-serif;
    padding-left: 14px;
  }
</style>