-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
490 lines (407 loc) · 13.2 KB
/
server.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var swig = require('swig');
// define default settings
var BOARD_DIFFICULTY = 'EASY',
SHOW_ERRORS = false,
ALLOW_ERRORS = true;
// define global variables
var rooms = {},
users = {},
leaderboard = [];
// define object constructors
function User(roomID, socketID) {
this.userID = generateUserID();
this.roomID = roomID;
this.socketID = socketID;
this.ready = false;
}
function Room(roomID, hostID) {
this.roomID = roomID;
this.hostID = hostID;
this.settings = new Settings();
this.full = false;
this.usersByID = [];
this.startBoard = null;
this.endBoard = null;
}
function Settings() {
this.difficulty = BOARD_DIFFICULTY;
this.showErrors = SHOW_ERRORS;
this.allowErrors = ALLOW_ERRORS;
}
//define templates
var layout_tpl = swig.compileFile('views/layout.html');
var header_tpl = swig.compileFile('views/header_tpl.html');
var single_player_tpl = swig.compileFile('views/single_player_tpl.html');
var header_content = header_tpl(
{
//single_player = single_player_tpl
});
var layout = layout_tpl(
{
header: header_content
});
/*
var single_player = single_player_tpl(
{
});*/
/*******************************************************
*
* begin everything
*
*******************************************************/
startListen();
// include all files stored in the static folder
app.use(express.static(path.join(__dirname, 'static')));
// initialize swig stuff
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
/*app.get('/', function(req, res) {
res.render('index', {
title: 'Home',
header: header_content
});
});*/
app.get('/', function(req, res) {
res.render('single_player_tpl', {
title: 'Home',
header: header_content
});
});
app.get('/multiplayer', function(req, res) {
res.render('multi_player_tpl', {
title: 'Multiplayer',
header: header_content
});
});
app.get('/leaderboard', function(req, res) {
res.render('leaderboard_tpl', {
title: 'Leaderboard',
header: header_content
});
});
io.on('connection', function(socket) {
console.log('a user connected');
// initialize user on join
var user = new User(null, socket.id);
users[user.userID] = user; // add user to global list
socket.uid = user.userID; // add socket var (MIGHT NOT BE NECESSARY)
socket.on('create_room', function() {
var roomID = generateRoomID();
// create room
rooms[roomID] = new Room(roomID, user.userID);
rooms[roomID].usersByID.push(user.userID);
// create board
rooms[roomID].fillBoard();
// initialize socket values
socket.join(roomID);
socket.room = roomID;
user.roomID = roomID;
socket.emit('room_created', roomID);
console.log('room: ' + roomID + ' created!');
});
socket.on('join_room', function(roomID) {
console.log("joining room");
if (!rooms[roomID]) {
console.log('room does not exist');
socket.emit('incorrect_room');
return;
}
if (rooms[roomID].full) {
console.log('room is full');
socket.emit('full_room');
return;
}
rooms[roomID].full = true;
rooms[roomID].usersByID.push(user.userID);
// initialize socket values
socket.join(roomID);
socket.room = roomID;
user.roomID = roomID;
io.to(roomID).emit('game_ready');
console.log('user joined room: ' + roomID);
});
socket.on('user_ready', function() {
var user = users[socket.uid],
room = rooms[user.roomID];
user.ready = true;
if (room.full && room.ready()) {
console.log('starting game!');
// room.fillBoard();
// TODO: generate board, store it here.
// then send it to the room. Make sure
// you keep the solution stored here too
io.to(room.roomID).emit('start_game', room.startBoard);
}
});
socket.on('client_board_update', function(id, num) {
var user = users[socket.uid],
room = rooms[user.roomID],
row = parseInt(id.substring(0, 1), 10),
col = parseInt(id.substring(1, 2), 10),
blockID = (row * 9) + col;
console.log(row, col);
console.log("user " + user.userID + " entered " + num + " in square id: " + blockID);
if (num) { // user added a number
if (room.endBoard[blockID] == num) {
socket.broadcast.to(room.roomID).emit('insert_entry', id);
} else {
socket.broadcast.to(room.roomID).emit('remove_entry', id);
}
} else { // user removed a number
socket.broadcast.to(room.roomID).emit('remove_entry', id);
}
});
socket.on('game_over', function() {
var user = users[socket.uid],
room = rooms[user.roomID];
socket.broadcast.to(room.roomID).emit('game_loss');
});
socket.on('disconnect', function() {
removeUser(socket.uid);
socket.leave();
console.log('a user left :(');
});
socket.on('print_stats', function() {
console.log('==========================');
console.log('rooms ')
for (var r in rooms) {
rooms[r].printRoom();
}
});
socket.on('update_leaderboard', function(nameAndTime) {
console.log('updating the leaderboard');
console.log(nameAndTime);
leaderboard.push(nameAndTime);
for (var leader in leaderboard){
console.log(leader);
}
socket.emit('leaderboard_updated', leaderboard);
});
socket.on('get_leaderboard', function() {
socket.emit('sent_leaderboard', leaderboard);
});
});
/*******************************************************
*
* utility functions
*
*******************************************************/
function startListen() {
var port = Number(process.env.PORT || 5000);
http.listen(port, function() {
console.log('listening on *:' + port);
});
}
function removeUser(uid) {
var user = users[uid],
room = rooms[user.roomID],
cand; // candidate to remove
// if user is not in a room, don't need to remove them from a room
if (!room) return;
cand = room.usersByID.indexOf(uid);
room.usersByID.splice(cand); // TODO: this may be fishy. check it later.
delete users[uid];
room.full = false;
if (room.usersByID.length === 0) {
console.log('deleting room');
delete rooms[room.roomID];
return;
}
if (room.hostID == uid) {
room.hostID = null;
console.log('host left. need transfer');
room.transferHost();
}
io.to(room.roomID).emit('restart_game');
// TODO: emit message allowing host to invite new player.
}
// generates a "unique" userID. should never duplicate
// found here: https://gist.github.com/gordonbrander/2230317
function generateUserID() {
return 'u_' + Math.random().toString(36).substr(2, 9);
}
// roomID uses same generation algo as userID, but the
// difference between the two is the prefix, 'u_' or 'r_'
function generateRoomID() {
return 'r_' + Math.random().toString(36).substr(2, 9);
}
/*******************************************************
*
* room prototypes
*
*******************************************************/
Room.prototype.transferHost = function() {
// TODO: write this
};
Room.prototype.printRoom = function() {
console.log('--------------------------');
console.log('room ' + this.roomID);
console.log(' | host: ' + this.hostID);
console.log(' | full: ' + this.full);
console.log(' | users:');
for (var i = 0; i < this.usersByID.length; i++) {
users[this.usersByID[i]].printUser();
}
console.log('--------------------------');
};
Room.prototype.ready = function() {
for (var i = 0; i < this.usersByID.length; i++) {
if (!users[this.usersByID[i]].ready) return false;
}
return true;
};
Room.prototype.printBoard = function() {
console.log("CURRENT BOARD (room: " + this.roomID);
printBoard(this.endBoard);
};
Room.prototype.printStartBoard = function() {
console.log("STARTING BOARD (room: " + this.roomID);
printBoard(this.startBoard);
};
Room.prototype.fillBoard = function() {
var board = new Array(81);
var randomNine = fishYatesShuffle(9);
//create base sudoku board
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
board[i * 9 + j] = (i * 3 + Math.floor(i / 3) + j) % 9 + 1;
}
}
//switch corresponding cols (Ex. the 2nd and 5th column)
for (var i = 0; i < 20; ++i) {
var col = Math.floor(Math.random() * 3);
do {
var swap = col + (Math.floor(Math.random() * 3) * 3); // + 0, +3, +6
} while (swap === 0)
for (var j = 0; j < 9; ++j) {
var tmp = board[col + (j * 9)];
board[col + (j * 9)] = board[swap + (j*9)];
board[swap + (j * 9)] = tmp;
}
}
//switch cols within section blocks (Ex. all columns in the first three
//columns will be swapped)
for (var i = 0; i < 20; ++i) {
var block = Math.floor(Math.random() * 3);
do {
var swap1 = Math.floor(Math.random() * 3) + block * 3;
var swap2 = Math.floor(Math.random() * 3) + block * 3;
} while (swap1 === swap2)
for (var j = 0; j < 9; ++j) {
var tmp = board[swap1 + (j * 9)];
board[swap1 + (j * 9)] = board[swap2 + (j * 9)];
board[swap2 + (j * 9)] = tmp;
}
}
//switch rows within section blocks (Ex. all rows in the first three rows will be swapped)
for (var i = 0; i < 20; ++i) {
var block = Math.floor(Math.random() * 3);
do {
var swap1 = Math.floor(Math.random() * 3) * 9 + block * 27;
var swap2 = Math.floor(Math.random() * 3) * 9 + block * 27;
} while (swap1 === swap2)
for (var j = 0; j < 9; ++j) {
var tmp = board[swap1 + j];
board[swap1 + j] = board[swap2 + j];
board[swap2 + j] = tmp;
}
}
//switch numbers (Ex. all 9s will be swapped with all 7s on the board)
for (var i = 0; i < 20; ++i) {
do {
var numb1 = Math.ceil(Math.random() * 9);
var numb2 = Math.ceil(Math.random() * 9);
} while (numb1 === numb2)
for (var j = 0; j < board.length; ++j) {
if (board[j] === numb1) board[j] = numb2;
else if (board[j] === numb2) board[j] = numb1;
}
}
this.endBoard = new Array(81);
var temp = "";
for (var i = 0; i < board.length; ++i)
{
this.endBoard[i] = board[i];
temp += board[i];
if ((i + 1) % 9 == 0) {
console.log(temp);
temp = "";
}
}
this.printBoard();
this.hideCells();
};
Room.prototype.hideCells = function() {
var board = this.endBoard.slice(0),
block = fishYatesShuffle(9);
for (var i = 0; i < 9; ++i) {
var cells = fishYatesShuffle(9),
cur_block = block.pop(),
block_row = Math.floor(cur_block / 3),
block_col = cur_block - block_row * 3;
for (var j = 0; j < 4; ++j) {
var cell = cells.pop(),
row = Math.floor(cell / 3),
col = cell - row * 3;
cell = row * 9 + block_row * 27 + col + block_col * 3;
board[cell] = 0;
// --board_size;
}
}
this.startBoard = board.slice(0);
this.printStartBoard();
};
/*******************************************************
*
* user prototypes
*
*******************************************************/
User.prototype.printUser = function() {
console.log(' | user ' + this.userID + ' (ready: ' + this.ready + ')');
};
/*******************************************************
*
* sudoku functions
*
*******************************************************/
function printBoard(board) {
if (board == null) {
return;
}
var row = "| ";
console.log('+---+---+---+---+---+---+---+---+---+');
for (var i = 0; i < board.length; i++) {
if (i % 9 == 0 && i > 0) {
console.log(row);
console.log('+---+---+---+---+---+---+---+---+---+');
row = "| ";
}
if (board[i] == 0) {
row += " | ";
} else {
row += board[i] + " | ";
}
}
console.log(row);
console.log('+---+---+---+---+---+---+---+---+---+');
}
function fishYatesShuffle(size) {
var fishYatesArray = [];
for (var i = 0; i < size; i++) {
fishYatesArray.push(i);
}
for (var j = size - 1; j >= 0; --j) {
var k = Math.floor(Math.random() * (j+1));
var cell1 = fishYatesArray[j];
var cell2 = fishYatesArray[k];
fishYatesArray[j] = cell2;
fishYatesArray[k] = cell1;
}
return fishYatesArray;
}