You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 lines
2.4 KiB

  1. let sqlite = require('sqlite3').verbose();
  2. let r = require('rethinkdb');
  3. let util = require('util');
  4. const config = {
  5. file: './storage.db',
  6. dbName: 'live',
  7. dropTables: true,
  8. maxBusy: 100,
  9. tables: [
  10. 'characterList',
  11. 'stash',
  12. 'skins',
  13. 'login',
  14. 'leaderboard',
  15. 'customMap',
  16. 'mail',
  17. 'customChannels',
  18. 'error',
  19. 'modLog',
  20. 'accountInfo',
  21. 'character'
  22. ]
  23. };
  24. let converter = {
  25. dbS: null,
  26. dbR: null,
  27. connection: null,
  28. res: null,
  29. busy: 0,
  30. records: [],
  31. currentTable: null,
  32. init: async function () {
  33. this.dbS = new sqlite.Database(config.file, this.onDbCreated.bind(this));
  34. },
  35. onDbCreated: async function () {
  36. this.connection = await r.connect({
  37. host: 'localhost',
  38. port: 28015
  39. });
  40. await this.setupRethink();
  41. await this.convertTables();
  42. },
  43. setupRethink: async function () {
  44. try {
  45. await r.dbCreate(config.dbName).run(this.connection);
  46. } catch (e) {}
  47. await this.connection.use(config.dbName);
  48. for (const table of config.tables) {
  49. try {
  50. if (config.dropTables) {
  51. try {
  52. await r.tableDrop(table).run(this.connection);
  53. } catch (e) {}
  54. }
  55. await r.tableCreate(table).run(this.connection);
  56. } catch (e) {
  57. if (!e.msg.includes('already exists'))
  58. console.log(e);
  59. }
  60. }
  61. },
  62. convertTables: async function () {
  63. for (let table of config.tables) {
  64. this.currentTable = table;
  65. this.records = await util.promisify(this.dbS.all.bind(this.dbS))(`SELECT * FROM ${table}`);
  66. console.log(`${table}: ${this.records.length} records`);
  67. await this.startConvert();
  68. console.log('done');
  69. }
  70. },
  71. startConvert: async function () {
  72. return new Promise(res => {
  73. this.res = res;
  74. this.work();
  75. });
  76. },
  77. work: function () {
  78. if (!this.records.length) {
  79. this.res();
  80. return;
  81. }
  82. if (this.busy === config.maxBusy)
  83. return;
  84. let record = this.records.pop();
  85. if (record) {
  86. this.processRecord(record);
  87. this.work();
  88. }
  89. },
  90. processRecord: async function (record) {
  91. this.busy++;
  92. let id = record.key;
  93. let value = record.value
  94. .split('`')
  95. .join('\'');
  96. let obj = value;
  97. if (!['login'].includes(this.currentTable)) {
  98. if (this.currentTable === 'mail' && value === '')
  99. value = '{}';
  100. obj = JSON.parse(value);
  101. }
  102. await r.table(this.currentTable)
  103. .insert({
  104. id,
  105. value: obj
  106. })
  107. .run(this.connection);
  108. this.busy--;
  109. this.work();
  110. }
  111. };
  112. converter.init();