-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverstate.ts
55 lines (44 loc) · 1.44 KB
/
serverstate.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
52
53
54
55
import { wsHandlers, wsi } from "./deps_int.ts";
import { Game } from "./Game.ts";
const games: Array<Game> = [];
export function createGame(selfId: string, opponentId: string): Game {
const game = new Game(selfId, opponentId);
games.push(game);
return game;
}
export function getGameById(id: string): Game | undefined {
return games.find((game) => game.selfId === id);
}
export function selfInGame(ownId: string): boolean {
return games.find((game) => game.selfId === ownId) !== undefined;
}
export function removeGame(ownId: string) {
const index = games.findIndex((game) => game.selfId === ownId);
if (index !== -1) games.splice(index, 1);
}
const pendingConnectRequests = new Map<string, string>();
export function createConnectRequest(
ownId: string,
opponentId: string
) {
pendingConnectRequests.set(ownId, opponentId);
startConnectRequestTimeout(ownId);
}
export function connectRequestMatches(ownId: string, opponentId: string) {
return pendingConnectRequests.get(ownId) === opponentId;
}
export function removeConnectRequest(ownId: string) {
pendingConnectRequests.delete(ownId);
}
export function existsConnectRequest(id: string) {
return pendingConnectRequests.has(id);
}
function startConnectRequestTimeout(id: string) {
// Connection requests timeout after 20 seconds
setTimeout(() => {
if (pendingConnectRequests.has(id)) {
wsHandlers.handleSendTimeoutMessage(id);
wsi.close(id);
}
}, 10_000);
}