main.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. import PlayerId from './players/player-id';
  3. import DuelController from './game-server/duel-controller';
  4. const io = require('socket.io');
  5. const server = io.listen(4000);
  6. let players = [];
  7. const authorizedPlayersNames = [ "jojo", "mart", "lily" ];
  8. let authorizedPlayers = new Map();
  9. authorizedPlayersNames.forEach(name => {
  10. authorizedPlayers.set(name,new PlayerId(name));
  11. });
  12. let addNewPlayer = function (playerSocket, playerName) {
  13. console.log('players length : ' + players.length + ' : ' + players);
  14. if (players.length < 2) {
  15. let newPlayer = new PlayerId(playerSocket,playerName);
  16. console.log('push player : ' + newPlayer);
  17. players.push(newPlayer);
  18. }
  19. if (players.length === 2) {
  20. let duelController = new DuelController(players[0], players[1]);
  21. }
  22. };
  23. server.on('connection', function (socket) {
  24. console.log("A player connected with id : " + socket.id);
  25. socket.on ('disconnect', () => {
  26. console.log("A player disconnected with id : " + socket.id);
  27. });
  28. socket.on('auth', (playerName, callback) => {
  29. console.log(' Received auth message, player name : ' + playerName);
  30. let response = {};
  31. if (! authorizedPlayersNames.includes(playerName)) {
  32. response = {
  33. res:"ko",
  34. message:playerName + " Not found"
  35. };
  36. }
  37. else if (authorizedPlayers.get(playerName).isConnected()) {
  38. response = {
  39. res:"ko",
  40. message:playerName + " already connected"
  41. };
  42. }
  43. else {
  44. response = {
  45. res:"ok",
  46. message:playerName + " connected"
  47. };
  48. authorizedPlayers.get(playerName).setConnected(true);
  49. authorizedPlayers.get(playerName).setSocket(socket);
  50. }
  51. callback(response);
  52. });
  53. });