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.
 
 
 

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