|
@@ -18,8 +18,6 @@ function Server() {
|
|
let authorizedPlayersNames = new Set();
|
|
let authorizedPlayersNames = new Set();
|
|
|
|
|
|
server.on('connection', function(socket) {
|
|
server.on('connection', function(socket) {
|
|
- console.log('A player connected with id : ' + socket.id);
|
|
|
|
-
|
|
|
|
socket.on('disconnect', reason => {
|
|
socket.on('disconnect', reason => {
|
|
console.log('A player disconnected, reason : ' + reason);
|
|
console.log('A player disconnected, reason : ' + reason);
|
|
if (
|
|
if (
|
|
@@ -31,10 +29,6 @@ function Server() {
|
|
if (username) {
|
|
if (username) {
|
|
let player = authorizedPlayers.get(username);
|
|
let player = authorizedPlayers.get(username);
|
|
|
|
|
|
- // First, tell game manager a player left, is he was playing
|
|
|
|
- if (player.isPlayingGameId >= 0) {
|
|
|
|
- gamesManager.playerLeft(player, true);
|
|
|
|
- }
|
|
|
|
// If player had created a game, remove it
|
|
// If player had created a game, remove it
|
|
removeAllGamesCreatedByPlayer(username)
|
|
removeAllGamesCreatedByPlayer(username)
|
|
.then(res => {
|
|
.then(res => {
|
|
@@ -44,6 +38,10 @@ function Server() {
|
|
}
|
|
}
|
|
})
|
|
})
|
|
.catch(err => console.log('Error removing game :>> ', err.message));
|
|
.catch(err => console.log('Error removing game :>> ', err.message));
|
|
|
|
+ // Tell game manager a player left, if he was playing
|
|
|
|
+ if (player.isPlayingGameId >= 0) {
|
|
|
|
+ gamesManager.playerLeft(player, true);
|
|
|
|
+ }
|
|
connectedPlayers.delete(socket.id);
|
|
connectedPlayers.delete(socket.id);
|
|
player.setConnected(false);
|
|
player.setConnected(false);
|
|
player.setSocket(null);
|
|
player.setSocket(null);
|
|
@@ -51,9 +49,7 @@ function Server() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
- socket.on('connect', () => {
|
|
|
|
- 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);
|
|
await updatePlayersFromDb();
|
|
await updatePlayersFromDb();
|
|
@@ -100,7 +96,7 @@ function Server() {
|
|
authorizedPlayers.get(playerName).setSocket(socket);
|
|
authorizedPlayers.get(playerName).setSocket(socket);
|
|
connectedPlayers.set(socket.id, playerName);
|
|
connectedPlayers.set(socket.id, playerName);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ console.log('auth called, result : ', response.message);
|
|
callback(response);
|
|
callback(response);
|
|
if (kickout === true) {
|
|
if (kickout === true) {
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
@@ -110,7 +106,6 @@ function Server() {
|
|
});
|
|
});
|
|
|
|
|
|
socket.on('games-list', async (playerName, callback) => {
|
|
socket.on('games-list', async (playerName, callback) => {
|
|
- console.log(' Received games-list message, player name : ' + playerName);
|
|
|
|
let response = {};
|
|
let response = {};
|
|
try {
|
|
try {
|
|
let games = await getJoinableGames(playerName);
|
|
let games = await getJoinableGames(playerName);
|
|
@@ -138,22 +133,24 @@ function Server() {
|
|
gamesManager.addPlayerInGame(
|
|
gamesManager.addPlayerInGame(
|
|
authorizedPlayers.get(connectedPlayers.get(socket.id)),
|
|
authorizedPlayers.get(connectedPlayers.get(socket.id)),
|
|
id,
|
|
id,
|
|
- false
|
|
|
|
|
|
+ false,
|
|
|
|
+ true,
|
|
|
|
+ { deck: game.deck, advRules: game.advRules }
|
|
);
|
|
);
|
|
- console.log('Force all clients to reload their games');
|
|
|
|
forceClientsReloadGames();
|
|
forceClientsReloadGames();
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- console.log('error in create-game:>> ', error);
|
|
|
|
|
|
+ console.log('In create-game catch error :>> ', error);
|
|
response = {
|
|
response = {
|
|
res: 'ko',
|
|
res: 'ko',
|
|
message: 'Error from server'
|
|
message: 'Error from server'
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
+ console.log('create-game called, result : ', response.message);
|
|
callback(response);
|
|
callback(response);
|
|
});
|
|
});
|
|
|
|
|
|
socket.on('join-game', async (gameDetails, callback) => {
|
|
socket.on('join-game', async (gameDetails, callback) => {
|
|
- let result = gamesManager.addPlayerInGame(
|
|
|
|
|
|
+ let result = await gamesManager.addPlayerInGame(
|
|
authorizedPlayers.get(connectedPlayers.get(socket.id)),
|
|
authorizedPlayers.get(connectedPlayers.get(socket.id)),
|
|
gameDetails.id,
|
|
gameDetails.id,
|
|
gameDetails.joinCreatedGame
|
|
gameDetails.joinCreatedGame
|
|
@@ -169,17 +166,19 @@ function Server() {
|
|
callback(response);
|
|
callback(response);
|
|
});
|
|
});
|
|
|
|
|
|
- socket.on('leave-game', async (username, callback) => {
|
|
|
|
|
|
+ socket.on('leave-game', (username, callback) => {
|
|
// Remove player from game he is playing
|
|
// Remove player from game he is playing
|
|
|
|
+ removeAllGamesCreatedByPlayer(username)
|
|
|
|
+ .then(res => {
|
|
|
|
+ if (res.affectedRows > 0) {
|
|
|
|
+ forceClientsReloadGames();
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ .catch(err => {
|
|
|
|
+ console.log('Error removing games : ' + err.message);
|
|
|
|
+ });
|
|
|
|
+
|
|
gamesManager.playerLeft(authorizedPlayers.get(username), false);
|
|
gamesManager.playerLeft(authorizedPlayers.get(username), false);
|
|
- try {
|
|
|
|
- let res = removeAllGamesCreatedByPlayer(username);
|
|
|
|
- if (res.affectedRows > 0) {
|
|
|
|
- forceClientsReloadGames();
|
|
|
|
- }
|
|
|
|
- } catch (err) {
|
|
|
|
- console.log('Error removing games : ' + err.message);
|
|
|
|
- }
|
|
|
|
callback(true);
|
|
callback(true);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
@@ -188,10 +187,8 @@ function Server() {
|
|
try {
|
|
try {
|
|
let usernames = await mariadbConn.getUsernames();
|
|
let usernames = await mariadbConn.getUsernames();
|
|
authorizedPlayersNames = new Set(usernames);
|
|
authorizedPlayersNames = new Set(usernames);
|
|
- console.log('authorizedPlayersNames update : ', authorizedPlayersNames);
|
|
|
|
authorizedPlayersNames.forEach(name => {
|
|
authorizedPlayersNames.forEach(name => {
|
|
if (!authorizedPlayers.has(name)) {
|
|
if (!authorizedPlayers.has(name)) {
|
|
- console.log('New username authorized :', name);
|
|
|
|
authorizedPlayers.set(name, new PlayerId(name));
|
|
authorizedPlayers.set(name, new PlayerId(name));
|
|
}
|
|
}
|
|
});
|
|
});
|