|
@@ -4,7 +4,6 @@ import DuelController from './game-server/duel-controller';
|
|
import MariadbConnector from './db/mariadb-connector';
|
|
import MariadbConnector from './db/mariadb-connector';
|
|
import ServerToolListner from './tools/server-tool-listener';
|
|
import ServerToolListner from './tools/server-tool-listener';
|
|
|
|
|
|
-
|
|
|
|
function Server() {
|
|
function Server() {
|
|
console.log('Starting 12 heroes server');
|
|
console.log('Starting 12 heroes server');
|
|
|
|
|
|
@@ -13,16 +12,12 @@ function Server() {
|
|
|
|
|
|
let mariadbConn = new MariadbConnector();
|
|
let mariadbConn = new MariadbConnector();
|
|
|
|
|
|
-
|
|
|
|
let authorizedPlayers = new Map();
|
|
let authorizedPlayers = new Map();
|
|
let connectedPlayers = new Map();
|
|
let connectedPlayers = new Map();
|
|
let authorizedPlayersNames = new Set();
|
|
let authorizedPlayersNames = new Set();
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
// let players = [];
|
|
// let players = [];
|
|
|
|
|
|
-
|
|
|
|
// let addNewPlayer = function (playerSocket, playerName) {
|
|
// let addNewPlayer = function (playerSocket, playerName) {
|
|
// console.log('players length : ' + players.length + ' : ' + players);
|
|
// console.log('players length : ' + players.length + ' : ' + players);
|
|
// if (players.length < 2) {
|
|
// if (players.length < 2) {
|
|
@@ -35,28 +30,34 @@ function Server() {
|
|
// }
|
|
// }
|
|
// };
|
|
// };
|
|
|
|
|
|
- server.on('connection', function (socket) {
|
|
|
|
- console.log("A player connected with id : " + socket.id);
|
|
|
|
|
|
+ server.on('connection', function(socket) {
|
|
|
|
+ console.log('A player connected with id : ' + socket.id);
|
|
|
|
|
|
- socket.on('disconnect', (reason) => {
|
|
|
|
- removeAllGamesCreatedByPlayer(connectedPlayers.get(socket.id));
|
|
|
|
|
|
+ socket.on('disconnect', reason => {
|
|
|
|
+ try {
|
|
|
|
+ removeAllGamesCreatedByPlayer(connectedPlayers.get(socket.id));
|
|
|
|
+ } catch (err) {
|
|
|
|
+ console.log('Error removing games : ' + err.message);
|
|
|
|
+ }
|
|
forceClientsReloadGames();
|
|
forceClientsReloadGames();
|
|
- if (reason === 'client namespace disconnect') {
|
|
|
|
- console.log("A player disconnected with id : " + socket.id);
|
|
|
|
|
|
+ if (
|
|
|
|
+ reason === 'client namespace disconnect' ||
|
|
|
|
+ reason === 'server namespace disconnect'
|
|
|
|
+ ) {
|
|
|
|
+ console.log('A player disconnected with id : ' + socket.id);
|
|
let playerName = connectedPlayers.get(socket.id);
|
|
let playerName = connectedPlayers.get(socket.id);
|
|
if (playerName) {
|
|
if (playerName) {
|
|
connectedPlayers.delete(socket.id);
|
|
connectedPlayers.delete(socket.id);
|
|
authorizedPlayers.get(playerName).setConnected(false);
|
|
authorizedPlayers.get(playerName).setConnected(false);
|
|
authorizedPlayers.get(playerName).setSocket(null);
|
|
authorizedPlayers.get(playerName).setSocket(null);
|
|
- console.log(playerName + " disconnected");
|
|
|
|
|
|
+ console.log(playerName + ' disconnected');
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- console.log("disconnected unexpectidly with reason : " + reason);
|
|
|
|
|
|
+ console.log('disconnected unexpectidly with reason : ' + reason);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
socket.on('connect', () => {
|
|
socket.on('connect', () => {
|
|
- console.log("Connected to server");
|
|
|
|
-
|
|
|
|
|
|
+ console.log('Connected to server');
|
|
});
|
|
});
|
|
socket.on('auth', async (playerName, callback) => {
|
|
socket.on('auth', async (playerName, callback) => {
|
|
console.log(' Received auth message, player name : ' + playerName);
|
|
console.log(' Received auth message, player name : ' + playerName);
|
|
@@ -66,31 +67,39 @@ function Server() {
|
|
let kickout = false;
|
|
let kickout = false;
|
|
if (!authorizedPlayersNames.has(playerName)) {
|
|
if (!authorizedPlayersNames.has(playerName)) {
|
|
response = {
|
|
response = {
|
|
- res: "ko",
|
|
|
|
- message: playerName + " Not found"
|
|
|
|
|
|
+ res: 'ko',
|
|
|
|
+ message: playerName + ' Not found'
|
|
};
|
|
};
|
|
kickout = true;
|
|
kickout = true;
|
|
}
|
|
}
|
|
// If the player is already connected and active
|
|
// If the player is already connected and active
|
|
- else if (authorizedPlayers.get(playerName).isConnected()
|
|
|
|
- && authorizedPlayers.get(playerName).getSocket().connected) {
|
|
|
|
|
|
+ else if (
|
|
|
|
+ authorizedPlayers.get(playerName).isConnected() &&
|
|
|
|
+ authorizedPlayers.get(playerName).getSocket().connected
|
|
|
|
+ ) {
|
|
response = {
|
|
response = {
|
|
- res: "ko",
|
|
|
|
- message: playerName + " already connected"
|
|
|
|
|
|
+ res: 'ko',
|
|
|
|
+ message: playerName + ' already connected'
|
|
};
|
|
};
|
|
kickout = true;
|
|
kickout = true;
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
// Player marked as connected but socket is down, clean it
|
|
// Player marked as connected but socket is down, clean it
|
|
if (authorizedPlayers.get(playerName).isConnected()) {
|
|
if (authorizedPlayers.get(playerName).isConnected()) {
|
|
- authorizedPlayers.get(playerName).getSocket().disconnect(true);
|
|
|
|
|
|
+ authorizedPlayers
|
|
|
|
+ .get(playerName)
|
|
|
|
+ .getSocket()
|
|
|
|
+ .disconnect(true);
|
|
} else {
|
|
} else {
|
|
// In case server did not detect disconnection and did not clean the pending created games
|
|
// In case server did not detect disconnection and did not clean the pending created games
|
|
- removeAllGamesCreatedByPlayer(playerName);
|
|
|
|
|
|
+ try {
|
|
|
|
+ removeAllGamesCreatedByPlayer(playerName);
|
|
|
|
+ } catch (err) {
|
|
|
|
+ console.log('Error removing games : ' + err.message);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
response = {
|
|
response = {
|
|
- res: "ok",
|
|
|
|
- message: playerName + " connected"
|
|
|
|
|
|
+ res: 'ok',
|
|
|
|
+ message: playerName + ' connected'
|
|
};
|
|
};
|
|
authorizedPlayers.get(playerName).setConnected(true);
|
|
authorizedPlayers.get(playerName).setConnected(true);
|
|
authorizedPlayers.get(playerName).setSocket(socket);
|
|
authorizedPlayers.get(playerName).setSocket(socket);
|
|
@@ -111,13 +120,12 @@ function Server() {
|
|
try {
|
|
try {
|
|
let games = await getJoinableGames(playerName);
|
|
let games = await getJoinableGames(playerName);
|
|
response = {
|
|
response = {
|
|
- res: "ok",
|
|
|
|
|
|
+ res: 'ok',
|
|
message: games
|
|
message: games
|
|
};
|
|
};
|
|
} catch (error) {
|
|
} catch (error) {
|
|
-
|
|
|
|
response = {
|
|
response = {
|
|
- res: "ko",
|
|
|
|
|
|
+ res: 'ko',
|
|
message: 'Error from server'
|
|
message: 'Error from server'
|
|
};
|
|
};
|
|
}
|
|
}
|
|
@@ -129,23 +137,32 @@ function Server() {
|
|
try {
|
|
try {
|
|
let id = await addGameDb(game);
|
|
let id = await addGameDb(game);
|
|
response = {
|
|
response = {
|
|
- res: "ok",
|
|
|
|
|
|
+ res: 'ok',
|
|
message: id
|
|
message: id
|
|
};
|
|
};
|
|
console.log('Force all clients to reload their games');
|
|
console.log('Force all clients to reload their games');
|
|
forceClientsReloadGames();
|
|
forceClientsReloadGames();
|
|
} catch (error) {
|
|
} catch (error) {
|
|
response = {
|
|
response = {
|
|
- res: "ko",
|
|
|
|
|
|
+ res: 'ko',
|
|
message: 'Error from server'
|
|
message: 'Error from server'
|
|
};
|
|
};
|
|
}
|
|
}
|
|
callback(response);
|
|
callback(response);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ socket.on('remove-created-game', async (username, callback) => {
|
|
|
|
+ try {
|
|
|
|
+ removeAllGamesCreatedByPlayer(username);
|
|
|
|
+ } catch (err) {
|
|
|
|
+ console.log('Error removing games : ' + err.message);
|
|
|
|
+ }
|
|
|
|
+ callback(true);
|
|
|
|
+ forceClientsReloadGames();
|
|
|
|
+ });
|
|
});
|
|
});
|
|
|
|
|
|
- let updatePlayersFromDb = async function () {
|
|
|
|
|
|
+ let updatePlayersFromDb = async function() {
|
|
try {
|
|
try {
|
|
let usernames = await mariadbConn.getUsernames();
|
|
let usernames = await mariadbConn.getUsernames();
|
|
authorizedPlayersNames = new Set(usernames);
|
|
authorizedPlayersNames = new Set(usernames);
|
|
@@ -163,11 +180,10 @@ function Server() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- let getCurrentListAuthorizedPlayers = function () {
|
|
|
|
|
|
+ let getCurrentListAuthorizedPlayers = function() {
|
|
return [...authorizedPlayersNames];
|
|
return [...authorizedPlayersNames];
|
|
};
|
|
};
|
|
- let addPlayerDb = async function (username) {
|
|
|
|
-
|
|
|
|
|
|
+ let addPlayerDb = async function(username) {
|
|
console.log('add player in db : ' + username);
|
|
console.log('add player in db : ' + username);
|
|
try {
|
|
try {
|
|
let response = await mariadbConn.addPlayer(username);
|
|
let response = await mariadbConn.addPlayer(username);
|
|
@@ -178,8 +194,7 @@ function Server() {
|
|
return 'KO ' + err.message;
|
|
return 'KO ' + err.message;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- let removePlayerDb = async function (username) {
|
|
|
|
-
|
|
|
|
|
|
+ let removePlayerDb = async function(username) {
|
|
console.log('remove player in db : ' + username);
|
|
console.log('remove player in db : ' + username);
|
|
try {
|
|
try {
|
|
let response = await mariadbConn.removePlayer(username);
|
|
let response = await mariadbConn.removePlayer(username);
|
|
@@ -191,8 +206,7 @@ function Server() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- let addGameDb = async function (game) {
|
|
|
|
-
|
|
|
|
|
|
+ let addGameDb = async function(game) {
|
|
try {
|
|
try {
|
|
let response = await mariadbConn.addNewGame(game);
|
|
let response = await mariadbConn.addNewGame(game);
|
|
return response;
|
|
return response;
|
|
@@ -202,7 +216,7 @@ function Server() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- let removeGameDb = async function (method, ...args) {
|
|
|
|
|
|
+ let removeGameDb = async function(method, ...args) {
|
|
try {
|
|
try {
|
|
let response;
|
|
let response;
|
|
switch (method) {
|
|
switch (method) {
|
|
@@ -234,8 +248,7 @@ function Server() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- let getJoinableGames = async function (username) {
|
|
|
|
-
|
|
|
|
|
|
+ let getJoinableGames = async function(username) {
|
|
try {
|
|
try {
|
|
let games = await mariadbConn.getJoinableGamesForPlayer(username);
|
|
let games = await mariadbConn.getJoinableGamesForPlayer(username);
|
|
return games;
|
|
return games;
|
|
@@ -246,17 +259,12 @@ function Server() {
|
|
};
|
|
};
|
|
|
|
|
|
let removeAllGamesCreatedByPlayer = async function(playerName) {
|
|
let removeAllGamesCreatedByPlayer = async function(playerName) {
|
|
- try {
|
|
|
|
- let result = await mariadbConn.removeCreatedGamesByPlayer1(playerName);
|
|
|
|
- return result;
|
|
|
|
- } catch (err) {
|
|
|
|
- console.log('KO : not able to remove created games : ' + err);
|
|
|
|
- throw err;
|
|
|
|
- }
|
|
|
|
|
|
+ let result = await mariadbConn.removeCreatedGamesByPlayer1(playerName);
|
|
|
|
+ return result;
|
|
};
|
|
};
|
|
|
|
|
|
- let forceClientsReloadGames = function () {
|
|
|
|
- server.emit('reload-games-list');
|
|
|
|
|
|
+ let forceClientsReloadGames = function() {
|
|
|
|
+ server.emit('reload-games-list');
|
|
};
|
|
};
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -268,7 +276,6 @@ function Server() {
|
|
removeGameDb,
|
|
removeGameDb,
|
|
getJoinableGames
|
|
getJoinableGames
|
|
};
|
|
};
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
let server = new Server();
|
|
let server = new Server();
|