1 |
- {"version":3,"sources":["../../src/db/mariadb-connector.js"],"names":["JOJOAPPS_SERVER_DB","JOJOAPPS_SERVER_DB_SOCKET","DATABASE_NAME","MariadbConnector","host","process","env","DB","port","DB_PORT","database","DB_NAME","console","log","mariaDbConfig","user","password","connectionLimit","socketPath","pool","mariadb","createPool","players","getConnection","conn","query","rows_players","u","username","push","end","val","game","player1","player2","deck","convertAdvRulesToString","advRules","status","data","insertId","gameId","removeGame","player1Name","playerName","days","queryStr","queryArgs","games","res","id","adv_rules","result","advRulesArray","advRulestStr","last","length","forEach","rule","index"],"mappings":"AAAA;;;;;;;;;;;;;;;;;AACA;;;;;;;;AAEA,IAAMA,kBAAkB,GAAG,WAA3B;AACA,IAAMC,yBAAyB,GAAG,yBAAlC;AACA,IAAMC,aAAa,GAAG,eAAtB;;IAEqBC,gB;AACnB,8BAAc;AAAA;AACZ,QAAIC,IAAI,GAAGC,OAAO,CAACC,GAAR,CAAYC,EAAZ,IAAkBP,kBAA7B;AACA,QAAIQ,IAAI,GAAGH,OAAO,CAACC,GAAR,CAAYG,OAAZ,IAAuB,CAAlC;AACA,QAAIC,QAAQ,GAAGL,OAAO,CAACC,GAAR,CAAYK,OAAZ,IAAuBT,aAAtC;AACAU,IAAAA,OAAO,CAACC,GAAR,CAAY,iBAAiBT,IAA7B;AACAQ,IAAAA,OAAO,CAACC,GAAR,CAAY,mBAAmBH,QAA/B;AAEA,QAAII,aAAa,GAAG;AAClBV,MAAAA,IAAI,EAAJA,IADkB;AAElBW,MAAAA,IAAI,EAAE,MAFY;AAGlBC,MAAAA,QAAQ,EAAE,YAHQ;AAIlBN,MAAAA,QAAQ,EAARA,QAJkB;AAKlBO,MAAAA,eAAe,EAAE;AALC,KAApB;;AAQA,QAAIT,IAAI,KAAK,CAAb,EAAgB;AACdM,MAAAA,aAAa,CAACI,UAAd,GAA2BjB,yBAA3B;AACAW,MAAAA,OAAO,CAACC,GAAR,CAAY,+BAA+BC,aAAa,CAACI,UAAzD;AACD,K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;;;AAOF,oBAAI5B,IAAI,IAAIA,IAAI,KAAK,IAArB,EAA2B;AACzBA,kBAAAA,IAAI,CAACM,GAAL;AACD;;;;;kDAEIsB,M;;;;;;;;;;;;;;;;;;;sHAEYF,E,EAAIX,I;;;;;;AAEnBa,gBAAAA,M,GAAS,E;;;uBAEE,KAAKjC,IAAL,CAAUI,aAAV,E;;;AAAbC,gBAAAA,I;;uBACeA,IAAI,CAACC,KAAL,CACb,uEADa,EAEbc,IAFa,EAGbW,EAHa,C;;;AAAfE,gBAAAA,M;;;;;AAMA,oBAAI5B,IAAI,IAAIA,IAAI,KAAK,IAArB,EAA2B;AACzBA,kBAAAA,IAAI,CAACM,GAAL;AACD;;;;;kDAEIsB,M;;;;;;;;;;;;;;;;;;;;;;AAGX,IAAIhB,uBAAuB,GAAG,SAA1BA,uBAA0B,CAASiB,aAAT,EAAwB;AACpD,MAAIC,YAAY,GAAG,EAAnB;AACA,MAAIC,IAAI,GAAGF,aAAa,CAACG,MAAd,GAAuB,CAAlC;AACAH,EAAAA,aAAa,CAACI,OAAd,CAAsB,UAACC,IAAD,EAAOC,KAAP,EAAiB;AACrCL,IAAAA,YAAY,IAAII,IAAhB;;AACA,QAAIC,KAAK,GAAGJ,IAAZ,EAAkB;AAChBD,MAAAA,YAAY,IAAI,GAAhB;AACD;AACF,GALD;AAMA,SAAOA,YAAP;AACD,CAVD","sourcesContent":["'use strict';\nimport mariadb from 'mariadb';\n\nconst JOJOAPPS_SERVER_DB = 'localhost';\nconst JOJOAPPS_SERVER_DB_SOCKET = '/run/mysqld/mysqld.sock';\nconst DATABASE_NAME = 'twelve_heroes';\n\nexport default class MariadbConnector {\n constructor() {\n let host = process.env.DB || JOJOAPPS_SERVER_DB;\n let port = process.env.DB_PORT || 0;\n let database = process.env.DB_NAME || DATABASE_NAME;\n console.log('DB server : ' + host);\n console.log('DB database : ' + database);\n\n let mariaDbConfig = {\n host,\n user: 'node',\n password: 'nodejs1234',\n database,\n connectionLimit: 5\n };\n\n if (port === 0) {\n mariaDbConfig.socketPath = JOJOAPPS_SERVER_DB_SOCKET;\n console.log('Will use DB UNIX socket : ' + mariaDbConfig.socketPath);\n } else {\n mariaDbConfig.port = port;\n console.log('Will use DB port : ' + port);\n }\n\n this.pool = mariadb.createPool(mariaDbConfig);\n }\n\n async getUsernames() {\n let conn;\n let players = [];\n try {\n conn = await this.pool.getConnection();\n\n const rows_players = await conn.query('SELECT username FROM players');\n for (const { username: u } of rows_players) {\n players.push(u);\n }\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n return players;\n }\n\n async addPlayer(username) {\n console.log('mariadb add player : ', username);\n let conn;\n try {\n conn = await this.pool.getConnection();\n const val = await conn.query(\n 'INSERT INTO players value (?, NOW(),NULL)',\n [username]\n );\n console.log('OK removing player : ', val);\n\n return val;\n } catch (err) {\n console.log('error adding player : ', err);\n throw err;\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n }\n async removePlayer(username) {\n console.log('mariadb remove player : ', username);\n let conn;\n try {\n conn = await this.pool.getConnection();\n const val = await conn.query('DELETE FROM players WHERE username=?', [\n username\n ]);\n console.log('OK removing player : ', val);\n return val;\n } catch (err) {\n console.log('error removing player : ', err);\n throw err;\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n }\n\n async addNewGame(game) {\n console.log('mariadb add game : ', game);\n let conn;\n try {\n conn = await this.pool.getConnection();\n const val = await conn.query(\n 'INSERT INTO games values (NULL,?,?,?,?,?,?,NOW())',\n [\n game.player1,\n game.player2,\n game.deck,\n convertAdvRulesToString(game.advRules),\n game.status,\n game.data\n ]\n );\n\n console.log('OK adding game : ', val);\n return val.insertId;\n } catch (err) {\n console.log('error adding game : ', err);\n throw err;\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n }\n\n removeGameById(gameId) {\n return this.removeGame('DELETE FROM games WHERE id=?', [gameId]);\n }\n removeGamesByPlayer1(player1Name) {\n return this.removeGame('DELETE FROM games WHERE player1=?', [player1Name]);\n }\n removeGamesByPlayerAny(playerName) {\n return this.removeGame('DELETE FROM games WHERE player1=? OR player2=?', [\n playerName,\n playerName\n ]);\n }\n removeGamesByStatus(status) {\n return this.removeGame('DELETE FROM games WHERE status=?', [status]);\n }\n removeGamesByDays(days) {\n return this.removeGame(\n 'DELETE FROM games WHERE datediff(NOW(),last_played) > ?',\n [days]\n );\n }\n removeFinishedGamesByDays(days) {\n return this.removeGame(\n \"DELETE FROM games WHERE status='FINISHED' AND datediff(NOW(),last_played) > ?\",\n [days]\n );\n }\n removeCreatedGamesByPlayer1(player1Name) {\n return this.removeGame(\n \"DELETE FROM games WHERE player1=? AND status='CREATED'\",\n [player1Name]\n );\n }\n\n async removeGame(queryStr, queryArgs) {\n console.log('mariadb remove game : ', queryStr);\n let conn;\n try {\n conn = await this.pool.getConnection();\n const val = await conn.query(queryStr, queryArgs);\n console.log('OK removing game : ', val);\n return val;\n } catch (err) {\n console.log('error removing game : ', err);\n throw err;\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n }\n\n async getJoinableGamesForPlayer(username) {\n let conn;\n let games = [];\n try {\n conn = await this.pool.getConnection();\n const res = await conn.query(\n \"SELECT * FROM games WHERE (status = 'CREATED' OR status = 'PAUSED' OR status = 'PLAYING') AND(player1 = ? OR player2 = '' OR player2 = ?)\",\n [username, username]\n );\n for (const { id, player1, player2, deck, adv_rules } of res) {\n games.push({ id, player1, player2, deck, adv_rules });\n }\n console.log(`returning for ${username} : `, games);\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n return games;\n }\n async getGameById(gameId) {\n let conn;\n let game = {};\n try {\n conn = await this.pool.getConnection();\n game = await conn.query('SELECT * FROM games WHERE id=?', gameId);\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n return game;\n }\n async updateGameStatus(id, status, player2 = null) {\n let conn;\n let result = {};\n try {\n conn = await this.pool.getConnection();\n if (player2) {\n result = await conn.query(\n 'UPDATE games SET player2=?, status = ?, last_played = NOW() WHERE games.id =?',\n player2,\n status,\n id\n );\n } else {\n result = await conn.query(\n 'UPDATE games SET status = ?, last_played = NOW() WHERE games.id =?',\n status,\n id\n );\n }\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n return result;\n }\n async updateGameData(id, data) {\n let conn;\n let result = {};\n try {\n conn = await this.pool.getConnection();\n result = await conn.query(\n 'UPDATE games SET game_data = ?, last_played = NOW() WHERE games.id =?',\n data,\n id\n );\n } finally {\n if (conn && conn !== null) {\n conn.end();\n }\n }\n return result;\n }\n}\nlet convertAdvRulesToString = function(advRulesArray) {\n let advRulestStr = '';\n let last = advRulesArray.length - 1;\n advRulesArray.forEach((rule, index) => {\n advRulestStr += rule;\n if (index < last) {\n advRulestStr += ',';\n }\n });\n return advRulestStr;\n};\n"],"file":"mariadb-connector.js"}
|