aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/components/log_viewer/LogTurn.svelte
blob: bfd2a37f7a44d9be3425955fd9fdc5e29b21c5ed (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
<script lang="ts">
  import type { PlayerLogTurn } from "$lib/log_parser";
  import Fa from "svelte-fa";
  import { faAngleRight, faAngleDown, faCopy } from "@fortawesome/free-solid-svg-icons";

  export let turnNum: number;
  export let logTurn: PlayerLogTurn;
  export let copyTurn: () => void;
  let expanded = false;

  const PLURAL_MAP = {
    dispatch: "dispatches",
    ship: "ships",
  };

  function pluralize(num: number, word: string): string {
    if (num == 1) {
      return `1 ${word}`;
    } else {
      return `${num} ${PLURAL_MAP[word]}`;
    }
  }

  function toggleExpand() {
    expanded = !expanded;
  }
</script>

<div class="turn">
  <div class="turn-header" on:click={toggleExpand}>
    <span>
      <span class="turn-header-icon">
        {#if expanded}
          <Fa icon={faAngleDown} />
        {:else}
          <Fa icon={faAngleRight} />
        {/if}
      </span>
      <span class="turn-header-text">
        Turn {turnNum}
      </span>
    </span>
    {#if logTurn.action?.type === "dispatches"}
      {pluralize(logTurn.action.dispatches.length, "dispatch")}
    {:else if logTurn.action?.type === "timeout"}
      <span class="turn-error">timeout</span>
    {:else if logTurn.action?.type === "bad_command"}
      <span class="turn-error">invalid command</span>
    {/if}
  </div>
  {#if expanded}
    <div class="turn-content">
      <div class="copy-turn" on:click={copyTurn}>
        <Fa icon={faCopy} />
        copy turn to clipboard
      </div>
      {#if logTurn.action?.type === "dispatches" && logTurn.action.dispatches.length > 0}
        <div class="dispatches-container">
          <div class="dispatches-header">dispatches</div>
          {#each logTurn.action.dispatches as dispatch}
            <div class="dispatch">
              <div class="dispatch-text">
                {pluralize(dispatch.ship_count, "ship")} from {dispatch.origin} to {dispatch.destination}
              </div>
              {#if dispatch.error}
                <span class="dispatch-error">{dispatch.error}</span>
              {/if}
            </div>
          {/each}
        </div>
      {:else if logTurn.action?.type === "bad_command"}
        <div class="bad-command-container">
          <div class="bad-command-text">{logTurn.action.command}</div>
          <div class="bad-command-error">Parse error: {logTurn.action.error}</div>
        </div>
      {/if}
      {#if logTurn.stderr.length > 0}
        <div class="stderr-header">stderr</div>
        <div class="stderr-text-box">
          {#each logTurn.stderr as stdErrMsg}
            <div class="stderr-text">{stdErrMsg}</div>
          {/each}
        </div>
      {/if}
    </div>
  {/if}
</div>

<style lang="scss">
  .turn {
    // padding: 4px 2px;
    color: #ccc;
  }

  .turn-header-icon {
    // padding-right: 0.2em;
    display: inline-block;
    width: 1em;
  }

  .turn-header {
    display: flex;
    justify-content: space-between;
  }

  .turn-header:hover {
    cursor: pointer;
    background-color: #333;
  }

  .copy-turn {
    margin: 4px 0;
  }

  .copy-turn:hover {
    text-decoration: underline;
    cursor: pointer;
    color: #fff;
  }

  .turn-header-text {
    color: #eee;
    font-size: 14px;
    font-weight: 600;
    text-transform: uppercase;
  }

  .turn-content {
    margin-bottom: 12px;
    margin-left: 8px;
  }

  .turn-error {
    color: red;
  }

  .dispatches-header {
    color: #fff;
    padding-top: 4px;
  }

  .dispatch {
    display: flex;
    justify-content: space-between;
    padding-left: 8px;
  }

  .dispatch-error {
    color: red;
  }

  .bad-command-container {
    border-left: 1px solid red;
    margin-left: 4px;
    padding-left: 8px;
  }

  .bad-command-text {
    font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
    padding-bottom: 4px;
  }

  .bad-command-error {
    color: whitesmoke;
  }

  .stderr-text {
    // font-family: monospace;
    font-family: "Consolas", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
    white-space: pre-wrap;
  }

  .stderr-header {
    color: #eee;
    padding-top: 4px;
  }

  .stderr-text-box {
    border-left: 1px solid #ccc;
    margin-left: 4px;
    padding-left: 8px;
  }
</style>