1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 'use strict';
- import PlayerId from './players/player-id';
- import DuelController from './game-server/duel-controller';
- 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) {
- let newPlayer = new PlayerId(playerSocket,playerName);
- console.log('push player : ' + newPlayer);
- players.push(newPlayer);
- }
- 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);
- socket.on ('disconnect', () => {
- console.log("A player disconnected with id : " + socket.id);
- });
- socket.on('auth', (playerName, callback) => {
- console.log(' Received auth message, player name : ' + 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);
- });
- });
|