server.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. import PlayerId from './players/player-id';
  3. import DuelController from './game-server/duel-controller';
  4. import MariadbConnector from './db/mariadb-connector';
  5. import ServerToolListner from './tools/server-tool-listener';
  6. function Server() {
  7. console.log('Starting 12 heroes server');
  8. const io = require('socket.io');
  9. const server = io.listen(2610);
  10. let mariadbConn = new MariadbConnector();
  11. let authorizedPlayers = new Map();
  12. let connectedPlayers = new Map();
  13. let authorizedPlayersNames = new Set();
  14. // let players = [];
  15. // let addNewPlayer = function (playerSocket, playerName) {
  16. // console.log('players length : ' + players.length + ' : ' + players);
  17. // if (players.length < 2) {
  18. // let newPlayer = new PlayerId(playerSocket, playerName);
  19. // console.log('push player : ' + newPlayer);
  20. // players.push(newPlayer);
  21. // }
  22. // if (players.length === 2) {
  23. // let duelController = new DuelController(players[0], players[1]);
  24. // }
  25. // };
  26. server.on('connection', function (socket) {
  27. console.log("A player connected with id : " + socket.id);
  28. socket.on('disconnect', (reason) => {
  29. if (reason === 'client namespace disconnect') {
  30. console.log("A player disconnected with id : " + socket.id);
  31. let playerName = connectedPlayers.get(socket.id);
  32. if (playerName) {
  33. connectedPlayers.delete(socket.id);
  34. authorizedPlayers.get(playerName).setConnected(false);
  35. authorizedPlayers.get(playerName).setSocket(null);
  36. console.log(playerName + " disconnected");
  37. }
  38. } else {
  39. console.log("disconnected unexpectidly with reason : " + reason);
  40. }
  41. });
  42. socket.on('connect', () => {
  43. console.log("Connected to server");
  44. });
  45. socket.on('auth', async (playerName, callback) => {
  46. console.log(' Received auth message, player name : ' + playerName);
  47. await updatePlayersFromDb();
  48. let response = {};
  49. let kickout = false;
  50. if (!authorizedPlayersNames.has(playerName)) {
  51. response = {
  52. res: "ko",
  53. message: playerName + " Not found"
  54. };
  55. kickout = true;
  56. }
  57. // If the player is already connected and active
  58. else if (authorizedPlayers.get(playerName).isConnected()
  59. && authorizedPlayers.get(playerName).getSocket().connected) {
  60. response = {
  61. res: "ko",
  62. message: playerName + " already connected"
  63. };
  64. kickout = true;
  65. }
  66. else {
  67. // Player marked as connected but socket is down
  68. if (authorizedPlayers.get(playerName).isConnected()) {
  69. authorizedPlayers.get(playerName).getSocket().disconnect(true);
  70. }
  71. response = {
  72. res: "ok",
  73. message: playerName + " connected"
  74. };
  75. authorizedPlayers.get(playerName).setConnected(true);
  76. authorizedPlayers.get(playerName).setSocket(socket);
  77. connectedPlayers.set(socket.id, playerName);
  78. }
  79. callback(response);
  80. if (kickout === true) {
  81. setTimeout(() => {
  82. socket.disconnect(true);
  83. }, 600);
  84. }
  85. });
  86. socket.on('games-list', async (playerName, callback) => {
  87. console.log(' Received games-list message, player name : ' + playerName);
  88. let response = {};
  89. try {
  90. let games = await getNonFinishedGames(playerName);
  91. response = {
  92. res: "ok",
  93. message: games
  94. };
  95. } catch (error) {
  96. response = {
  97. res: "ko",
  98. message: 'Error from server'
  99. };
  100. }
  101. callback(response);
  102. });
  103. });
  104. let updatePlayersFromDb = async function () {
  105. try {
  106. let usernames = await mariadbConn.getUsernames();
  107. authorizedPlayersNames = new Set(usernames);
  108. console.log('authorizedPlayersNames update : ', authorizedPlayersNames);
  109. authorizedPlayersNames.forEach(name => {
  110. if (!authorizedPlayers.has(name)) {
  111. console.log('New username authorized :', name);
  112. authorizedPlayers.set(name, new PlayerId(name));
  113. }
  114. });
  115. return 'ok';
  116. } catch (error) {
  117. console.log('Error getting usernames from DB : ', error);
  118. return error.Error;
  119. }
  120. };
  121. let getCurrentListAuthorizedPlayers = function () {
  122. return [...authorizedPlayersNames];
  123. };
  124. let addPlayerDb = async function (username) {
  125. console.log('add player in db : ' + username);
  126. try {
  127. let response = await mariadbConn.addPlayer(username);
  128. authorizedPlayersNames.add(username);
  129. authorizedPlayers.set(username, new PlayerId(username));
  130. return 'OK ' + JSON.stringify(response);
  131. } catch (err) {
  132. return 'KO ' + err.message;
  133. }
  134. };
  135. let removePlayerDb = async function (username) {
  136. console.log('remove player in db : ' + username);
  137. try {
  138. let response = await mariadbConn.removePlayer(username);
  139. authorizedPlayersNames.delete(username);
  140. authorizedPlayers.delete(username);
  141. return 'OK ' + JSON.stringify(response);
  142. } catch (err) {
  143. return 'KO ' + err.message;
  144. }
  145. };
  146. let addGameDb = async function (game) {
  147. try {
  148. let response = await mariadbConn.addNewGame(game);
  149. return 'OK ' + JSON.stringify(response);
  150. } catch (err) {
  151. return 'KO ' + err.message;
  152. }
  153. };
  154. let removeGameDb = async function (method, ...args) {
  155. try {
  156. let response;
  157. switch (method) {
  158. case 'byId':
  159. response = await mariadbConn.removeGameById(args[0]);
  160. break;
  161. case 'byPlayer1':
  162. response = await mariadbConn.removeGamesByPlayer1(args[0]);
  163. break;
  164. case 'byPlayerAny':
  165. response = await mariadbConn.removeGamesByPlayerAny(args[0]);
  166. break;
  167. case 'byStatus':
  168. response = await mariadbConn.removeGamesByStatus(args[0]);
  169. break;
  170. case 'byDays':
  171. response = await mariadbConn.removeGamesByDays(args[0]);
  172. break;
  173. case 'finishedByDays':
  174. response = await mariadbConn.removeFinishedGamesByDays(args[0]);
  175. break;
  176. default:
  177. return 'KO : Unknown method to remove game from DB : ' + method;
  178. }
  179. return 'OK ' + JSON.stringify(response);
  180. } catch (err) {
  181. return 'KO ' + err.message;
  182. }
  183. };
  184. let getNonFinishedGames = async function (username) {
  185. try {
  186. let games = await mariadbConn.getNonFinishedGamesForPlayer(username);
  187. return games;
  188. } catch (err) {
  189. console.log('KO : not able to get games : ' + err);
  190. throw err;
  191. }
  192. };
  193. return {
  194. updatePlayersFromDb,
  195. getCurrentListAuthorizedPlayers,
  196. addPlayerDb,
  197. removePlayerDb,
  198. addGameDb,
  199. removeGameDb,
  200. getNonFinishedGames
  201. };
  202. }
  203. let server = new Server();
  204. let serverToolListner = new ServerToolListner(server);
  205. serverToolListner.listen(1664);