'use strict'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _mariadb = _interopRequireDefault(require("mariadb")); function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var JOJOAPPS_SERVER_DB = 'localhost'; var JOJOAPPS_SERVER_DB_SOCKET = '/run/mysqld/mysqld.sock'; var DATABASE_NAME = 'twelve_heroes'; var MariadbConnector = /*#__PURE__*/function () { function MariadbConnector() { (0, _classCallCheck2.default)(this, MariadbConnector); var host = process.env.DB || JOJOAPPS_SERVER_DB; var port = process.env.DB_PORT || 0; var database = process.env.DB_NAME || DATABASE_NAME; console.log('DB server : ' + host); console.log('DB database : ' + database); var mariaDbConfig = { host: host, user: 'node', password: 'nodejs1234', database: database, connectionLimit: 5 }; if (port === 0) { mariaDbConfig.socketPath = JOJOAPPS_SERVER_DB_SOCKET; console.log('Will use DB UNIX socket : ' + mariaDbConfig.socketPath); } else { mariaDbConfig.port = port; console.log('Will use DB port : ' + port); } this.pool = _mariadb.default.createPool(mariaDbConfig); } (0, _createClass2.default)(MariadbConnector, [{ key: "getUsernames", value: function () { var _getUsernames = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() { var conn, players, rows_players, _iterator, _step, u; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: players = []; _context.prev = 1; _context.next = 4; return this.pool.getConnection(); case 4: conn = _context.sent; _context.next = 7; return conn.query('SELECT username FROM players'); case 7: rows_players = _context.sent; _iterator = _createForOfIteratorHelper(rows_players); try { for (_iterator.s(); !(_step = _iterator.n()).done;) { u = _step.value.username; players.push(u); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } case 10: _context.prev = 10; if (conn && conn !== null) { conn.end(); } return _context.finish(10); case 13: return _context.abrupt("return", players); case 14: case "end": return _context.stop(); } } }, _callee, this, [[1,, 10, 13]]); })); function getUsernames() { return _getUsernames.apply(this, arguments); } return getUsernames; }() }, { key: "addPlayer", value: function () { var _addPlayer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(username) { var conn, val; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: console.log('mariadb add player : ', username); _context2.prev = 1; _context2.next = 4; return this.pool.getConnection(); case 4: conn = _context2.sent; _context2.next = 7; return conn.query('INSERT INTO players value (?, NOW(),NULL)', [username]); case 7: val = _context2.sent; console.log('OK removing player : ', val); return _context2.abrupt("return", val); case 12: _context2.prev = 12; _context2.t0 = _context2["catch"](1); console.log('error adding player : ', _context2.t0); throw _context2.t0; case 16: _context2.prev = 16; if (conn && conn !== null) { conn.end(); } return _context2.finish(16); case 19: case "end": return _context2.stop(); } } }, _callee2, this, [[1, 12, 16, 19]]); })); function addPlayer(_x) { return _addPlayer.apply(this, arguments); } return addPlayer; }() }, { key: "removePlayer", value: function () { var _removePlayer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(username) { var conn, val; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: console.log('mariadb remove player : ', username); _context3.prev = 1; _context3.next = 4; return this.pool.getConnection(); case 4: conn = _context3.sent; _context3.next = 7; return conn.query('DELETE FROM players WHERE username=?', [username]); case 7: val = _context3.sent; console.log('OK removing player : ', val); return _context3.abrupt("return", val); case 12: _context3.prev = 12; _context3.t0 = _context3["catch"](1); console.log('error removing player : ', _context3.t0); throw _context3.t0; case 16: _context3.prev = 16; if (conn && conn !== null) { conn.end(); } return _context3.finish(16); case 19: case "end": return _context3.stop(); } } }, _callee3, this, [[1, 12, 16, 19]]); })); function removePlayer(_x2) { return _removePlayer.apply(this, arguments); } return removePlayer; }() }, { key: "addNewGame", value: function () { var _addNewGame = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(game) { var conn, val; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: console.log('mariadb add game : ', game); _context4.prev = 1; _context4.next = 4; return this.pool.getConnection(); case 4: conn = _context4.sent; _context4.next = 7; return conn.query('INSERT INTO games values (NULL,?,?,?,?,?,?,NOW())', [game.player1, game.player2, game.deck, convertAdvRulesToString(game.advRules), game.status, game.data]); case 7: val = _context4.sent; console.log('OK adding game : ', val); return _context4.abrupt("return", val.insertId); case 12: _context4.prev = 12; _context4.t0 = _context4["catch"](1); console.log('error adding game : ', _context4.t0); throw _context4.t0; case 16: _context4.prev = 16; if (conn && conn !== null) { conn.end(); } return _context4.finish(16); case 19: case "end": return _context4.stop(); } } }, _callee4, this, [[1, 12, 16, 19]]); })); function addNewGame(_x3) { return _addNewGame.apply(this, arguments); } return addNewGame; }() }, { key: "removeGameById", value: function removeGameById(gameId) { return this.removeGame('DELETE FROM games WHERE id=?', [gameId]); } }, { key: "removeGamesByPlayer1", value: function removeGamesByPlayer1(player1Name) { return this.removeGame('DELETE FROM games WHERE player1=?', [player1Name]); } }, { key: "removeGamesByPlayerAny", value: function removeGamesByPlayerAny(playerName) { return this.removeGame('DELETE FROM games WHERE player1=? OR player2=?', [playerName, playerName]); } }, { key: "removeGamesByStatus", value: function removeGamesByStatus(status) { return this.removeGame('DELETE FROM games WHERE status=?', [status]); } }, { key: "removeGamesByDays", value: function removeGamesByDays(days) { return this.removeGame('DELETE FROM games WHERE datediff(NOW(),last_played) > ?', [days]); } }, { key: "removeFinishedGamesByDays", value: function removeFinishedGamesByDays(days) { return this.removeGame("DELETE FROM games WHERE status='FINISHED' AND datediff(NOW(),last_played) > ?", [days]); } }, { key: "removeCreatedGamesByPlayer1", value: function removeCreatedGamesByPlayer1(player1Name) { return this.removeGame("DELETE FROM games WHERE player1=? AND status='CREATED'", [player1Name]); } }, { key: "removeGame", value: function () { var _removeGame = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(queryStr, queryArgs) { var conn, val; return _regenerator.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: console.log('mariadb remove game : ', queryStr); _context5.prev = 1; _context5.next = 4; return this.pool.getConnection(); case 4: conn = _context5.sent; _context5.next = 7; return conn.query(queryStr, queryArgs); case 7: val = _context5.sent; console.log('OK removing game : ', val); return _context5.abrupt("return", val); case 12: _context5.prev = 12; _context5.t0 = _context5["catch"](1); console.log('error removing game : ', _context5.t0); throw _context5.t0; case 16: _context5.prev = 16; if (conn && conn !== null) { conn.end(); } return _context5.finish(16); case 19: case "end": return _context5.stop(); } } }, _callee5, this, [[1, 12, 16, 19]]); })); function removeGame(_x4, _x5) { return _removeGame.apply(this, arguments); } return removeGame; }() }, { key: "getJoinableGamesForPlayer", value: function () { var _getJoinableGamesForPlayer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(username) { var conn, games, res, _iterator2, _step2, _step2$value, id, player1, player2, deck, adv_rules; return _regenerator.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: games = []; _context6.prev = 1; _context6.next = 4; return this.pool.getConnection(); case 4: conn = _context6.sent; _context6.next = 7; return conn.query("SELECT * FROM games WHERE (status = 'CREATED' OR status = 'PAUSED' OR status = 'PLAYING') AND(player1 = ? OR player2 = '' OR player2 = ?)", [username, username]); case 7: res = _context6.sent; _iterator2 = _createForOfIteratorHelper(res); try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { _step2$value = _step2.value, id = _step2$value.id, player1 = _step2$value.player1, player2 = _step2$value.player2, deck = _step2$value.deck, adv_rules = _step2$value.adv_rules; games.push({ id: id, player1: player1, player2: player2, deck: deck, adv_rules: adv_rules }); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } console.log("returning for ".concat(username, " : "), games); case 11: _context6.prev = 11; if (conn && conn !== null) { conn.end(); } return _context6.finish(11); case 14: return _context6.abrupt("return", games); case 15: case "end": return _context6.stop(); } } }, _callee6, this, [[1,, 11, 14]]); })); function getJoinableGamesForPlayer(_x6) { return _getJoinableGamesForPlayer.apply(this, arguments); } return getJoinableGamesForPlayer; }() }, { key: "getGameById", value: function () { var _getGameById = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(gameId) { var conn, game; return _regenerator.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: game = {}; _context7.prev = 1; _context7.next = 4; return this.pool.getConnection(); case 4: conn = _context7.sent; _context7.next = 7; return conn.query('SELECT * FROM games WHERE id=?', gameId); case 7: game = _context7.sent; case 8: _context7.prev = 8; if (conn && conn !== null) { conn.end(); } return _context7.finish(8); case 11: return _context7.abrupt("return", game); case 12: case "end": return _context7.stop(); } } }, _callee7, this, [[1,, 8, 11]]); })); function getGameById(_x7) { return _getGameById.apply(this, arguments); } return getGameById; }() }, { key: "updateGameStatus", value: function () { var _updateGameStatus = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(id, status) { var player2, conn, result, _args8 = arguments; return _regenerator.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: player2 = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null; result = {}; _context8.prev = 2; _context8.next = 5; return this.pool.getConnection(); case 5: conn = _context8.sent; if (!player2) { _context8.next = 12; break; } _context8.next = 9; return conn.query('UPDATE games SET player2=?, status = ?, last_played = NOW() WHERE games.id =?', player2, status, id); case 9: result = _context8.sent; _context8.next = 15; break; case 12: _context8.next = 14; return conn.query('UPDATE games SET status = ?, last_played = NOW() WHERE games.id =?', status, id); case 14: result = _context8.sent; case 15: _context8.prev = 15; if (conn && conn !== null) { conn.end(); } return _context8.finish(15); case 18: return _context8.abrupt("return", result); case 19: case "end": return _context8.stop(); } } }, _callee8, this, [[2,, 15, 18]]); })); function updateGameStatus(_x8, _x9) { return _updateGameStatus.apply(this, arguments); } return updateGameStatus; }() }, { key: "updateGameData", value: function () { var _updateGameData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee9(id, data) { var conn, result; return _regenerator.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: result = {}; _context9.prev = 1; _context9.next = 4; return this.pool.getConnection(); case 4: conn = _context9.sent; _context9.next = 7; return conn.query('UPDATE games SET game_data = ?, last_played = NOW() WHERE games.id =?', data, id); case 7: result = _context9.sent; case 8: _context9.prev = 8; if (conn && conn !== null) { conn.end(); } return _context9.finish(8); case 11: return _context9.abrupt("return", result); case 12: case "end": return _context9.stop(); } } }, _callee9, this, [[1,, 8, 11]]); })); function updateGameData(_x10, _x11) { return _updateGameData.apply(this, arguments); } return updateGameData; }() }]); return MariadbConnector; }(); exports.default = MariadbConnector; var convertAdvRulesToString = function convertAdvRulesToString(advRulesArray) { var advRulestStr = ''; var last = advRulesArray.length - 1; advRulesArray.forEach(function (rule, index) { advRulestStr += rule; if (index < last) { advRulestStr += ','; } }); return advRulestStr; }; //# sourceMappingURL=mariadb-connector.js.map