-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
234 lines (210 loc) · 6.68 KB
/
server.js
File metadata and controls
234 lines (210 loc) · 6.68 KB
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
const express = require('express');
const fs = require('fs')
const ws = require('ws');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const app = express();
const web_client_path = __dirname + '/web_client/';
const html_path = web_client_path + 'html/';
const js_path = web_client_path + 'js/';
const css_path = web_client_path + 'css/';
const js_lib_path = js_path + 'lib/';
const img_path = web_client_path + 'images/';
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(cookieParser()); // for parsing cookies
app.get('/js/:file', async function (req, res) {
try {
const file = js_path + req.params.file
const exists = fs.existsSync(file)
if (exists) res.sendFile(file);
else res.sendStatus(404);
} catch (err) {
console.error('ERROR:', err);
res.sendStatus(500); // Internal Server Error
};
});
app.get('/js/lib/:file', async function (req, res) {
try {
const file = js_lib_path + req.params.file
const exists = fs.existsSync(file)
if (exists) res.sendFile(file);
else res.sendStatus(404);
} catch (err) {
console.error('ERROR:', err);
res.sendStatus(500); // Internal Server Error
};
});
app.get('/images/:file', async function (req, res) {
try {
const file = img_path + req.params.file
const exists = fs.existsSync(file)
if (exists) res.sendFile(file);
else res.sendStatus(404);
} catch (err) {
console.error('ERROR:', err);
res.sendStatus(500); // Internal Server Error
};
});
app.get('/css/:file', async function (req, res) {
try {
const file = css_path + req.params.file
const exists = fs.existsSync(file)
if (exists) res.sendFile(file);
else res.sendStatus(404);
} catch (err) {
console.error('ERROR:', err);
res.sendStatus(500); // Internal Server Error
};
});
app.get('/', async function (req, res) {
res.sendFile(html_path + 'index.html');
});
app.post('/play', async function (req, res) {
//console.log(req.body.user);
res.cookie('EXPLODINGNAUTS_USER', req.body.user);
res.redirect('/game')
});
app.get('/game', async function (req, res) {
res.sendFile(html_path + 'game.html');
});
let server = require('http').createServer(app);
let websocketServer = new ws.Server({ server });
let connections = {}
let deck = [];
let turn = 0;
let turnOrder = 0;
let startTimer = 30;
let timerStarted = false;
let cardIds = {
explodingKitten: 0,
attack: 1,
defuse: 2,
nope: 3,
seeTheFuture: 4,
skip: 5,
favor: 6,
shuffle: 7,
rainbowCat: 8,
hairyPotatoCat: 9,
tacoCat: 10,
beardCat: 11,
cattermelon: 12
};
websocketServer.on('connection', ws => {
let user;
ws.on('message', message => {
let sMessage = message.match(/(.+?)\0(.+)/)
console.log(sMessage, message)
let data = sMessage[2]
switch (sMessage[1].toUpperCase()) {
case 'HANDSHAKE':
if (Object.keys(connections).length == 0) {
deck=[];
generateDeck();
}
connections[data] = ws
ws.send(`USER_LIST\0${JSON.stringify(Object.keys(connections))}`)
for (let connection of Object.values(connections)) {
if(connection != ws)
connection.send(`NEW_USER\0${data}`)
}
user = data;
timerStarted = true;
connections[user].send(`DRAW_CARD\0${cardIds.defuse}`)
break;
case 'ADD_CARDS':
for (let username in connections) {
connections[username].send(`ADD_CARDS\0${data}`)
}
if(data.indexOf("SeeTheFuture")!=-1){
let list = [];
for (let i = 0; i < 3; i++) {
list.push(deck[i])
}
connections[user].send(`ANS_SEETHEFUTURE\0${JSON.stringify(list)}`)
}
break;
case 'DRAW_CARD':
connections[user].send(`DRAW_CARD\0${deck.shift()}`)
turn++;
break;
case "GET_DECK":
connections[user].send(`ACTUAL_DECK\0${JSON.stringify(deck)}`)
break;
case "SET_DECK":
deck=JSON.parse(data);
break;
}
});
ws.on('close', () => {
if (user) {
delete connections[user];
for (let connection of Object.values(connections)) {
connection.send(`USER_DISCONNECTED\0${user}`)
}
}
});
// let timer=setInterval(() => {
// if(timerStarted && startTimer>0){
// startTimer-=1;
// connections[user].send(`STARTING_TIME\0${Math.floor(startTimer)}`)
// }
// if(startTimer<=0){
// clearInterval(this);
// }
// },1000)
});
//["ExplodingKitten", "Attack", "Defuse", "Nope", "SeeTheFuture", "Skip", "Favor", "Shuffle", "RainbowCat", "HairyPotatoCat", "TacoCat", "BeardCat", "Cattermelon"]
function generateDeck(players = 5) {
let cards = []
for (let i = 0; i < players - 1; i++) {
cards.push(cardIds.explodingKitten)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.attack)
}
for (let i = 0; i < Math.max(1 + players, 6 - players); i++) {
cards.push(cardIds.defuse)
}
for (let i = 0; i < 5; i++) {
cards.push(cardIds.nope)
}
for (let i = 0; i < 5; i++) {
cards.push(cardIds.seeTheFuture)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.skip)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.favor)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.shuffle)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.rainbowCat)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.hairyPotatoCat)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.tacoCat)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.beardCat)
}
for (let i = 0; i < 4; i++) {
cards.push(cardIds.cattermelon)
}
// Shuffle deck
let numberOfCards = cards.length;
for (let c = 0; c < numberOfCards; c++) {
let index=Math.floor(Math.random()*cards.length);
deck.push(cards[index])
cards.splice(index,1)
}
}
server.listen(8080, function () {
console.log('listening on *:' + 8080);
});