-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
152 lines (139 loc) · 4.88 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
'use strict';
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const cors = require('cors');
const PORT = process.env.PORT || 3005;
const INDEX = '/index.html';
const { addRoom, getRoom, getAllRooms, deleteRoom, addPlayerToRoom, getPlayerWithRoomCodeAndSocketID, removePlayerFromRoomWithSocketID, initiateGame, searchPlayerInLeavePlayer, playerReJoinGameAfterLeave, deletePlayerFromLeavePlayer, checkUserCanJoinOrReJoinRoom, flipCardInRoom } = require('./src/utils/RoomManager');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
// Run when client connects
io.on('connection', (socket) => {
console.log('player connected !');
/**
* Va vérifié si le joueur peu rejoindre un salon directement via le lien, car il c'est déjà inscrit mais peu avoir eu une déconnexion
*/
socket.on('verifyUserIsOnLeavePlayerOrInGameRoom', (roomCode, cookieUserID) => {
const canJoin = checkUserCanJoinOrReJoinRoom(roomCode, cookieUserID);
if (!canJoin) {
const room = getRoom(roomCode);
if (room !== undefined) {
if (room.isStarted) {
socket.emit('roomExistButAlreadyStarted');
return;
}
}
socket.emit('onUserCannotJoinCauseOfNotInRoom');
}
});
/**
* Quand un utilisateur essaye de rejoindre on va le rechercher dans la liste des joueurs ayant quitte
*/
socket.on('userAttemptToJoinRoom', (roomCode, cookieUserID) => {
const room = getRoom(roomCode);
if (room !== undefined) {
const player = searchPlayerInLeavePlayer(roomCode, cookieUserID);
if (player) {
socket.emit('userFoundInLeavePlayer', player);
}
}
});
/**
* Si l'utilisateur a confirmer qu'il souhaiter rejoindre malgrès la déconnexion on le reinsert dans son salon
*
*/
socket.on('userConfirmedJoiningAgain', (roomCode, cookieUserID) => {
const player = playerReJoinGameAfterLeave(roomCode, cookieUserID, socket.id);
if (player) {
socket.roomCode = roomCode;
socket.join(roomCode);
socket.emit('onJoiningAgainSuccess');
socket.broadcast.to(roomCode).emit('userJoinedRoomAfterLeave', player.username);
} else {
socket.emit('onJoiningAgainFail');
}
});
/**
* Si l'utilisateur a confirmer qu'il ne souhaité par revenir en jeu alors on le retire des joueurs ayant quittés
*/
socket.on('deleteLeavePlayerIfExist', (roomCode, cookieUserID) => {
deletePlayerFromLeavePlayer(roomCode, cookieUserID);
});
/**
* Quand un joueur créer un partie
*/
socket.on('playerCreateGame', ({ roomName, username }, cookiePlayerID, gameOptions) => {
const room = addRoom(roomName, gameOptions);
addPlayerToRoom(username, room.code, socket.id, cookiePlayerID, true);
socket.roomCode = room.code;
socket.join(room.code);
socket.emit('successfullyCreateGame', room);
socket.broadcast.to(room.code).emit('newRoomCreated', room);
});
/**
* Quand un joueur tente de rejoindre une salle
*/
socket.on('joiningRoom', ({ username, roomCode }, cookiePlayerID) => {
const room = getRoom(roomCode);
if (room !== undefined) {
if (room.isStarted) {
socket.emit('gameAlreadyStarted');
} else {
const result = addPlayerToRoom(username, roomCode, socket.id, cookiePlayerID);
if (result !== false) {
socket.roomCode = roomCode;
socket.join(roomCode);
socket.emit('successfullyJoinedRoom');
socket.broadcast.to(roomCode).emit('userJoinedRoom', username);
} else {
socket.emit('errorWhenJoiningRoom');
}
}
}
});
/**
* Quand l'hote clique sur le bouton de lancement de la partie
*/
socket.on('hostLaunchGame', () => {
const room = initiateGame(socket.roomCode);
if (room) {
io.to(socket.roomCode).emit('gameLaunching', room);
} else {
socket.emit('failedToLaunchGame');
}
});
socket.on('flipCard', (cardID, cardRank, cardNumber) => {
const canFlip = flipCardInRoom(socket.roomCode, cardID, cardRank, cardNumber);
if (canFlip === true) {
io.to(socket.roomCode).emit('onCardFlip', cardID, cardRank);
} else {
socket.emit('onCardCannotFlip', canFlip);
}
});
/**
* Quand un utilisateur se déconnecte
*/
socket.on('disconnect', () => {
const player = getPlayerWithRoomCodeAndSocketID(socket.roomCode, socket.id);
if (player) {
removePlayerFromRoomWithSocketID(socket.roomCode, socket.id);
io.to(player.roomCode).emit('userDisconnect', player.username, getRoom(player.roomCode));
}
});
});
app.use(cors());
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
app.get('/', (req, res) => res.sendFile(INDEX, { root: __dirname + '/public/' }));
app.get('/api/room/get/all', (req, res) => {
res.send(JSON.stringify(getAllRooms()));
});
app.get('/api/room/get/:roomCode', (req, res) => {
const room = getRoom(req.params.roomCode);
if (room !== undefined) {
res.send(JSON.stringify(room));
} else {
res.send(JSON.stringify({ error: `Pas de salle correspondante au code : [${req.params.roomCode}]` }));
}
});