main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. import PlayerId from './players/player-id';
  3. import DuelController from './game-server/duel-controller';
  4. console.log('Starting 12 heroes server');
  5. const io = require('socket.io');
  6. const server = io.listen(4000);
  7. let players = [];
  8. const authorizedPlayersNames = ["jojo", "mart", "lily"];
  9. let authorizedPlayers = new Map();
  10. let connectedPlayers = new Map();
  11. authorizedPlayersNames.forEach(name => {
  12. authorizedPlayers.set(name, new PlayerId(name));
  13. });
  14. let addNewPlayer = function (playerSocket, playerName) {
  15. console.log('players length : ' + players.length + ' : ' + players);
  16. if (players.length < 2) {
  17. let newPlayer = new PlayerId(playerSocket, playerName);
  18. console.log('push player : ' + newPlayer);
  19. players.push(newPlayer);
  20. }
  21. if (players.length === 2) {
  22. let duelController = new DuelController(players[0], players[1]);
  23. }
  24. };
  25. server.on('connection', function (socket) {
  26. console.log("A player connected with id : " + socket.id);
  27. socket.on('disconnect', (reason) => {
  28. if (reason === 'client namespace disconnect') {
  29. console.log("A player disconnected with id : " + socket.id);
  30. let playerName = connectedPlayers.get(socket.id);
  31. if (playerName) {
  32. connectedPlayers.delete(socket.id);
  33. authorizedPlayers.get(playerName).setConnected(false);
  34. authorizedPlayers.get(playerName).setSocket(null);
  35. console.log(playerName + " disconnected");
  36. }
  37. } else {
  38. console.log("disconnected unexpectidly with reason : " + reason);
  39. }
  40. });
  41. socket.on('connect', () => {
  42. console.log("Connected to server");
  43. });
  44. socket.on('auth', (playerName, callback) => {
  45. console.log(' Received auth message, player name : ' + playerName);
  46. let response = {};
  47. let kickout = false;
  48. if (!authorizedPlayersNames.includes(playerName)) {
  49. response = {
  50. res: "ko",
  51. message: playerName + " Not found"
  52. };
  53. kickout = true;
  54. }
  55. // If the player is already connected and active
  56. else if (authorizedPlayers.get(playerName).isConnected()
  57. && authorizedPlayers.get(playerName).getSocket().connected) {
  58. response = {
  59. res: "ko",
  60. message: playerName + " already connected"
  61. };
  62. kickout = true;
  63. }
  64. else {
  65. // Player marked as connected but socket is down
  66. if (authorizedPlayers.get(playerName).isConnected()) {
  67. authorizedPlayers.get(playerName).getSocket().disconnect(true);
  68. }
  69. response = {
  70. res: "ok",
  71. message: playerName + " connected"
  72. };
  73. authorizedPlayers.get(playerName).setConnected(true);
  74. authorizedPlayers.get(playerName).setSocket(socket);
  75. connectedPlayers.set(socket.id, playerName);
  76. }
  77. callback(response);
  78. if (kickout === true) {
  79. setTimeout(() => {
  80. socket.disconnect(true);
  81. }, 600);
  82. }
  83. });
  84. socket.on('games-list', (playerName, callback) => {
  85. console.log(' Received games-list message, player name : ' + playerName);
  86. let games= {
  87. game1: {p1:"jojo", p2:'lily',mode:'faction', rules:[]},
  88. game2: {p1:"mart", p2:'',mode:'tournament', rules:['popularity', 'discard']},
  89. game3: {p1:"jojo", p2:'mart',mode:'draft', rules:['discard']}
  90. };
  91. let response = {
  92. res: "ok",
  93. message: games
  94. };
  95. callback(response);
  96. });
  97. });