-
Notifications
You must be signed in to change notification settings - Fork 6
/
game.js
103 lines (92 loc) · 2.66 KB
/
game.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file
* Handles games
*/
const Logger = require('./logger.js');
const PlayerPool = require('./playerpool.js');
module.exports = function Game(id, type, token, players, botlevel) {
this.disconnected = false;
this.gc = false;
this.id = id;
this.ip = '';
this.type = type;
this.players = players;
this.token = token;
this.botlevel = botlevel;
// Set players to ingame
this.players.forEach((uid) => {
const player = PlayerPool.getPlayer(uid);
if (player) {
player.setGame(id);
}
});
};
module.exports.prototype.getServerData = function getServerData() {
const players = [];
this.players.forEach((uid) => {
const player = PlayerPool.getPlayer(uid);
if (player && player.data) {
// We overwrite the session token here with the match token to allow for
// reconnection to games across user sessions.
player.data.sid = this.token + uid;
players.push(player.data);
} else {
Logger.log(`player not found (${uid})`);
}
});
if (this.type === 'bot' || this.type === 'custombot') {
players.push({ type: 'bot', level: this.botlevel });
}
return {
matchId: this.id,
type: this.type,
token: this.token,
players,
};
};
module.exports.prototype.setServerIp = function setServerIp(ip) {
this.ip = ip;
};
module.exports.prototype.start = function start() {
this.players.forEach((uid) => {
const player = PlayerPool.getPlayer(uid);
if (player) {
PlayerPool.getPlayer(uid).send('ClientGameServerReady', {
ip: this.ip,
token: this.token + uid,
});
}
});
if (this.type === 'bot' || this.type === 'custombot') {
Logger.log(`Match ${this.id} started on ${this.ip} (${this.type} (${this.botlevel}), ${this.players.toString()})`);
} else {
Logger.log(`Match ${this.id} started on ${this.ip} (${this.type}, ${this.players.toString()})`);
}
};
module.exports.prototype.cancel = function cancel() {
// Tell players about it
this.players.forEach((uid) => {
const player = PlayerPool.getPlayer(uid);
if (player) {
player.setGame(0);
player.send('ClientErrorMessage', 'Could not create game.');
}
});
this.gc = true;
};
module.exports.prototype.isRunning = function isRunning() {
return this.disconnected === false;
};
module.exports.prototype.setRunning = function setRunning() {
this.disconnected = false;
};
module.exports.prototype.presumeDisconnect = function presumeDisconnect() {
this.disconnected = Date.now();
};
module.exports.prototype.destroy = function destroy() {
this.gc = true;
this.players.forEach((uid) => {
const player = PlayerPool.getPlayer(uid);
if (player) player.setGame(0);
});
};