Advertisement
Guest User

zzzzzz

a guest
Feb 29th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.38 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Sumowanie przybywającego wsparcia
  3. // @description Skrypt sumuje wsparcie z nadchodzących własnych komend i komend współplemieńców
  4. // @author stivens, Makak
  5. // @include https://pl*.plemiona.pl/game.php?*screen=overview
  6. // @include https://pl*.plemiona.pl/game.php?*screen=overview&*
  7. // @include https://pl*.plemiona.pl/game.php?*screen=info_village*
  8. // @version 1.1.2
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. var gui_content = '';
  17. var doing_ajax = false;
  18. var game_data = TribalWars.getGameData();
  19. var world = game_data.world;
  20. var storage_name = 'commands_script_' + world;
  21. var current_village_id = game_data.village.id;
  22. var village_id = current_village_id;
  23. var commands_selector = '#commands_incomings tr';
  24. var wall_level = game_data.village.buildings.wall;
  25. var t = (game_data.player.sitter !== '0') ? '&t=' + game_data.player.id : '';
  26.  
  27. if (game_data.screen === 'info_village') {
  28. village_id = window.location.href.match(/&id=(\d+)[^\d]?.*$/)[1];
  29. commands_selector = '#commands_incomings tr, #commands_outgoings tr';
  30. wall_level = 20;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. function create_gui() {
  38. var filter_data = load_filter_data_for_village(village_id);
  39.  
  40. gui_content = `<table class="vis" style="width:100%">
  41. <tr style="height: 25px">
  42. <td colspan="4" id="script_table_header"></td>
  43. </tr>
  44. <tr id="script_table">
  45. <td><input type="radio" name="filter" id="commands_script_filter_false" checked> Pokaż wszystko</td>
  46. <td><input type="radio" name="filter" id="commands_script_filter">Pokaż co wejdzie do podanej daty</td>
  47. <td><input type="text" id="filter_date" size="5" value="` + filter_data.date + `"></td>
  48. <td><input type="text" id="filter_hour"size="8" value="` + filter_data.time + `"></td>
  49. </tr>
  50. </table>
  51. <div id="commands_script_units"></div>
  52. <hr>
  53. <p style="text-align:right;margin:3px !important;"><a id="commands_script_simulator_link" href="" target="_blank">Symulator</a></p>`;
  54.  
  55. var gui;
  56. if (game_data.screen == 'info_village') {
  57. gui = $('<div>');
  58. $(gui).attr('id', 'commands_script');
  59. $(gui).css('margin-bottom', '30px');
  60. $(gui).html(gui_content);
  61. $('th:contains(Notatki)').parent().parent().parent().parent().prepend((gui));
  62. $('#script_table_header').parent().before('<tr><th colspan="4">Sumowanie przybywającego wsparcia</th></tr>');
  63. } else {
  64. gui = $('#show_incoming_units').clone();
  65. $(gui).attr('id', 'commands_script');
  66. $(gui).find('.head').text('Sumowanie przybywającego wsparcia');
  67. $(gui).find('table').remove();
  68. $(gui).find('.widget_content').attr('style', 'padding:3px');
  69. $(gui).find('.widget_content').html(gui_content);
  70. $('#show_incoming_units').before(gui);
  71. }
  72.  
  73.  
  74. if (get_commands_count(-1, true, true) === 0) {
  75. $('#commands_script').hide();
  76. }
  77.  
  78. $('input[type=radio]').change(function() {
  79. refresh_gui();
  80. });
  81.  
  82. $('#filter_date, #filter_hour').on('focusout', function() {
  83. refresh_gui();
  84. });
  85.  
  86. $('body').on('click', '#script_table_header', function (){
  87. update_commands_data(get_filter_timestamp());
  88. toggle_load_button();
  89. });
  90. }
  91.  
  92. function refresh_gui() {
  93. var timestamp = get_filter_timestamp();
  94. if (timestamp !== -1) {
  95. update_filter_data(village_id, $('#filter_date').val().trim(), $('#filter_hour').val().trim(), timestamp);
  96. }
  97.  
  98. var own_commands_troops = get_troops_by_commands_ids(get_commands_ids(timestamp, true, false));
  99. var ally_commands_troops = get_troops_by_commands_ids(get_commands_ids(timestamp, false, true));
  100. var in_village_troops = get_in_village_troops();
  101. var all_troops = sum_troops_objects(sum_troops_objects(own_commands_troops, ally_commands_troops), in_village_troops);
  102. update_units_table(own_commands_troops, ally_commands_troops, in_village_troops, all_troops);
  103.  
  104. $('#commands_script_units table td').css('background-color', 'rgb(244, 228, 188)');
  105. $('#commands_script_simulator_link').attr('href', get_simulator_link(all_troops, wall_level));
  106.  
  107. toggle_load_button();
  108. }
  109.  
  110. function create_units_table(units) {
  111. var header = '', incoming_own = '', incoming_allies = '', in_village = '', all = '';
  112. for (var unit in units) {
  113. if (units.hasOwnProperty(unit)) {
  114. header += '<th style="text-align:center"><img src="' + image_base + 'unit/unit_' + unit + '.png"></th>';
  115. incoming_own += '<td style="text-align:center"><span id="commands_script_own_' + unit + '">' + 0 + '</span></td>';
  116. incoming_allies += '<td style="text-align:center"><span id="commands_script_ally_' + unit + '">' + 0 + '</span></td>';
  117. in_village += '<td style="text-align:center"><span id="commands_script_in_village_' + unit + '">' + 0 + '</span></td>';
  118. all += '<td style="text-align:center"><span id="commands_script_all_' + unit + '">' + 0 + '</span></td>';
  119. }
  120. }
  121.  
  122. return `<table style="width:100%">
  123. <tr><th>Typ wojsk</th>` + header + `</tr>
  124. <tr><td>Własne komendy</td>` + incoming_own + `</tr>
  125. <tr><td>Cudze komendy</td>` + incoming_allies + `</tr>
  126. <tr><td>W wiosce</td>` + in_village + `</tr>
  127. <tr><td>Razem</td>` + all + `</tr>
  128. </table>`;
  129. }
  130.  
  131. function update_units_table(own_commands, allies_commands, in_village, all) {
  132. var unit;
  133. var n = 0;
  134. for (unit in all) {
  135. if (all.hasOwnProperty(unit)) {
  136. n++;
  137. }
  138. }
  139.  
  140. if ($('#commands_script_units th img').length !== n) {
  141. $('#commands_script_units').html(create_units_table(all));
  142. } else {
  143. $('#commands_script_units span').text('0');
  144. }
  145.  
  146.  
  147. for (unit in own_commands) {
  148. if (own_commands.hasOwnProperty(unit)) {
  149. $('#commands_script_own_' + unit).text(own_commands[unit]);
  150. }
  151. }
  152. for (unit in allies_commands) {
  153. if (allies_commands.hasOwnProperty(unit)) {
  154. $('#commands_script_ally_' + unit).text(allies_commands[unit]);
  155. }
  156. }
  157. for (unit in in_village) {
  158. if (in_village.hasOwnProperty(unit)) {
  159. $('#commands_script_in_village_' + unit).text(in_village[unit]);
  160. }
  161. }
  162. for (unit in all) {
  163. if (all.hasOwnProperty(unit)) {
  164. $('#commands_script_all_' + unit).text(all[unit]);
  165. }
  166. }
  167. }
  168.  
  169. function toggle_load_button() {
  170. var timestamp = get_filter_timestamp();
  171. var total_commands_count = get_commands_count(timestamp, true, true);
  172. var unknown_commands_count = get_unknown_commands_count(timestamp, true, true);
  173.  
  174. if (total_commands_count === unknown_commands_count && total_commands_count > 0) {
  175. $('#script_table_header').html('<button type="button" style="width:100%">Kliknij aby wczytać komendy</button>').css('text-align', 'center');
  176. }
  177. else if (unknown_commands_count > 0) {
  178. $('#script_table_header').html('<button type="button" style="width:100%">Kliknij aby wczytać ' + get_unknown_commands_count(timestamp, true, true) + ' nieznane komendy</button>').css('text-align', 'center');
  179. }
  180. else if (unknown_commands_count === 0) {
  181. gui_content = 'Wczytano wszystkie komendy<br>' + gui_content;
  182. $('#script_table_header').html('<span style="text-align-center; width:100%;">Wczytano wszystkie komendy</span>').css('text-align', 'center');
  183. }
  184. }
  185.  
  186. function generate_links_to_filter() {
  187. if (get_commands_count(-1, true, true) !== 0) {
  188. var button = '<td><span class="small command_script_filterbutton" style="cursor: pointer; display: block; width: 16px; height: 22px; overflow: hidden; background: transparent url(' + image_base + '/index/group-jump.png) no-repeat;"><img src="' + image_base + '/blank-16x22.png" alt="Wprowadź czas tej komendy do filtra" title="Wprowadź czas tej komendy do filtra"></span></td>';
  189.  
  190. $('#commands_outgoings tr:first th:last, #commands_incomings tr:first th:last').after('<th></th>');
  191. $('#ignored_commands_bar th:first').after('<th></th>');
  192. var inc_commands = $('#commands_incomings .no_ignored_command, #commands_outgoings .command-row');
  193. $(inc_commands).each(function() {
  194. $(this).find('td:last').after(button);
  195. $(this).find('.command_script_filterbutton').data('time', $(this).find('td:not(:first-child):first').text());
  196. });
  197.  
  198.  
  199. $('.command_script_filterbutton').on('click', function() {
  200. $('#commands_script_filter_false').prop('checked', true);
  201.  
  202. var date_parts = string_to_date_parts($(this).data('time'));
  203. $('#filter_date').val(format_number(date_parts.day) + '.' + format_number(date_parts.month));
  204. $('#filter_hour').val(format_number(date_parts.hour) + ':' + format_number(date_parts.minute) + ':' + format_number(date_parts.second));
  205.  
  206. $('#commands_script_filter').prop('checked', true);
  207. $('#commands_script_filter').trigger('change');
  208.  
  209. UI.SuccessMessage('Zmieniono datę filtrowania komend', 500);
  210. return false;
  211. });
  212. }
  213. }
  214.  
  215. function load_units_popup_cache() {
  216. var commands = get_commands_data_by_ids(get_commands_ids(-1, true, true));
  217. for (var command_id in commands) {
  218. if (commands.hasOwnProperty(command_id)) {
  219. window.Command.details_cache[command_id] = commands[command_id].original_object;
  220. }
  221. }
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. function load_commands_list() {
  229. if (localStorage.getItem(storage_name) !== null) {
  230. return JSON.parse(localStorage.getItem(storage_name));
  231. }
  232. return {};
  233. }
  234.  
  235. function save_commands_list(list) {
  236. localStorage.setItem(storage_name, JSON.stringify(list));
  237. }
  238.  
  239. function remove_old_commands() {
  240. var current_timestamp = get_current_timestamp();
  241. var list = load_commands_list();
  242.  
  243. for (var command_id in list) {
  244. if (list.hasOwnProperty(command_id)) {
  245. if (list[command_id].timestamp < current_timestamp) {
  246. delete list[command_id];
  247. }
  248. }
  249. }
  250.  
  251. save_commands_list(list);
  252. }
  253.  
  254. function add_command_to_list(command_id, command) {
  255. var list = load_commands_list();
  256. list[command_id] = command;
  257. save_commands_list(list);
  258. }
  259.  
  260.  
  261.  
  262.  
  263.  
  264. function load_filter_data() {
  265. if (localStorage.getItem(storage_name + '_filter') !== null) {
  266. return JSON.parse(localStorage.getItem(storage_name + '_filter'));
  267. }
  268. return {};
  269. }
  270.  
  271. function save_filter_data(data) {
  272. localStorage.setItem(storage_name + '_filter', JSON.stringify(data));
  273. }
  274.  
  275. function remove_old_filter_data() {
  276. var current_timestamp = get_current_timestamp();
  277. var data = load_filter_data();
  278.  
  279. for (var village_id in data) {
  280. if (data.hasOwnProperty(village_id)) {
  281. if (data[village_id].timestamp < current_timestamp) {
  282. delete data[village_id];
  283. }
  284. }
  285. }
  286.  
  287. save_filter_data(data);
  288. }
  289.  
  290. function update_filter_data(village_id, input_date, input_time, timestamp) {
  291. var data = load_filter_data();
  292. data[village_id] = {date: input_date, time: input_time, timestamp: timestamp};
  293. save_filter_data(data);
  294. }
  295.  
  296. function load_filter_data_for_village(village_id) {
  297. var data = load_filter_data();
  298.  
  299. if (data.hasOwnProperty(village_id)) {
  300. return {date: data[village_id].date, time: data[village_id].time};
  301. } else {
  302. var now = new Date();
  303. return {date: now.getDate() + '.' + (now.getMonth() + 1), time: '23:59:59'};
  304. }
  305. }
  306.  
  307.  
  308.  
  309.  
  310.  
  311. function get_current_timestamp() {
  312. return Math.floor(Date.now() / 1000);
  313. }
  314.  
  315. function string_to_date_parts(date) {
  316. var day = 0, month = 0, hour = 0, minute = 0, second = 0;
  317.  
  318. date = date.trim().match(/^(dzisiaj|jutro|dnia \d{2}\.\d{2}\.) o (\d{2}):(\d{2}):(\d{2}):(\d{3})$/);
  319. if (date !== null) {
  320. if (date[1] === 'dzisiaj') {
  321. var today = new Date();
  322. day = today.getDate();
  323. month = today.getMonth() + 1;
  324. } else if (date[1] === 'jutro') {
  325. var tomorrow = new Date();
  326. tomorrow.setDate(tomorrow.getDate() + 1);
  327. day = tomorrow.getDate();
  328. month = tomorrow.getMonth() + 1;
  329. } else {
  330. var m = date[1].match(/(\d{2})\.(\d{2})/);
  331. day = m[1];
  332. month = m[2];
  333. }
  334.  
  335. hour = date[2];
  336. minute = date[3];
  337. second = date[4];
  338. }
  339.  
  340. return {day: Number(day), month: Number(month), hour: Number(hour), minute: Number(minute), second: Number(second)};
  341. }
  342.  
  343. function date_parts_to_timestamp(date_parts) {
  344. var year = new Date().getFullYear();
  345. return Math.floor(new Date(year, date_parts.month - 1, date_parts.day, date_parts.hour, date_parts.minute, date_parts.second).getTime() / 1000);
  346. }
  347.  
  348. function string_to_timestamp(date) {
  349. return date_parts_to_timestamp(string_to_date_parts(date));
  350. }
  351.  
  352. function get_filter_timestamp() {
  353. if ($('#commands_script_filter').is(':checked')) {
  354. var input_date = $('#filter_date').val().trim().match(/^(\d{1,2})\.(\d{1,2})$/);
  355. var input_time = $('#filter_hour').val().trim().match(/^(\d{1,2}):(\d{1,2}):(\d{1,2})$/);
  356.  
  357. if (input_date === null || input_time === null) {
  358. UI.ErrorMessage('Podano datę lub czas w złym formacie (dd.mm jako data, hh:mm:ss jako czas)', 5000);
  359. } else if (input_date[1] > 31 || input_date[2] > 12 || input_time[1] > 23 || input_time[2] > 59 || input_time[3] > 59) {
  360. UI.ErrorMessage('Podano nieprawidłową datę lub czas (wpisano np. 70 w liczbę minut lub inny tego typu błąd)', 5000);
  361. } else {
  362. var timestamp = date_parts_to_timestamp({day: input_date[1], month: input_date[2], hour: input_time[1], minute: input_time[2], second: input_time[3]});
  363.  
  364. if (timestamp < get_current_timestamp()) {
  365. UI.ErrorMessage('Podano niepoprawnę datę i czas (podano datę/czas w przeszłości)', 5000);
  366. } else {
  367. return timestamp;
  368. }
  369. }
  370. }
  371.  
  372. $('#commands_script_filter_false').prop('checked', true);
  373. return -1;
  374. }
  375.  
  376. function format_number(n) {
  377. n = Number(n);
  378.  
  379. if (n < 10) {
  380. return '0' + n;
  381. } else {
  382. return n;
  383. }
  384. }
  385.  
  386.  
  387.  
  388.  
  389.  
  390. function sum_troops_objects(units1, units2) {
  391. var troops = {};
  392. var unit;
  393.  
  394. for (unit in units1) {
  395. if (units1.hasOwnProperty(unit)) {
  396. troops[unit] = units1[unit];
  397. }
  398. }
  399.  
  400. for (unit in units2) {
  401. if (units2.hasOwnProperty(unit)) {
  402. if (troops.hasOwnProperty(unit)) {
  403. troops[unit] += units2[unit];
  404. } else {
  405. troops[unit] = units2[unit];
  406. }
  407. }
  408. }
  409.  
  410. return troops;
  411. }
  412.  
  413. function get_in_village_troops() {
  414. if (game_data.screen === 'overview') {
  415. return troops_in_village_overview();
  416. } else {
  417. return troops_in_info_village();
  418. }
  419. }
  420.  
  421. function troops_in_village_overview() {
  422. var troops = {};
  423.  
  424. $('#show_units').find('table tr:visible:not(:first-child, :last-child)').each(function() {
  425. troops[$(this).find('a').attr('data-unit')] = parseInt($(this).find('strong').text());
  426. });
  427.  
  428. return troops;
  429. }
  430.  
  431. function troops_in_info_village() {
  432. var troops = {};
  433. var elements = $('h3:contains("Obrona") + table tr');
  434.  
  435. var headers = elements.first().find('th');
  436. headers = headers.slice(1, headers.length - 1);
  437. headers.each(function() {
  438. var unit = $(this).find('a').data('unit');
  439.  
  440. if (unit !== 'militia') {
  441. troops[unit] = 0;
  442. }
  443. });
  444.  
  445. var rows = elements.slice(1).find('td').each(function() {
  446. if ($(this).hasClass('unit-item')) {
  447. for (var unit in troops) {
  448. if (troops.hasOwnProperty(unit) && $(this).hasClass('unit-item-' + unit)) {
  449. troops[unit] += Number($(this).text().trim());
  450. }
  451. }
  452. }
  453. });
  454.  
  455. return troops;
  456. }
  457.  
  458. function get_simulator_link(def_units, wall_level) {
  459. var link = '/game.php?screen=place&mode=sim&simulate&att_axe=6200&att_light=3100&att_ram=400&luck=0&def_wall=' + wall_level;
  460.  
  461. for (var unit in def_units) {
  462. if (def_units.hasOwnProperty(unit)) {
  463. link += '&def_' + unit + '=' + def_units[unit];
  464. }
  465. }
  466.  
  467. return link;
  468. }
  469.  
  470.  
  471.  
  472.  
  473.  
  474. function get_commands_ids(timestamp, get_own_commands_ids, get_ally_commands_ids) {
  475. var list = [];
  476.  
  477. $(commands_selector).each(function() {
  478. var span = $(this).find('span.command_hover_details').first();
  479.  
  480. if (span.length === 0) {
  481. return;
  482. }
  483.  
  484. if (span.data('command-type') !== 'support') {
  485. return;
  486. }
  487.  
  488. if (timestamp !== -1 && string_to_timestamp(span.closest('td').next('td').text()) >= timestamp) {
  489. return;
  490. }
  491.  
  492. if ((span.hasClass('commandicon-ally') === false && get_own_commands_ids) || (span.hasClass('commandicon-ally') && get_ally_commands_ids)) {
  493. list.push(span.data('command-id'));
  494. }
  495. });
  496.  
  497. return list;
  498. }
  499.  
  500. function get_commands_count(timestamp, get_own_commands_ids, get_ally_commands_ids) {
  501. return get_commands_ids(timestamp, get_own_commands_ids, get_ally_commands_ids).length;
  502. }
  503.  
  504. function get_unknown_commands_ids(list_of_ids) {
  505. var list = load_commands_list();
  506. var unknown_ids = [];
  507.  
  508. for (var i = 0, n = list_of_ids.length; i < n; i++) {
  509. if (list.hasOwnProperty(list_of_ids[i]) === false) {
  510. unknown_ids.push(list_of_ids[i]);
  511. }
  512. }
  513.  
  514. return unknown_ids;
  515. }
  516.  
  517. function get_unknown_commands_count(timestamp, get_own_commands_ids, get_ally_commands_ids) {
  518. var commands_ids = get_commands_ids(timestamp, get_own_commands_ids, get_ally_commands_ids);
  519. var unknown_commands_ids = get_unknown_commands_ids(commands_ids);
  520. return unknown_commands_ids.length;
  521. }
  522.  
  523. function download_command_data(command_id) {
  524. $.get(
  525. '/game.php?village=' + current_village_id + t + '&screen=info_command&ajax=details&id=' + command_id + '&client_time=' + get_current_timestamp()
  526. ).done(function(data) {
  527. var command = convert_ajax_response(data);
  528. add_command_to_list(command_id, command);
  529. window.Command.details_cache[command_id] = command.original_object;
  530. doing_ajax = false;
  531. refresh_gui();
  532. }).error(function() {
  533. UI.ErrorMessage('Wystąpił błąd podczas pobierania danych o komendach. Odśwież stronę aby spróbować ponownie.', 2000);
  534. doing_ajax = false;
  535. });
  536. }
  537.  
  538. function convert_ajax_response(data) {
  539. data = JSON.parse(data);
  540. var command = {timestamp: Number(data.time_arrival.date), units: {}, original_object: data};
  541.  
  542. for (var unit in data.units) {
  543. if (data.units.hasOwnProperty(unit)) {
  544. command.units[unit] = Number(data.units[unit].count);
  545. }
  546. }
  547.  
  548. return command;
  549. }
  550.  
  551. function update_commands_data(timestamp) {
  552. var commands_ids = get_unknown_commands_ids(get_commands_ids(timestamp, true, true)), i = 0, n = commands_ids.length;
  553. if (n > 0) {
  554. var interval_id = setInterval(function() {
  555. if (doing_ajax === false) {
  556. doing_ajax = true;
  557. download_command_data(commands_ids[i]);
  558. i++;
  559.  
  560. if (i == n) {
  561. clearInterval(interval_id);
  562. }
  563. }
  564. }, 75);
  565. }
  566. }
  567.  
  568. function get_commands_data_by_ids(list_of_ids) {
  569. var list = load_commands_list();
  570. var commands = {};
  571.  
  572. for (var i = 0, n = list_of_ids.length; i < n; i++) {
  573. if (list.hasOwnProperty(list_of_ids[i])) {
  574. commands[list_of_ids[i]] = list[list_of_ids[i]];
  575. }
  576. }
  577.  
  578. return commands;
  579. }
  580.  
  581. function get_troops_by_commands_ids(list_of_ids) {
  582. var commands = get_commands_data_by_ids(list_of_ids);
  583. var troops = {};
  584. var first_command = true;
  585. var unit;
  586.  
  587. for (var command_id in commands) {
  588. if (commands.hasOwnProperty(command_id)) {
  589. if (first_command) {
  590. first_command = false;
  591.  
  592. for (unit in commands[command_id].units) {
  593. if (commands[command_id].units.hasOwnProperty(unit)) {
  594. troops[unit] = commands[command_id].units[unit];
  595. }
  596. }
  597. } else {
  598. for (unit in commands[command_id].units) {
  599. if (commands[command_id].units.hasOwnProperty(unit)) {
  600. troops[unit] += commands[command_id].units[unit];
  601. }
  602. }
  603. }
  604. }
  605. }
  606.  
  607. return troops;
  608. }
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615. remove_old_commands();
  616. remove_old_filter_data();
  617. create_gui();
  618. refresh_gui();
  619. generate_links_to_filter();
  620. load_units_popup_cache();
  621. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement