~starkingdoms/starkingdoms

ref: b8e076d536b7aba4c2ec67e3cc4fa4f2e39ce32f starkingdoms/starkingdoms-client/src/pages/Play.svelte -rw-r--r-- 5.8 KiB
b8e076d5 — core ci work 7 1 year, 8 months ago
                                                                                
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
<script lang="ts">
  import HeartIcon from "../icons/HeartIcon.svelte";

  import createDebug from "debug";
  import { hub_connect, sendPacket } from "../hub.ts";
  import "../css/style.scss";
  import "../css/themes/catppuccin-mocha.scss";
  import { DEFAULT_CONFIG, loadConfig } from "../config.ts";
  import Chatbox from "../components/Chatbox.svelte";
  import { onMount } from "svelte";
  import Popup from "../components/ui/Popup.svelte";
  import Checkbox from "../components/ui/Checkbox.svelte";
  import { global } from "../globals.ts";
  import { PacketType } from "../protocol.ts";
  import * as PIXI from "pixi.js";
  import Button from "../components/ui/Button.svelte";

  let config = DEFAULT_CONFIG;

  let velocity;
  let x_pos;
  let y_pos;
  let energy;

  //let antialiasing: boolean = false;

  let chatbox: Chatbox;

  const logger = createDebug("main");
  logger(
    `Hello, world! StarKingdoms ${APP_VERSION} (${COMMIT_HASH}) at your service!`,
  );
  logger("Current view: /play");

  /*function toggleAntialiasing() {
    global.rendering!.app.renderer.reset();
    PIXI.settings.RENDER_OPTIONS.antialias = antialiasing;
    logger("Antialiasing is now", antialiasing ? "on" : "off");
  }*/

  function handlewheel(e: WheelEvent) {
    let delta = e.shiftKey ? 0.01 : 0.1;
    if (e.deltaY < 0) {
      global.scale += delta;
    } else {
      global.scale -= delta;
    }
    if (global.scale > 5) {
      global.scale = 5;
    } else if (global.scale < 0.05) {
      global.scale = 0.05;
    }
  }

  function beam_out() {
    if (!global.saveEligible) {
      chatbox.addMessage(
        "server-message",
        "[Save] Not eligible to beam out, please get closer to a planet",
      );
      return;
    }
    let req_packet: Packet = {
      t: PacketType.RequestSave,
      c: {
        old_save: window.localStorage.getItem("save"),
      },
    };
    sendPacket(global.client!, req_packet);
    global.leaving = true;
  }

  onMount(async () => {
    config = await loadConfig();

    chatbox.addMessage("server-message", "Connecting to the game server...");

    let params = new URLSearchParams(window.location.search);

    if (!params.has("srv")) {
      chatbox.addMessage(
        "server-error",
        "Server ID missing. Redirecting to main menu in 5 seconds.",
      );
      setTimeout(() => {
        window.location.href = "/";
      }, 5000);
      return;
    }
    if (!params.has("username")) {
      chatbox.addMessage(
        "server-error",
        "Username missing. Redirecting to main menu in 5 seconds.",
      );
      setTimeout(() => {
        window.location.href = "/";
      }, 5000);
      return;
    }
    let server_id = Number.parseInt(params.get("srv")!);
    let username = params.get("username")!;

    if (!config.environments[server_id]) {
      chatbox.addMessage(
        "server-error",
        "The selected server is currently unavailable. Redirecting to main menu in 5 seconds.",
      );
      setTimeout(() => {
        window.location.href = "/";
      }, 5000);
      return;
    }

    let server = config.environments[server_id];

    await hub_connect(
      server.clientHubUrl,
      username,
      chatbox,
      velocity,
      x_pos,
      y_pos,
      energy,
    );
  });
</script>

<div class="game" id="gamewindow" on:wheel={handlewheel}></div>

<Popup
  draggable
  title="Packet Log"
  style="max-width: 300px; width: min-content;"
  class="log-hidden log-container"
  id="packet-log">
  <table class="log">
    <thead>
      <tr class="log-wfull">
        <th class="log-wfull log-lalign log-header log-w50">#</th>
        <th class="log-wfull log-lalign log-header">Dir</th>
        <th class="log-wfull log-lalign log-header log-w240">Type</th>
        <th class="log-wfull log-lalign log-header">Delta</th>
      </tr>
    </thead>
    <tbody id="log_body"></tbody>
  </table>
  <hr />
  <h1>Packet Explorer</h1>
  <p id="explorer_selected" class="mono">Selected: --</p>
  <table class="mono json" id="explorer_json"></table>
</Popup>

<Chatbox bind:this={chatbox} />

<div class="options">
  <Popup
    title="Options"
    id="options"
    draggable
    minimizable
    style="width: 20vw; position: absolute;">
    <label for="dev_mode">Developer mode (Advanced)</label>
    <Checkbox
      title="Enables tools intended only for the developers. Buggy. Use at your own risk." />
  </Popup>
</div>

<div class="hud" id="hud">
  <Popup notitle title="" id="hud-content-wrapper">
    <table>
      <thead>
        <tr>
          <th></th>
          <th class="hud-d">Position</th>
          <th class="hud-d">Velocity</th>
          <th class="hud-d">Track Angle</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td id="leave">
            <Button on:click={beam_out}>Beam out</Button>
          </td>
          <td id="pos">
            <span id="pos-val-x" bind:this={x_pos}>--</span>
            ,
            <span id="pos-val-y" bind:this={y_pos}>--</span>
          </td>
          <td id="velocity">
            <span id="velocity-val" bind:this={velocity}>--</span>
          </td>
          <td id="track">
            <span id="track-val">--</span>
          </td>
        </tr>
        <tr>
          <td></td>
          <td class="hud-d"><strong>Energy:</strong></td>
          <td
            colspan="2"
            id="energy-bg"
            style="height:100%;width:100%;padding:0;box-sizing:border-box;border: 0.125em solid rgb(var(--text));border-radius: 0.5em;overflow:hidden;">
            <div
              id="energy"
              style="height:22px;background-color:#Af8f4f;"
              bind:this={energy}>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </Popup>
</div>

<span class="footer-left">
  StarKingdoms Client {APP_VERSION} ({COMMIT_HASH})
</span>
<span class="footer-right">
  Made with <HeartIcon class="footer-icon" /> by the StarKingdoms team
</span>