-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
115 lines (96 loc) · 3.41 KB
/
index.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
var express = require('express')
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var game = require("./schema")
var path = require('path')
var os = require('os');
// serve production build
app.use(express.static(path.join(__dirname, 'client', 'build')));
// session
var session = require('express-session') // for express
var sharedsession = require("express-socket.io-session"); // for socket.io
// create a session
var iosession = session({
resave: true,
saveUninitialized: true,
secret: 'keyboard cat',
name: "cah_cookie_session"
});
app.use(iosession);
io.use(sharedsession(iosession, { autoSave: true }));
app.get('/session', (req, res) => {
if (req.session.name) {
res.json(`welcome: ${req.session.name}`)
} else {
req.session.name = "Yusuf"
res.json(`welcome for the first time!`)
}
});
io.on('connect', (client) => {
console.log(`host ${os.hostname()} | client | ${client.handshake.sessionID}`)
// StartGameScreen
client.on('getLobbyState', (partyCode) => {
client.join(partyCode);
let response = game.getLobbyState(partyCode, client.handshake.sessionID, (success, message) => {
// console.log(`Round ended, going to judge-selecting ${success} | ${message}`)
io.to(partyCode).emit('newGameState');
});
client.emit("getLobbyState", response);
});
client.on('joinParty', ({ partyCode, name }) => {
game.joinGame(partyCode, client.handshake.sessionID, name);
io.to(partyCode).emit('newLobbyState');
});
// PlayerSelectionScreen
client.on('getPlayerRoundState', (partyCode) => {
// console.log(`${client.handshake.sessionID} | getPlayerRoundState`)
client.join(partyCode);
let gameState = game.getPlayerRoundState(partyCode, client.handshake.sessionID);
client.emit('getPlayerRoundState', gameState);
});
client.on('playCard', (partyCode, cardID) => {
game.playCard(partyCode, cardID, client.handshake.sessionID, (success, message) => {
console.log(`playCard | ${success} | ${message}`)
if (success) {
io.to(partyCode).emit('newGameState');
}
});
});
client.on('judgeSelectCard', (partyCode, cardID) => {
game.judgeSelectCard(partyCode, cardID, client.handshake.sessionID, (success, message) => {
console.log(`judgeSelectCard | ${success} | ${message} | ${client.handshake.sessionID}`)
if (success) {
io.to(partyCode).emit('newGameState')
}
})
});
client.on('shuffleCards', (partyCode, sourceIdx, destIdx) => {
game.shuffleCards(partyCode, sourceIdx, destIdx, client.handshake.sessionID, (success, message) => {
console.log(`shuffleCards | ${client.handshake.sessionID} | ${success} | ${message}`)
if(success) {
client.emit('newGameState')
}
})
})
client.on('endRound', partyCode => {
game.endRound(partyCode, (success, message) => {
console.log(`endRound | ${success} | ${message} | ${client.handshake.sessionID}`)
if (success) {
io.to(partyCode).emit('newGameState');
}
});
});
client.on('disconnect', function () {
console.log(`client DISCONNECTED: session(${client.handshake.sessionID})`)
});
});
// serve routing to build
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
// open server
const PORT = process.env.PORT || 5000
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
});