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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
import { byId, money, node, sha256Hex, toast, toneFor } from "./dom";
import { Gateway } from "./net";
import { renderBlackjack } from "./blackjack";
import { renderCrash, stopCrashAnimation } from "./crash";
import { renderHistory } from "./history";
import { renderRoulette } from "./roulette";
import { renderSlots, stopSlotsAnimation } from "./slots";
import type { View } from "./types";
const authScreen = byId("auth-screen");
const lobbyScreen = byId("lobby-screen");
const gameScreen = byId("game-screen");
const historyScreen = byId("history-screen");
const sessionBar = byId("session-bar");
const accountName = byId("account-name");
const accountBalance = byId("account-balance");
const gameGrid = byId("game-grid");
const gameBoard = byId("game-board");
const gameSide = byId("game-side");
const gameTitle = byId("game-title");
const gameBadge = byId("game-badge");
const historyBody = byId("history-body");
const authForm = byId<HTMLFormElement>("auth-form");
const authUser = byId<HTMLInputElement>("auth-user");
const authPass = byId<HTMLInputElement>("auth-pass");
const authError = byId("auth-error");
const authSubmit = byId<HTMLButtonElement>("auth-submit");
const tabLogin = byId<HTMLButtonElement>("tab-login");
const tabRegister = byId<HTMLButtonElement>("tab-register");
const reconnectOverlay = byId("reconnect-overlay");
const reconnectMessage = byId("reconnect-message");
const replacedOverlay = byId("replaced-overlay");
let view: View | null = null;
let currentUser: string | null = null;
let lastBalance: number | null = null;
let authMode: "login" | "register" = "login";
let infoFor: string | null = null;
let serverOffset = 0;
let reconnectAttempts = 0;
const bets = new Map<string, number>([
["slots", 10],
["roulette", 10],
["blackjack", 10],
["crash", 10],
]);
const titles: Record<string, string> = {
slots: "Slots",
roulette: "Roulette",
blackjack: "Blackjack",
crash: "Crash",
};
const descriptions: Record<string, string> = {
slots: "Five reels, five paylines. Match symbols from the left.",
roulette: "European single-zero wheel with inside and outside bets.",
blackjack: "Six-deck shoe. Blackjack pays 3:2.",
crash: "A rising multiplier. Cash out before it crashes.",
};
const basePath = location.pathname.includes("/play/")
? location.pathname.slice(0, location.pathname.indexOf("/play/"))
: "";
const wsUrl = `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}${basePath}/ws`;
const gateway = new Gateway(wsUrl, {
onHello(hello) {
serverOffset = hello.time - Date.now();
currentUser = hello.user ?? null;
gameBadge.textContent = `v${hello.version}`;
},
onView(next) {
view = next;
if (next.screen !== "slots") stopSlotsAnimation();
if (next.screen !== "crash") stopCrashAnimation();
// Game and history views carry no account field; the lobby is the source
// of truth for who is signed in. A null lobby user is a logout or an
// expired session.
if (typeof next.balance === "number") lastBalance = next.balance;
if (next.screen === "lobby" && "user" in next) {
currentUser = (next.user as string | null) ?? null;
if (currentUser) authPass.value = "";
else lastBalance = null;
}
reconnectAttempts = 0;
reconnectOverlay.hidden = true;
render();
if (currentUser && view.balance === null && infoFor !== currentUser) {
infoFor = currentUser;
gateway.send("Info");
}
if (!currentUser) infoFor = null;
},
onNotice(text) {
if (
text.startsWith("Connected to:") ||
text.startsWith("Fingerprint:") ||
text.startsWith("User:") ||
text.startsWith("Account:") ||
text.startsWith("Balance:")
) {
return;
}
toast(text, toneFor(text));
if (text.startsWith("Account created")) {
setAuthMode("login");
authPass.value = "";
authUser.focus();
}
authSubmit.disabled = false;
authSubmit.textContent = authMode === "login" ? "Log in" : "Create account";
},
onError(text) {
toast(text, "bad");
authSubmit.disabled = false;
authSubmit.textContent = authMode === "login" ? "Log in" : "Create account";
},
onReplaced() {
replacedOverlay.hidden = false;
},
onPong() {
/* The gateway answered; the socket is alive. */
},
onOpen() {
reconnectOverlay.hidden = true;
},
onClosed(willRetry) {
if (replacedOverlay.hidden === false) return;
if (!willRetry) {
reconnectMessage.textContent = "The connection could not be restored. Reload the page to try again.";
reconnectOverlay.hidden = false;
return;
}
reconnectAttempts += 1;
reconnectMessage.textContent =
reconnectAttempts > 1
? "Still trying to reach the game server…"
: "Trying to reach the game server…";
reconnectOverlay.hidden = false;
},
});
function send(command: unknown): void {
gateway.send(command);
}
function serverNow(): number {
return Date.now() + serverOffset;
}
function setAuthMode(mode: "login" | "register"): void {
authMode = mode;
tabLogin.classList.toggle("active", mode === "login");
tabRegister.classList.toggle("active", mode === "register");
authSubmit.textContent = mode === "login" ? "Log in" : "Create account";
authPass.autocomplete = mode === "login" ? "current-password" : "new-password";
authError.textContent = "";
}
function show(screen: "auth" | "lobby" | "game" | "history"): void {
authScreen.hidden = screen !== "auth";
lobbyScreen.hidden = screen !== "lobby";
gameScreen.hidden = screen !== "game";
historyScreen.hidden = screen !== "history";
}
function setBalance(balance: number | null | undefined): void {
accountBalance.textContent = money(balance ?? null);
}
function render(): void {
if (!view) return;
const user = currentUser;
if (user) {
sessionBar.hidden = false;
accountName.textContent = String(user);
} else {
sessionBar.hidden = true;
}
setBalance(lastBalance);
if (!user) {
show("auth");
authError.textContent = view.login_error ? String(view.login_error) : "";
if (view.screen === "lobby" && view.connected === false) {
authError.textContent = "The game server is not reachable yet. Retrying…";
}
return;
}
switch (view.screen) {
case "lobby":
show("lobby");
renderLobby(view);
break;
case "history":
show("history");
renderHistory(historyBody, view);
break;
case "slots":
case "roulette":
case "blackjack":
case "crash":
show("game");
gameTitle.textContent = titles[view.screen] ?? view.screen;
gameBadge.textContent = badge(view);
if (view.screen === "slots") renderSlots(gameBoard, gameSide, view, send);
else if (view.screen === "roulette") renderRoulette(gameBoard, gameSide, view, send);
else if (view.screen === "blackjack") renderBlackjack(gameBoard, gameSide, view, send);
else renderCrash(gameBoard, gameSide, view, send, { now: serverNow });
break;
default:
show("auth");
}
}
function badge(view: View): string {
if (view.screen === "crash") return String(view.status ?? "");
if (view.screen === "blackjack") return String(view.status ?? "");
if (view.screen === "slots") return String(view.phase ?? "");
if (view.screen === "roulette") return String(view.phase ?? "");
return "";
}
function renderLobby(lobby: View): void {
gameGrid.replaceChildren();
const list: [string, string][] = lobby.games ?? [];
for (const [id, name] of list) {
const card = node("article", `game-card ${id}`);
card.append(node("h3", undefined, name || titles[id] || id));
card.append(node("p", undefined, descriptions[id] ?? "Play a round."));
const row = node("div", "row wrap");
const label = node("label", "field");
label.style.flex = "1";
label.append(node("span", "label", "Bet"));
const input = node("input", "input bet-input") as HTMLInputElement;
input.type = "number";
input.min = "1";
input.step = "10";
input.value = String(bets.get(id) ?? 10);
input.addEventListener("change", () => {
const value = Math.max(1, Math.floor(Number(input.value) || 1));
input.value = String(value);
bets.set(id, value);
});
label.append(input);
const play = node("button", "btn primary", id === "roulette" ? "Open table" : "Play");
play.addEventListener("click", () => {
const bet = Math.max(1, Math.floor(Number(input.value) || 1));
bets.set(id, bet);
send({ Play: { game: id, bet } });
});
row.append(label, play);
card.append(row);
gameGrid.append(card);
}
if (list.length === 0) {
gameGrid.append(node("p", "muted", "The server did not advertise any games."));
}
}
// Auth tabs and form -------------------------------------------------------
tabLogin.addEventListener("click", () => setAuthMode("login"));
tabRegister.addEventListener("click", () => setAuthMode("register"));
authForm.addEventListener("submit", async (event) => {
event.preventDefault();
const user = authUser.value.trim();
const password = authPass.value;
if (!user || !password) {
authError.textContent = "Enter a username and a password.";
return;
}
authSubmit.disabled = true;
authSubmit.textContent = "Working…";
authError.textContent = "";
try {
const credential = await sha256Hex(password);
if (authMode === "login") {
send({ Login: { user, credential } });
} else {
send({ Register: { user, credential } });
}
} catch {
authError.textContent = "This browser cannot hash the password (needs a secure context).";
authSubmit.disabled = false;
authSubmit.textContent = authMode === "login" ? "Log in" : "Create account";
}
});
// Chrome and navigation ----------------------------------------------------
byId("logout").addEventListener("click", () => send("Logout"));
byId("show-history").addEventListener("click", () => send({ History: { limit: 50 } }));
byId("refresh-lobby").addEventListener("click", () => send("Info"));
byId("history-back").addEventListener("click", () => send("ShowLobby"));
byId("leave-game").addEventListener("click", () => {
if (!view) return;
const live =
(view.screen === "blackjack" && view.status === "player") ||
(view.screen === "crash" && view.status === "running");
if (live && !window.confirm("Leaving now forfeits the active bet. Continue?")) return;
send("Leave");
});
byId("reload-replaced").addEventListener("click", () => location.reload());
byId("reload-reconnect").addEventListener("click", () => location.reload());
window.addEventListener("resize", () => {
window.clearTimeout((window as any).__resizeTimer);
(window as any).__resizeTimer = window.setTimeout(() => {
if (view) render();
}, 140);
});
// A periodic ping keeps the gateway session visibly alive and refreshes the
// server clock estimate without changing game state.
window.setInterval(() => {
if (currentUser) gateway.ping();
}, 25_000);
gateway.connect();
|