settings_migrator.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* eslint-disable no-param-reassign */
  2. "use strict";
  3. const DefaultSettings = {
  4. "npc": {
  5. // For bank NPC.
  6. // The "type" is a "type" from S_REQUEST_CONTRACT packet.
  7. // The "value" is a "container" from S_VIEW_WARE_EX packet.
  8. "bank": {
  9. "type": 26,
  10. "value": 1
  11. },
  12. "gbank": {
  13. "type": 26,
  14. "value": 3
  15. },
  16. "pbank": {
  17. "type": 26,
  18. "value": 9
  19. },
  20. "cbank": {
  21. "type": 26,
  22. "value": 12
  23. },
  24. // For other NPCs.
  25. // You can use "npcsummoner" command to enable debug for get values.
  26. "store": {
  27. "type": 9,
  28. "value": 70310,
  29. "gameId": null,
  30. "templateId": 2019,
  31. "huntingZoneId": 183
  32. },
  33. "sstore": {
  34. "type": 9,
  35. "value": 250,
  36. "gameId": null,
  37. "templateId": 2109,
  38. "huntingZoneId": 183
  39. },
  40. "bel": {
  41. "type": 50,
  42. "value": 141,
  43. "gameId": null,
  44. "templateId": 2045,
  45. "huntingZoneId": 183
  46. },
  47. "vg": {
  48. "type": 49,
  49. "value": 609,
  50. "gameId": null,
  51. "templateId": 2058,
  52. "huntingZoneId": 183
  53. }
  54. }
  55. };
  56. module.exports = function MigrateSettings(from_ver, to_ver, settings) {
  57. if (from_ver === undefined) return { ...DefaultSettings, ...settings };
  58. else if (from_ver === null) return DefaultSettings;
  59. else {
  60. from_ver = Number(from_ver);
  61. to_ver = Number(to_ver);
  62. if (from_ver + 1 < to_ver) {
  63. settings = MigrateSettings(from_ver, from_ver + 1, settings);
  64. return MigrateSettings(from_ver + 1, to_ver, settings);
  65. }
  66. const oldsettings = settings;
  67. switch (to_ver) {
  68. default:
  69. settings = Object.assign(DefaultSettings, {});
  70. for (const option in oldsettings) {
  71. if (settings[option] !== undefined) {
  72. settings[option] = MigrateOption(settings[option], oldsettings[option], ["gameId"]);
  73. }
  74. }
  75. }
  76. return settings;
  77. }
  78. };
  79. function MigrateOption(option, oldoption, excludes) {
  80. if (oldoption === undefined) {
  81. oldoption = option;
  82. }
  83. if (Array.isArray(option)) {
  84. for (const key of Object.keys(option)) {
  85. option[key] = MigrateOption(option[key], oldoption[key], excludes);
  86. }
  87. }
  88. if (Object.getPrototypeOf(option) === Object.prototype) {
  89. for (const key of Object.keys(option)) {
  90. if (excludes.includes(key)) {
  91. option[key] = oldoption[key] || null;
  92. } else {
  93. option[key] = MigrateOption(option[key], oldoption[key], excludes);
  94. }
  95. }
  96. }
  97. return option;
  98. }