forked from hofan41/rippednames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
282 lines (199 loc) · 8.91 KB
/
server.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
'use strict';
const App = require('express')();
const GameModule = require('./game');
const Http = require('http').Server(App);
const Io = require('socket.io')(Http);
const Game = GameModule.Game;
const Player = GameModule.Player;
const internals = {};
internals.port = process.env.PORT || 3000;
internals.maxGameInstances = 100;
internals.gameInstances = {};
internals.numUsers = 0;
App.get('/', (req, res) => {
res.sendFile(__dirname + '/html/index.html');
});
Io.on('connection', (socket) => {
internals.numUsers++;
socket.username = '';
socket.gameId = '';
console.log('User has connected');
socket.on('disconnect', () => {
--internals.numUsers;
// If this socket has been assigned a username and a game:
if (socket.username !== '' && socket.gameId !== '') {
// Remove the username from the player list for their game
internals.gameInstances[socket.gameId].players.delete(socket.username);
// Get this socket to leave the room it previously joined
socket.leave(socket.gameId);
console.log(socket.username + ' left game ' + socket.gameId);
// If there are no more players in this game instance:
if (internals.gameInstances[socket.gameId].players.size === 0) {
// Remove the game
delete internals.gameInstances[socket.gameId];
console.log('Removing game ' + socket.gameId);
}
}
console.log('User has disconnected');
});
socket.on('create game', (data, callback) => {
if (data.username === '') {
callback(failStatus('Invalid username'));
console.log(data.username + ' is unable to create new game due to invalid username');
return;
}
// If the current number of concurrent game instances has reached the max:
if (Object.keys(internals.gameInstances) >= internals.maxGameInstances) {
callback(failStatus('Max concurrent game instances reached'));
console.log(data.username + ' is unable to create new game');
return;
}
let gameCreated = false;
let attemptCount = 0;
const maxAttempts = 10;
// Loop until a unique gameId is generated or until all attempts have been exhausted:
do {
const gameId = getRandomGameId();
// If the gameId does not already exist:
if (!internals.gameInstances.hasOwnProperty(gameId)) {
// Associate this socket with the username and gameId
socket.username = data.username;
socket.gameId = gameId;
internals.gameInstances[gameId] = { players: new Set(), game: new Game(gameId, new Player(data.username)) };
// Add the username the players set
internals.gameInstances[gameId].players.add(data.username);
// Join this socket to a room identified by the gameId
socket.join(gameId);
gameCreated = true;
break;
}
++attemptCount;
}
while (attemptCount < maxAttempts);
if (gameCreated) {
callback( { status: 'success', gameId: socket.gameId } );
console.log(data.username + ' created new game ' + socket.gameId);
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers(socket.gameId));
}
else {
callback( failStatus('Unable to create new game') );
console.log(data.username + ' is unable to create new game');
}
});
socket.on('join game', (data, callback) => {
if (data.username === '') {
callback(failStatus('Invalid username'));
console.log(data.username + ' is unable to join game due to invalid username');
return;
}
// If the gameId already exists:
if (internals.gameInstances.hasOwnProperty(data.gameId)) {
// If the client's username is a duplicate of another player in this game:
if (internals.gameInstances[data.gameId].players.has(data.username)) {
callback(failStatus('Unable to join game due to duplicate username'));
console.log(data.username + ' is unable to join game ' + data.gameId + ' due to duplicate username');
return;
}
// Associate this socket with the username and gameId
socket.username = data.username;
socket.gameId = data.gameId;
// Add the username the players set
internals.gameInstances[data.gameId].players.add(data.username);
// Add a new player to the game
internals.gameInstances[data.gameId].game.AddPlayer(new Player(data.username));
// Join this socket to a room identified by the gameId
socket.join(data.gameId);
callback(successStatus());
console.log(data.username + ' joined game ' + data.gameId);
}
// Otherwise, the gameId does not exist:
else {
callback(failStatus('Nonexistant game ID'));
console.log(data.username + ' is unable to join nonexistant game ' + data.gameId);
}
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers(data.gameId));
});
socket.on('select team', (data, callback) => {
if (data.team === 'red') {
internals.gameInstances[socket.gameId].AssignPlayerToTeam(socket.username, 0);
}
else {
internals.gameInstances[socket.gameId].AssignPlayerToTeam(socket.username, 1);
}
callback(successStatus());
console.log(socket.username + ' selected to be on ' + data.team + 'team in game ' + socket.gameId);
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers());
});
socket.on('select as spymaster', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' selected to be a spymaster in game ' + socket.gameId);
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers(socket.gameId));
});
socket.on('randomize team', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' randomized the team in game ' + socket.gameId);
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers(socket.gameId));
});
socket.on('start game', (data, callback) => {
// If the minimum requirements to start the game have been met:
if (internals.gameInstances[socket.gameId].IsReadyToStart()) {
// Start the game
internals.gameInstances[socket.gameId].Start();
callback(successStatus());
console.log(socket.username + ' started game ' + socket.gameId);
Io.to(socket.gameId).emit('update game state', getCurrentPlayers(socket.gameId));
}
//Otherwise, the game is not ready to start:
else {
callback(failStatus('Game cannot start with current settings'));
console.log(socket.username + ' was unable to start game ' + socket.gameId);
}
});
socket.on('reset game', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' reseted game ' + socket.gameId);
Io.to(socket.gameId).emit('update team settings', getCurrentPlayers(socket.gameId));
});
socket.on('provide clue', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' provided clue ' + data.word + ':' + data.count + ' in game ' + socket.gameId);
Io.to(socket.gameId).emit('update game state');
});
socket.on('select word', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' started game ' + socket.gameId);
Io.to(socket.gameId).emit('update game state');
});
socket.on('pass turn', (data, callback) => {
callback(successStatus());
console.log(socket.username + ' started game ' + socket.gameId);
Io.to(socket.gameId).emit('update game state');
});
});
Http.listen(internals.port, () => {
console.log('listening on *:' + internals.port);
});
const getCurrentPlayers = function (gameId) {
if (!internals.gameInstances.hasOwnProperty(gameId)) {
return [];
}
const playerList = [];
for (const player of internals.gameInstances[gameId].players.keys()) {
playerList.push(player);
}
return playerList;
};
const successStatus = () => {
return { status: 'success' };
};
const failStatus = function (msg) {
return { status: 'fail', message: msg };
};
const getRandomGameId = () => {
let randomGameId = '';
const possibleChars = 'abcdefghijklmnopqrstuvwxyz';
for (let i = 0; i < 6; ++i) {
randomGameId += possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));
}
return randomGameId;
};