-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
358 lines (310 loc) · 9.54 KB
/
main.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const express = require('express');
const bodyParser = require('body-parser');
const _ = require('lodash');
const path = require('path');
const cron = require('node-cron');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const PORT = process.env.PORT || 8080;
const VERSION = require('./package.json').version;
app.use(express.static('./public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ strict: true }));
const Member = require('./core/Member');
const Lobby = require('./core/Lobby');
const Persistence = require('./core/Persistence');
const GAMES = require('./gameInfo.js');
const EMOTES = [
'smile',
'meh',
'frown',
'heart',
'bug',
'hand rock',
'hand paper',
'hand scissors',
'question',
'exclamation',
'wait',
'write',
'check',
'times',
'thumbs up',
'thumbs down',
];
io.on('connection', socket => {
const player = new Member(socket);
socket.emit('member:id', player.id);
socket.emit('version', VERSION);
socket.on('member:name', name => {
if(!player.lobby) {
socket.emit('lobby:leave');
return;
}
player.interact();
// remove zero width no break spaces, trim spaces
name = name.replace(/[\u200B-\u200D\uFEFF\n\t]/g, '').trim()
if(name.length > 0 && name.length < 16) {
player.name = name;
socket.emit('member:nameOk', true);
if(player.lobby) {
player.lobby.updateMembers();
player.lobby.sendLobbyInfo();
}
} else {
socket.emit('member:nameOk', false);
}
});
// Create a lobby if the player is not in one
socket.on('lobby:create', () => {
if(!player.lobby) {
player.interact();
const lobby = new Lobby();
const code = lobby.code;
player.lobby = lobby;
Lobby.lobbies[code] = lobby;
socket.emit('lobby:join', code);
Lobby.lobbies[code].addMember(player);
console.log(new Date(), `-- [lobby ${code}] created`);
}
});
// Allow players to request current lobby info
socket.on('lobby:info', () => {
if(player.lobby) {
player.socket.emit('lobby:info', player.lobby.genLobbyInfo());
}
});
// Allow players to request current lobby info
socket.on('game:info', () => {
const lobby = player.lobby;
if(lobby && lobby.game) {
const game = lobby.game;
player.socket.emit('game:info', game.getState());
lobby.getPlayerState(player.id);
}
});
// Let a player join a lobby with a code
socket.on('lobby:join', code => {
code = code.toLowerCase();
if(!player.lobby && Lobby.lobbyExists(code)) {
player.interact();
// lobby is stored in persistence, load it into memory
if (!Lobby.lobbies[code]) {
console.log(new Date(), `-- [lobby ${code}] restored`);
const saveData = Persistence.restoreLobbyState(code);
const lobby = new Lobby(saveData);
Lobby.lobbies[code] = lobby;
}
player.lobby = Lobby.lobbies[code];
socket.emit('lobby:join', code);
Lobby.lobbies[code].addMember(player);
}
});
socket.on('lobby:replace', pid => {
if(player.lobby) {
player.interact();
player.lobby.replacePlayer(player, pid);
} else {
socket.emit('lobby:leave');
}
});
socket.on('lobby:emote', emote => {
if(player.lobby) {
const now = Date.now();
if(now - player.lastEmote < 400 || !EMOTES.includes(emote))
return;
player.activity = now;
player.lastEmote = now;
player.lobby.emitAll('lobby:emote', player.id, emote);
} else {
socket.emit('lobby:leave');
}
});
// Allow an admin player to change what game is being played
socket.on('lobby:game:set', game => {
if(player.isAdmin()) {
player.lobby.setGame(game);
}
});
// Allow an admin player to change what game is being played
socket.on('game:start', game => {
if(player.isAdmin()) {
player.interact();
player.lobby.startGame();
if (player.lobby.game)
console.log(new Date(), `-- [lobby ${player.lobby.code}] started game ${player.lobby.selectedGame}`);
}
});
socket.on('game:end', game => {
if(player.isAdmin()) {
player.interact();
player.lobby.endGame();
console.log(new Date(), `-- [lobby ${player.lobby.code}] ended game ${player.lobby.selectedGame}`);
}
});
// Toggle whether this member is a spectator
socket.on('lobby:spectate', () => {
if(player.lobby) {
player.lobby.toggleSpectate(player);
} else {
socket.emit('lobby:leave');
}
});
// Let admins make players spectators
socket.on('lobby:admin:toggle', targetId => {
if(player.isAdmin && targetId !== player.id) {
const targetPlayer = player.lobby.players.find(p => p.id === targetId);
if(targetPlayer && targetPlayer.member) {
player.lobby.toggleSpectate(targetPlayer.member);
}
}
});
// Change the admin
socket.on('lobby:admin:grant', targetId => {
if(player.isAdmin && targetId !== player.id) {
player.interact();
const targetPlayer = player.lobby.players.find(p => p.id === targetId);
if(targetPlayer && targetPlayer.member) {
player.lobby.admin = targetPlayer.id;
player.lobby.sendLobbyInfo();
}
}
});
// Core gameplay messages
socket.on('game:message', (type, data) => {
if(player.lobby) {
player.interact();
// Error handling
player.lobby.attempt(() => {
player.lobby.gameMessage(player.id, type, data);
});
} else {
socket.emit('lobby:leave');
}
});
// Change game config if the player is an admin
socket.on('lobby:game:config', (name, val) => {
if(player.isAdmin()) {
player.interact();
// Error handling
player.lobby.attempt(() => {
player.lobby.setConfig(name, val);
});
}
});
// Leave the lobby if a player is in one
socket.on('lobby:leave', () => {
player.name = '';
Lobby.removePlayer(player);
});
// Remove the player from a lobby on disconnection
socket.on('disconnect', data => {
Lobby.removePlayer(player);
Member.removePlayer(player);
});
});
// Determine if a lobby exists
app.get('/api/v1/lobby/:code', (req, res) => {
const code = req.params.code.toLowerCase();
if(Lobby.lobbyExists(code)) {
res.status(200).json({
message: 'Lobby Exists',
});
} else {
res.status(404).json({
message: 'Lobby Does Not Exist',
});
}
});
app.get('/api/v1/info', (req, res) => {
let lobbies = 0, games = 0, players = 0, idleLobbies = 0, idlePlayers = 0, lobbyPlayers = 0, rocketcrabs = 0;
const gameDistribution = {}, playerDistribution = {};
for (const c in Lobby.lobbies) {
const l = Lobby.lobbies[c];
if (l.members.length > 1) {
if (l.rocketcrab)
++rocketcrabs;
if (l.game) {
const game = l.selectedGame;
const onlinePlayers = _.countBy(l.players, p => p.connected && !!p.member);
if (!gameDistribution[l.selectedGame]) {
gameDistribution[l.selectedGame] = 0;
playerDistribution[l.selectedGame] = 0;
}
++games;
++gameDistribution[l.selectedGame];
playerDistribution[l.selectedGame] += l.members.length;
players += l.members.length;
} else {
++lobbies;
lobbyPlayers += l.members.length;
}
} else {
++idleLobbies;
idlePlayers += l.members.length;
}
}
res.status(200).json({
clients: io.engine.clientsCount, // number of connected sockets
idleLobbies,
idlePlayers,
lobbies,
lobbyPlayers,
games,
players,
gameDistribution,
playerDistribution,
rocketcrabs,
});
});
// rocketcrab api
app.post('/api/v1/rocketcrab', (req, res) => {
const { game, version } = req.body;
if (version !== 1) return res.status(426).json({message: 'Unsupported version'});
if (!GAMES[game]) return res.status(404).json({message: 'Game not found'});
// create an empty lobby prefixed with rc
const code = Lobby.newCode('rc');
const lobby = new Lobby();
lobby.code = code;
lobby.rocketcrab = true;
lobby.setGame(game);
Lobby.lobbies[code] = lobby;
console.log(new Date(), `-- [${code}] created rocketcrab for ${game}`);
// respond with the code
return res.json({ code, version: 1 });
});
// handle the application closing - save lobbies if there are any
function exitHandler(options, exitCode) {
// save all the current lobbies
_.each(Lobby.lobbies, lobby => {
if(lobby._saved)
return;
lobby._saved = true;
Persistence.saveLobbyState(lobby);
if(lobby.game) {
lobby.game.stop();
lobby.game.cleanup();
}
});
if (options.cleanup) console.log('clean exit');
if (exitCode || exitCode === 0) console.log('exit code', exitCode);
if (options.exit) process.exit();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
// Cull saves every monday at 4am lol
Persistence.cullSaves();
cron.schedule('0 0 4 * * Monday', Persistence.cullSaves);
// cull empty lobbies every minute
cron.schedule('0 * * * * *', Lobby.cullEmpty);
cron.schedule('0 * * * * *', Member.cullInactive);
// Every request goes through the index, Vue will handle 404s
app.use((req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// Start the webserver
server.listen(PORT, () => console.log(`Started ${VERSION} server on :${PORT}!`));