forked from jiang-it/no-thanks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
417 lines (378 loc) · 10.8 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* This is the client's game implementation
* game.js
*/
/**
* io is the global io structure
* io.games is an associative array indexed by gameID
* Each entry is an associative array {
* gameID : string
* playerIDs : string array
* inProgress : bool
* finished : bool
* game = {
* playerOrder : string array
* currentDeck : int array
* cardsThrownOut : int array
* currentCard : int
* currentBid : int
* currentTurn : int
* foreach player in playerIDs = {
* hand : int array
* coins : int
* }
* }
* }
*/
/**
* Players may:
* Connect : 'connect'
* Create a room : 'create'
* Join a room : 'join', msg.gameID
* Start a game : 'start'
* Pass : 'pass'
* Pick up a card : 'take'
* End a game : 'end'
* Leave a room : 'leave'
*/
exports.initGame = function(io, gameSocket) {
// Client error problem reporting
// Reports an error back to the client
// The client has sent back an invalid option
function problem(errorMessage) {
gameSocket.emit('problem', {message : errorMessage});
}
gameSocket.on('create', msg => {
let gameID = ((Math.random() * 1000) | 0).toString();
if (gameSocket.gameID) { problem(`Already in game, gameID: ${gameSocket.gameID}`); }
while (io.games[gameID]) { gameID = ((Math.random() * 1000) | 0).toString(); }
io.games[gameID] = {
gameID : gameID,
playerIDs : [msg.username],
inProgress : false,
finished : false,
};
gameSocket.gameID = gameID;
gameSocket.emit('created', { gameID : gameID, username : msg.username });
gameSocket.join(gameID);
gameSocket.emit('joined', { gameID : gameID, playerIDs : io.games[gameID].playerIDs });
});
gameSocket.on('join', msg => {
if ((!msg) || (!msg.gameID)) {
problem('No gameID specified');
return;
}
gameID = msg.gameID;
if (!io.games[gameID]) {
problem(gameID + ' does not match a gameID in session. Create a new game to start a new one');
return;
}
if (gameSocket.gameID) {
problem('Already in game, gameID: ' + gameSocket.gameID);
return;
}
if (io.games[gameID].inProgress) {
problem('Game has already started');
return;
}
if (io.games[gameID].playerIDs.length >= 6) {
problem('Game is already filled to max (6)');
return;
}
if (io.games[gameID].playerIDs[msg.username]) {
problem('Username is already taken');
return;
}
gameSocket.gameID = gameID;
io.games[gameID].playerIDs.push(msg.username);
io.sockets.in(gameID).emit('newplayer', {username : msg.username});
gameSocket.join(gameID);
gameSocket.emit('joined', {
gameID : gameID,
username: msg.username,
playerIDs : io.games[gameID].playerIDs})
});
// startGame is called when the user tries starting the game they are in
function startGame(msg) {
// Figure out which game room the socket is currently in
gameID = gameSocket.gameID;
// Throw error if
// User is not in a game
// The game has already been started
if (!gameID) {
problem('Not currently in a game. Create a new game to play');
return;
}
if (!io.games[gameID]) {
// Server side error!
problem('Server is exploding');
// console.log('Horrible Error, gameId not found in games list');
return;
}
if (io.games[gameID].inProgress) {
problem('Game has already started');
return;
}
// Set the struct to start
io.games[gameID].finished = false;
io.games[gameID].inProgress = true;
// Then set up that game struct
// Shuffle the deck
// Remove some cards (5)
// Choose an order for players
fullDeck = shuffleFullDeck();
removedCards = fullDeck.splice(0,4);
playerOrder = shuffle(io.games[gameID].playerIDs);
game = {
playerOrder : playerOrder,
currentDeck : fullDeck,
cardsThrownOut : removedCards,
currentCard : fullDeck.pop(),
currentBid : 0,
currentTurn : 0,
};
// Add each player to the game
for (var i = 0; i < game.playerOrder.length; i++) {
game[game.playerOrder[i]] = {
hand : [],
coins : 8
};
}
io.games[gameID].game = game;
// Then let everyone in the room know that the game has started.
io.sockets.in(gameID).emit('started', {
playerOrder : game.playerOrder
});
// Let everyone know whose turn it is
io.sockets.in(gameID).emit('turn', {
username : game.playerOrder[(game.currentTurn % game.playerOrder.length)],
card : game.currentCard,
bid : game.currentBid
});
}
gameSocket.on('start', startGame);
function takeCard(msg) {
gameID = gameSocket.gameID;
if (!gameID) {
problem('Not in a game. Create a new game to start playing.');
return;
}
if (!io.games[gameID]) {
problem('Server error in takeCard')
return;
}
if (!io.games[gameID].inProgress) {
problem('Start the game in order to play');
return;
}
if (io.games[gameID].finished) {
problem('The Game is Finished');
return;
}
game = io.games[gameID].game;
if (!(msg.username == game.playerOrder[game.currentTurn % (game.playerOrder.length)])) {
problem('Not your turn');
return;
}
game[msg.username].hand.push(game.currentCard);
game[msg.username].coins += game.currentBid;
// Alert everyone
io.sockets.in(gameID).emit('taken', {
username : msg.username,
card : game.currentCard,
bid : game.currentBid
});
game.currentBid = 0;
// Increment the turn once
//game.currentTurn += 1;
if (game.currentDeck.length > 0) {
game.currentCard = game.currentDeck.pop();
// Let everyone know whose turn it is
io.sockets.in(gameID).emit('turn', {
username : game.playerOrder[(game.currentTurn % game.playerOrder.length)],
card : game.currentCard,
bid : game.currentBid
});
} else {
io.games[gameID].finished = true;
io.games[gameID].inProgress = false;
results = [];
for (var i = 0; i < game.playerOrder.length; i++) {
results[i] = {
username : game.playerOrder[i],
hand : game[game.playerOrder[i]].hand,
score : scoreHand(game[game.playerOrder[i]].hand, game[game.playerOrder[i]].coins)
};
}
io.sockets.in(gameID).emit('ended', {
results : results
});
}
}
gameSocket.on('take', takeCard);
// When the user passes a card
function passCard(msg) {
gameID = gameSocket.gameID;
// Throw an error if
// The user is not in a game
// The game has not started
// It is not the user's turn
// The user does not have enough money to bid
// The game is finished
if (!gameID) {
problem('Not in a game. Create a new game to start playing.');
return;
}
if (!io.games[gameID]) {
// console.log('Game not found, abort abort');
return;
}
if (!io.games[gameID].inProgress) {
problem('Start the game in order to play');
return;
}
game = io.games[gameID].game;
if (!(msg.username == game.playerOrder[game.currentTurn % (game.playerOrder.length)])) {
problem('Not your turn');
return;
}
if (game[msg.username].coins == 0) {
problem('Not enough money to pass. You must take the card.');
return;
}
if (io.games[gameID].finished) {
problem('The Game is Finished');
return;
}
// Otherwise
// Take one money from the user and add it to the current bid
game[msg.username].coins -= 1;
game.currentBid += 1;
game.currentTurn += 1;
// Let everyone know that they've passed
io.sockets.in(gameID).emit('passed', {
username : msg.username,
card : game.currentCard,
bid : game.currentBid
});
io.sockets.in(gameID).emit('turn', {
username : game.playerOrder[(game.currentTurn % game.playerOrder.length)],
card : game.currentCard,
bid : game.currentBid
});
}
gameSocket.on('pass', passCard);
function endGame(msg) {
// Throw an error if
// The user is not in a game
// The game has not started
gameID = gameSocket.gameID;
if (!gameID) {
problem('Not in a game so cannot end game');
return;
}
if (!io.games[gameID]) {
// console.log('Game not found, abort abort');
return;
}
if (!io.games[gameID].inProgress) {
problem('Game has not yet started. Cannot end game');
return;
}
// End the game and
io.games[gameID].inProgress = false;
// Let everyone know
results = [];
for (var i = 0; i < game.playerOrder.length; i++) {
results[i] = {
username : game.playerOrder[i],
hand : game[game.playerOrder[i]].hand,
score : scoreHand(game[game.playerOrder[i]].hand, game[game.playerOrder[i]].coins)
};
}
// console.log(results);
io.sockets.in(gameID).emit('ended', {
results : results
});
}
gameSocket.on('end', endGame);
const onLeave = msg => {
gameID = gameSocket.gameID;
if (!gameID) {
problem('No game to leave');
return;
}
if (!io.games[gameID]) { return; }
if (io.games[gameID].inProgress) {
problem('Cannot leave in the middle of a game');
return;
}
const index = msg === undefined
? 0
: io.games[gameID].playerIDs.indexOf(msg.username);
const username = io.games[gameID].playerIDs[index];
io.games[gameID].playerIDs.splice(index, 1);
// Leave the SocketIO room
gameSocket.leave(gameID);
// Remove flag on the user socket
delete gameSocket.gameID;
// Alert everyone in the game
io.sockets.in(gameID).emit('playerleft', {username : username});
if (io.games[gameID].playerIDs.length == 0) {
delete io.games[gameID];
}
// Let the client know
gameSocket.emit('exited');
};
gameSocket.on('leave', onLeave);
function disconnect() {
gameID = gameSocket.gameID;
if (!gameID || !io.games[gameID]) { return; }
else if (io.games[gameID].finished) { onLeave(); }
else if (io.games[gameID].inProgress) {
endGame();
onLeave();
}
}
gameSocket.on('disconnect', disconnect);
}
/**
* Shuffle taken from StackOverflow
* http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
*/
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function shuffleFullDeck() {
// Create our deck
start = 3;
end = 35;
deck = []
for (var i = start; i <= end; i++) {
deck.push(i);
}
return shuffle(deck);
}
function scoreHand(hand, moneys) {
hand.sort((a,b) => parseInt(a) - parseInt(b));
score = hand[0];
for (var i = 1; i < hand.length; i++) {
if (hand[i-1] != (hand[i] - 1)) {
score += hand[i];
}
}
return !hand || hand.length === 0
? moneys
: score - moneys;
}