-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
85 lines (65 loc) · 1.79 KB
/
model.js
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
let gameTypes = require("./games");
let onGoingGames = {};
const allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const codeLength = 4;
function generateGameId() {
let id = "";
for (var i = 0; i < codeLength; i++) {
id += allowedChars.charAt(Math.floor(Math.random() * allowedChars.length));
}
return id;
}
const GAME_TIMEOUT = 10 * 60 * 1000; //10 mins
//kill a game, after time has elapsed
function killGame(id) {
console.log("Killing " + id);
//close all websockets
let game = onGoingGames[id];
Object.values(game.allPlayers).forEach((p, i) => {
p.ws.close();
});
//delete the game from memory
delete onGoingGames[id];
}
function resetTimeout(game) {
//clear existing timeout
if (game.timeout) {
clearTimeout(game.timeout);
}
//create a new one
game.timeout = setTimeout(killGame, GAME_TIMEOUT, game.id);
}
exports.resetTimeoutForGame = function (id) {
resetTimeout(onGoingGames[id]);
}
exports.startGame = function (gameType) {
//check if a game type with that id exists
if (gameTypes[gameType]) {
let id = generateGameId();
console.log(id);
let game = new gameTypes[gameType].Game(id);
onGoingGames[id] = game;
resetTimeout(game);
return game;
} else {
console.log("No game of type " + gameType);
//there isn't a type of game with that id
return null;
}
}
exports.getGame = function (id) {
id = id.toUpperCase();
console.log(Object.keys(onGoingGames))
return onGoingGames[id];
}
exports.getGameTypes = () => gameTypes;
class Player {
constructor (username, ws) {
this.username = username;
this.ws = ws;
}
sendMsg(msg) {
this.ws.send(JSON.stringify(msg));
}
}
exports.Player = Player;