您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

251 行
5.1 KiB

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