server.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 getJoinableGames(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. socket.on('create-game', async (game, callback) => {
  104. let response = {};
  105. try {
  106. let games = await addGameDb(game);
  107. response = {
  108. res: "ok",
  109. message: games
  110. };
  111. } catch (error) {
  112. response = {
  113. res: "ko",
  114. message: 'Error from server'
  115. };
  116. }
  117. callback(response);
  118. });
  119. });
  120. let updatePlayersFromDb = async function () {
  121. try {
  122. let usernames = await mariadbConn.getUsernames();
  123. authorizedPlayersNames = new Set(usernames);
  124. console.log('authorizedPlayersNames update : ', authorizedPlayersNames);
  125. authorizedPlayersNames.forEach(name => {
  126. if (!authorizedPlayers.has(name)) {
  127. console.log('New username authorized :', name);
  128. authorizedPlayers.set(name, new PlayerId(name));
  129. }
  130. });
  131. return 'ok';
  132. } catch (error) {
  133. console.log('Error getting usernames from DB : ', error);
  134. return error.Error;
  135. }
  136. };
  137. let getCurrentListAuthorizedPlayers = function () {
  138. return [...authorizedPlayersNames];
  139. };
  140. let addPlayerDb = async function (username) {
  141. console.log('add player in db : ' + username);
  142. try {
  143. let response = await mariadbConn.addPlayer(username);
  144. authorizedPlayersNames.add(username);
  145. authorizedPlayers.set(username, new PlayerId(username));
  146. return 'OK ' + JSON.stringify(response);
  147. } catch (err) {
  148. return 'KO ' + err.message;
  149. }
  150. };
  151. let removePlayerDb = async function (username) {
  152. console.log('remove player in db : ' + username);
  153. try {
  154. let response = await mariadbConn.removePlayer(username);
  155. authorizedPlayersNames.delete(username);
  156. authorizedPlayers.delete(username);
  157. return 'OK ' + JSON.stringify(response);
  158. } catch (err) {
  159. return 'KO ' + err.message;
  160. }
  161. };
  162. let addGameDb = async function (game) {
  163. try {
  164. let response = await mariadbConn.addNewGame(game);
  165. return 'OK ' + JSON.stringify(response);
  166. } catch (err) {
  167. return 'KO ' + err.message;
  168. }
  169. };
  170. let removeGameDb = async function (method, ...args) {
  171. try {
  172. let response;
  173. switch (method) {
  174. case 'byId':
  175. response = await mariadbConn.removeGameById(args[0]);
  176. break;
  177. case 'byPlayer1':
  178. response = await mariadbConn.removeGamesByPlayer1(args[0]);
  179. break;
  180. case 'byPlayerAny':
  181. response = await mariadbConn.removeGamesByPlayerAny(args[0]);
  182. break;
  183. case 'byStatus':
  184. response = await mariadbConn.removeGamesByStatus(args[0]);
  185. break;
  186. case 'byDays':
  187. response = await mariadbConn.removeGamesByDays(args[0]);
  188. break;
  189. case 'finishedByDays':
  190. response = await mariadbConn.removeFinishedGamesByDays(args[0]);
  191. break;
  192. default:
  193. return 'KO : Unknown method to remove game from DB : ' + method;
  194. }
  195. return 'OK ' + JSON.stringify(response);
  196. } catch (err) {
  197. return 'KO ' + err.message;
  198. }
  199. };
  200. let getJoinableGames = async function (username) {
  201. try {
  202. let games = await mariadbConn.getJoinableGamesForPlayer(username);
  203. return games;
  204. } catch (err) {
  205. console.log('KO : not able to get games : ' + err);
  206. throw err;
  207. }
  208. };
  209. return {
  210. updatePlayersFromDb,
  211. getCurrentListAuthorizedPlayers,
  212. addPlayerDb,
  213. removePlayerDb,
  214. addGameDb,
  215. removeGameDb,
  216. getJoinableGames
  217. };
  218. }
  219. let server = new Server();
  220. let serverToolListner = new ServerToolListner(server);
  221. serverToolListner.listen(1664);