Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

214 linhas
4.2 KiB

  1. //Helpers
  2. const { getFactionBlueprint } = require('../config/factions/helpers');
  3. //Component
  4. module.exports = {
  5. type: 'reputation',
  6. list: [],
  7. factions: {},
  8. init: function (blueprint) {
  9. let list = ((blueprint || {}).list || []);
  10. delete blueprint.list;
  11. list.forEach(function (l) {
  12. let bpt = getFactionBlueprint(l.id);
  13. if (!bpt)
  14. return;
  15. this.list.push({
  16. id: l.id,
  17. rep: l.rep,
  18. tier: null
  19. });
  20. this.calculateTier(l.id);
  21. }, this);
  22. },
  23. getTier: function (factionId) {
  24. let faction = this.list.find(l => l.id === factionId);
  25. if (!faction) {
  26. this.discoverFaction(factionId);
  27. faction = this.list.find(l => l.id === factionId);
  28. }
  29. return faction?.tier ?? 3;
  30. },
  31. canEquipItem: function (item) {
  32. let itemFactions = item.factions;
  33. let fLen = itemFactions.length;
  34. for (let i = 0; i < fLen; i++) {
  35. let f = itemFactions[i];
  36. if (this.getTier(f.id) < f.tier)
  37. return false;
  38. }
  39. return true;
  40. },
  41. calculateTier: function (factionId) {
  42. let blueprint = getFactionBlueprint(factionId);
  43. let faction = this.list.find(l => l.id === factionId);
  44. let rep = faction.rep;
  45. let tier = 0;
  46. let tiers = blueprint.tiers;
  47. let tLen = tiers.length;
  48. for (let i = 0; i < tLen; i++) {
  49. let t = tiers[i];
  50. tier = i - 1;
  51. if (t.rep > rep)
  52. break;
  53. else if (i === tLen - 1)
  54. tier = i;
  55. }
  56. if (tier < 0)
  57. tier = 0;
  58. faction.tier = tier;
  59. return tier;
  60. },
  61. getReputation: function (factionId, gain) {
  62. let fullSync = false;
  63. let blueprint = getFactionBlueprint(factionId);
  64. let faction = this.list.find(l => l.id === factionId);
  65. if (!faction) {
  66. fullSync = true;
  67. this.list.push({
  68. id: factionId,
  69. rep: blueprint.initialRep,
  70. tier: null
  71. });
  72. faction = this.list[this.list.length - 1];
  73. }
  74. faction.rep += gain;
  75. let oldTier = faction.tier;
  76. this.calculateTier(factionId);
  77. let action = 'gained';
  78. if (gain < 0)
  79. action = 'lost';
  80. this.obj.social.notifySelf({
  81. className: (action === 'gained') ? 'color-greenB' : 'color-redA',
  82. message: 'you ' + action + ' ' + Math.abs(gain) + ' reputation with ' + blueprint.name,
  83. type: 'rep'
  84. });
  85. if (faction.tier !== oldTier) {
  86. this.sendMessage(blueprint.tiers[faction.tier].name, blueprint.name, (faction.tier > oldTier));
  87. this.obj.equipment.unequipFactionGear(faction.id, faction.tier);
  88. }
  89. this.syncFaction(factionId, fullSync);
  90. },
  91. sendMessage: function (tierName, factionName, didIncrease) {
  92. this.obj.social.notifySelf({
  93. className: didIncrease ? 'color-greenB' : 'color-redA',
  94. message: 'you are now ' + tierName + ' with ' + factionName,
  95. type: 'rep'
  96. });
  97. },
  98. discoverFaction (factionId) {
  99. if (this.list.some(l => l.id === factionId))
  100. return;
  101. let blueprint = getFactionBlueprint(factionId);
  102. if (!blueprint)
  103. return;
  104. this.list.push({
  105. id: factionId,
  106. rep: blueprint.initialRep,
  107. tier: null
  108. });
  109. let tier = blueprint.tiers[this.calculateTier(factionId)].name.toLowerCase();
  110. if (!blueprint.noGainRep) {
  111. this.obj.social.notifySelf({
  112. className: 'q4',
  113. message: 'you are now ' + tier + ' with ' + blueprint.name,
  114. type: 'rep'
  115. });
  116. }
  117. this.syncFaction(factionId, true);
  118. },
  119. save: function () {
  120. return {
  121. type: 'reputation',
  122. list: this.list
  123. };
  124. },
  125. simplify: function (self) {
  126. if (!self)
  127. return null;
  128. let sendList = this.list
  129. .map(function (l) {
  130. let result = {};
  131. let blueprint = getFactionBlueprint(l.id);
  132. extend(result, l, blueprint);
  133. return result;
  134. }, this);
  135. return {
  136. type: 'reputation',
  137. list: sendList
  138. };
  139. },
  140. syncFaction: function (factionId, full) {
  141. let l = this.list.find(f => (f.id === factionId));
  142. let faction = {
  143. id: factionId,
  144. rep: l.rep,
  145. tier: l.tier
  146. };
  147. if (full) {
  148. let blueprint = getFactionBlueprint(factionId);
  149. extend(faction, l, blueprint);
  150. }
  151. this.obj.syncer.setArray(true, 'reputation', 'modifyRep', faction);
  152. },
  153. events: {
  154. afterKillMob: function (mob) {
  155. if (!mob.mob)
  156. return;
  157. let grantRep = mob.mob.grantRep;
  158. if (!grantRep) {
  159. let deathRep = mob.mob.deathRep;
  160. if (deathRep)
  161. this.getReputation(mob.aggro.faction, deathRep);
  162. return;
  163. }
  164. for (let r in grantRep)
  165. this.getReputation(r, grantRep[r]);
  166. }
  167. }
  168. };