mariadb-connector.js 4.3 KB

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