~starkingdoms/starkingdoms

ref: ccb7ac1036a620f213bb5d24a3332091074e3667 starkingdoms/starkingdoms-client/src/routes/ingame/main.ts -rw-r--r-- 2.6 KiB
ccb7ac10 — ghostlyzsh Merge branch 'bevy_rewrite' of https://gitlab.com/starkingdoms.tk/starkingdoms.tk into bevy_rewrite 2 years 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
import createDebug from "debug";
import { ClientHub, hub_connect } from "../../hub.ts";
import "../../css/style.scss";
import "../../css/themes/catppuccin-mocha.scss";
import { Part, Planet } from "../../protocol.ts";
import * as PIXI from "pixi.js";
import { addMessage } from "../../chat.ts";
import { loadConfig } from "../../config.ts";

export interface GlobalData {
  client: ClientHub | null;
  me: GlobalMe | null;

  players_map: Map<number, string>;
  inverse_players_map: Map<string, number>;

  planets_map: Map<number, Planet>;

  parts_map: Map<number, Part>;

  up: boolean;
  down: boolean;
  left: boolean;
  right: boolean;

  rendering: GlobalRendering | null;
}

export interface GlobalRendering {
  app: PIXI.Application;
  player_text_map: Map<number, PIXI.Text>;
  planet_sprite_map: Map<number, PIXI.Sprite>;
  part_sprite_map: Map<number, PIXI.Sprite>;
}

export interface GlobalMe {
  username: string;
  part_id: number;
}

export const global: GlobalData = {
  client: null,
  me: null,
  players_map: new Map(),
  inverse_players_map: new Map(),
  planets_map: new Map(),
  parts_map: new Map(),
  up: false,
  down: false,
  left: false,
  right: false,
  rendering: null,
};

async function init() {
  (window as any).__initialized = true;
  // @ts-ignore
  init = undefined;

  const config = await loadConfig();

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

  document.getElementById("footer-left")!.innerHTML =
    `StarKingdoms Client ${APP_VERSION} (${COMMIT_HASH})`;

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

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

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

  let server_id = params.get("srv")!;
  let username = params.get("username")!;

  let server = config.servers[server_id];

  await hub_connect(server.clientHubUrl, username);
}

if ((window as any).__initialized === undefined) {
  init();
}

export function player(): Part | undefined {
  if (global.me !== null) {
    return global.parts_map.get(global.me!.part_id);
  } else {
    return undefined;
  }
}