server.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. 'use strict';
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  5. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  6. var _playerId = _interopRequireDefault(require("./players/player-id"));
  7. var _gamesManager = _interopRequireDefault(require("./game-server/games-manager"));
  8. var _mariadbConnector = _interopRequireDefault(require("./db/mariadb-connector"));
  9. var _serverToolListener = _interopRequireDefault(require("./tools/server-tool-listener"));
  10. function Server() {
  11. console.log('Starting 12 heroes server');
  12. var io = require('socket.io');
  13. var server = io.listen(2610);
  14. var mariadbConn = new _mariadbConnector.default();
  15. var gamesManager = new _gamesManager.default(server, mariadbConn);
  16. var authorizedPlayers = new Map();
  17. var connectedPlayers = new Map();
  18. var authorizedPlayersNames = new Set();
  19. server.on('connection', function (socket) {
  20. console.log('A player connected with id : ' + socket.id);
  21. socket.on('disconnect', function (reason) {
  22. console.log('A player disconnected, reason : ' + reason);
  23. if (reason === 'client namespace disconnect' || reason === 'server namespace disconnect' || reason === 'transport close') {
  24. var username = connectedPlayers.get(socket.id);
  25. if (username) {
  26. var player = authorizedPlayers.get(username); // If player had created a game, remove it
  27. removeAllGamesCreatedByPlayer(username).then(function (res) {
  28. if (res.affectedRows > 0) {
  29. // Tell other clients to reload their games if there were
  30. forceClientsReloadGames();
  31. }
  32. }).catch(function (err) {
  33. return console.log('Error removing game :>> ', err.message);
  34. }); // Tell game manager a player left, if he was playing
  35. if (player.isPlayingGameId >= 0) {
  36. gamesManager.playerLeft(player, true);
  37. }
  38. connectedPlayers.delete(socket.id);
  39. player.setConnected(false);
  40. player.setSocket(null);
  41. console.log(username + ' disconnected');
  42. }
  43. }
  44. });
  45. socket.on('connect', function () {
  46. console.log('Connected to server');
  47. });
  48. socket.on('auth', /*#__PURE__*/function () {
  49. var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(playerName, callback) {
  50. var response, kickout;
  51. return _regenerator.default.wrap(function _callee$(_context) {
  52. while (1) {
  53. switch (_context.prev = _context.next) {
  54. case 0:
  55. console.log(' Received auth message, player name : ' + playerName);
  56. _context.next = 3;
  57. return updatePlayersFromDb();
  58. case 3:
  59. response = {};
  60. kickout = false;
  61. if (authorizedPlayersNames.has(playerName)) {
  62. _context.next = 10;
  63. break;
  64. }
  65. response = {
  66. res: 'ko',
  67. message: playerName + ' Not found'
  68. };
  69. kickout = true;
  70. _context.next = 31;
  71. break;
  72. case 10:
  73. if (!(authorizedPlayers.get(playerName).isConnected() && authorizedPlayers.get(playerName).getSocket().connected)) {
  74. _context.next = 15;
  75. break;
  76. }
  77. response = {
  78. res: 'ko',
  79. message: playerName + ' already connected'
  80. };
  81. kickout = true;
  82. _context.next = 31;
  83. break;
  84. case 15:
  85. if (!authorizedPlayers.get(playerName).isConnected()) {
  86. _context.next = 19;
  87. break;
  88. }
  89. authorizedPlayers.get(playerName).getSocket().disconnect(true);
  90. _context.next = 27;
  91. break;
  92. case 19:
  93. _context.prev = 19;
  94. _context.next = 22;
  95. return removeAllGamesCreatedByPlayer(playerName);
  96. case 22:
  97. _context.next = 27;
  98. break;
  99. case 24:
  100. _context.prev = 24;
  101. _context.t0 = _context["catch"](19);
  102. console.log('Error removing games : ' + _context.t0.message);
  103. case 27:
  104. response = {
  105. res: 'ok',
  106. message: playerName + ' connected'
  107. };
  108. authorizedPlayers.get(playerName).setConnected(true);
  109. authorizedPlayers.get(playerName).setSocket(socket);
  110. connectedPlayers.set(socket.id, playerName);
  111. case 31:
  112. callback(response);
  113. if (kickout === true) {
  114. setTimeout(function () {
  115. socket.disconnect(true);
  116. }, 600);
  117. }
  118. case 33:
  119. case "end":
  120. return _context.stop();
  121. }
  122. }
  123. }, _callee, null, [[19, 24]]);
  124. }));
  125. return function (_x, _x2) {
  126. return _ref.apply(this, arguments);
  127. };
  128. }());
  129. socket.on('games-list', /*#__PURE__*/function () {
  130. var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(playerName, callback) {
  131. var response, games;
  132. return _regenerator.default.wrap(function _callee2$(_context2) {
  133. while (1) {
  134. switch (_context2.prev = _context2.next) {
  135. case 0:
  136. console.log(' Received games-list message, player name : ' + playerName);
  137. response = {};
  138. _context2.prev = 2;
  139. _context2.next = 5;
  140. return getJoinableGames(playerName);
  141. case 5:
  142. games = _context2.sent;
  143. response = {
  144. res: 'ok',
  145. message: games
  146. };
  147. _context2.next = 12;
  148. break;
  149. case 9:
  150. _context2.prev = 9;
  151. _context2.t0 = _context2["catch"](2);
  152. response = {
  153. res: 'ko',
  154. message: 'Error from server'
  155. };
  156. case 12:
  157. callback(response);
  158. case 13:
  159. case "end":
  160. return _context2.stop();
  161. }
  162. }
  163. }, _callee2, null, [[2, 9]]);
  164. }));
  165. return function (_x3, _x4) {
  166. return _ref2.apply(this, arguments);
  167. };
  168. }());
  169. socket.on('create-game', /*#__PURE__*/function () {
  170. var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(game, callback) {
  171. var response, id;
  172. return _regenerator.default.wrap(function _callee3$(_context3) {
  173. while (1) {
  174. switch (_context3.prev = _context3.next) {
  175. case 0:
  176. response = {};
  177. _context3.prev = 1;
  178. _context3.next = 4;
  179. return addGameDb(game);
  180. case 4:
  181. id = _context3.sent;
  182. response = {
  183. res: 'ok',
  184. message: id
  185. };
  186. gamesManager.addPlayerInGame(authorizedPlayers.get(connectedPlayers.get(socket.id)), id, false, true, {
  187. deck: game.deck,
  188. advRules: game.advRules
  189. });
  190. console.log('Force all clients to reload their games');
  191. forceClientsReloadGames();
  192. _context3.next = 15;
  193. break;
  194. case 11:
  195. _context3.prev = 11;
  196. _context3.t0 = _context3["catch"](1);
  197. console.log('error in create-game:>> ', _context3.t0);
  198. response = {
  199. res: 'ko',
  200. message: 'Error from server'
  201. };
  202. case 15:
  203. callback(response);
  204. case 16:
  205. case "end":
  206. return _context3.stop();
  207. }
  208. }
  209. }, _callee3, null, [[1, 11]]);
  210. }));
  211. return function (_x5, _x6) {
  212. return _ref3.apply(this, arguments);
  213. };
  214. }());
  215. socket.on('join-game', /*#__PURE__*/function () {
  216. var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(gameDetails, callback) {
  217. var result, response;
  218. return _regenerator.default.wrap(function _callee4$(_context4) {
  219. while (1) {
  220. switch (_context4.prev = _context4.next) {
  221. case 0:
  222. result = gamesManager.addPlayerInGame(authorizedPlayers.get(connectedPlayers.get(socket.id)), gameDetails.id, gameDetails.joinCreatedGame);
  223. response = {};
  224. if (result === true) {
  225. response.res = 'ok';
  226. response.message = 'Game joined';
  227. } else {
  228. response.res = 'ko';
  229. response.message = 'Unable to join game';
  230. }
  231. callback(response);
  232. case 4:
  233. case "end":
  234. return _context4.stop();
  235. }
  236. }
  237. }, _callee4);
  238. }));
  239. return function (_x7, _x8) {
  240. return _ref4.apply(this, arguments);
  241. };
  242. }());
  243. socket.on('leave-game', /*#__PURE__*/function () {
  244. var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(username, callback) {
  245. var res;
  246. return _regenerator.default.wrap(function _callee5$(_context5) {
  247. while (1) {
  248. switch (_context5.prev = _context5.next) {
  249. case 0:
  250. // Remove player from game he is playing
  251. try {
  252. res = removeAllGamesCreatedByPlayer(username);
  253. if (res.affectedRows > 0) {
  254. forceClientsReloadGames();
  255. }
  256. } catch (err) {
  257. console.log('Error removing games : ' + err.message);
  258. }
  259. gamesManager.playerLeft(authorizedPlayers.get(username), false);
  260. callback(true);
  261. case 3:
  262. case "end":
  263. return _context5.stop();
  264. }
  265. }
  266. }, _callee5);
  267. }));
  268. return function (_x9, _x10) {
  269. return _ref5.apply(this, arguments);
  270. };
  271. }());
  272. });
  273. var updatePlayersFromDb = /*#__PURE__*/function () {
  274. var _ref6 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6() {
  275. var usernames;
  276. return _regenerator.default.wrap(function _callee6$(_context6) {
  277. while (1) {
  278. switch (_context6.prev = _context6.next) {
  279. case 0:
  280. _context6.prev = 0;
  281. _context6.next = 3;
  282. return mariadbConn.getUsernames();
  283. case 3:
  284. usernames = _context6.sent;
  285. authorizedPlayersNames = new Set(usernames);
  286. console.log('authorizedPlayersNames update : ', authorizedPlayersNames);
  287. authorizedPlayersNames.forEach(function (name) {
  288. if (!authorizedPlayers.has(name)) {
  289. console.log('New username authorized :', name);
  290. authorizedPlayers.set(name, new _playerId.default(name));
  291. }
  292. });
  293. return _context6.abrupt("return", 'ok');
  294. case 10:
  295. _context6.prev = 10;
  296. _context6.t0 = _context6["catch"](0);
  297. console.log('Error getting usernames from DB : ', _context6.t0);
  298. return _context6.abrupt("return", _context6.t0.Error);
  299. case 14:
  300. case "end":
  301. return _context6.stop();
  302. }
  303. }
  304. }, _callee6, null, [[0, 10]]);
  305. }));
  306. return function updatePlayersFromDb() {
  307. return _ref6.apply(this, arguments);
  308. };
  309. }();
  310. var getCurrentListAuthorizedPlayers = function getCurrentListAuthorizedPlayers() {
  311. return (0, _toConsumableArray2.default)(authorizedPlayersNames);
  312. };
  313. var addPlayerDb = /*#__PURE__*/function () {
  314. var _ref7 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(username) {
  315. var response;
  316. return _regenerator.default.wrap(function _callee7$(_context7) {
  317. while (1) {
  318. switch (_context7.prev = _context7.next) {
  319. case 0:
  320. console.log('add player in db : ' + username);
  321. _context7.prev = 1;
  322. _context7.next = 4;
  323. return mariadbConn.addPlayer(username);
  324. case 4:
  325. response = _context7.sent;
  326. authorizedPlayersNames.add(username);
  327. authorizedPlayers.set(username, new _playerId.default(username));
  328. return _context7.abrupt("return", 'OK ' + JSON.stringify(response));
  329. case 10:
  330. _context7.prev = 10;
  331. _context7.t0 = _context7["catch"](1);
  332. return _context7.abrupt("return", 'KO ' + _context7.t0.message);
  333. case 13:
  334. case "end":
  335. return _context7.stop();
  336. }
  337. }
  338. }, _callee7, null, [[1, 10]]);
  339. }));
  340. return function addPlayerDb(_x11) {
  341. return _ref7.apply(this, arguments);
  342. };
  343. }();
  344. var removePlayerDb = /*#__PURE__*/function () {
  345. var _ref8 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(username) {
  346. var response;
  347. return _regenerator.default.wrap(function _callee8$(_context8) {
  348. while (1) {
  349. switch (_context8.prev = _context8.next) {
  350. case 0:
  351. console.log('remove player in db : ' + username);
  352. _context8.prev = 1;
  353. _context8.next = 4;
  354. return mariadbConn.removePlayer(username);
  355. case 4:
  356. response = _context8.sent;
  357. authorizedPlayersNames.delete(username);
  358. authorizedPlayers.delete(username);
  359. return _context8.abrupt("return", 'OK ' + JSON.stringify(response));
  360. case 10:
  361. _context8.prev = 10;
  362. _context8.t0 = _context8["catch"](1);
  363. return _context8.abrupt("return", 'KO ' + _context8.t0.message);
  364. case 13:
  365. case "end":
  366. return _context8.stop();
  367. }
  368. }
  369. }, _callee8, null, [[1, 10]]);
  370. }));
  371. return function removePlayerDb(_x12) {
  372. return _ref8.apply(this, arguments);
  373. };
  374. }();
  375. var addGameDb = /*#__PURE__*/function () {
  376. var _ref9 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(game) {
  377. var response;
  378. return _regenerator.default.wrap(function _callee9$(_context9) {
  379. while (1) {
  380. switch (_context9.prev = _context9.next) {
  381. case 0:
  382. _context9.prev = 0;
  383. _context9.next = 3;
  384. return mariadbConn.addNewGame(game);
  385. case 3:
  386. response = _context9.sent;
  387. return _context9.abrupt("return", response);
  388. case 7:
  389. _context9.prev = 7;
  390. _context9.t0 = _context9["catch"](0);
  391. console.log('KO ' + _context9.t0.message);
  392. throw _context9.t0;
  393. case 11:
  394. case "end":
  395. return _context9.stop();
  396. }
  397. }
  398. }, _callee9, null, [[0, 7]]);
  399. }));
  400. return function addGameDb(_x13) {
  401. return _ref9.apply(this, arguments);
  402. };
  403. }();
  404. var removeGameDb = /*#__PURE__*/function () {
  405. var _ref10 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10(method) {
  406. var response,
  407. _args10 = arguments;
  408. return _regenerator.default.wrap(function _callee10$(_context10) {
  409. while (1) {
  410. switch (_context10.prev = _context10.next) {
  411. case 0:
  412. _context10.prev = 0;
  413. _context10.t0 = method;
  414. _context10.next = _context10.t0 === 'byId' ? 4 : _context10.t0 === 'byPlayer1' ? 8 : _context10.t0 === 'byPlayerAny' ? 12 : _context10.t0 === 'byStatus' ? 16 : _context10.t0 === 'byDays' ? 20 : _context10.t0 === 'finishedByDays' ? 24 : 28;
  415. break;
  416. case 4:
  417. _context10.next = 6;
  418. return mariadbConn.removeGameById(_args10.length <= 1 ? undefined : _args10[1]);
  419. case 6:
  420. response = _context10.sent;
  421. return _context10.abrupt("break", 29);
  422. case 8:
  423. _context10.next = 10;
  424. return mariadbConn.removeGamesByPlayer1(_args10.length <= 1 ? undefined : _args10[1]);
  425. case 10:
  426. response = _context10.sent;
  427. return _context10.abrupt("break", 29);
  428. case 12:
  429. _context10.next = 14;
  430. return mariadbConn.removeGamesByPlayerAny(_args10.length <= 1 ? undefined : _args10[1]);
  431. case 14:
  432. response = _context10.sent;
  433. return _context10.abrupt("break", 29);
  434. case 16:
  435. _context10.next = 18;
  436. return mariadbConn.removeGamesByStatus(_args10.length <= 1 ? undefined : _args10[1]);
  437. case 18:
  438. response = _context10.sent;
  439. return _context10.abrupt("break", 29);
  440. case 20:
  441. _context10.next = 22;
  442. return mariadbConn.removeGamesByDays(_args10.length <= 1 ? undefined : _args10[1]);
  443. case 22:
  444. response = _context10.sent;
  445. return _context10.abrupt("break", 29);
  446. case 24:
  447. _context10.next = 26;
  448. return mariadbConn.removeFinishedGamesByDays(_args10.length <= 1 ? undefined : _args10[1]);
  449. case 26:
  450. response = _context10.sent;
  451. return _context10.abrupt("break", 29);
  452. case 28:
  453. return _context10.abrupt("return", 'KO : Unknown method to remove game from DB : ' + method);
  454. case 29:
  455. return _context10.abrupt("return", 'OK ' + JSON.stringify(response));
  456. case 32:
  457. _context10.prev = 32;
  458. _context10.t1 = _context10["catch"](0);
  459. return _context10.abrupt("return", 'KO ' + _context10.t1.message);
  460. case 35:
  461. case "end":
  462. return _context10.stop();
  463. }
  464. }
  465. }, _callee10, null, [[0, 32]]);
  466. }));
  467. return function removeGameDb(_x14) {
  468. return _ref10.apply(this, arguments);
  469. };
  470. }();
  471. var getJoinableGames = /*#__PURE__*/function () {
  472. var _ref11 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(username) {
  473. var games;
  474. return _regenerator.default.wrap(function _callee11$(_context11) {
  475. while (1) {
  476. switch (_context11.prev = _context11.next) {
  477. case 0:
  478. _context11.prev = 0;
  479. _context11.next = 3;
  480. return mariadbConn.getJoinableGamesForPlayer(username);
  481. case 3:
  482. games = _context11.sent;
  483. return _context11.abrupt("return", games);
  484. case 7:
  485. _context11.prev = 7;
  486. _context11.t0 = _context11["catch"](0);
  487. console.log('KO : not able to get games : ' + _context11.t0);
  488. throw _context11.t0;
  489. case 11:
  490. case "end":
  491. return _context11.stop();
  492. }
  493. }
  494. }, _callee11, null, [[0, 7]]);
  495. }));
  496. return function getJoinableGames(_x15) {
  497. return _ref11.apply(this, arguments);
  498. };
  499. }();
  500. var removeAllGamesCreatedByPlayer = /*#__PURE__*/function () {
  501. var _ref12 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(playerName) {
  502. var result;
  503. return _regenerator.default.wrap(function _callee12$(_context12) {
  504. while (1) {
  505. switch (_context12.prev = _context12.next) {
  506. case 0:
  507. _context12.next = 2;
  508. return mariadbConn.removeCreatedGamesByPlayer1(playerName);
  509. case 2:
  510. result = _context12.sent;
  511. return _context12.abrupt("return", result);
  512. case 4:
  513. case "end":
  514. return _context12.stop();
  515. }
  516. }
  517. }, _callee12);
  518. }));
  519. return function removeAllGamesCreatedByPlayer(_x16) {
  520. return _ref12.apply(this, arguments);
  521. };
  522. }();
  523. var forceClientsReloadGames = function forceClientsReloadGames() {
  524. server.emit('reload-games-list');
  525. };
  526. return {
  527. updatePlayersFromDb: updatePlayersFromDb,
  528. getCurrentListAuthorizedPlayers: getCurrentListAuthorizedPlayers,
  529. addPlayerDb: addPlayerDb,
  530. removePlayerDb: removePlayerDb,
  531. addGameDb: addGameDb,
  532. removeGameDb: removeGameDb,
  533. getJoinableGames: getJoinableGames
  534. };
  535. }
  536. var server = new Server();
  537. var serverToolListner = new _serverToolListener.default(server);
  538. serverToolListner.listen(1664);
  539. //# sourceMappingURL=server.js.map