settings_migrator.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. const DefaultSettings = {
  3. "npc": {
  4. "bank": {
  5. "type": 26,
  6. "value": 1
  7. },
  8. "gbank": {
  9. "type": 26,
  10. "value": 3
  11. },
  12. "pbank": {
  13. "type": 26,
  14. "value": 9
  15. },
  16. "cbank": {
  17. "type": 26,
  18. "value": 12
  19. },
  20. "store": {
  21. "type": 9,
  22. "value": 70310,
  23. "id": null,
  24. "templateId": 2019
  25. },
  26. "sstore": {
  27. "type": 9,
  28. "value": 250,
  29. "id": null,
  30. "templateId": 2109
  31. },
  32. "vg": {
  33. "type": 49,
  34. "value": 609,
  35. "id": null,
  36. "templateId": 2058
  37. }
  38. }
  39. };
  40. module.exports = function MigrateSettings(from_ver, to_ver, settings) {
  41. if (from_ver === undefined) return { ...DefaultSettings, ...settings };
  42. else if (from_ver === null) return DefaultSettings;
  43. else {
  44. from_ver = Number(from_ver);
  45. to_ver = Number(to_ver);
  46. if (from_ver + 1 < to_ver) {
  47. settings = MigrateSettings(from_ver, from_ver + 1, settings);
  48. return MigrateSettings(from_ver + 1, to_ver, settings);
  49. }
  50. const oldsettings = settings;
  51. switch (to_ver) {
  52. default:
  53. settings = Object.assign(DefaultSettings, {});
  54. for (const option in oldsettings) {
  55. if (settings[option] !== undefined) {
  56. settings[option] = MigrateOption(settings[option], oldsettings[option], ["id"]);
  57. }
  58. }
  59. }
  60. return settings;
  61. }
  62. };
  63. function MigrateOption(option, oldoption, excludes) {
  64. if (oldoption === undefined) {
  65. oldoption = option;
  66. }
  67. if (Array.isArray(option)) {
  68. for (const key of Object.keys(option)) {
  69. option[key] = MigrateOption(option[key], oldoption[key], excludes);
  70. }
  71. }
  72. if (Object.getPrototypeOf(option) === Object.prototype) {
  73. option = { ...option, ...oldoption };
  74. for (const key of Object.keys(option)) {
  75. if (excludes.includes(key)) {
  76. option[key] = oldoption[key] || null;
  77. } else {
  78. option[key] = MigrateOption(option[key], oldoption[key], excludes);
  79. }
  80. }
  81. }
  82. return option;
  83. }