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.
 
 
 

232 line
4.7 KiB

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