mariadb-connector.js 5.6 KB

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