-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
389 lines (351 loc) · 13 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
const express = require('express')
const app = express()
const server = require('http').Server(app)
var io = require("socket.io")(server);
const exec = require('child_process').exec
var visibleFields, jsonBoard;
var mysql = require('mysql');
var bodyParser = require('body-parser');
var session = require('express-session');
// database connection
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'sudokuDB',
});
connection.connect(function(err) {
if (err) {
console.log('An error occured when connecting to database:', err);
} else {
console.log("Server connection went succesfully - go to http://127.0.0.1:8080/");
}
});
app.use(express.static(__dirname + '/public'));
app.set('views', './views')
app.set('view engine', 'ejs')
app.use(express.urlencoded({
extended: true
}))
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/', (req, res) => {
req.session.loggedin = false;
res.render('login')
});
app.get('/login', (req, res) => {
req.session.loggedin = false;
res.render('login')
});
app.post('/login', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
connection.query('SELECT * FROM users WHERE NAME = ? AND password = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.render('index', {
rooms: rooms,
login: username
});
} else {
response.redirect('/');
}
response.end();
});
} else {
response.send('Please enter both Username and Password.');
response.end();
}
});
app.post('/activate', function(request, response) {
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
function(token, done) {
var email = request.body.email;
var username = request.session.username;
request.session.email = email;
if (username && email) {
connection.query('SELECT * FROM users WHERE email = ? AND name = ?', [email, username], function(error, results, fields) {
if (results.length <= 0) {
// no such account
return response.redirect('/activate');
} else {
connection.query('INSERT INTO tokens(token) VALUES(?)', [token], function(error, results, fields) {
if (!error) {
// session token set to validate token accessed from external link (email)
request.session.token = token;
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'your_gmail_mail',
pass: 'your_password'
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
to: email,
from: 'Sudoku Multiplayer',
subject: 'Sudoku Account Activation',
text: 'Activate your account using this link:\n\n' + 'http://' + request.headers.host + '/activate/' + token + '\n\n'
};
smtpTransport.sendMail(mailOptions, function(err) {
done(err, 'done');
});
response.render('checkemail');
} else {
response.redirect('/login');
}
})
}
});
}
}
])
});
app.get('/activate/:token', function(request, response) {
if (request.session.token === request.params.token) {
connection.query('UPDATE users SET activated = 1 WHERE name = ?', [request.session.username], function(error, results) {
if (!error) {
response.render('activateconfirmation', {
token: request.params.token,
user: request.session.username
});
} else {
response.send('Could not activate your account. Contact consumer support for more details ' +
'at sudoku.support@fakemail.com');
}
});
} else {
response.render('login');
}
});
app.post('/confirm', function(request, response) {
request.session.username = null;
request.session.email = null;
response.redirect('login');
});
app.post('/register1', function(request, response) {
request.session.loggedin = false;
response.render('register');
});
app.post('/register2', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
connection.query('INSERT INTO users(name, password) VALUES(?, ?)', [username, password], function(error, results, fields) {
if (!error) {
request.session.loggedin = true;
request.session.username = username;
}
response.redirect('/login');
response.end();
});
} else {
response.send('Please enter both Username and Password.');
response.end();
}
});
app.get('/index', function(request, response) {
if (request.session.loggedin) {
io.emit('update-rooms', rooms);
response.render('index', {
rooms: rooms,
login: this.login
})
} else {
response.send('Please login to view this page!');
}
response.end();
});
const rooms = {}
app.post('/room', (req, res) => {
if (rooms[req.body.room] != null) {
return res.redirect('/index')
}
rooms[req.body.room] = {
users: {},
is_game_played: false,
sudoku_answer: []
}
res.redirect(req.body.room)
// Send message that new room was created
io.emit('update-rooms', rooms)
})
app.get('/:room', (req, res) => {
if (rooms[req.params.room] == null) {
return res.redirect('/index')
}
res.render('room', {
roomName: req.params.room,
login: this.login
})
})
server.listen(8080)
io.on('connection', socket => {
socket.on('new-user', (room, name, points, is_playing, is_eliminated) => {
socket.join(room)
rooms[room].users[socket.id] = {
name,
points,
is_playing,
is_eliminated
}
socket.to(room).broadcast.emit('user-connected', name)
})
socket.on('send-chat-message', (room, message) => {
socket.to(room).broadcast.emit('chat-message', {
message: message,
name: rooms[room].users[socket.id].name
})
});
socket.on('submit-sudoku', (room, submited_sudoku, id) => {
if (rooms[room].users[id].has_submitted === false && rooms[room].users[id].is_playing === true) {
rooms[room].users[id].has_submitted = true;
rooms[room].users[socket.id].points = calculate_points(1, 0, -1, preprocess_sudoku(submited_sudoku, rooms[room].sudoku_answer));
io.in(room).emit('first-message', rooms[room].users[id].name);
var check = sort_results(rooms[room].users);
if (check !== null) {
var last_place_id = check[check.length - 1][2];
rooms[room].users[last_place_id].is_eliminated = true;
io.in(room).emit('game-has-ended', check);
rooms[room].is_game_played = false;
}
}
})
socket.on('start-game', (room, minutes, id, difficulty, gamemode) => {
if (difficulty == 4) {
visibleFields = Math.floor((Math.random() * 9) + 70) // 70 or 79
} else {
visibleFields = Math.floor((Math.random() * 9) + 27 + (difficulty - 1) * 9) //27,36 or 36,45 or 45,54
}
if (rooms[room].is_game_played == false) {
var no_rounds = Object.keys(rooms[room].users).length - 1;
start_new_game(room, minutes, socket, gamemode, no_rounds, true);
}
})
socket.on('disconnect', () => {
getUserRooms(socket).forEach(room => {
socket.to(room).broadcast.emit('user-disconnected', rooms[room].users[socket.id].name)
delete rooms[room].users[socket.id]
if (Object.keys(rooms[room].users).length == 0) {
delete rooms[room];
io.emit('update-rooms', rooms);
}
})
});
})
function getUserRooms(socket) {
return Object.entries(rooms).reduce((names, [name, room]) => {
if (room.users[socket.id] != null) names.push(name)
return names
}, [])
}
function preprocess_sudoku(client_array, solver_array) {
var correct_fields = 0;
var void_fields = 0;
var incorrect_fields = 0;
for (var a = 0; a < client_array.length; a++) {
for (var b = 0; b < client_array.length; b++) {
var c = parseInt(client_array[a][b]);
if (!isNaN(c)) {
if (c === solver_array[a][b]) correct_fields++;
else incorrect_fields++;
} else {
if (client_array[a][b] === '') void_fields++;
else incorrect_fields++;
}
}
}
return [correct_fields, void_fields, incorrect_fields];
}
function calculate_points(a, b, c, d = [correct_f, void_f, incorrect_f]) {
return a * d[0] + b * d[1] + c * d[2]
}
function sort_results(associative_array) {
var array = [];
for (var user in associative_array) {
if (associative_array[user].has_submitted === false && associative_array[user].is_playing === true) {
return null;
}
if (associative_array[user].has_submitted === true) {
array.push([associative_array[user].name, associative_array[user].points - visibleFields, user]);
if (associative_array[user].is_playing === true) {
associative_array[user].is_playing = false;
}
}
}
array.sort(function(a, b) {
return b[1] - a[1];
});
return array;
}
function createBoard(callback) {
const cmdstring = ' java -cp .\\src generator.BoardCreatorMain ' + visibleFields.toString()
exec(cmdstring, function(error, stdout, stderr) {
if (error !== null) {
console.log("An error occured:" + stderr);
callback(stderr);
}
callback(null, stdout);
return stdout;
})
}
function start_new_game(room, minutes, socket, gamemode, no_rounds, is_first_round) {
createBoard(function(err, out) {
jsonBoard = out //exec is async, everything what needs a board has to run from here
const sudoku = JSON.parse(jsonBoard)
io.in(room).emit('send-minutes-message', {
minutes: minutes,
name: rooms[room].users[socket.id].name,
boolean: rooms[room].is_game_played,
sudoku: sudoku.start_sudoku
});
rooms[room].is_game_played = true;
rooms[room].sudoku_answer = sudoku.solver;
for (user in rooms[room].users) {
rooms[room].users[user].points = 0;
rooms[room].users[user].has_submitted = false;
if (gamemode === 5 || is_first_round) {
rooms[room].users[user].is_eliminated = false;
}
if (rooms[room].users[user].is_eliminated === false) {
rooms[room].users[user].is_playing = true;
}
}
var countdown = 60 * minutes;
const time = setInterval(function() {
countdown--;
io.in(room).emit('timer', {
countdown: countdown
});
if (rooms[room] != null) {
if (countdown < 1 || rooms[room].is_game_played === false) {
clearInterval(time);
if (rooms[room] != null) {
rooms[room].is_game_played = false;
no_rounds--;
if (gamemode == 6 && no_rounds > 0) {
start_new_game(room, minutes, socket, gamemode, no_rounds, false);
}
}
}
}
}, 1000);
});
}