aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/components/Visualizer.svelte
blob: 9859a596b4f00d25e1bb0a2052c0c2cba2d0855b (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
<script lang="ts">
  import { onDestroy, onMount } from "svelte";
  import * as visualizer from "pw-visualizer";
  import init_wasm_module from "planetwars-rs";
  import planetwars_wasm_module from "planetwars-rs/planetwars_rs_bg.wasm?url";
  import { PLAYER_COLORS } from "$lib/constants";

  export let matchLog = null;
  export let matchData: object; // match object as returned by api

  let initialized = false;

  onMount(async () => {
    await init_wasm_module(planetwars_wasm_module);

    visualizer.init();
    initialized = true;
    visualizer.set_loading(false);
  });

  onDestroy(() => {
    // TODO: do a more thorough cleanup
    visualizer.stop();
  });

  $: if (initialized) {
    if (matchLog === null) {
      visualizer.set_loading(true);
    } else {
      console.log(matchLog);
      let instanceLog = extractGameStates(matchLog);
      visualizer.set_instance(instanceLog);
      visualizer.set_loading(false);
    }
  }

  function extractGameStates(matchLog: string): string {
    // TODO: find a better way to do this
    return matchLog
      .split("\n")
      .slice(0, -1)
      .filter((line) => JSON.parse(line)["type"] == "gamestate")
      .join("\n");
  }
</script>

<div id="main" class="loading">
  <canvas id="canvas" />
  <div id="name" />
  <div class="ui-topright">
    <slot name="menu" />
    <ul class="player-labels">
      {#each matchData["players"] as player, i}
        <li style="color:{PLAYER_COLORS[i]}">{player["bot_name"] || "player"}</li>
      {/each}
    </ul>
  </div>

  <div id="meta">
    <div id="turnCounter">0 / 0</div>
    <div>
      <span>Ms per frame:&nbsp;</span>
      <input type="number" id="speed" value="300" />
    </div>
    <div class="slidecontainer">
      <input type="range" min="0" max="1" value="1" class="slider" id="turnSlider" />
    </div>
  </div>
  <div class="lds-roller">
    <div />
    <div />
    <div />
    <div />
    <div />
    <div />
    <div />
    <div />
  </div>
</div>

<style scoped>
  @import "pw-visualizer/src/style.css";

  .ui-topright {
    position: absolute;
    top: 10px;
    right: 10px;
    color: white;
  }

  .player-labels {
    list-style: none;
    padding-left: 0;
    margin-top: 0.5em;
  }

  .player-labels li {
    text-align: right;
    margin-bottom: 0.5em;
  }
</style>