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.
 
 
 

244 lines
5.3 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'js/system/globals',
  5. 'js/objects/objects',
  6. 'html!ui/templates/party/template',
  7. 'css!ui/templates/party/styles',
  8. 'html!ui/templates/party/templateInvite',
  9. 'html!ui/templates/party/templatePartyMember',
  10. 'js/config'
  11. ], function (
  12. events,
  13. client,
  14. globals,
  15. objects,
  16. template,
  17. styles,
  18. templateInvite,
  19. templatePartyMember,
  20. config
  21. ) {
  22. return {
  23. tpl: template,
  24. invite: null,
  25. party: null,
  26. postRender: function () {
  27. this.onEvent('onGetInvite', this.onGetInvite.bind(this));
  28. this.onEvent('onGetParty', this.onGetParty.bind(this));
  29. this.onEvent('onPartyDisband', this.onPartyDisband.bind(this));
  30. this.onEvent('onGetConnectedPlayer', this.onGetConnectedPlayer.bind(this));
  31. this.onEvent('onGetPartyStats', this.onGetPartyStats.bind(this));
  32. this.onEvent('onTogglePartyView', this.onTogglePartyView.bind(this));
  33. this.onTogglePartyView(config.partyView);
  34. },
  35. onGetConnectedPlayer: function (msg) {
  36. if (!window.player)
  37. return;
  38. const { party } = this;
  39. const { player: { serverId: playerId, zoneId: playerZone } } = window;
  40. if (!party)
  41. return;
  42. if (!(msg instanceof Array))
  43. msg = [msg];
  44. msg.forEach(m => {
  45. const { id: mId, zoneId: mZone } = m;
  46. if (party.indexOf(m.id) === -1)
  47. return;
  48. if (mId === playerId) {
  49. party.forEach(p => {
  50. const mObj = globals.onlineList.find(o => o.id === p);
  51. let el = this.find('.member[memberId="' + p + '"]');
  52. el.removeClass('differentZone');
  53. if (mObj.zoneId !== mZone)
  54. el.addClass('differentZone');
  55. });
  56. } else {
  57. let el = this.find('.member[memberId="' + m.id + '"]');
  58. el.removeClass('differentZone');
  59. if (m.zoneId !== playerZone)
  60. el.addClass('differentZone');
  61. el.find('.txtLevel').html('level: ' + m.level);
  62. }
  63. });
  64. },
  65. onGetPartyStats: function (id, stats) {
  66. let party = this.party;
  67. if (!party)
  68. return;
  69. let el = this.find('.member[memberId="' + id + '"]');
  70. if (el.length === 0)
  71. return;
  72. if ((stats.hp !== null) && (stats.hpMax !== null)) {
  73. let hpPercentage = Math.min(100, (stats.hp / stats.hpMax) * 100);
  74. el.find('.statHp').css('width', hpPercentage + '%');
  75. }
  76. if ((stats.mana !== null) && (stats.manaMax !== null)) {
  77. let manaPercentage = Math.min((stats.mana / stats.manaMax) * 100, 100);
  78. el.find('.statMana').css('width', manaPercentage + '%');
  79. }
  80. if (stats.level !== null)
  81. el.find('.txtLevel').html('level: ' + stats.level);
  82. },
  83. onPartyDisband: function () {
  84. this.find('.party .list')
  85. .empty();
  86. },
  87. onGetParty: function (party) {
  88. // Destroy invite frame if you join a party
  89. if (this.invite)
  90. this.destroyInvite();
  91. let container = this.find('.party .list')
  92. .empty();
  93. this.party = party;
  94. if (!party)
  95. return;
  96. party.forEach(p => {
  97. if (p === window.player.serverId)
  98. return;
  99. let player = globals.onlineList.find(o => o.id === p);
  100. let playerName = player ? player.name : 'unknown';
  101. let level = 'level: ' + (player ? player.level : '?');
  102. let html = templatePartyMember
  103. .replace('$NAME$', playerName)
  104. .replace('$LEVEL$', level);
  105. let el = $(html)
  106. .appendTo(container)
  107. .attr('memberId', p)
  108. .on('contextmenu', this.showContext.bind(this, playerName, p));
  109. if (player.zoneId !== window.player.zoneId)
  110. el.addClass('differentZone');
  111. //Find stats
  112. let memberObj = objects.objects.find(o => o.serverId === p);
  113. if ((memberObj) && (memberObj.stats))
  114. this.onGetPartyStats(p, memberObj.stats.values);
  115. });
  116. },
  117. showContext: function (charName, id, e) {
  118. events.emit('onContextMenu', [{
  119. text: 'whisper',
  120. callback: events.emit.bind(events, 'onDoWhisper', charName)
  121. }, {
  122. text: 'remove from party',
  123. callback: this.removeFromParty.bind(this, id)
  124. }, {
  125. text: 'leave party',
  126. callback: this.leaveParty.bind(this)
  127. }], e);
  128. e.preventDefault();
  129. return false;
  130. },
  131. onGetInvite: function (sourceId) {
  132. if (this.invite)
  133. this.destroyInvite();
  134. let sourcePlayer = globals.onlineList.find(o => o.id === sourceId);
  135. let html = templateInvite
  136. .replace('$NAME$', sourcePlayer.name);
  137. let el = $(html)
  138. .appendTo(this.el);
  139. el
  140. .find('.btn')
  141. .on('click', this.destroyInvite.bind(this));
  142. this.invite = {
  143. fromId: sourcePlayer.id,
  144. fromName: sourcePlayer.name,
  145. el: el
  146. };
  147. },
  148. destroyInvite: function (e) {
  149. if (e) {
  150. if ($(e.target).hasClass('btnAccept'))
  151. this.acceptInvite();
  152. else
  153. this.declineInvite();
  154. }
  155. this.invite.el.remove();
  156. this.invite = null;
  157. events.emit('onUiHover', false);
  158. },
  159. acceptInvite: function () {
  160. client.request({
  161. cpn: 'social',
  162. method: 'acceptInvite',
  163. data: {
  164. targetId: this.invite.fromId
  165. }
  166. });
  167. },
  168. declineInvite: function () {
  169. client.request({
  170. cpn: 'social',
  171. method: 'declineInvite',
  172. data: {
  173. targetId: this.invite.fromId
  174. }
  175. });
  176. },
  177. removeFromParty: function (id) {
  178. client.request({
  179. cpn: 'social',
  180. method: 'removeFromParty',
  181. data: {
  182. id
  183. }
  184. });
  185. },
  186. leaveParty: function () {
  187. client.request({
  188. cpn: 'social',
  189. method: 'leaveParty'
  190. });
  191. },
  192. onTogglePartyView: function (state) {
  193. this.el.removeClass('full compact minimal');
  194. this.el.addClass(state);
  195. }
  196. };
  197. });