mariadb-connector.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. import mariadb from 'mariadb';
  3. const JOJOAPPS_SERVER_DB='localhost';
  4. // const JOJOAPPS_SERVER_DB_PORT= 1986;
  5. const JOJOAPPS_SERVER_DB_SOCKET= '/run/mysqld/mysqld.sock';
  6. const DATABASE_NAME='twelve_heroes';
  7. export default class MariadbConnector {
  8. constructor() {
  9. let host = process.env.DB || JOJOAPPS_SERVER_DB;
  10. let port = process.env.DB_PORT || 0;
  11. let database = process.env.DB_NAME || DATABASE_NAME;
  12. console.log('DB server : ' + host);
  13. console.log('DB database : ' + database);
  14. let mariaDbConfig={
  15. host,
  16. user: 'node',
  17. password: 'nodejs1234',
  18. database,
  19. connectionLimit: 5
  20. };
  21. if (port === 0) {
  22. mariaDbConfig.socketPath = JOJOAPPS_SERVER_DB_SOCKET;
  23. console.log('Will use DB UNIX socket : ' + mariaDbConfig.socketPath);
  24. } else {
  25. mariaDbConfig.port=port;
  26. console.log('Will use DB port : ' + port);
  27. }
  28. this.pool = mariadb.createPool(mariaDbConfig);
  29. }
  30. async getUsernames() {
  31. let conn;
  32. let players = [];
  33. try {
  34. conn = await this.pool.getConnection();
  35. const rows_players = await conn.query("SELECT username FROM players");
  36. for (const { username: u } of rows_players) {
  37. players.push(u);
  38. }
  39. } finally {
  40. if (conn && conn !== null) {
  41. conn.end();
  42. }
  43. }
  44. return players;
  45. }
  46. async addPlayer(username) {
  47. console.log('mariadb add player : ', username);
  48. let conn;
  49. try {
  50. conn = await this.pool.getConnection();
  51. const val = await conn.query("INSERT INTO players value (?, NOW(),NULL)", [username]);
  52. console.log('OK removing player : ', val);
  53. return val;
  54. } catch (err) {
  55. console.log('error adding player : ', err);
  56. throw err;
  57. } finally {
  58. if (conn && conn !== null) {
  59. conn.end();
  60. }
  61. }
  62. }
  63. async removePlayer(username) {
  64. console.log('mariadb remove player : ', username);
  65. let conn;
  66. try {
  67. conn = await this.pool.getConnection();
  68. const val = await conn.query("DELETE FROM players WHERE username=?", [username]);
  69. console.log('OK removing player : ', val);
  70. return val;
  71. } catch (err) {
  72. console.log('error removing player : ', err);
  73. throw err;
  74. } finally {
  75. if (conn && conn !== null) {
  76. conn.end();
  77. }
  78. }
  79. }
  80. async addNewGame(game) {
  81. console.log('mariadb add game : ', game);
  82. let conn;
  83. try {
  84. conn = await this.pool.getConnection();
  85. const val = await conn.query("INSERT INTO games values (NULL,?,?,?,?,?,?,NOW())",
  86. [game.player1,
  87. game.player2,
  88. game.deck,
  89. convertAdvRulesToString(game.advRules),
  90. game.status,
  91. game.data]);
  92. console.log('OK adding game : ', val);
  93. return val.insertId;
  94. } catch (err) {
  95. console.log('error adding game : ', err);
  96. throw err;
  97. } finally {
  98. if (conn && conn !== null) {
  99. conn.end();
  100. }
  101. }
  102. }
  103. removeGameById(gameId) {
  104. return this.removeGame("DELETE FROM games WHERE id=?", [gameId]);
  105. }
  106. removeGamesByPlayer1(player1Name) {
  107. return this.removeGame("DELETE FROM games WHERE player1=?", [player1Name]);
  108. }
  109. removeGamesByPlayerAny(playerName) {
  110. return this.removeGame("DELETE FROM games WHERE player1=? OR player2=?", [playerName, playerName]);
  111. }
  112. removeGamesByStatus(status) {
  113. return this.removeGame("DELETE FROM games WHERE status=?", [status]);
  114. }
  115. removeGamesByDays(days) {
  116. return this.removeGame("DELETE FROM games WHERE datediff(NOW(),last_played) > ?", [days]);
  117. }
  118. removeFinishedGamesByDays(days) {
  119. return this.removeGame("DELETE FROM games WHERE status='FINISHED' AND datediff(NOW(),last_played) > ?", [days]);
  120. }
  121. async removeGame(queryStr, queryArgs) {
  122. console.log('mariadb remove game : ', queryStr);
  123. let conn;
  124. try {
  125. conn = await this.pool.getConnection();
  126. const val = await conn.query(queryStr, queryArgs);
  127. console.log('OK removing game : ', val);
  128. return val;
  129. } catch (err) {
  130. console.log('error removing game : ', err);
  131. throw err;
  132. } finally {
  133. if (conn && conn !== null) {
  134. conn.end();
  135. }
  136. }
  137. }
  138. async getNonFinishedGamesForPlayer(username) {
  139. let conn;
  140. let games = [];
  141. try {
  142. conn = await this.pool.getConnection();
  143. const res = await conn.query(
  144. "SELECT * FROM`games` WHERE status != 'FINISHED' AND(player1 = ? OR player2 = '' OR player2 = ?)",
  145. [username,username]);
  146. for (const { player1,player2,deck,adv_rules } of res) {
  147. games.push({player1,player2,deck,adv_rules});
  148. }
  149. console.log(`returning for ${username} : `, games);
  150. } finally {
  151. if (conn && conn !== null) {
  152. conn.end();
  153. }
  154. }
  155. return games;
  156. }
  157. }
  158. let convertAdvRulesToString = function (advRulesArray) {
  159. let advRulestStr = '';
  160. let last = advRulesArray.length - 1;
  161. advRulesArray.forEach((rule, index) => {
  162. advRulestStr += rule;
  163. if (index < last) {
  164. advRulestStr += ',';
  165. }
  166. });
  167. return advRulestStr;
  168. };