|
@@ -2,11 +2,18 @@
|
|
|
import PlayerId from './players/player-id';
|
|
|
import DuelController from './game-server/duel-controller';
|
|
|
|
|
|
-const io = require('socket.io'); //(http)
|
|
|
+const io = require('socket.io');
|
|
|
const server = io.listen(4000);
|
|
|
|
|
|
let players = [];
|
|
|
|
|
|
+const authorizedPlayersNames = [ "jojo", "mart", "lily" ];
|
|
|
+let authorizedPlayers = new Map();
|
|
|
+
|
|
|
+authorizedPlayersNames.forEach(name => {
|
|
|
+ authorizedPlayers.set(name,new PlayerId(name));
|
|
|
+});
|
|
|
+
|
|
|
let addNewPlayer = function (playerSocket, playerName) {
|
|
|
console.log('players length : ' + players.length + ' : ' + players);
|
|
|
if (players.length < 2) {
|
|
@@ -17,7 +24,7 @@ let addNewPlayer = function (playerSocket, playerName) {
|
|
|
if (players.length === 2) {
|
|
|
let duelController = new DuelController(players[0], players[1]);
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
server.on('connection', function (socket) {
|
|
|
console.log("A player connected with id : " + socket.id);
|
|
@@ -25,9 +32,31 @@ server.on('connection', function (socket) {
|
|
|
console.log("A player disconnected with id : " + socket.id);
|
|
|
});
|
|
|
|
|
|
- socket.on('auth', (playerName) => {
|
|
|
+ socket.on('auth', (playerName, callback) => {
|
|
|
console.log(' Received auth message, player name : ' + playerName);
|
|
|
- addNewPlayer(socket, playerName);
|
|
|
+ let response = {};
|
|
|
+ if (! authorizedPlayersNames.includes(playerName)) {
|
|
|
+ response = {
|
|
|
+ res:"ko",
|
|
|
+ message:playerName + " Not found"
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else if (authorizedPlayers.get(playerName).isConnected()) {
|
|
|
+ response = {
|
|
|
+ res:"ko",
|
|
|
+ message:playerName + " already connected"
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ response = {
|
|
|
+ res:"ok",
|
|
|
+ message:playerName + " connected"
|
|
|
+ };
|
|
|
+ authorizedPlayers.get(playerName).setConnected(true);
|
|
|
+ authorizedPlayers.get(playerName).setSocket(socket);
|
|
|
+
|
|
|
+ }
|
|
|
+ callback(response);
|
|
|
});
|
|
|
|
|
|
-})
|
|
|
+});
|