'use strict'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _playerId = _interopRequireDefault(require("./players/player-id")); var _gamesManager = _interopRequireDefault(require("./game-server/games-manager")); var _mariadbConnector = _interopRequireDefault(require("./db/mariadb-connector")); var _serverToolListener = _interopRequireDefault(require("./tools/server-tool-listener")); function Server() { console.log('Starting 12 heroes server'); var io = require('socket.io'); var server = io.listen(2610); var mariadbConn = new _mariadbConnector.default(); var gamesManager = new _gamesManager.default(server, mariadbConn); var authorizedPlayers = new Map(); var connectedPlayers = new Map(); var authorizedPlayersNames = new Set(); server.on('connection', function (socket) { console.log('A player connected with id : ' + socket.id); socket.on('disconnect', function (reason) { console.log('A player disconnected, reason : ' + reason); if (reason === 'client namespace disconnect' || reason === 'server namespace disconnect' || reason === 'transport close') { var username = connectedPlayers.get(socket.id); if (username) { var player = authorizedPlayers.get(username); // If player had created a game, remove it removeAllGamesCreatedByPlayer(username).then(function (res) { if (res.affectedRows > 0) { // Tell other clients to reload their games if there were forceClientsReloadGames(); } }).catch(function (err) { return console.log('Error removing game :>> ', err.message); }); // Tell game manager a player left, if he was playing if (player.isPlayingGameId >= 0) { gamesManager.playerLeft(player, true); } connectedPlayers.delete(socket.id); player.setConnected(false); player.setSocket(null); console.log(username + ' disconnected'); } } }); socket.on('connect', function () { console.log('Connected to server'); }); socket.on('auth', /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(playerName, callback) { var response, kickout; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: console.log(' Received auth message, player name : ' + playerName); _context.next = 3; return updatePlayersFromDb(); case 3: response = {}; kickout = false; if (authorizedPlayersNames.has(playerName)) { _context.next = 10; break; } response = { res: 'ko', message: playerName + ' Not found' }; kickout = true; _context.next = 31; break; case 10: if (!(authorizedPlayers.get(playerName).isConnected() && authorizedPlayers.get(playerName).getSocket().connected)) { _context.next = 15; break; } response = { res: 'ko', message: playerName + ' already connected' }; kickout = true; _context.next = 31; break; case 15: if (!authorizedPlayers.get(playerName).isConnected()) { _context.next = 19; break; } authorizedPlayers.get(playerName).getSocket().disconnect(true); _context.next = 27; break; case 19: _context.prev = 19; _context.next = 22; return removeAllGamesCreatedByPlayer(playerName); case 22: _context.next = 27; break; case 24: _context.prev = 24; _context.t0 = _context["catch"](19); console.log('Error removing games : ' + _context.t0.message); case 27: response = { res: 'ok', message: playerName + ' connected' }; authorizedPlayers.get(playerName).setConnected(true); authorizedPlayers.get(playerName).setSocket(socket); connectedPlayers.set(socket.id, playerName); case 31: callback(response); if (kickout === true) { setTimeout(function () { socket.disconnect(true); }, 600); } case 33: case "end": return _context.stop(); } } }, _callee, null, [[19, 24]]); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); socket.on('games-list', /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(playerName, callback) { var response, games; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: console.log(' Received games-list message, player name : ' + playerName); response = {}; _context2.prev = 2; _context2.next = 5; return getJoinableGames(playerName); case 5: games = _context2.sent; response = { res: 'ok', message: games }; _context2.next = 12; break; case 9: _context2.prev = 9; _context2.t0 = _context2["catch"](2); response = { res: 'ko', message: 'Error from server' }; case 12: callback(response); case 13: case "end": return _context2.stop(); } } }, _callee2, null, [[2, 9]]); })); return function (_x3, _x4) { return _ref2.apply(this, arguments); }; }()); socket.on('create-game', /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(game, callback) { var response, id; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: response = {}; _context3.prev = 1; _context3.next = 4; return addGameDb(game); case 4: id = _context3.sent; response = { res: 'ok', message: id }; gamesManager.addPlayerInGame(authorizedPlayers.get(connectedPlayers.get(socket.id)), id, false, true, { deck: game.deck, advRules: game.advRules }); console.log('Force all clients to reload their games'); forceClientsReloadGames(); _context3.next = 15; break; case 11: _context3.prev = 11; _context3.t0 = _context3["catch"](1); console.log('error in create-game:>> ', _context3.t0); response = { res: 'ko', message: 'Error from server' }; case 15: callback(response); case 16: case "end": return _context3.stop(); } } }, _callee3, null, [[1, 11]]); })); return function (_x5, _x6) { return _ref3.apply(this, arguments); }; }()); socket.on('join-game', /*#__PURE__*/function () { var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(gameDetails, callback) { var result, response; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: result = gamesManager.addPlayerInGame(authorizedPlayers.get(connectedPlayers.get(socket.id)), gameDetails.id, gameDetails.joinCreatedGame); response = {}; if (result === true) { response.res = 'ok'; response.message = 'Game joined'; } else { response.res = 'ko'; response.message = 'Unable to join game'; } callback(response); case 4: case "end": return _context4.stop(); } } }, _callee4); })); return function (_x7, _x8) { return _ref4.apply(this, arguments); }; }()); socket.on('leave-game', /*#__PURE__*/function () { var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(username, callback) { var res; return _regenerator.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: // Remove player from game he is playing try { res = removeAllGamesCreatedByPlayer(username); if (res.affectedRows > 0) { forceClientsReloadGames(); } } catch (err) { console.log('Error removing games : ' + err.message); } gamesManager.playerLeft(authorizedPlayers.get(username), false); callback(true); case 3: case "end": return _context5.stop(); } } }, _callee5); })); return function (_x9, _x10) { return _ref5.apply(this, arguments); }; }()); }); var updatePlayersFromDb = /*#__PURE__*/function () { var _ref6 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6() { var usernames; return _regenerator.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.prev = 0; _context6.next = 3; return mariadbConn.getUsernames(); case 3: usernames = _context6.sent; authorizedPlayersNames = new Set(usernames); console.log('authorizedPlayersNames update : ', authorizedPlayersNames); authorizedPlayersNames.forEach(function (name) { if (!authorizedPlayers.has(name)) { console.log('New username authorized :', name); authorizedPlayers.set(name, new _playerId.default(name)); } }); return _context6.abrupt("return", 'ok'); case 10: _context6.prev = 10; _context6.t0 = _context6["catch"](0); console.log('Error getting usernames from DB : ', _context6.t0); return _context6.abrupt("return", _context6.t0.Error); case 14: case "end": return _context6.stop(); } } }, _callee6, null, [[0, 10]]); })); return function updatePlayersFromDb() { return _ref6.apply(this, arguments); }; }(); var getCurrentListAuthorizedPlayers = function getCurrentListAuthorizedPlayers() { return (0, _toConsumableArray2.default)(authorizedPlayersNames); }; var addPlayerDb = /*#__PURE__*/function () { var _ref7 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(username) { var response; return _regenerator.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: console.log('add player in db : ' + username); _context7.prev = 1; _context7.next = 4; return mariadbConn.addPlayer(username); case 4: response = _context7.sent; authorizedPlayersNames.add(username); authorizedPlayers.set(username, new _playerId.default(username)); return _context7.abrupt("return", 'OK ' + JSON.stringify(response)); case 10: _context7.prev = 10; _context7.t0 = _context7["catch"](1); return _context7.abrupt("return", 'KO ' + _context7.t0.message); case 13: case "end": return _context7.stop(); } } }, _callee7, null, [[1, 10]]); })); return function addPlayerDb(_x11) { return _ref7.apply(this, arguments); }; }(); var removePlayerDb = /*#__PURE__*/function () { var _ref8 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(username) { var response; return _regenerator.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: console.log('remove player in db : ' + username); _context8.prev = 1; _context8.next = 4; return mariadbConn.removePlayer(username); case 4: response = _context8.sent; authorizedPlayersNames.delete(username); authorizedPlayers.delete(username); return _context8.abrupt("return", 'OK ' + JSON.stringify(response)); case 10: _context8.prev = 10; _context8.t0 = _context8["catch"](1); return _context8.abrupt("return", 'KO ' + _context8.t0.message); case 13: case "end": return _context8.stop(); } } }, _callee8, null, [[1, 10]]); })); return function removePlayerDb(_x12) { return _ref8.apply(this, arguments); }; }(); var addGameDb = /*#__PURE__*/function () { var _ref9 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(game) { var response; return _regenerator.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.prev = 0; _context9.next = 3; return mariadbConn.addNewGame(game); case 3: response = _context9.sent; return _context9.abrupt("return", response); case 7: _context9.prev = 7; _context9.t0 = _context9["catch"](0); console.log('KO ' + _context9.t0.message); throw _context9.t0; case 11: case "end": return _context9.stop(); } } }, _callee9, null, [[0, 7]]); })); return function addGameDb(_x13) { return _ref9.apply(this, arguments); }; }(); var removeGameDb = /*#__PURE__*/function () { var _ref10 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee10(method) { var response, _args10 = arguments; return _regenerator.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: _context10.prev = 0; _context10.t0 = method; _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; break; case 4: _context10.next = 6; return mariadbConn.removeGameById(_args10.length <= 1 ? undefined : _args10[1]); case 6: response = _context10.sent; return _context10.abrupt("break", 29); case 8: _context10.next = 10; return mariadbConn.removeGamesByPlayer1(_args10.length <= 1 ? undefined : _args10[1]); case 10: response = _context10.sent; return _context10.abrupt("break", 29); case 12: _context10.next = 14; return mariadbConn.removeGamesByPlayerAny(_args10.length <= 1 ? undefined : _args10[1]); case 14: response = _context10.sent; return _context10.abrupt("break", 29); case 16: _context10.next = 18; return mariadbConn.removeGamesByStatus(_args10.length <= 1 ? undefined : _args10[1]); case 18: response = _context10.sent; return _context10.abrupt("break", 29); case 20: _context10.next = 22; return mariadbConn.removeGamesByDays(_args10.length <= 1 ? undefined : _args10[1]); case 22: response = _context10.sent; return _context10.abrupt("break", 29); case 24: _context10.next = 26; return mariadbConn.removeFinishedGamesByDays(_args10.length <= 1 ? undefined : _args10[1]); case 26: response = _context10.sent; return _context10.abrupt("break", 29); case 28: return _context10.abrupt("return", 'KO : Unknown method to remove game from DB : ' + method); case 29: return _context10.abrupt("return", 'OK ' + JSON.stringify(response)); case 32: _context10.prev = 32; _context10.t1 = _context10["catch"](0); return _context10.abrupt("return", 'KO ' + _context10.t1.message); case 35: case "end": return _context10.stop(); } } }, _callee10, null, [[0, 32]]); })); return function removeGameDb(_x14) { return _ref10.apply(this, arguments); }; }(); var getJoinableGames = /*#__PURE__*/function () { var _ref11 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(username) { var games; return _regenerator.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: _context11.prev = 0; _context11.next = 3; return mariadbConn.getJoinableGamesForPlayer(username); case 3: games = _context11.sent; return _context11.abrupt("return", games); case 7: _context11.prev = 7; _context11.t0 = _context11["catch"](0); console.log('KO : not able to get games : ' + _context11.t0); throw _context11.t0; case 11: case "end": return _context11.stop(); } } }, _callee11, null, [[0, 7]]); })); return function getJoinableGames(_x15) { return _ref11.apply(this, arguments); }; }(); var removeAllGamesCreatedByPlayer = /*#__PURE__*/function () { var _ref12 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee12(playerName) { var result; return _regenerator.default.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: _context12.next = 2; return mariadbConn.removeCreatedGamesByPlayer1(playerName); case 2: result = _context12.sent; return _context12.abrupt("return", result); case 4: case "end": return _context12.stop(); } } }, _callee12); })); return function removeAllGamesCreatedByPlayer(_x16) { return _ref12.apply(this, arguments); }; }(); var forceClientsReloadGames = function forceClientsReloadGames() { server.emit('reload-games-list'); }; return { updatePlayersFromDb: updatePlayersFromDb, getCurrentListAuthorizedPlayers: getCurrentListAuthorizedPlayers, addPlayerDb: addPlayerDb, removePlayerDb: removePlayerDb, addGameDb: addGameDb, removeGameDb: removeGameDb, getJoinableGames: getJoinableGames }; } var server = new Server(); var serverToolListner = new _serverToolListener.default(server); serverToolListner.listen(1664); //# sourceMappingURL=server.js.map