-
Notifications
You must be signed in to change notification settings - Fork 1
/
textTwist.js
352 lines (304 loc) · 9.06 KB
/
textTwist.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
/* eslint-disable no-console */
const { words, shuffle } = require('./lib');
/**
* Globals
*/
let io;
let gameSocket;
const db = {};
/*
* Default Configurations
*/
const WORD_LENGTH = 6;
const TIME_LIMIT = 120;
/* *******************************
* *
* GAME FUNCTIONS *
* *
******************************* */
/**
* Reset the game
* @param roomId The Game ID aka room ID
*/
function resetGame(roomId) {
console.log('inside resetGame');
db[roomId].gameStarted = false;
delete db[roomId].scoreBoard[this.id];
clearInterval(db[roomId].timer);
// If both the players have left the room, destory the room!
if (Object.keys(db[roomId].scoreBoard).length === 0) {
delete db[roomId];
} else {
Object.keys(db[roomId].scoreBoard).forEach((id) => {
db[roomId].scoreBoard[id].score = 0;
});
io.sockets.in(roomId).emit('resetGame', {
gameId: roomId,
mySocketId: this.id,
});
}
console.log(db);
}
/**
* Handler while a client is being disconnected
* Clear the memory allocated for the client
* Notify other player and Reset the game
*/
function whileDisconnecting() {
console.log(`client ${this.id} is being disconnected`);
if (db.clientRoom === undefined) return;
const roomId = db.clientRoom[this.id];
if (roomId === undefined) return;
console.log(this.id, 'disconnected from', roomId);
delete db.clientRoom[this.id];
resetGame.call(this, roomId);
}
/**
* All players have joined. Start the game!
* @param gameId The game ID aka room ID
*/
function prepareGame(data) {
const sock = this;
const { scoreBoard } = db[data.gameId];
const emitPayload = {
mySocketId: sock.id,
gameId: data.gameId,
scoreBoard,
timeLimit: db[data.gameId].config.timeLimit,
};
console.log('gonna begin new game');
io.sockets.in(data.gameId).emit('beginNewGame', emitPayload);
}
/**
* Restart the game
* @param data {{ gameId: int }}
*/
function restartGame(data) {
const room = db[data.gameId];
if (room === undefined) {
this.emit('startNewGame', {});
return;
}
room.foundWords = {};
room.gameStarted = true;
room.wordData = {};
Object.keys(room.scoreBoard).forEach((id) => {
room.scoreBoard[id].score = 0;
});
prepareGame.call(this, data);
}
function sendDefaultConfig() {
this.emit('defaultConfig', { wordLength: WORD_LENGTH, timeLimit: TIME_LIMIT });
}
/**
* Create a new game
* @param data {{ name: string, wordLength: number, timeLimit: number }}
*/
function createNewGame(data) {
// Create a unique Socket.IO Room
const thisGameId = Math.floor(Math.random() * 100000);
// create a client room map
db.clientRoom = db.clientRoom || {};
db.clientRoom[this.id] = thisGameId;
// Return the Room ID (gameId) and the socket ID (mySocketId) to the client
this.emit('newGameCreated', { gameId: thisGameId, mySocketId: this.id });
// Join the Room and wait for the players
this.join(thisGameId.toString());
db[thisGameId] = {
foundWords: {},
gameStarted: false,
scoreBoard: {
[this.id]: {
name: data.name,
score: 0,
},
},
config: {
wordLength: data.wordLength || WORD_LENGTH,
timeLimit: data.timeLimit || TIME_LIMIT,
},
};
}
/**
* This function does all the work of getting a new words from the pile
* and organizing the data to be sent back to the clients.
* @param gameId The Game ID
* @returns {{ word: string, allWordsLength: array }}
*/
function getWordData(gameId) {
const randomWord = words.randomWord(db[gameId].config.wordLength);
const allWords = words.allWords(randomWord);
const wordData = {
word: shuffle(randomWord.split('')).join('').toUpperCase(), // Displayed Word
answers: allWords, // Correct Answers
allWordsLength: allWords.map(word => word.length), // Correct Answers
};
db[gameId].wordData = wordData;
return wordData;
}
/**
* Get a word for the host, and a list of words for the player.
* @param gameId The room identifier
*/
function sendWord(gameId) {
const data = getWordData.call(this, gameId);
io.sockets.in(gameId).emit('newWordData', {
word: data.word,
allWordsLength: data.allWordsLength,
});
}
/**
* Find the winner by analysing the Scoreboard
* @param scoreBoard The score board
* @returns {{ name: string, id: string }}
*/
function findWinner(scoreBoard) {
const players = Object.keys(scoreBoard);
const player0 = scoreBoard[players[0]];
const player1 = scoreBoard[players[1]];
const winner = player0.score > player1.score
? { name: player0.name, id: players[0] }
: { name: player1.name, id: players[1] };
return player0.score === player1.score ? {} : winner;
}
/**
* End the game and send the game result to clients
* @param gameId The Game ID
*/
function endGame(gameId) {
console.log('inside endGame');
const { scoreBoard, timer } = db[gameId];
clearInterval(timer);
db[gameId].gameStarted = false;
const winner = findWinner(scoreBoard);
console.log('scoreBoard', scoreBoard);
console.log('winner', winner);
io.sockets.in(gameId).emit('endGame', {
scoreBoard,
winner: winner.name,
winnerID: winner.id,
});
}
/**
* Check the word sent by client
* @param data {{ word: string, gameId: int }}
*/
function checkWord(data) {
console.log('*** checkWord ***');
const word = data.word.toLowerCase();
const thisRoom = db[data.gameId];
const { wordData, gameStarted, foundWords } = thisRoom;
if (!thisRoom || !gameStarted) return;
const wordIndex = wordData.answers.indexOf(word);
const emitPayload = { word, index: wordIndex };
emitPayload.socketId = this.id;
if (wordIndex === -1) {
emitPayload.incorrectWord = true;
} else if (foundWords[word] === true) {
emitPayload.alreadyTaken = true;
} else {
const { scoreBoard } = thisRoom;
scoreBoard[this.id].score += word.length;
console.log(scoreBoard);
foundWords[word] = true;
emitPayload.scoreBoard = scoreBoard;
}
io.sockets.in(data.gameId).emit('wordChecked', emitPayload);
if (Object.keys(foundWords).length === wordData.answers.length) {
endGame.call(this, data.gameId);
}
}
/**
* Start the countdown timer
* @param gameId The Game ID
*/
function startTimer(gameId) {
console.log('inside startTimer');
const socket = this;
let countdown = db[gameId].config.timeLimit;
const timer = setInterval(() => {
countdown -= 1;
io.sockets.in(gameId).emit('timer', { countdown });
if (countdown <= 0) {
endGame.call(socket, gameId);
}
}, 1000);
db[gameId].timer = timer;
}
/**
* Countdown finished and the game begins!
* @param gameId The Game ID
*/
function hostStartGame(gameId) {
console.log('Game Started.');
db[gameId].gameStarted = true;
sendWord.call(this, gameId);
startTimer.call(this, gameId);
}
/* *****************************
* *
* PLAYER FUNCTIONS *
* *
***************************** */
/**
* A player clicked the 'START GAME' button.
* Attempt to connect them to the room that matches
* the gameId entered by the player.
* @param data {{ gameId: int, playerName: string }}
*/
function joinGame(data) {
// A reference to the player's Socket.IO socket object
const sock = this;
// Look up the room ID in the Socket.IO adapter object.
const room = gameSocket.adapter.rooms[data.gameId];
// If the room exists...
if (room !== undefined) {
// attach the socket id to the emitPayload object.
const emitPayload = data;
emitPayload.mySocketId = sock.id;
db.clientRoom[sock.id] = data.gameId;
// Join the room
sock.join(data.gameId);
console.log(`Player ${data.playerName} joining game: ${data.gameId}`);
// Emit an event notifying the clients that the player has joined the room.
io.sockets.in(data.gameId).emit('guestJoinedRoom', emitPayload);
db[data.gameId].scoreBoard[this.id] = {
name: data.playerName,
score: 0,
};
console.log(db[data.gameId].scoreBoard);
} else {
// Otherwise, send an error message back to the player.
this.emit('errorMessage', { message: 'This room does not exist.' });
}
}
/* *************************
* *
* GAME LOGIC *
* *
************************* */
/**
* This function is called by index.js to initialize a new game instance.
* @param sio The Socket.IO library
* @param socket The socket object for the connected client.
*/
function initGame(sio, socket) {
io = sio;
gameSocket = socket;
// On new connection
gameSocket.emit('connected', { message: 'You are connected!' });
// Game events
gameSocket.on('getDefaultConfig', sendDefaultConfig);
gameSocket.on('createNewGame', createNewGame);
gameSocket.on('startNewGame', prepareGame);
gameSocket.on('countdownFinished', hostStartGame);
gameSocket.on('joinGame', joinGame);
gameSocket.on('checkWord', checkWord);
gameSocket.on('restartGame', restartGame);
gameSocket.on('disconnecting', whileDisconnecting);
// Error handling
gameSocket.on('error', (error) => { console.error(error); });
}
module.exports = {
initGame,
};