-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
51 lines (41 loc) · 1.7 KB
/
index.ts
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
import * as Debug from "~CardLib/Debug";
import { IGameInfo } from "~CardLib/IGameInfo";
import { IGamePresenter } from "~CardLib/Presenter/IGamePresenter";
import Klondike from "~Games/Klondike/GameInfo";
import KlondikeEx from "~Games/KlondikeEx/GameInfo";
import Pyramid from "~Games/Pyramid/GameInfo";
const gameInfos = new Map<string, IGameInfo>();
gameInfos.set(Klondike.gameId, Klondike);
gameInfos.set(KlondikeEx.gameId, KlondikeEx);
gameInfos.set(Pyramid.gameId, Pyramid);
window.addEventListener("load", () => {
const tableHolder = document.getElementById("tableHolder") ?? document.body;
let currentGame: IGamePresenter | undefined;
const refreshGame = () => {
if (currentGame) {
currentGame.dispose();
currentGame = undefined;
}
const hash = window.location.hash;
const qPos = hash.indexOf("?");
let params;
let gameKey;
if (qPos >= 0) {
params = new URLSearchParams(hash.substr(qPos + 1));
gameKey = hash.substr(1, qPos - 1);
} else if (hash.includes("&") || hash.includes("?") || hash.includes("=")) {
params = new URLSearchParams(hash.substr(1));
gameKey = params.get("game");
} else {
params = new URLSearchParams("");
gameKey = hash.substr(1);
}
if (!gameKey) gameKey = Klondike.gameId;
const gameInfo = gameInfos.get(gameKey.toLowerCase());
if (!gameInfo) Debug.error(`Unknown game ${gameKey}.`);
currentGame = gameInfo.gamePresenterFactory.createGame(tableHolder, params);
currentGame.start();
};
window.addEventListener("hashchange", refreshGame);
refreshGame();
});