mariadb-connector.js 4.3 KB

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