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.
 
 
 

245 lines
5.0 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 === null) {
  30. console.log('No faction blueprint found');
  31. return;
  32. }
  33. factionBlueprint = extend(true, {}, factionBase, factionBlueprint);
  34. this.factions[factionBlueprint.id] = factionBlueprint;
  35. return factionBlueprint;
  36. },
  37. getTier: function (factionId) {
  38. let faction = this.list.find(l => l.id === factionId);
  39. if (!faction) {
  40. this.discoverFaction(factionId);
  41. faction = this.list.find(l => l.id === factionId);
  42. }
  43. return (faction || {
  44. tier: 3
  45. }).tier;
  46. },
  47. canEquipItem: function (item) {
  48. let factions = item.factions;
  49. let fLen = factions.length;
  50. for (let i = 0; i < fLen; i++) {
  51. let f = factions[i];
  52. if (this.getTier(f.id) < f.tier)
  53. return false;
  54. }
  55. return true;
  56. },
  57. calculateTier: function (factionId) {
  58. let blueprint = this.getBlueprint(factionId);
  59. let faction = this.list.find(l => l.id === factionId);
  60. let rep = faction.rep;
  61. let tier = 0;
  62. let tiers = blueprint.tiers;
  63. let tLen = tiers.length;
  64. for (let i = 0; i < tLen; i++) {
  65. let t = tiers[i];
  66. tier = i - 1;
  67. if (t.rep > rep)
  68. break;
  69. else if (i === tLen - 1)
  70. tier = i;
  71. }
  72. if (tier < 0)
  73. tier = 0;
  74. faction.tier = tier;
  75. return tier;
  76. },
  77. getReputation: function (factionId, gain) {
  78. let fullSync = (this.factions[factionId] === null);
  79. let blueprint = this.getBlueprint(factionId);
  80. let faction = this.list.find(l => l.id === factionId);
  81. if (!faction) {
  82. this.list.push({
  83. id: factionId,
  84. rep: blueprint.initialRep,
  85. tier: null
  86. });
  87. faction = this.list[this.list.length - 1];
  88. }
  89. faction.rep += gain;
  90. let oldTier = faction.tier;
  91. this.calculateTier(factionId);
  92. let action = 'gained';
  93. if (gain < 0)
  94. action = 'lost';
  95. this.obj.instance.syncer.queue('onGetMessages', {
  96. id: this.obj.id,
  97. messages: [{
  98. class: (action === 'gained') ? 'color-greenB' : 'color-redA',
  99. message: 'you ' + action + ' ' + Math.abs(gain) + ' reputation with ' + blueprint.name,
  100. type: 'rep'
  101. }]
  102. }, [this.obj.serverId]);
  103. if (faction.tier !== oldTier) {
  104. this.sendMessage(blueprint.tiers[faction.tier].name, blueprint.name, (faction.tier > oldTier));
  105. this.obj.equipment.unequipFactionGear(faction.id, faction.tier);
  106. }
  107. this.syncFaction(factionId, fullSync);
  108. },
  109. sendMessage: function (tierName, factionName, didIncrease) {
  110. this.obj.instance.syncer.queue('onGetMessages', {
  111. id: this.obj.id,
  112. messages: [{
  113. class: didIncrease ? 'color-greenB' : 'color-redA',
  114. message: 'you are now ' + tierName + ' with ' + factionName,
  115. type: 'rep'
  116. }]
  117. }, [this.obj.serverId]);
  118. },
  119. discoverFaction (factionId) {
  120. if (this.list.some(l => l.id === factionId))
  121. return;
  122. let fullSync = (this.factions[factionId] === null);
  123. let blueprint = this.getBlueprint(factionId);
  124. if (!blueprint)
  125. return;
  126. this.list.push({
  127. id: factionId,
  128. rep: blueprint.initialRep,
  129. tier: null
  130. });
  131. let tier = blueprint.tiers[this.calculateTier(factionId)].name.toLowerCase();
  132. if (!blueprint.noGainRep) {
  133. this.obj.instance.syncer.queue('onGetMessages', {
  134. id: this.obj.id,
  135. messages: [{
  136. class: 'q4',
  137. message: 'you are now ' + tier + ' with ' + blueprint.name,
  138. type: 'rep'
  139. }]
  140. }, [this.obj.serverId]);
  141. }
  142. this.syncFaction(factionId, fullSync);
  143. },
  144. save: function () {
  145. return {
  146. type: 'reputation',
  147. list: this.list
  148. };
  149. },
  150. simplify: function (self) {
  151. if (!self)
  152. return null;
  153. let sendList = this.list
  154. .map(function (l) {
  155. let result = {};
  156. let blueprint = this.getBlueprint(l.id);
  157. extend(true, result, l, blueprint);
  158. return result;
  159. }, this);
  160. return {
  161. type: 'reputation',
  162. list: sendList
  163. };
  164. },
  165. syncFaction: function (factionId, full) {
  166. let l = this.list.find(l => (l.id === factionId));
  167. let faction = {
  168. id: factionId,
  169. rep: l.rep,
  170. tier: l.tier
  171. };
  172. if (full) {
  173. let blueprint = this.getBlueprint(factionId);
  174. extend(true, faction, l, blueprint);
  175. }
  176. this.obj.syncer.setArray(true, 'reputation', 'modifyRep', faction);
  177. },
  178. events: {
  179. afterKillMob: function (mob) {
  180. if (!mob.mob)
  181. return;
  182. let grantRep = mob.mob.grantRep;
  183. if (!grantRep) {
  184. let deathRep = mob.mob.deathRep;
  185. if (deathRep)
  186. this.getReputation(mob.aggro.faction, deathRep);
  187. return;
  188. }
  189. for (let r in grantRep)
  190. this.getReputation(r, grantRep[r]);
  191. }
  192. }
  193. };