main.js 3.2 KB

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