~starkingdoms/starkingdoms

ref: 4c76d73942c6e2ed2e0842f3e38a6f51d76e32da starkingdoms/starkingdoms-client/src/pages/Home.svelte -rw-r--r-- 4.8 KiB
4c76d739 — ghostlyzsh fmt 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
<script lang="ts">
  import { DEFAULT_CONFIG, loadConfig } from "../config.ts";
  import createDebug from "debug";
  import "../css/themes/catppuccin-mocha.scss";
  import "../css/style.scss";
  import { parseJwt } from "../jwt.ts";
  import HeartIcon from "../icons/HeartIcon.svelte";
  import WarningIcon from "../icons/WarningIcon.svelte";
  import Popup from "../components/ui/Popup.svelte";
  import Button from "../components/ui/Button.svelte";
  import TextInput from "../components/ui/TextInput.svelte";
  import ColumnTable from "../components/ui/ColumnTable.svelte";
  import Column from "../components/ui/Column.svelte";
  import VerticalRule from "../components/ui/VerticalRule.svelte";
  import HorizontalGap from "../components/ui/HorizontalGap.svelte";

  let config = DEFAULT_CONFIG;
  // Top-level await. Sets the default config, and overwrites it when the new config is avail. Thanks reactivity!
  (async () => {
    config = await loadConfig();
  })();

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

  let is_logged_in = false;
  let token_username = undefined;

  if (window.localStorage.getItem("stk-token") != null) {
    let token = window.localStorage.getItem("stk-token")!;
    let token_parsed: any = parseJwt(token);
    if (token_parsed !== undefined) {
      is_logged_in = true;
      token_username = token_parsed.username;
    }
  }

  const is_development = window.localStorage.getItem("stk-mode") === "debug";

  let nonprod_warning: HTMLSpanElement;

  let server: number = 0;
  let username = "";

  function playGame() {
    window.location.href = `/play/?srv=${server}&username=${username}`;
  }

  function updateNonprodWarning() {
    if (
      config.environments[server] !== undefined &&
      !config.environments[server].isProduction
    ) {
      nonprod_warning.classList.remove("hidden");
    } else {
      nonprod_warning.classList.add("hidden");
    }
  }
</script>

<Popup title="StarKingdoms" centered id="title-popup" style="max-width: 800px;">
  <ColumnTable>
    <Column>
      <h2>About</h2>
      StarKingdoms is a browser game about floating through space.
    </Column>
    <VerticalRule />
    <Column>
      <h2>Join Game</h2>

      <form id="join-fm" class="form" on:submit|preventDefault={playGame}>
        <label for="username" class="label">Username</label>
        <TextInput
          style="width: 100%; max-width: 100%;"
          bind:value={username}
          id="username"
          required
          autocomplete="off" />

        <label class="label" for="select">Server</label>
        <select
          bind:value={server}
          on:change={updateNonprodWarning}
          id="select"
          class="select"
          autocomplete="off"
          required>
          <option disabled value="">
            <em>Choose a server..</em>
          </option>
          {#each config.environments as env, i}
            {#if !(env.isDevelopment && !is_development)}
              <option value={i}>{env.name}</option>
            {/if}
          {/each}
        </select>

        <Button style="width: 100%; max-width: 100%;">Launch!</Button>
      </form>
    </Column>
    <VerticalRule />
    <Column>
      <h2>Tools & Options</h2>
      <Button style="width: 100%;" disabled>Options (soon!)</Button>
      <HorizontalGap />
      {#if window.localStorage.getItem("save") !== null}
        <a href="/shipeditor/" style="color: var(--text)">
          <Button style="width: 100%;">Ship Editor</Button>
        </a>
      {/if}
    </Column>
  </ColumnTable>
  <div>
    <span bind:this={nonprod_warning} class="server-danger hidden">
      <WarningIcon class="server-danger-icon" />
      Here be dragons! You have a
      <b>prerelease server</b>
      selected. Expect bugs, and save data on this server may be wiped at any time.
    </span>

    <span id="account-info" class="account-info">
      {#if is_logged_in}
        Logged in as {token_username}! Saves will be stored in your browser
        during the alpha, but will be stored on the server soon!
        <!-- TODO: Make this not a <a> (messing with styles) -->
        <a
          href="#"
          on:click={() => {
            window.localStorage.removeItem("stk-token");
            window.location.reload();
          }}>
          Logout
        </a>
      {:else}
        You are not logged in. Save data will be stored in your browser cache. <a
          href="/login/">
          Log in
        </a>
        to save your data on the server and sync it between devices.
      {/if}
    </span>
  </div>
</Popup>

<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>