-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (85 loc) · 2.96 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require('fs');
const {compileETag} = require("express/lib/utils");
const port = 1825;
app.use(express.static('public'));
app.use("/style/style.css", express.static(__dirname + '/style/style.css'));
app.use("/style/style-jeux.css", express.static(__dirname + '/style/style-jeux.css'));
app.use("/style/style-room.css", express.static(__dirname + '/style/style-room.css'));
app.use("/js/client.js", express.static(__dirname + '/js/client.js'));
app.use("/js/VuePlateau.js", express.static(__dirname + '/js/VuePlateau.js'));
app.use("/images/fav4.ico", express.static(__dirname + '/images/fav4.ico'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/templates/index.html');
});
app.get('/games/game-lobby', (req, res) => {
res.sendFile(__dirname + '/templates/games/game-lobby.html');
});
app.get('/games/room-lobby', (req, res) => {
res.sendFile(__dirname + '/templates/games/room-lobby.html');
});
http.listen(port, () => {
console.log("Server running !");
});
let rooms = [];
io.on('connection', (socket) => {
console.log("New connection !" + socket.id);
socket.on('playerData', (data) => {
console.log("Player data received !");
console.log(data);
let room = null;
if (!data.roomId) {
room = createRoom(data);
console.log(data.socketId);
console.log("Room created !");
} else {
room = rooms.find(room => room.id === data.roomId);
console.log("Room found !");
data.socketId = socket.id;
if (room === undefined) {
return;
}
room.players.push(data);
}
//data.socketId = socket.id;
socket.join(room.id);
io.to(socket.id).emit('roomData', room.id);
if (room.players.length === 2) {
io.to(room.id).emit('startGame', room.players);
}
});
socket.on('playedCell', (data) => {
io.to(data.roomId).emit('playedCell', data);
});
socket.on('getRooms', () => {
io.to(socket.id).emit('rooms', rooms);
});
socket.on('playerWin', (data) => {
io.to(data.roomId).emit('playerWin', data);
});
socket.on('disconnect', () => {
let room = null;
rooms.forEach(r => {
r.players.forEach(p => {
console.log(p.socketId + " " + socket.id);
if (p.socketId === socket.id && p.host) {
room = r;
rooms = rooms.filter(r => r !== room);
}
})
})
});
});
function createRoom(player) {
const room = { id: generateRoomId(), players: [] };
player.roomId = room.id;
room.players.push(player);
rooms.push(room);
return room;
}
function generateRoomId() {
return Math.random().toString(36).substr(2, 9);
}