Advertisement
musaad-sa

Untitled

Apr 19th, 2021
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Tribe Members Incoming Overview
  3. // @description Adds a nice overview of all tribe members receiving incoming attacks.
  4. // @author Devilicious#9733 (discord)
  5. // @version 1.0
  6. // @grant none
  7. // @Ticket t13598530
  8. // @Gekeurd op 27-12-2019
  9. // @include https://*.tribalwars.ae*screen=ally&mode=members
  10. // @updateURL https://devilicious.dev/api/tw/scripts/user/tribe_overview_incomings.user.js
  11. // @downloadURL https://devilicious.dev/api/tw/scripts/user/tribe_overview_incomings.user.js
  12. // ==/UserScript==
  13.  
  14. /**
  15. Change log
  16. * 0.1 - Initial script
  17. * 0.2 - Support keuring
  18. * 0.3 - Collapsable incomings per member
  19. * 0.4 - Reworked table
  20. * 1.0 - Used a different screen for tribe leaders only. Script will now show units that are on their way.
  21. **/
  22.  
  23. const settings = {
  24. 'tribe_leader': {
  25. body_selector: 'tbody > tr:odd',
  26. show_next_row: true,
  27. url: 'members_defense'
  28. },
  29. 'normal': {
  30. body_selector: 'tbody > tr:not(:first)',
  31. show_next_row: false,
  32. url: 'members_troops'
  33. }
  34. }
  35.  
  36. if (window.location.href.indexOf('screen=ally&mode=members') < 0) {
  37. window.location.assign(game_data.link_base_pure + "ally&mode=members");
  38. }
  39.  
  40. if (typeof window.$.twAjax === 'undefined') {
  41. window.$.twAjax = (function () {
  42. let Ajax = function (options, promise) {
  43. this.options = options;
  44. this.promise = promise;
  45. };
  46.  
  47. let Queue = (() => {
  48. let Queue = function () {
  49. this.list = [];
  50. this.working = false;
  51. this.length = 0;
  52. };
  53.  
  54. Queue.prototype.doNext = function () {
  55. let item = this.dequeue(),
  56. self = this;
  57.  
  58. $.ajax(item.options).done(function () {
  59. item.promise.resolve.apply(null, arguments);
  60. self.start();
  61. }).fail(function () {
  62. item.promise.reject.apply(null, arguments);
  63. self.start();
  64. });
  65. };
  66.  
  67. Queue.prototype.start = function () {
  68. if (this.length) {
  69. this.working = true;
  70. this.doNext();
  71. } else {
  72. this.working = false;
  73. }
  74. };
  75.  
  76. Queue.prototype.dequeue = function () {
  77. this.length -= 1;
  78. return this.list.shift();
  79. };
  80.  
  81. Queue.prototype.enqueue = function (item) {
  82. this.list.push(item);
  83. this.length += 1;
  84.  
  85. if (!this.working) {
  86. this.start();
  87. }
  88. };
  89.  
  90. return Queue;
  91. })();
  92.  
  93. let orchestrator = (() => {
  94. // Create 5 queues to distribute requests on
  95. let queues = (() => {
  96. const needed = 5;
  97. let arr = [];
  98.  
  99. for (let i = 0; i < needed; i++) {
  100. arr[i] = new Queue();
  101. }
  102.  
  103. return arr;
  104. })();
  105.  
  106. return (item) => {
  107. let leastBusyQueue = queues.map(q => q.length).reduce((next, curr) => (curr < next) ? curr : next, 0);
  108. queues[leastBusyQueue].enqueue(item);
  109. };
  110. })();
  111.  
  112. return function (options) {
  113. let promise = $.Deferred(),
  114. item = new Ajax(options, promise);
  115.  
  116. orchestrator(item);
  117.  
  118. return promise;
  119. };
  120. })();
  121. }
  122.  
  123. const getPlayerOverviewHtml = (playerId, url) => $.twAjax({url: `${game_data.link_base_pure}ally&mode=${url}&player_id=${playerId}`});
  124. const showErrorNotEnoughRights = (element) => element.append('<td class="lit-item" style="text-align: center"><img src="graphic/error.png" title="Deze speler heeft niet de juiste permissie gegeven om incomings te laten zien."></td>');
  125.  
  126. function insertIncomingAttacksPerPlayer(player, element) {
  127. const role = $('#ally_content tr.selected').find(`td:eq(${$('.lead').closest('th').index()}) img[src*="green.png"]`).length > 0 ? 'tribe_leader' : 'normal';
  128.  
  129. return $.when(getPlayerOverviewHtml(player.id, settings[role].url)).done(function (html) {
  130. const overviewTable = $(html).find('#ally_content a[href*="screen=info_village"]').first().closest('table');
  131. if (overviewTable.find('tr:first-child th').length > 1) { //Check if at least 2 columns exist. Otherwise user has not given permission to show incoming attacks.
  132. const incomingsAmountNumber = overviewTable.find('tbody > tr:first-child > th:last-child').text().trim().match(/\d+/g);
  133. if (incomingsAmountNumber) {
  134. const incomingsAmount = incomingsAmountNumber.map(Number);
  135. if (incomingsAmount > 0) {
  136. element.after('</br>');
  137. overviewTable.find(settings[role].body_selector).each(function () {
  138. const incomingPerVillageAmount = $(this).find('td:last-child');
  139. if (incomingPerVillageAmount.text().trim() !== '0') {
  140. const clonedRow = incomingPerVillageAmount.css({
  141. background: "#c1a264",
  142. color: "black",
  143. }).parent().clone().prop('class', `incomings_${player.id}_village`).hide();
  144. if (settings[role].show_next_row) {
  145. const nextRow = $(this).next().clone().prop('class', `incomings_${player.id}_troops`).hide();
  146. element.after(nextRow);
  147. }
  148. element.after(clonedRow);
  149. }
  150. });
  151. const clonedHeader = overviewTable.find('tbody > tr:first-child').clone().prop('id', `incomings_${player.id}_list`);
  152. clonedHeader.find('th:last-child').html('<img src="graphic/unit/att.png" alt="">');
  153. element.after(clonedHeader.hide());
  154. $('#tribe_overview_incomings_table tbody').append(
  155. `<tr>
  156. <td valign="top" width="70%">
  157. ${$(`a[href*="screen=info_player&id=${player.id}"]`).closest('td').html()}
  158. </td>
  159. <td valign="top" width="30%">
  160. <img src="graphic/unit/att.png" title="Binnenkomende aanvallen" style="vertical-align: -2px" alt="" class="">
  161. <strong><a style="cursor: pointer" id="incomings_${player.id}_totalAmount" title="Click on this to collapse incomings">${incomingsAmount}</a></strong>
  162. </td>
  163. </tr>`);
  164. $('#tribe_overview_incomings_table').parent().closest('table').show();
  165. }
  166. element.append(`<td class="lit-item"><strong>(${incomingsAmount})</strong></td>`);
  167. const incomingsAmountElement = `#incomings_${player.id}_totalAmount`;
  168. $(incomingsAmountElement).click(function () {
  169. $(`#incomings_${player.id}_list, .incomings_${player.id}_village, .incomings_${player.id}_troops`).toggle();
  170. });
  171. } else {
  172. showErrorNotEnoughRights(element);
  173. }
  174. } else {
  175. showErrorNotEnoughRights(element);
  176. }
  177. });
  178. }
  179.  
  180. const tribeMembersTable = $('#content_value a[href*="screen=info_player"]').first().closest('table');
  181. tribeMembersTable.find('tr').first().append('<th><img src="graphic/unit/att.png" alt=""></th>')
  182. tribeMembersTable.find('.row_a, .row_b').each(function () {
  183. insertIncomingAttacksPerPlayer({
  184. id: $(this).find('a').first().attr('href').match(/id=(\d+)/)[1],
  185. name: $(this).find('a').first().text()
  186. }, $(this));
  187. });
  188.  
  189.  
  190. let overviewHtml = `<table width="50%" cellspacing="0" cellpadding="0">
  191. <tbody>
  192. <tr>
  193. <td width="40%" valign="top">
  194. <div class="vis">
  195. <h4><img src="graphic/unit/att.png" title="Binnenkomende aanvallen" style="vertical-align: -2px" alt="" class="">Tribe overview incomings <img src="graphic/unit/att.png" title="Binnenkomende aanvallen" style="vertical-align: -2px" alt="" class=""></h4>
  196. <table id="tribe_overview_incomings_table" width="100%" class="vis">
  197. <tbody>
  198. </tbody>
  199. </table>
  200. </div>
  201. </td>
  202. </tr>
  203. </tbody></table>`;
  204. $('#ally_content').before(overviewHtml);
  205. $('#tribe_overview_incomings_table').parent().closest('table').hide();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement