-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
493 lines (422 loc) · 15.7 KB
/
index.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
491
492
493
/* -------------------------------------------------------------------------- */
/* Libraries */
/* -------------------------------------------------------------------------- */
const express = require("express");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const path = require("path");
const mysql = require("mysql");
const bodyParser = require("body-parser");
const sharedsession = require('express-socket.io-session');
const { calculScore } = require('./back/score');
const {
body,
validationResult
} = require("express-validator");
const jsonParse = bodyParser.json();
// const urlencodedParse = bodyParser.urlencoded({extended: false});
const manageUser = require("./back/manageUser");
const BSGame = require("./back/BattlesheepGame");
const {
connect
} = require("http2");
const {
BDD
} = require("./db/bdd");
const {
all
} = require("express/lib/application");
const {
hostname
} = require("os");
const Verif = require('./back/weapons');
const Database = new BDD();
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
/* ------------------------- Session initialization ------------------------- */
const session = require("express-session")({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 2 * 60 * 60 * 1000,
secure: false,
},
});
app.use(jsonParse);
app.use(session);
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use('/favicon.ico', express.static(path.join(__dirname, 'public', 'img', 'favicon.ico')));
if (app.get("env") === "production") {
app.set("trust proxy", 1);
session.cookie.secure = true;
}
io.use(sharedsession(session, {
// Session automatically change if changement
autoSave: true
}));
/**
* Obtain the higher id of an object
* @param {Object} obj
* @return {String} max id
*/
function getMaxKey(obj) {
let result = -1;
Object.keys(obj).forEach(key => {
if (key > result) result = key;
});
return result;
}
/* -------------------------------------------------------------------------- */
/* Get the different request */
/* -------------------------------------------------------------------------- */
// get the index page
app.get("/", (req, res) => {
res.render("index", {
title: "BattleSheep by ZephyrStudio",
description: "Welcome in our Web project !",
scripts: [{
name: "home",
type: "module",
}, {
name: "threejs_check",
type: "module",
}],
});
});
// get the signup page
app.get("/signup", (req, res) => {
// Here : check if the user is already connected
// If he's not, send him the signup page
// else, redirect him to the scoreboard page
if (!req.session.username) {
res.render("signup", {
title: "BattleSheep | Sign up, Log in",
description: "Sign up or log in to BattleSheep",
scripts: [{
name: "http",
type: "text/javascript",
},
{
name: "signup",
type: "text/javascript",
},
],
});
} else {
res.redirect("/lobby");
return;
}
});
// post request to signup
app.post("/signup", body("pseudo").isLength({
min: 3
}).trim().escape(),
body("password").isLength({
min: 3
}).trim(),
(req, res) => {
console.log("--- SIGN UP ---");
let pseudo = req.body.pseudo;
let password = req.body.password;
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.error(errors);
res.status(400).json({
errors: errors.array(),
});
} else {
manageUser.signUp(password, (mdp) => {
Database.signUp(pseudo, mdp, (e) => {
if (e == true) {
req.session.username = req.body.pseudo;
req.session.save();
res.send('OK');
}
});
});
}
}
);
// post request to login
app.post("/login", body("pseudo").isLength({
min: 3,
}).trim().escape(),
body("password").isLength({
min: 3,
})
.trim(),
(req, res) => {
console.log("--- LOG IN ---");
let pseudo = req.body.pseudo;
let password = req.body.password;
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.error(errors);
res.status(400).json({
errors: errors.array(),
});
} else {
manageUser.signIn(password, (mdp) => {
Database.signIn(pseudo, mdp, (e) => {
if (e == true) {
req.session.username = req.body.pseudo;
req.session.save();
res.send('OK');
}
});
});
}
}
);
// get the rule page
app.get("/rules", (req, res) => {
res.render("rules", {
title: "BattleSheep | Rules",
description: "BattleSheep rules",
scripts: [{
name: "home",
type: "module",
}, {
name: "threejs_check",
type: "module",
}],
});
});
// get the lobby page
app.get("/lobby", (req, res) => {
if (!req.session.username) {
res.redirect("/");
return;
}
Database.refreshScore(req.session.username, "", "", (scoreboard) => {
res.render("lobby", {
username: req.session.username,
scoreboard
});
});
});
// get the game page
app.get("/game", (req, res) => {
if (!req.session.username) {
res.redirect("/");
return;
}
res.render("game");
});
// post request to logout
app.post("/logout", (req, res) => {
console.log("--- DECONNEXION ---");
req.session.destroy();
res.send('OK');
});
// get the not available page (if browser = firefox for example)
app.get("/not_available", (req, res) => res.render("not_available"));
// Capture 404 requests
app.use((req, res) => res.render("404"));
/* -------------------------------------------------------------------------- */
/* ROOMS */
/* -------------------------------------------------------------------------- */
let allRooms = {};
let allGames = {};
let disconnectedUsers = [];
// connection to socket
io.on("connection", (socket) => {
const username = socket.handshake.session.username;
if (socket.handshake.session.idRoom === undefined) {
console.log("--- LOBBY ---");
console.log("Connexion de ", username, " au Lobby");
} else {
let idRoom = socket.handshake.session.idRoom
console.log("--- GAME ---")
console.log("Connexion de ", username, " à la room ", idRoom);
socket.join(idRoom);
if (!disconnectedUsers.includes(username)) {
const id = allRooms[idRoom][0].name === username ? 0 : 1;
socket.emit("resultPlayerId", id);
if (allRooms[idRoom] && allRooms[idRoom].length == 2) {
io.to(idRoom).emit("timeToPlay");
}
}
}
if (disconnectedUsers.includes(username)) {
const idRoom = socket.handshake.session.idRoom;
io.to(idRoom).emit("disconnection");
socket.leave(idRoom);
socket.handshake.session.idRoom = undefined;
disconnectedUsers.splice(disconnectedUsers.indexOf(username), 1);
}
/* -------------------------------------------------------------------------- */
/* Lobby */
/* -------------------------------------------------------------------------- */
// get and emit to everyone his score and rooms already created
socket.on('login', () => {
Database.refreshScore(socket.handshake.session.username, "", "", (a) => {
io.emit("display-score", a);
});
io.emit("display-rooms", allRooms);
socket.emit("display-username", socket.handshake.session.username);
});
// emit to the everyone the score of the actual rooms
socket.on("get-score", user => {
Database.refreshScore(user, "", "", (a) => {
io.emit("display-room-score", user, a.first.score);
});
})
/* -------------------------------------------------------------------------- */
/* Rooms */
/* -------------------------------------------------------------------------- */
// create a new room and emit it to everyone on lobby
socket.on("host-room", () => {
let username = socket.handshake.session.username;
let res = Number(getMaxKey(allRooms)) + 1;
let data = {
name: username,
playerId: 0,
validGrid: false
};
// Create room and game
allRooms[res] = [data];
allGames[res] = new BSGame(res);
allGames[res].addPlayer(username);
console.log(username, " is hosting room-", res);
socket.handshake.session.idRoom = res;
io.emit("display-rooms", allRooms);
socket.disconnect();
});
// join the room clicked (by the hostname) and emit to everyone to hide the joined room
socket.on("join-room", (hostName) => {
let res = Object.keys(allRooms).findIndex(key => allRooms[key][0].name == hostName)
if (allRooms[res] && allRooms[res].length < 2) {
let username = socket.handshake.session.username;
if (hostName != username) {
let data = {
name: username,
playerId: 1,
validGrid: false
};
allRooms[res].push(data);
allGames[res].addPlayer(username);
socket.handshake.session.idRoom = res;
console.log(username, " Joined room : room-", res, " hosted by ", hostName);
io.emit("hide-card", hostName);
socket.disconnect();
}
}
});
/* -------------------------------------------------------------------------- */
/* Game */
/* -------------------------------------------------------------------------- */
// the player ("playerId") play at the "x", "y" coordinates with the "weapon"
socket.on("playerPlayed", (x, y, playerId, weapon) => {
// The player with the id "playerId", played on the case with the position "x", "y", with the weapon "weapon"
const idRoom = socket.handshake.session.idRoom;
const currentGame = allGames[idRoom];
const touchedId = playerId === 0 ? 1 : 0;
const touchedGrid = currentGame.playerStartGrids[touchedId];
// Check attack data
let isCorrect = Verif.isCoordValid(x, y);
isCorrect &&= (currentGame.currentPlayer === playerId);
isCorrect &&= (Verif.isWeapon(weapon));
isCorrect &&= (!currentGame.weaponsUsed[playerId].includes(weapon));
if (!isCorrect) return;
console.log("### Player", playerId, "is playing (room " + idRoom + ") ###");
console.log("Weapon :", weapon);
console.log("Target position :", x, y);
// Calcul damages
const result = Verif.attack(touchedGrid, weapon, x, y, currentGame.history, touchedId);
for (let i = 0; i < result.length; i++)
result[i].playerId = touchedId;
console.log("The player touched :", result);
// Update the player's score
let nbNewFoundSheep = 0;
result.forEach(res => {
if (res.state === 2)
nbNewFoundSheep++;
})
currentGame.scores[playerId] += calculScore(currentGame.chrono.getTimeSeconds(), nbNewFoundSheep);
// Update the game state
result.forEach(damage => {
currentGame.addToHistory(damage);
});
currentGame.currentPlayer = touchedId;
if (weapon !== "Shears")
currentGame.weaponsUsed[playerId].push(weapon);
// Store scores
if (currentGame.isGameFinished)
Database.refreshScore(currentGame.players[playerId], currentGame.players[touchedId], Math.floor(currentGame.scores[playerId]));
// Get the touched flock
const flock = Verif.propagationWrapper(currentGame.playerStartGrids[touchedId], x, y);
// Get the flock state
let isFlockDown = true;
if (flock)
isFlockDown = flock.every( coord => currentGame.isInHistory(coord.x, coord.y, touchedId) );
else isFlockDown = false;
// Send the refresh to the front-end
const players = io.sockets.adapter.rooms.get(idRoom);
for (const p of players) {
const pSocket = io.sockets.sockets.get(p);
const pUsername = pSocket.handshake.session.username;
const pId = allRooms[idRoom].findIndex(e => e.name == pUsername);
pSocket.emit(
"resultPlay",
currentGame.playerStartGrids[pId],
currentGame.currentPlayer,
currentGame.history,
currentGame.weaponsUsed[pId],
currentGame.chrono.minutes,
currentGame.chrono.seconds,
currentGame.isGameFinished,
isFlockDown,
Math.floor(currentGame.scores[pId]),
weapon
);
}
});
// Verify the grid when the user validate it, if both grid are verified, start gameplay
socket.on("checkGrid", (grid) => {
let idRoom = socket.handshake.session.idRoom;
let username = socket.handshake.session.username;
let nbSheep = 0;
for (let x = 0; x < 10; x++) {
for (let y = 0; y < 10; y++) {
if (grid[x][y] != undefined) nbSheep++;
}
}
if (nbSheep === 20) {
let idPlayer = allRooms[idRoom].findIndex(e => e.name == username)
allRooms[idRoom][idPlayer].validGrid = true;
allGames[idRoom].playerStartGrids[idPlayer] = grid;
if (allRooms[idRoom].every(e => e.validGrid == true)) {
io.to(idRoom).emit("startGameplay");
allGames[idRoom].chrono.incrementChrono();
return;
} else return socket.emit("resultGrid", true);
}
return socket.emit("resultGrid", false);
});
/* -------------------------------------------------------------------------- */
/* Disconnection */
/* -------------------------------------------------------------------------- */
socket.on("disconnect", (reason) => {
console.log("Disconnection of ", socket.handshake.session.username, " reason : ", reason);
let idRoom = socket.handshake.session.idRoom;
if (reason == "transport close") {
disconnectedUsers.push(socket.handshake.session.username);
if (allRooms[idRoom] && allRooms[idRoom].length == 2) {
allRooms[idRoom].pop();
allRooms[idRoom][0].username = "!";
} else {
delete allRooms[idRoom];
}
socket.to(idRoom).emit("disconnection");
}
});
});
http.listen(process.env.APP_PORT, () => {
console.log("Serveur lancé sur le port", process.env.APP_PORT);
});