-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
459 lines (417 loc) · 11.6 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
// TODO4: Reconnect feature (Instead of register new we could use old playerID if player has)
// ----------------------------------------------------------------
// -- Module setting
// ----------------------------------------------------------------
var express = require('express')
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var path = require('path');
const fs = require('fs');
var parse = require('csv-parse/lib/sync');
require('should');
// ----------------------------------------------------------------
// -- Path and listen setting
// ----------------------------------------------------------------
server.listen(8080);
app.use("/guess",express.static(path.join(__dirname, 'public')));
// ----------------------------------------------------------------
// -- Global DSs
// ----------------------------------------------------------------
// Global timestamp issuing PID to players
var globalIDStamp = 0;
// Global dictionary to maintain player states
var playerState = {};
var roomStates = {}
var viableTimeLimit = [60,120,180,360];
var defaultWordListName = "";
var wordLists = {"" : []};
// ----------------------------------------------------------------
// -- Configuration funcs
// ----------------------------------------------------------------
function parseGameConfiguration(filename = "game.conf"){
// Parse viableTimeLimit, DictionaryList from game.conf
fs.readFile(filename,{encoding : "UTF-8",flag: "r"}, (err,fd) => {
if(err) throw err;
let input = fd;
conf = JSON.parse(input);
Promise.all(conf["wordLists"].map((v,i) => loadVocabulary(v[0],v[1])))
.then((resolve,reject) => {
let tmp = Object.keys(wordLists);
if(tmp.length==0) reject(-1);
// Temporary pick the first
defaultWordListName = tmp[1];
resolve(0);
});
timeLimits = conf["viableTimeLimit"];
viableTimeLimit = timeLimits;
});
}
// Parse to in memory wordLists
function loadVocabulary(listname,filename){
return new Promise((resolve, reject) => {
fs.readFile(filename,{encoding : "UTF-8",flag: "r"}, (err,fd) => {
if(err) reject(err);
let input = fd;
wordLists[listname] = parse(input)[0];
resolve(0);
});
});
}
// ----------------------------------------------------------------
// -- DB related funcs
// ----------------------------------------------------------------
// TODO
// ----------------------------------------------------------------
// -- RoomRelated funcs
// ----------------------------------------------------------------
function initRoomInfo(rid){
let roomInfo =
{
playerList : [],
timelimit : 60,
TLCur : 0,
noDupMode : false,
curWordListName : defaultWordListName,
gameInfo : {}
};
setRoomInfo(rid,roomInfo);
}
function setRoomInfo(rid,info){
roomStates[rid.toString()] = info;
}
function getRoomInfo(rid){
if (!(rid in roomStates)){
initRoomInfo(rid);
}
return roomStates[rid.toString()];
}
function getGameInfo(rid){
return roomStates[rid.toString()].gameInfo;
}
function setGameInfo(rid,info){
roomStates[rid.toString()].gameInfo = info;
}
// ----------------------------------------------------------------
// -- InRoom & Game funcs
// ----------------------------------------------------------------
function getPlayerState(pid){
return playerState[pid.toString()];
}
function setPlayerState(pid,state){
playerState[pid.toString()] = state;
}
// TODO: Add room feature & control broadcast domain
function registerNewPlayer(socket){
let newclient = {
playerID : globalIDStamp,
// This is a replicated pointer from RoomInfo.playerList, Consistency should be cared
currentRoom : -1,
playerName : "Nobody",
state : "Outside",
isRoomMaster : false,
isReady4Gaming: false,
// Player/Watcher
playerRole : "",
_socket : socket
};
setPlayerState(globalIDStamp,newclient);
globalIDStamp+=1;
return getPlayerState(globalIDStamp-1);
}
function playerLeaveRoom(pid){
let rid = getPlayerState(pid).currentRoom;
if(rid == -1){
return;
}
let roomInfo = getRoomInfo(rid);
roomInfo.playerList = remove(roomInfo.playerList,pid);
let player = getPlayerState(pid);
player.state = "Outside";
player.playerRole = "";
player.currentRoom = -1;
if(player.isRoomMaster){
//When RoomMaster was removed we need reelection
reelectMaster(rid);
}
notifyRoomInfo(rid);
}
function playerJoinRoom(pid,rid){
let plyState = getPlayerState(pid);
let roomInfo = getRoomInfo(rid);
playerLeaveRoom(pid);
// Join new Room
if(roomInfo.playerList.length==0){
plyState.isRoomMaster = true;
plyState.isReady4Gaming = true;
}
plyState.currentRoom = rid;
plyState.state = "InRoom";
roomInfo.playerList.push(pid);
}
function notifyRoomInfo(rid){
// Pushing all info to client
playerBuf = []
let roomInfo = getRoomInfo(rid);
for(let i=0;i<roomInfo.playerList.length;i++){
let id = roomInfo.playerList[i];
let pinfo = getPlayerState(id);
playerBuf.push({
playerID : pinfo.playerID,
playerName : pinfo.playerName,
state : pinfo.state,
isRoomMaster : pinfo.isRoomMaster,
isReady4Gaming: pinfo.isReady4Gaming,
// Player/Watcher
playerRole : pinfo.playerRole
});
}
io.to(roomchannel(rid)).emit('roomInfo',{
timelimit : roomInfo.timelimit,
players : playerBuf
});
}
function reelectMaster(rid){
let roomInfo = getRoomInfo(rid);
let len = roomInfo.playerList.length;
if(len==0){
// It's no point to do anymore.
return;
}
let target = Math.floor((Math.random()*len));
let id = roomInfo.playerList[target];
playerState[id].isRoomMaster = true;
playerState[id].isReady4Gaming = true;
}
function removePlayer(pid){
let rid = getPlayerState(pid).currentRoom;
let roomInfo = getRoomInfo(rid);
roomInfo.playerList = remove(roomInfo.playerList,pid);
let player = getPlayerState(pid);
setPlayerState(pid,null);
if(player.isRoomMaster){
//When RoomMaster was removed we need reelection
reelectMaster(rid);
}
notifyRoomInfo(rid);
}
function isAllReady(rid){
let roomInfo = getRoomInfo(rid);
// If only one player in room, refuse to start
if(roomInfo.playerList.length<2){
return false;
}
for(let i=0;i<roomInfo.playerList.length;i++){
let id=roomInfo.playerList[i];
let pinfo = getPlayerState(id);
if(pinfo.isReady4Gaming==false){
return false;
}
}
return true;
}
function initGameInfo(rid){
let roomInfo = getRoomInfo(rid);
let gameInfo = {
time : roomInfo.timelimit,
wordGuessed : {},
correctNum : 0,
skipNum : 0,
};
setGameInfo(rid,gameInfo);
}
function getNextWord(rid){
let roomInfo = getRoomInfo(rid);
const wordList = wordLists[roomInfo.curWordListName];
let len = wordList.length;
let target = Math.floor((Math.random()*len));
return wordList[target];
}
function notifyNextWord(rid){
let roomInfo = getRoomInfo(rid);
let gameInfo = getGameInfo(rid);
let nextword = getNextWord(rid);
if(roomInfo.noDupMode){
while(!gameInfo.wordGuessed[nextword]){
nextword = getNextWord(rid);
}
}
gameInfo.wordGuessed[nextword] = true;
for(let i=0;i<roomInfo.playerList.length;i++){
let id=roomInfo.playerList[i];
let pinfo = getPlayerState(id);
if(pinfo.playerRole!="Player"){
pinfo._socket.emit('wordToGuess',nextword);
}
else{
pinfo._socket.emit('wordToGuess',"");
}
}
}
function notifyGameEnd(rid){
notifyGameInfo(rid);
io.to(roomchannel(rid)).emit('gameEnd',"");
cleanUpGame(rid);
}
function notifyGameInfo(rid){
// TODO: Pushing time & credit update
io.to(roomchannel(rid)).emit('gameInfo',getGameInfo(rid));
}
function initGame(rid){
// Select the player
let roomInfo = getRoomInfo(rid);
let len = roomInfo.playerList.length;
let target = Math.floor((Math.random()*len));
for(let i=0;i<len;i++){
let id = roomInfo.playerList[i];
let player = getPlayerState(id);
player.state = "Gaming";
if(i!=target){
player.playerRole = "Watcher";
}
else{
player.playerRole = "Player";
}
}
notifyRoomInfo(rid);
}
function cleanUpGame(rid){
let roomInfo = getRoomInfo(rid);
initGameInfo(rid);
for(let i=0;i<roomInfo.playerList.length;i++){
let id=roomInfo.playerList[i];
let pinfo = getPlayerState(id);
pinfo.state = "InRoom";
pinfo.playerRole = "";
}
notifyRoomInfo(rid);
}
function clockTimeout(rid){
let gameInfo = getGameInfo(rid);
if(gameInfo.time!=0){
gameInfo.time-=1;
notifyGameInfo(rid);
setTimeout(clockTimeout,1000,rid);
}
else{
notifyGameEnd(rid);
}
}
function startGame(rid){
initGame(rid);
initGameInfo(rid);
notifyGameInfo(rid);
// Start
setTimeout(clockTimeout,1000,rid)
io.to(roomchannel(rid)).emit("Start","");
notifyNextWord(rid);
}
function roomchannel(rid){
return "_room" +rid.toString();
}
// ----------------------------------------------------------------
// -- Main Loop part
// ----------------------------------------------------------------
// Start
parseGameConfiguration("game.conf");
// Register all request handling func
io.on('connection', function (socket) {
// Client States: Outside -> InRoom <--> Gaming
let clientState = registerNewPlayer(socket);
let roomId = -1;
socket.emit("id",clientState.playerID);
// Can notify the basic setting of players (or declined until player join room)
socket.on("join",(rid) =>{
playerJoinRoom(clientState.playerID,rid);
roomId = rid;
clientState._socket.join(roomchannel(rid));
notifyRoomInfo(rid);
});
socket.on("disconnect",(reason) => {
removePlayer((clientState.playerID));
});
// InRoom Related
socket.on("changeTimeLimit",()=>{
if(!sanityCheckR(clientState,roomId)){
return;
}
let roomInfo = getRoomInfo(roomId);
roomInfo.TLCur = (roomInfo.TLCur + 1) % viableTimeLimit.length;
roomInfo.timelimit = viableTimeLimit[roomInfo.TLCur];
notifyRoomInfo(roomId);
});
socket.on("changeName",(newName) => {
if(!sanityCheckR(clientState,roomId)){
return;
}
clientState.playerName = newName;
notifyRoomInfo(roomId);
});
socket.on("Ready",() => {
if(!sanityCheckR(clientState,roomId)){
return;
}
clientState.isReady4Gaming = true;
notifyRoomInfo(roomId);
});
socket.on("Unready",() => {
if(!sanityCheckR(clientState,roomId)){
return;
}
clientState.isReady4Gaming = false;
notifyRoomInfo(roomId);
});
socket.on("Start",() => {
if(!sanityCheckR(clientState,roomId)){
return;
}
if(clientState.isRoomMaster){
if(isAllReady(roomId)){
startGame(roomId);
}
}
});
// Game Related
socket.on("Next",(result) => {
if(!sanityCheckG(clientState,roomId)){
return;
}
let gameInfo = getGameInfo(roomId);
if(result=="Correct"){
gameInfo.correctNum += 1;
notifyNextWord(roomId);
}
else if(result=="Skip"){
gameInfo.skipNum += 1
notifyNextWord(roomId);
}
});
});
function sanityCheckR(clientState,roomId){
if(clientState.state!="InRoom"){
return false;
}
if(roomId == -1){
return false;
}
return true;
}
function sanityCheckG(clientState,roomId){
if(clientState.state!="Gaming"){
return false;
}
if(roomId == -1){
return false;
}
return true;
}
// ----------------------------------------------------------------
// -- Utility functions
// ----------------------------------------------------------------
function remove(array,val){
let index = array.indexOf(val);
if (index > -1) {
array.splice(index, 1);
}
return array;
}