No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

234 líneas
4.6 KiB

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