Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

255 рядки
5.4 KiB

  1. let util = require('util');
  2. let serverConfig = require('../config/serverConfig');
  3. //eslint-disable-next-line no-process-env
  4. if ((process.env.db || serverConfig.db) === 'rethink') {
  5. module.exports = require('./ioRethink');
  6. return;
  7. }
  8. module.exports = {
  9. db: null,
  10. file: '../../data/storage.db',
  11. buffer: [],
  12. processing: [],
  13. tables: {
  14. character: null,
  15. characterList: null,
  16. stash: null,
  17. skins: null,
  18. login: null,
  19. leaderboard: null,
  20. customMap: null,
  21. mail: null,
  22. customChannels: null,
  23. error: null,
  24. modLog: null,
  25. accountInfo: null
  26. },
  27. init: async function (cbReady) {
  28. let sqlite = require('sqlite3').verbose();
  29. this.db = new sqlite.Database(this.file, this.onDbCreated.bind(this, cbReady));
  30. },
  31. onDbCreated: function (cbReady) {
  32. let db = this.db;
  33. let tables = this.tables;
  34. let scope = this;
  35. db.serialize(function () {
  36. for (let t in tables) {
  37. db.run(`
  38. CREATE TABLE ${t} (key VARCHAR(50), value TEXT)
  39. `, scope.onTableCreated.bind(scope, t));
  40. }
  41. cbReady();
  42. }, this);
  43. },
  44. onTableCreated: async function (table) {
  45. },
  46. //ent, field
  47. get: function (options) {
  48. let key = options.ent;
  49. let table = options.field;
  50. options.query = `SELECT * FROM ${table} WHERE key = '${key}' LIMIT 1`;
  51. this.db.get(options.query, this.done.bind(this, options));
  52. },
  53. getAsync: async function (options) {
  54. return await this.queue({
  55. type: 'get',
  56. options: options
  57. });
  58. },
  59. getAllAsync: async function (options) {
  60. return await this.queue({
  61. type: 'getAll',
  62. options: options
  63. });
  64. },
  65. delete: function (options) {
  66. let key = options.ent;
  67. let table = options.field;
  68. options.query = `DELETE FROM ${table} WHERE key = '${key}'`;
  69. this.db.run(options.query, this.done.bind(this, options));
  70. },
  71. deleteAsync: async function (options) {
  72. await this.queue({
  73. type: 'delete',
  74. options: options
  75. });
  76. },
  77. //ent, field, value
  78. set: function (options) {
  79. let key = options.ent;
  80. let table = options.field;
  81. this.db.get(`SELECT 1 FROM ${table} where key = '${key}'`, this.doesExist.bind(this, options));
  82. },
  83. doesExist: function (options, err, result) {
  84. let key = options.ent;
  85. let table = options.field;
  86. let query = `INSERT INTO ${table} (key, value) VALUES('${key}', '${options.value}')`;
  87. if (result)
  88. query = `UPDATE ${table} SET value = '${options.value}' WHERE key = '${key}'`;
  89. this.db.run(query, this.done.bind(this, options));
  90. },
  91. setAsync: async function (options) {
  92. await this.queue({
  93. type: 'set',
  94. options: options
  95. });
  96. },
  97. queue: async function (config) {
  98. let resolve = null;
  99. let promise = new Promise(function (res) {
  100. resolve = res;
  101. });
  102. this.buffer.push({
  103. resolve: resolve,
  104. config: config
  105. });
  106. this.process();
  107. return promise;
  108. },
  109. process: async function () {
  110. let next = this.buffer.splice(0, 1);
  111. if (!next.length)
  112. return;
  113. next = next[0];
  114. let config = next.config;
  115. let options = config.options;
  116. let res = null;
  117. try {
  118. if (config.type === 'get')
  119. res = await this.processGet(options);
  120. else if (config.type === 'getAll')
  121. res = await this.processGetAll(options);
  122. else if (config.type === 'set')
  123. await this.processSet(options);
  124. else if (config.type === 'delete')
  125. await this.processDelete(options);
  126. } catch (e) {
  127. if (e.toString().indexOf('unrecognized token') > -1)
  128. _.log(e);
  129. _.log(e);
  130. this.buffer.splice(0, 0, next);
  131. setTimeout(this.process.bind(this), 10);
  132. return;
  133. }
  134. next.resolve(res);
  135. setTimeout(this.process.bind(this), 10);
  136. },
  137. processGet: async function (options) {
  138. let res = await util.promisify(this.db.get.bind(this.db))(`SELECT * FROM ${options.table} WHERE key = '${options.key}' LIMIT 1`);
  139. if (res) {
  140. res = res.value;
  141. if (options.clean) {
  142. res = res
  143. .split('`')
  144. .join('\'')
  145. .replace(/''+/g, '\'');
  146. }
  147. if (!options.noParse)
  148. res = JSON.parse(res);
  149. } else if (!options.noParse && !options.noDefault)
  150. res = options.isArray ? [] : {};
  151. return res;
  152. },
  153. processGetAll: async function (options) {
  154. let res = await util.promisify(this.db.all.bind(this.db))(`SELECT * FROM ${options.table}`);
  155. if (res) {
  156. if (options.clean) {
  157. res.forEach(r => {
  158. r.value = r.value
  159. .split('`')
  160. .join('\'')
  161. .replace(/''+/g, '\'');
  162. });
  163. }
  164. if (!options.noParse) {
  165. if (!res)
  166. res = options.isArray ? [] : {};
  167. else {
  168. res.forEach(r => {
  169. r.value = JSON.parse(r.value);
  170. });
  171. }
  172. }
  173. } else if (!options.noParse && !options.noDefault)
  174. res = options.isArray ? [] : {};
  175. return res;
  176. },
  177. processSet: async function (options) {
  178. let table = options.table;
  179. let key = options.key;
  180. let value = options.value;
  181. if (options.serialize)
  182. value = JSON.stringify(value);
  183. let exists = await util.promisify(this.db.get.bind(this.db))(`SELECT * FROM ${table} WHERE key = '${key}' LIMIT 1`);
  184. let query = `INSERT INTO ${table} (key, value) VALUES('${key}', '${value}')`;
  185. if (exists)
  186. query = `UPDATE ${table} SET value = '${value}' WHERE key = '${key}'`;
  187. await util.promisify(this.db.run.bind(this.db))(query);
  188. },
  189. processDelete: async function (options) {
  190. let table = options.table;
  191. let key = options.key;
  192. let query = `DELETE FROM ${table} WHERE key = '${key}'`;
  193. await util.promisify(this.db.run.bind(this.db))(query);
  194. },
  195. done: function (options, err, result) {
  196. result = result || {
  197. value: null
  198. };
  199. if (options.callback)
  200. options.callback(result.value);
  201. }
  202. };