123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- '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
|