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.
 
 
 

234 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. else if (i == tLen - 1)
  66. tier = i;
  67. }
  68. if (tier < 0)
  69. tier = 0;
  70. faction.tier = tier;
  71. return tier;
  72. },
  73. getReputation: function(factionId, gain) {
  74. var fullSync = (this.factions[factionId] == null);
  75. var blueprint = this.getBlueprint(factionId);
  76. var faction = this.list.find(l => l.id == factionId);
  77. if (!faction) {
  78. this.list.push({
  79. id: factionId,
  80. rep: blueprint.initialRep,
  81. tier: null
  82. });
  83. faction = this.list[this.list.length - 1];
  84. }
  85. faction.rep += gain;
  86. var oldTier = faction.tier;
  87. this.calculateTier(factionId);
  88. var action = 'gained';
  89. if (gain < 0)
  90. action = 'lost';
  91. this.obj.instance.syncer.queue('onGetMessages', {
  92. id: this.obj.id,
  93. messages: [{
  94. class: 'q1',
  95. message: 'you ' + action + ' ' + Math.abs(gain) + ' reputation with ' + blueprint.name,
  96. type: 'rep'
  97. }]
  98. }, [this.obj.serverId]);
  99. if (faction.tier != oldTier) {
  100. this.sendMessage(blueprint.tiers[faction.tier].name, blueprint.name);
  101. this.obj.equipment.unequipFactionGear(faction.id, faction.tier);
  102. }
  103. this.syncFaction(factionId, fullSync);
  104. },
  105. sendMessage: function(tierName, factionName) {
  106. this.obj.instance.syncer.queue('onGetMessages', {
  107. id: this.obj.id,
  108. messages: [{
  109. class: 'q4',
  110. message: 'you are now ' + tierName + ' with ' + factionName,
  111. type: 'rep'
  112. }]
  113. }, [this.obj.serverId]);
  114. },
  115. discoverFaction(factionId) {
  116. if (this.list.some(l => l.id == factionId))
  117. return;
  118. var fullSync = (this.factions[factionId] == null);
  119. var blueprint = this.getBlueprint(factionId);
  120. this.list.push({
  121. id: factionId,
  122. rep: blueprint.initialRep,
  123. tier: null
  124. });
  125. var tier = blueprint.tiers[this.calculateTier(factionId)].name.toLowerCase();
  126. this.obj.instance.syncer.queue('onGetMessages', {
  127. id: this.obj.id,
  128. messages: [{
  129. class: 'q4',
  130. message: 'you are now ' + tier + ' with ' + blueprint.name,
  131. type: 'rep'
  132. }]
  133. }, [this.obj.serverId]);
  134. this.syncFaction(factionId, fullSync);
  135. },
  136. save: function() {
  137. return {
  138. type: 'reputation',
  139. list: this.list
  140. };
  141. },
  142. simplify: function(self) {
  143. if (!self)
  144. return null;
  145. var sendList = this.list
  146. .map(function(l) {
  147. var result = {};
  148. var blueprint = this.getBlueprint(l.id);
  149. extend(true, result, l, blueprint);
  150. return result;
  151. }, this);
  152. return {
  153. type: 'reputation',
  154. list: sendList
  155. };
  156. },
  157. syncFaction: function(factionId, full) {
  158. var l = this.list.find(l => (l.id == factionId));
  159. var faction = {
  160. id: factionId,
  161. rep: l.rep
  162. };
  163. if (full) {
  164. var blueprint = this.getBlueprint(factionId);
  165. extend(true, faction, l, blueprint);
  166. }
  167. this.obj.syncer.setArray(true, 'reputation', 'modifyRep', faction);
  168. },
  169. events: {
  170. afterKillMob: function(mob) {
  171. if (!mob.mob)
  172. return;
  173. var grantRep = mob.mob.grantRep;
  174. if (!grantRep) {
  175. var deathRep = mob.mob.deathRep;
  176. if (deathRep)
  177. this.getReputation(mob.aggro.faction, deathRep);
  178. return;
  179. }
  180. for (var r in grantRep) {
  181. this.getReputation(r, grantRep[r]);
  182. }
  183. }
  184. }
  185. };
  186. });