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.
 
 
 

178 lines
3.7 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'js/system/globals',
  5. 'html!ui/templates/online/template',
  6. 'css!ui/templates/online/styles',
  7. 'html!ui/templates/online/templateListItem'
  8. ], function (
  9. events,
  10. client,
  11. globals,
  12. template,
  13. styles,
  14. templateListItem
  15. ) {
  16. return {
  17. tpl: template,
  18. centered: true,
  19. onlineList: [],
  20. modal: true,
  21. hasClose: true,
  22. actions: [],
  23. postRender: function () {
  24. globals.onlineList = this.onlineList;
  25. this.onEvent('onGetConnectedPlayer', this.onGetConnectedPlayer.bind(this));
  26. this.onEvent('onGetDisconnectedPlayer', this.onGetDisconnectedPlayer.bind(this));
  27. this.onEvent('onGetSocialActions', this.onGetSocialActions.bind(this));
  28. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  29. this.onEvent('onShowOnline', this.toggle.bind(this));
  30. },
  31. onGetSocialActions: function (actions) {
  32. this.actions = actions;
  33. },
  34. onKeyDown: function (key) {
  35. if (key === 'o')
  36. this.toggle();
  37. },
  38. onAfterShow: function () {
  39. this.build();
  40. },
  41. onGetConnectedPlayer: function (list) {
  42. if (!list.length)
  43. list = [list];
  44. let onlineList = this.onlineList;
  45. list.forEach(function (l) {
  46. let exists = onlineList.find(function (o) {
  47. return (o.name === l.name);
  48. });
  49. if (exists)
  50. $.extend(true, exists, l);
  51. else
  52. onlineList.push(l);
  53. });
  54. onlineList
  55. .sort(function (a, b) {
  56. if (a.level === b.level) {
  57. if (a.name > b.name)
  58. return 1;
  59. return -1;
  60. } return b.level - a.level;
  61. });
  62. if (this.shown)
  63. this.build();
  64. },
  65. onGetDisconnectedPlayer: function (playerName) {
  66. let onlineList = this.onlineList;
  67. onlineList.spliceWhere(function (o) {
  68. return (o.name === playerName);
  69. });
  70. if (this.shown)
  71. this.build();
  72. },
  73. build: function () {
  74. let headingText = this.el.find('.heading-text');
  75. let playerCount = this.onlineList.length;
  76. headingText.html(`online players (${playerCount})`);
  77. let container = this.el.find('.list');
  78. container
  79. .children(':not(.heading)')
  80. .remove();
  81. this.onlineList.forEach(function (l) {
  82. let html = templateListItem
  83. .replace('$NAME$', l.name)
  84. .replace('$LEVEL$', l.level)
  85. .replace('$CLASS$', l.class);
  86. let el = $(html)
  87. .appendTo(container)
  88. .on('contextmenu', this.showContext.bind(this, l));
  89. if (isMobile)
  90. el.on('mousedown', this.showContext.bind(this, l));
  91. }, this);
  92. },
  93. showContext: function (char, e) {
  94. if (char.name !== window.player.name) {
  95. const extraActions = this.actions.map(({ command, text }) => {
  96. return {
  97. text,
  98. callback: this.performAction.bind(this, command, char.name)
  99. };
  100. });
  101. const isBlocked = window.player.social.isPlayerBlocked(char.name);
  102. const actions = [{
  103. text: 'invite to party',
  104. callback: this.invite.bind(this, char.id)
  105. }, {
  106. text: 'whisper',
  107. callback: events.emit.bind(events, 'onDoWhisper', char.name)
  108. }, {
  109. text: isBlocked ? 'unblock' : 'block',
  110. callback: this.block.bind(this, char.name)
  111. }, ...extraActions];
  112. events.emit('onBeforeOnlineListContext', char.id, actions);
  113. events.emit('onContextMenu', actions, e);
  114. }
  115. e.preventDefault();
  116. return false;
  117. },
  118. performAction: function (command, charName) {
  119. client.request({
  120. cpn: 'social',
  121. method: 'chat',
  122. data: {
  123. message: `/${command} ${charName}`
  124. }
  125. });
  126. },
  127. block: function (charName) {
  128. const isBlocked = window.player.social.isPlayerBlocked(charName);
  129. let method = isBlocked ? 'unblock' : 'block';
  130. this.performAction(method, charName);
  131. },
  132. invite: function (charId) {
  133. this.hide();
  134. client.request({
  135. cpn: 'social',
  136. method: 'getInvite',
  137. data: {
  138. targetId: charId
  139. }
  140. });
  141. }
  142. };
  143. });