Переглянути джерело

clean up & server authentication dev

Jojo 5 роки тому
батько
коміт
ca18f2f4d3

+ 34 - 5
server/main.js

@@ -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);
   });
 
-})
+});

+ 12 - 2
server/players/player-id.js

@@ -1,16 +1,26 @@
 'use strict';
 
 export default class PlayerId {
-  constructor(playerSocket, playerName = '', playerColor = 'unk') {
-    this.playerSocket = playerSocket;
+  constructor(playerName = '', playerColor = 'unk') {
+    this.playerSocket = null;
     this.playerName = playerName;
     this.playerColor = playerColor;
+    this.connected=false;
   }
 
+  setSocket(socket) {
+    this.playerSocket = socket;
+  }
   setPlayerName(playerName) {
     this.playerName = playerName;
   }
   setPlayerColor(playerColor) {
     this.playerColor = playerColor;
   }
+  setConnected(connected) {
+    this.isConnected = connected;
+  }
+  isConnected() {
+    return this.isConnected;
+  }
 }

+ 15 - 4
src/common/socket-service.js

@@ -13,9 +13,9 @@ const SERVER_URL = process.env.SERVER_URL || 'http://localhost';
 const SERVER_PORT = process.env.SERVER_PORT || 4000;
 
 export default class SocketService {
-  constructor(namespace) {
+  constructor() {
 
-    this.ioClient = io.connect(SERVER_URL + ":" + SERVER_PORT + '/');
+    //this.ioClient = io.connect(SERVER_URL + ":" + SERVER_PORT + '/');
   }
 
   connect(serverUrl, serverPort) {
@@ -27,9 +27,20 @@ export default class SocketService {
       console.log("Connection with server lost");
 
     });
+    this.ioClient.on('connect', () => {
+      console.log("Connected to server");
+
+    });
   }
   auth(name) {
-    console.log("Authenticate to server player name : " + name);
-    this.ioClient.emit('auth', name);
+    return new Promise(function (resolve, reject) {
+      this.ioClient.emit('auth', name, function (response) {
+        if (response.res === "ok") {
+          resolve(response.message);
+        } else {
+          reject(response.message);
+        }
+      });
+    });
   }
 }

+ 0 - 16
src/game/control/behaviors/deck-building-steps.js

@@ -1,16 +0,0 @@
-'use strict';
-
-
-export const DeckBuildingSteps = self => ({
-  selectFactionDeck: (renderer,heroFactory) => {
-    self.player.faction = renderer.getInput(self.player.name + ' to choose faction');
-    self.player.setHeroesDeck(heroFactory.getFactionRandom(self.player.faction));
-  },
-  selectDraftHeroes: (heroesDraftBurst) => {
-    console.log('Choose 2 heroes among : ', heroesDraftBurst);
-  },
-  selectTournamentDeck: (allHeroes) => {
-    console.log('Choose 12 heroes among : ', allHeroes);
-  }
-
-});

+ 0 - 10
src/game/control/player-controller.js

@@ -1,10 +0,0 @@
-'use-strict';
-import {DeckBuildingSteps} from './behaviors/deck-building-steps';
-
-export const PlayerControllerLocal = function(player) {
-  const self = {
-    player
-  };
-
-  return Object.assign(self, DeckBuildingSteps(self));
-};

+ 4 - 3
src/game/views/faction-deck-building-scene.js

@@ -21,10 +21,11 @@ export default class FactionDeckBuildingScene extends DeckBuildingScene {
 
     setTimeout(() => {
       let factionOptions = Object.values(Faction).filter(faction => faction !== Faction.NONE);
-      let pBlueFaction = Utils.promptSelectAmongOptions("Select Player Blue Faction", factionOptions);
-
+      //let pBlueFaction = Utils.promptSelectAmongOptions("Select Player Blue Faction", factionOptions);
+      let pBlueFaction = "meca";
       factionOptions = factionOptions.filter(faction => faction !== pBlueFaction);
-      let pRedFaction = Utils.promptSelectAmongOptions("Select Player Red Faction", factionOptions);
+      //let pRedFaction = Utils.promptSelectAmongOptions("Select Player Red Faction", factionOptions);
+      let pRedFaction = "orcs";
       this.gameEventListener.onFactionsSelected(pBlueFaction, pRedFaction );
     }, 100);
   }

+ 3 - 3
src/menu/control/menu-controller.js

@@ -14,9 +14,9 @@ export default class MenuController {
 
     this.mainMenuScene = new MainMenuScene(this.menuEventListener());
 
-    //this.socketService = new SocketService();
-    //this.socketService.connect('http://localhost', 4000);
-    //this.socketService.auth('Jojo');
+    this.socketService = new SocketService();
+    this.socketService.connect('http://localhost', 4000);
+    this.socketService.auth('jojo');
   }
 
   displayMainMenu() {

+ 7 - 0
src/menu/control/server-login.js

@@ -0,0 +1,7 @@
+'use strict';
+
+export default class ServerLogin {
+  constructor() {
+
+  }
+}

+ 3 - 2
src/menu/views/main-menu-scene.js

@@ -45,8 +45,9 @@ export default class MainMenuScene extends Phaser.Scene {
       let pBlueName = prompt("Player blue name : ");
       let pRedName = prompt("Player red name : ");
 
-      let gameDeckModeOptions = Object.values(GameDeckMode);
-      let gameDeckMode = Utils.promptSelectAmongOptions("Select Deck Mode", gameDeckModeOptions);
+      //let gameDeckModeOptions = Object.values(GameDeckMode);
+      //let gameDeckMode = Utils.promptSelectAmongOptions("Select Deck Mode", gameDeckModeOptions);
+      let gameDeckMode = "factions";
       let advancedRules = [];
       console.log("Calling listener");
       this.menuEventListener.onNewPnpGame(pBlueName, pRedName, gameDeckMode, advancedRules);