-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
550 lines (482 loc) · 20.8 KB
/
app.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//Libary Imports
const express = require('express');
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require('passport');
const socket = require('socket.io')
const path = require('path');
//File Imports
const User = require("./models/User");
const Lobby = require("./models/Lobby");
const users = require("./routes/api/users");
const chatrooms = require("./routes/api/chatrooms");
const lobbies = require("./routes/api/lobbies");
const PokerGame = require("./frontend/src/components/poker/game");
const GameLogic = require("./frontend/src/components/blackjack/blackjack/blackjack")
//SETUP
const app = express();
const server = require('http').createServer(app)
const db = require("./config/keys").mongoURI;
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`listening on port ${PORT}`)); //This console log is just for testing
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("Connected to mongoDB"))
.catch(err => console.log(err))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
require('./config/passport')(passport);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('frontend/build'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
})
}
//Routes
app.use("/api/users", users);
app.use("/api/chatrooms", chatrooms);
app.use("/api/lobbies", lobbies);
//Websockets Setup
const lobbyServer = socket(server)
//Lobby Server
const lobbiesCollection = {};
lobbyServer.on("connection", (socket) => {
console.log("made connection with socket " + socket.id);
let localLobbyId
let localPokerLobby;
let localBJLobby
//helper methods
const findUserAndUpdateBalance = (balance, _id) => {
User.findById(_id).then(user => {
const balanceDiff = balance - user.balance;
user.set({ balance })
user.save().then(user => socket.emit("updateBalance", balance));
if (balanceDiff > 0) {
lobbyServer.in(localLobbyId).emit("slotsWinner", user.username, balanceDiff)
}
})
}
const findUserByUsernameAndUpdateBalance = (username, balance) => {
User.updateOne({ username }, { balance })
.then(user => console.log("success"))
.catch(err => console.log(err))
}
const findLobbyByIdAndChangeCapacity = num => {
Lobby.findById(localLobbyId).then(lobby => {
lobby.set({ currentCapacity: lobby.currentCapacity + num});
lobby.save().then(lobby => {
setTimeout(() => {
if (lobby.currentCapacity <= 0) {
lobby.remove();
}
}, 30000)
});
}).catch(err => console.log(err));
}
const pokerGameOver = () => {
if (localPokerLobby.game.cycle === 4 || localPokerLobby.game.players.length === 1) {
const winner = localPokerLobby.game.getWinner();
// lobbyServer.in(localLobbyId + "poker").emit("updateBalance", winner.bananas);
lobbyServer.to(winner.socketId).emit("updateBalance", winner.bananas);
findUserByUsernameAndUpdateBalance(winner.username, winner.bananas);
lobbyServer.in(localLobbyId + "poker").emit("playerWon", winner)
setTimeout(() => {
if (localPokerLobby.game.players.length > 1) {
localPokerLobby.game.handleNewHand();
lobbyServer.in(localLobbyId + "poker").emit("newGame",
localPokerLobby.game.players.map(player => {
return {
handle: player.handle,
bananas: player.bananas,
hand: player.hand,
bigBlind: player.bigBlind,
smallBlind: player.smallBlind,
}
}), localPokerLobby.game.currentPlayers[0].handle);
}
}, 10000)
setTimeout(() => {
if (localPokerLobby.game.players.length > 1) {
lobbyServer.in(localLobbyId + "poker").emit("aboutToStart")
} else {
lobbyServer.in(localLobbyId + "poker").emit("needMorePlayers")
}
}, 7000)
}
}
const bJGameOver = () => {
localBJLobby.game.compareHands();
// Update player's balances on the front end
let playersObj = {};
localBJLobby.game.players.forEach(player => {
playersObj[player.userId] = player.balance;
findUserByUsernameAndUpdateBalance(player.userId, player.balance);
lobbyServer.to(player.socketId).emit("winOrLose", player.balance);
lobbyServer.to(player.socketId).emit("updateBalance", player.balance);
})
// socket.emit("updateBalance", localBJLobby.game.getPlayerBySocketId(socket.id).balance);
lobbyServer.in(localLobbyId + "bj").emit("updatePlayersBalance", playersObj)
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase, localBJLobby.game.players[0].userId);
socket.emit("resetBJGame")
lobbyServer.in(localLobbyId + "bj").emit("aboutToStart")
setTimeout(() => {
localBJLobby.game.resetGame()
if (localBJLobby.game.players.length) {
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase)
lobbyServer.in(localLobbyId + "bj").emit("sendDealer", localBJLobby.game.dealer)
lobbyServer.in(localLobbyId + "bj").emit("resetPlayers", null)
lobbyServer.in(localLobbyId + "bj").emit("changeTurn", localBJLobby.game.players[0].userId)
}
}, 5000)
}
//chat
socket.on("chat", (data) => {
lobbyServer.in(localLobbyId).emit("receiveMessage", data);
});
socket.on("pokerChat", data => {
lobbyServer.in(localLobbyId + "poker").emit("receivePokerMessage", data);
})
socket.on("bjChat", data => {
lobbyServer.in(localLobbyId + "bj").emit("receiveBJMessage", data);
})
//lobbies
socket.on("joinLobby", (lobbyId, username) => {
localLobbyId = lobbyId
socket.join(lobbyId)
if (lobbiesCollection[lobbyId]) {
lobbiesCollection[lobbyId].players[socket.id] = {
x: 200,
y: 250,
playerId: socket.id,
username
}
} else {
lobbiesCollection[lobbyId] = {
players: {},
id: lobbyId,
bJ: {
game: new GameLogic.Blackjack(),
},
poker: {
game: new PokerGame(),
}
}
lobbiesCollection[lobbyId].players[socket.id] = {
x: 200,
y: 250,
playerId: socket.id,
username
}
}
findLobbyByIdAndChangeCapacity(1);
localPokerLobby = lobbiesCollection[lobbyId].poker
localBJLobby = lobbiesCollection[lobbyId].bJ;
socket.to(lobbyId).emit('newPlayer', lobbiesCollection[lobbyId].players[socket.id]);
socket.emit('lobbyPlayers', lobbiesCollection[lobbyId].players);
});
socket.on('playerMovement', position => {
const movedPlayer = lobbiesCollection[localLobbyId].players[socket.id];
movedPlayer.x = position.x;
movedPlayer.y = position.y;
socket.to(localLobbyId).emit("playerMoved", movedPlayer)
})
socket.on("leaveLobby", () => {
socket.leave(localLobbyId);
delete lobbiesCollection[localLobbyId].players[socket.id];
findLobbyByIdAndChangeCapacity(-1);
})
//games
// slots
socket.on("slotChange", (balance, _id) => {
findUserAndUpdateBalance(balance, _id);
})
// poker
socket.on("joinPokerGame", () => {
//send him information about other players in game
socket.emit("currentPokerPlayers", localPokerLobby.game.players.map(player => {
return {
handle: player.handle,
bananas: player.bananas,
hand: player.hand,
bigBlind: player.bigBlind,
smallBlind: player.smallBlind,
}
}), localPokerLobby.game.communityCards, localPokerLobby.game.turnStarted);
socket.join(localLobbyId + "poker")
})
socket.on("addPokerGamePlayer", (username, balance) => {
// add him to the game
if (localPokerLobby.game.addPlayer(username, socket.id, balance)) {
const player = localPokerLobby.game.players[0]
lobbyServer.in(localLobbyId + "poker").emit("addPokerGamePlayer", {
handle: player.handle,
bananas: player.bananas,
hand: player.hand,
bigBlind: player.bigBlind,
smallBlind: player.smallBlind,
});
if (localPokerLobby.game.players.length > 1 && !localPokerLobby.game.turnStarted) {
lobbyServer.in(localLobbyId + "poker").emit("aboutToStart")
setTimeout(() => {
localPokerLobby.game.startGame();
lobbyServer.in(localLobbyId + "poker").emit("gameStarted", localPokerLobby.game.currentPlayers[0].handle )
}, 3000)
}
}
// send other players information that he joined
})
socket.on("playerCalled", username => {
const currentPlayer = localPokerLobby.game.currentPlayers[0];
localPokerLobby.game.handleCall();
socket.emit("updateBalance", currentPlayer.bananas);
findUserByUsernameAndUpdateBalance(currentPlayer.handle, currentPlayer.bananas);
lobbyServer.in(localLobbyId + "poker").emit("playerCalled",
username,
localPokerLobby.game.pot,
localPokerLobby.game.currentPlayers[0].handle,
localPokerLobby.game.communityCards,
localPokerLobby.game.raised,
localPokerLobby.game.bet )
pokerGameOver();
})
socket.on("playerRaised", (username, amount) => {
const currentPlayer = localPokerLobby.game.currentPlayers[0];
if (localPokerLobby.game.handleRaise(amount)) {
socket.emit("updateBalance", currentPlayer.bananas);
findUserByUsernameAndUpdateBalance(currentPlayer.handle, currentPlayer.bananas);
lobbyServer.in(localLobbyId + "poker").emit("playerRaised",
username,
amount,
localPokerLobby.game.currentPlayers[0].handle,
localPokerLobby.game.communityCards,
localPokerLobby.game.raised,
localPokerLobby.game.bet)
pokerGameOver();
} else {
socket.emit("alert", "You must enter a valid amount")
}
})
socket.on("playerChecked", username => {
localPokerLobby.game.handleCheck();
lobbyServer.in(localLobbyId + "poker").emit("playerChecked",
username,
localPokerLobby.game.currentPlayers[0].handle,
localPokerLobby.game.communityCards,
localPokerLobby.game.raised,
localPokerLobby.game.bet )
pokerGameOver();
})
socket.on("playerFolded", username => {
localPokerLobby.game.handleFold();
lobbyServer.in(localLobbyId + "poker").emit("playerFolded",
username,
localPokerLobby.game.currentPlayers[0].handle,
localPokerLobby.game.communityCards,
localPokerLobby.game.raised,
localPokerLobby.game.bet );
if (localPokerLobby.game.cycle === 4) {
const winner = localPokerLobby.game.getWinner();
findUserByUsernameAndUpdateBalance(winner.handle, winner.bananas);
socket.emit("updateBalance", winner.bananas);
lobbyServer.in(localLobbyId + "poker").emit("playerWon", winner)
setTimeout(() => {
if (localPokerLobby.game.players.length > 1) {
localPokerLobby.game.handleNewHand();
lobbyServer.in(localLobbyId + "poker").emit("newGame",
localPokerLobby.game.players.map(player => {
return {
handle: player.handle,
bananas: player.bananas,
hand: player.hand,
bigBlind: player.bigBlind,
smallBlind: player.smallBlind,
}
}), localPokerLobby.game.currentPlayers[0].handle);
}
}, 10000)
setTimeout(() => {
if (localPokerLobby.game.players.length > 1) {
lobbyServer.in(localLobbyId + "poker").emit("aboutToStart")
} else {
lobbyServer.in(localLobbyId + "poker").emit("needMorePlayers")
}
}, 7000)
}
})
// bj
socket.on("joinBJGame", (username, balance) => {
socket.emit(
"currentBJPlayers",
localBJLobby.game.players.map(player => {
return {
userId: player.userId,
pool: player.pool,
poolSplit: player.poolSplit,
balance: player.balance,
hand: player.hand,
handSplit: player.handSplit,
handValue: player.getHandValue(),
splitHandValue: player.getHandValue()
}
})
);
socket.emit("addPlayerToBJ", username)
localBJLobby.game.addPlayer(socket.id, username, balance)
socket.join(localLobbyId + "bj")
const player = localBJLobby.game.players[localBJLobby.game.players.length-1]
lobbyServer.in(localLobbyId + "bj").emit("newBJPlayer", {
userId: player.userId,
pool: player.pool,
poolSplit: player.poolSplit,
balance: player.balance,
hand: player.hand,
handSplit: player.handSplit,
handValue: player.getHandValue(),
splitHandValue: player.getHandValue()
}, localBJLobby.game.currentPhase, localBJLobby.game.players[0].userId )
})
socket.on("bet", (amount) => {
const currentPlayer = localBJLobby.game.players[0];
if (localBJLobby.game.getBetFromCurrentTurn(amount)) {
if (localBJLobby.game.currentPhase === "options") {
const playerCards = {};
localBJLobby.game.players.forEach(player => playerCards[player.userId] = player.hand)
const dealerCards = localBJLobby.game.dealer.hand
lobbyServer.in(localLobbyId + "bj").emit("dealPlayerCards", playerCards)
lobbyServer.in(localLobbyId + "bj").emit("dealDealerCards", dealerCards)
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase);
lobbyServer.in(localLobbyId + "bj").emit("lastBetter",
localBJLobby.game.players[localBJLobby.game.players.length-1].userId,
localBJLobby.game.players[localBJLobby.game.players.length - 1].balance)
lobbyServer.in(localLobbyId + "bj").emit("changeTurn",
localBJLobby.game.players[0].userId)
} else {
lobbyServer.in(localLobbyId + "bj").emit("changeTurn",
localBJLobby.game.players[0].userId)
}
findUserByUsernameAndUpdateBalance(currentPlayer.userId, currentPlayer.balance);
socket.emit("updateBalance", currentPlayer.balance);
lobbyServer.in(localLobbyId + "bj").emit("playerBet", amount, currentPlayer.userId)
} else {
lobbyServer.in(localLobbyId + "bj").emit("betFailed")
}
})
socket.on("playerHit", () => {
localBJLobby.game.players[0].hit();
const playerCards = {};
// Below was added in place of the commented
localBJLobby.game.players.forEach(player => {
playerCards[player.userId] = player.hand;
})
if (localBJLobby.game.checkCurrentPlayerBust()) {
// Switches to next player, since cCPB will run nextTurn if the current player
const player = localBJLobby.game.players[localBJLobby.game.players.length-1];
lobbyServer.in(localLobbyId + "bj").emit("playerBust", player.userId)
lobbyServer.in(localLobbyId + "bj").emit("changeTurn", localBJLobby.game.players[0].userId)
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase)
if (localBJLobby.game.currentPhase === "dealer") {
localBJLobby.game.dealerHit();
const dealerHand = localBJLobby.game.dealer.hand;
lobbyServer.in(localLobbyId + "bj").emit("dealDealerCards", dealerHand)
lobbyServer.in(localLobbyId + "bj").emit("sendDealer", localBJLobby.game.dealer)
bJGameOver();
}
} else {
const player = localBJLobby.game.players[0];
lobbyServer.in(localLobbyId + "bj").emit("playerHit", player.userId)
}
lobbyServer.in(localLobbyId + "bj").emit("dealPlayerCards", playerCards)
})
socket.on("payoutPlayers", () => {
// localBJLobby.game.compareHands();
// // Update player's balances on the front end
// let playersObj = {};
// localBJLobby.game.players.forEach(player => {
// playersObj[player.userId] = player.balance;
// findUserByUsernameAndUpdateBalance(player.userId, player.balance);
// })
// socket.emit("updateBalance", localBJLobby.game.getPlayerBySocketId(socket.id).balance);
// lobbyServer.in(localLobbyId + "bj").emit("updatePlayersBalance", playersObj)
// lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase, localBJLobby.game.players[0].userId);
})
socket.on("playerStand", () => {
localBJLobby.game.players[0].stand();
localBJLobby.game.nextTurn();
lobbyServer.in(localLobbyId + "bj").emit("changeTurn", localBJLobby.game.players[0].userId)
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase);
if(localBJLobby.game.currentPhase === 'dealer'){
//Change phase
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase);
lobbyServer.in(localLobbyId + "bj").emit("changeTurn", localBJLobby.game.players[0].userId);
lobbyServer.in(localLobbyId + "bj").emit("standOption", localBJLobby.game.players[0].userId)
localBJLobby.game.dealerHit();
const dealerHand = localBJLobby.game.dealer.hand;
lobbyServer.in(localLobbyId + "bj").emit("dealDealerCards", dealerHand)
lobbyServer.in(localLobbyId + "bj").emit("sendDealer", localBJLobby.game.dealer)
bJGameOver();
} else {
lobbyServer.in(localLobbyId + "bj").emit("changePhase", localBJLobby.game.currentPhase);
lobbyServer.in(localLobbyId + "bj").emit("changeTurn", localBJLobby.game.players[0].userId);
lobbyServer.in(localLobbyId + "bj").emit("standOption", localBJLobby.game.players[0].userId)
}
})
//leave games
socket.on("leavePokerGame", () => {
if (localLobbyId) {
socket.leave(localLobbyId + "poker");
const pokerPlayer = localPokerLobby.game.getPlayerBySocketId(socket.id)
if (pokerPlayer) {
localPokerLobby.game.removePlayer(pokerPlayer)
lobbyServer.in(localLobbyId + "poker").emit("removePlayer", pokerPlayer.handle)
if (localPokerLobby.game.players.length === 0) {
localPokerLobby.game = new PokerGame();
}
pokerGameOver();
}
}
})
socket.on("leaveBJGame", () => {
if (localLobbyId) {
socket.leave(localLobbyId + "bj")
const bJPlayer = lobbiesCollection[localLobbyId].bJ.game.getPlayerBySocketId(socket.id)
lobbyServer.in(localLobbyId + "bj").emit("removePlayer", bJPlayer.userId)
lobbiesCollection[localLobbyId].bJ.game.removePlayer(bJPlayer)
if (!lobbiesCollection[localLobbyId].bJ.game.players.length) {
lobbiesCollection[localLobbyId].bJ.game.resetGame();
}
// emit method that removes player to all players still playing
}
})
//disconect
socket.on("disconnect", () => {
if (localLobbyId) {
findLobbyByIdAndChangeCapacity(-1);
delete lobbiesCollection[localLobbyId].players[socket.id]
const bJPlayer = lobbiesCollection[localLobbyId].bJ.game.getPlayerBySocketId(socket.id)
if (bJPlayer) {
lobbyServer.in(localLobbyId + "bj").emit("removePlayer", bJPlayer.userId)
lobbiesCollection[localLobbyId].bJ.game.removePlayer(bJPlayer)
if (!lobbiesCollection[localLobbyId].bJ.game.players.length) {
lobbiesCollection[localLobbyId].bJ.game.resetGame();
}
}
const pokerPlayer = localPokerLobby.game.getPlayerBySocketId(socket.id)
if (pokerPlayer) {
localPokerLobby.game.removePlayer(pokerPlayer)
lobbyServer.in(localLobbyId + "poker").emit("removePlayer", pokerPlayer.handle);
if (localPokerLobby.game.players.length === 0) {
localPokerLobby.game = new PokerGame();
}
pokerGameOver();
}
socket.to(localLobbyId).emit('removePlayerSprite', socket.id)
}
})
})
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get("/", (req, res) => {
res.send("Hello App!")
});