FunnyPocketBook

Tribal Wars - Backtime

Mar 31st, 2018
1,752
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Backtime times in inc screen
  3. // @version      0.5.2
  4. // @description  New table column with backtimes if the attacks are labelled correctly (lcav, hcav, noble etc.)
  5. // @author       FunnyPocketBook
  6. // @match        https://*/game.php?village=*&screen=overview_villages&mode=incomings*
  7. // @grant        none
  8. // ==/UserScript==
  9. var table = document.querySelector("#incomings_table > tbody > tr:nth-child(1)");
  10. var newBacktime = document.createElement("th"); // Table head for Backtime
  11. newBacktime.setAttribute("style", "width:100px");
  12. newBacktime.innerHTML = "Backtime";
  13. table.appendChild(newBacktime);
  14.  
  15. var domain = document.domain;
  16. var unitSpeed;
  17. var worldSpeed;
  18. var speed;
  19.  
  20. // Get world and unit speed
  21. $(document).ready(function() {
  22.     $.ajax({
  23.         type: "GET",
  24.         url: "https://" + domain + "/interface.php?func=get_config",
  25.         dataType: "xml",
  26.         success: function(xml) {
  27.             unitSpeed = parseFloat(xml.querySelector("config > unit_speed").innerHTML);
  28.             worldSpeed = parseFloat(xml.querySelector("config > speed").innerHTML);
  29.             speed = unitSpeed * worldSpeed; // Set factor of unit speed
  30.             backtimeCreate();
  31.         }
  32.     });
  33. });
  34.  
  35.  
  36.  
  37. // Creates elements to put in the backtime times
  38. function backtimeCreate() {
  39.     "use strict";
  40.     var tableLength = document.querySelector("#incomings_table > tbody").rows.length;
  41.     // Make the bottom bar longer to make it look prettier
  42.     if (document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)") !== null) {
  43.         var bottomTh = document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)");
  44.         bottomTh.setAttribute("colspan", "7");
  45.     } else if (document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)") !== null) {
  46.         var bottomTh1 = document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)");
  47.         bottomTh1.setAttribute("colspan", "7");
  48.     }
  49.  
  50.     // For every command, do
  51.     for (var i = 2; i < tableLength; i++) {
  52.         // Get attack names, remove any spaces and line breaks, make them lower case
  53.         var attackName = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(1) > span > span > a:nth-child(1) > span.quickedit-label").innerHTML.replace(/(\r\n|\n|\r)/gm, "").replace(/ /g, "").toLowerCase();
  54.  
  55.         // Get coordinates of origin and destination village
  56.         var destination = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(2) > a").innerHTML.slice(-12).substr(0, 7);
  57.         var origin = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(3) > a").innerHTML.slice(-12).substr(0, 7);
  58.  
  59.         // Get x and y of destination and origin
  60.         var x1 = destination.substr(0, 3);
  61.         var y1 = destination.slice(-3);
  62.         var x2 = origin.substr(0, 3);
  63.         var y2 = origin.slice(-3);
  64.  
  65.         // Calculate the exact distance between both villages
  66.         var x = Math.abs(x1 - x2);
  67.         var y = Math.abs(y1 - y2);
  68.         x = x * x;
  69.         y = y * y;
  70.         var distance = Math.sqrt(x + y);
  71.  
  72.         var unitSpeedDefault;
  73.         // Set the slowest unit speed based on the label of the attack
  74.         if (attackName.includes("axe") || attackName.includes("spear") || attackName.includes("archer")) {
  75.             unitSpeedDefault = 18;
  76.         } else if (attackName.includes("sword")) {
  77.             unitSpeedDefault = 22;
  78.         } else if (attackName.includes("spy")) {
  79.             unitSpeedDefault = 9;
  80.         } else if (attackName.includes("lcav") || attackName.includes("marcher")) {
  81.             unitSpeedDefault = 10;
  82.         } else if (attackName.includes("hcav")) {
  83.             unitSpeedDefault = 11;
  84.         } else if (attackName.includes("ram") || attackName.includes("cat")) {
  85.             unitSpeedDefault = 30;
  86.         } else if (attackName.includes("noble")) {
  87.             unitSpeedDefault = 35;
  88.         }
  89.         var unitSpeedReal = unitSpeedDefault / speed; // Minutes per field
  90.         var time = unitSpeedReal * distance; // Duration of the attack in minutes
  91.         time = convertToTime(time); // Convert the minutes to a string in the format HH:MM:SS
  92.         // Get the arrival time
  93.         var incTime = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(6)").innerText;
  94.         var subIncTime = incTime.slice(-12);
  95.         subIncTime = subIncTime.substring(0, 8);
  96.         var backtime = calculate(subIncTime, time); // Calculate the time when the troops of the attacker are back at his village
  97.         // Create td to put in the backtime time
  98.         var backtimeTd = document.createElement("td");
  99.         backtimeTd.setAttribute("id", "backtimeTd" + i);
  100.         backtimeTd.innerHTML = backtime;
  101.         var tr = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ")");
  102.         tr.appendChild(backtimeTd);
  103.         if (attackName !== "axe" && attackName !== "spear" && attackName !== "archer" && attackName !== "sword" && attackName !== "spy" && attackName !== "lcav" && attackName !== "marcher" && attackName !== "hcav" && attackName !== "ram" && attackName !== "cat" && attackName !== "noble") {
  104.             document.getElementById("backtimeTd" + i).innerHTML = "Please label the incoming attack correctly";
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110. // Convert minutes to HH:MM:SS
  111. function convertToTime(duration) {
  112.     "use strict";
  113.     var seconds = (duration - parseInt(duration)) * 60;
  114.     seconds = Math.round(seconds);
  115.     duration = parseInt(duration);
  116.     var minutes = duration % 60;
  117.     duration = duration - minutes;
  118.     var hours = duration / 60;
  119.     hours = ("0" + hours).slice(-2);
  120.     minutes = ("0" + minutes).slice(-2);
  121.     seconds = ("0" + seconds).slice(-2);
  122.     return hours + ":" + minutes + ":" + seconds;
  123. }
  124.  
  125. // Add two times
  126. function calculate(time1, time2) {
  127.     "use strict";
  128.     var time1Split = time1.split(":");
  129.     var time2Split = time2.split(":");
  130.     var s1 = parseInt(time1Split[2]);
  131.     var m1 = parseInt(time1Split[1]);
  132.     var h1 = parseInt(time1Split[0]);
  133.     var s2 = parseInt(time2Split[2]);
  134.     var m2 = parseInt(time2Split[1]);
  135.     var h2 = parseInt(time2Split[0]);
  136.     var s = s1 + s2;
  137.     var m = m1 + m2;
  138.     var h = h1 + h2;
  139.     if (s >= 60) {
  140.         s = s - 60;
  141.         m = m + 1;
  142.     }
  143.     if (m >= 60) {
  144.         m = m - 60;
  145.         h = h + 1;
  146.     }
  147.     var days = 0;
  148.     while (h >= 24) {
  149.         h = h - 24;
  150.         days++;
  151.     }
  152.     var hr = h;
  153.     var min = m;
  154.     var sec = s;
  155.     var day;
  156.     if (days === 0) {
  157.         day = "same day as arrival at ";
  158.     } else if (days === 1) {
  159.         day = "one day after arrival at ";
  160.     } else {
  161.         day = days + " days after arrival at ";
  162.     }
  163.     hr = ("0" + hr).slice(-2);
  164.     min = ("0" + min).slice(-2);
  165.     sec = ("0" + sec).slice(-2);
  166.     return day + hr + ":" + min + ":" + sec;
  167. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment