aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
blob: f21a0381ab4666e2f85d4e519148754267580b01 (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
<script lang="ts">
  import { parsePlayerLog, PlayerLog } from "$lib/log_parser";
  import LogTurn from "./LogTurn.svelte";

  export let matchLog: string;
  export let matchData: object;
  export let playerId: number;

  let playerLog: PlayerLog;
  let showRawStderr = false;

  async function copyTurn(turnNum: number) {
    // find state for turnNum
    let gamestate = matchLog
      .split("\n")
      .slice(0, -1)
      .map((line) => JSON.parse(line))
      .filter((json) => json["type"] == "gamestate")
      .at(turnNum);

    let numPlayers = matchData["players"].length;
    let rotatePlayerNum = (playerNum: number | null) => {
      if (playerNum === null) {
        return null;
      }
      return ((numPlayers + playerNum - playerId) % numPlayers) + 1;
    };

    gamestate["planets"].forEach((planet) => {
      planet["owner"] = rotatePlayerNum(planet["owner"]);
    });
    gamestate["expeditions"].forEach((expedition) => {
      expedition["owner"] = rotatePlayerNum(expedition["owner"]);
    });

    await navigator.clipboard.writeText(JSON.stringify(gamestate));
  }

  $: if (matchLog) {
    playerLog = parsePlayerLog(playerId, matchLog);
  } else {
    playerLog = [];
  }
</script>

<div class="output">
  {#if showRawStderr}
    <div class="output-text stderr-text">
      {playerLog.flatMap((turn) => turn.stderr).join("\n")}
    </div>
  {:else}
    <!-- The log should be rerendered when playerId changes -->
    {#key playerId}
      <div class="log-contents">
        {#each playerLog as logTurn, turnNum}
          <LogTurn {logTurn} {turnNum} copyTurn={() => copyTurn(turnNum)} />
        {/each}
      </div>
    {/key}
  {/if}
</div>

<style lang="scss">
  .output {
    background-color: rgb(41, 41, 41);
  }
</style>