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.
 
 
 

184 lines
4.1 KiB

  1. define([
  2. 'items/generator',
  3. 'config/skins'
  4. ], function(
  5. generator,
  6. skins
  7. ) {
  8. return {
  9. items: {},
  10. cdMax: 10,
  11. blueprint: null,
  12. init: function(blueprint) {
  13. this.faction = blueprint.faction;
  14. this.blueprint = blueprint;
  15. },
  16. getItems: function(requestedBy) {
  17. var name = requestedBy.name;
  18. var requestLevel = requestedBy.stats.values.level;
  19. var list = this.items[name];
  20. if (!list) {
  21. list = {
  22. items: [],
  23. level: requestLevel,
  24. cd: this.cdMax
  25. };
  26. this.items[name] = list;
  27. this.regenList(list);
  28. } else if ((list.level != requestLevel) || (Math.random() < 2))
  29. this.regenList(list);
  30. var reputation = requestedBy.reputation;
  31. var result = list.items
  32. .map(function(i) {
  33. var item = extend(true, {}, i);
  34. if (item.effects) {
  35. item.stats = {
  36. stats: '???'
  37. };
  38. item.quality = 0;
  39. item.name = item.type;
  40. item.effects = item.effects
  41. .map(e => ({
  42. factionId: e.factionId,
  43. text: e.text,
  44. properties: e.properties
  45. }));
  46. }
  47. if (item.factions) {
  48. item.factions = item.factions.map(function(f) {
  49. var faction = reputation.getBlueprint(f.id);
  50. var factionTier = reputation.getTier(f.id);
  51. var noEquip = null;
  52. if (factionTier < f.tier)
  53. noEquip = true;
  54. return {
  55. name: faction.name,
  56. tier: f.tier,
  57. tierName: ['Hated', 'Hostile', 'Unfriendly', 'Neutral', 'Friendly', 'Honored', 'Revered', 'Exalted'][f.tier],
  58. noEquip: noEquip
  59. };
  60. }, this);
  61. }
  62. return item;
  63. });
  64. return result;
  65. },
  66. regenList: function(list) {
  67. var blueprint = this.blueprint;
  68. list.items = null;
  69. list.items = [];
  70. var faction = require('./config/factions/' + blueprint.faction.id);
  71. var statGenerator = faction.uniqueStat;
  72. var itemCount = blueprint.items.min + ~~(Math.random() * (blueprint.items.max - blueprint.items.min));
  73. for (var i = 0; i < itemCount; i++) {
  74. var minLevel = Math.max(1, list.level * 0.75);
  75. var maxLevel = list.level * 1.25;
  76. var level = ~~(minLevel + (Math.random() * (maxLevel - minLevel)));
  77. var item = generator.generate({
  78. noSpell: true,
  79. magicFind: 150,
  80. level: level
  81. });
  82. var randomQuality = ~~(Math.random() * 5);
  83. item.worth = Math.pow(item.level, 1.5) + (Math.pow((randomQuality + 1), 2) * 10)
  84. var id = 0;
  85. list.items.forEach(function(checkItem) {
  86. if (checkItem.id >= id)
  87. id = checkItem.id + 1;
  88. });
  89. item.id = id;
  90. generator.removeStat(item);
  91. statGenerator.generate(item);
  92. item.factions = [{}];
  93. item.factions[0].id = blueprint.faction.id;
  94. item.factions[0].tier = blueprint.faction.tier;
  95. list.items.push(item);
  96. }
  97. var extra = blueprint.items.extra;
  98. if (!extra)
  99. return;
  100. var eLen = extra.length;
  101. for (var i = 0; i < eLen; i++) {
  102. var e = extra[i];
  103. var item = extend(true, {}, e);
  104. if (item.type == 'skin') {
  105. var skinBlueprint = skins.getBlueprint(item.id);
  106. item.name = skinBlueprint.name;
  107. item.sprite = skinBlueprint.sprite;
  108. }
  109. list.items.push(item);
  110. }
  111. },
  112. canBuy: function(itemId, requestedBy, action) {
  113. var item = null;
  114. if (action == 'buy')
  115. item = this.findItem(itemId, requestedBy.name);
  116. else if (action == 'buyback')
  117. item = this.findBuyback(itemId, requestedBy.name);
  118. var result = true;
  119. if (item.faction)
  120. result = requestedBy.reputation.canEquipItem(item);
  121. if (!result) {
  122. requestedBy.instance.syncer.queue('onGetMessages', {
  123. id: requestedBy.id,
  124. messages: [{
  125. class: 'q0',
  126. message: `your reputation is too low to buy that item`,
  127. type: 'info'
  128. }]
  129. }, [requestedBy.serverId]);
  130. }
  131. return result;
  132. },
  133. findItem: function(itemId, sourceName) {
  134. var list = this.items[sourceName];
  135. if (!list)
  136. return null;
  137. return list.items.find(i => i.id == itemId);
  138. },
  139. removeItem: function(itemId, sourceName) {
  140. var list = this.items[sourceName];
  141. if (!sourceName)
  142. return null;
  143. return list.items.spliceFirstWhere(i => i.id == itemId);
  144. }
  145. };
  146. });