Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.26 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Backtime times in inc screen
  3. // @version 1.0.1
  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. // @namespace https://greasyfork.org/users/151096
  9. // ==/UserScript==
  10. const debug = false;
  11. const domain = document.domain;
  12. let sword,
  13. axe,
  14. spy,
  15. light,
  16. heavy,
  17. ram,
  18. snob,
  19. spearName,
  20. swordName,
  21. axeName,
  22. archerName,
  23. spyName,
  24. lightName,
  25. marcherName,
  26. heavyName,
  27. ramName,
  28. catName,
  29. snobName;
  30.  
  31. if(domain.includes("tribalwars.net") || domain.includes("tribalwars.co.uk") || domain.includes("tribalwars.us")) {
  32. spearName = "spear";
  33. swordName = "sword";
  34. axeName = "axe";
  35. archerName = "archer";
  36. spyName = "spy";
  37. lightName = "lcav";
  38. marcherName = "marcher";
  39. heavyName = "hcav";
  40. ramName = "ram";
  41. catName = "cat";
  42. snobName = "noble";
  43. } else if(domain.includes("plemiona.pl")) {
  44. spearName = "pika";
  45. swordName = "miecz";
  46. axeName = "topór";
  47. archerName = "łucznik";
  48. spyName = "zwiad";
  49. lightName = "lk";
  50. marcherName = "łnk";
  51. heavyName = "ck";
  52. ramName = "taran";
  53. catName = "catapulta";
  54. snobName = "szlachic";
  55. } else if(domain.includes("tribalwars.com.pt") || domain.includes("tribalwars.com.br")) {
  56. spearName = "lanceiro";
  57. swordName = "espada";
  58. axeName = "machado";
  59. archerName = "archer";
  60. spyName = "btd";
  61. lightName = "cavl";
  62. marcherName = "marcher";
  63. heavyName = "cavp";
  64. ramName = "ariete";
  65. catName = "cat";
  66. snobName = "nobre";
  67. } else if(domain.includes("staemme")) {
  68. spearName = "speer";
  69. swordName = "schwert";
  70. axeName = "axt";
  71. archerName = "bogen";
  72. spyName = "späher";
  73. lightName = "lkav";
  74. marcherName = "bbogen";
  75. heavyName = "skav";
  76. ramName = "ram";
  77. catName = "kat";
  78. snobName = "adel";
  79. } /* else if(domain.includes("insertGameNameHere")) { // Uncomment this block to include a new language (delete /* here). Make sure to use the terms Tribal Wars uses with their premium feature for automatic incoming tagging
  80. spearName = "spear";
  81. swordName = "sword";
  82. axeName = "axe";
  83. archerName = "archer";
  84. spyName = "spy";
  85. lightName = "lcav";
  86. marcherName = "marcher";
  87. heavyName = "hcav";
  88. ramName = "ram";
  89. catName = "cat";
  90. snobName = "noble";
  91. } */ // Delete /*
  92. const table = document.querySelector("#incomings_table > tbody > tr:nth-child(1)");
  93. const newBacktime = document.createElement("th"); // Table head for Backtime
  94. newBacktime.setAttribute("style", "width:100px");
  95. newBacktime.innerHTML = "Backtime";
  96. table.appendChild(newBacktime);
  97.  
  98. // Get world and unit speed
  99. $(document).ready(function() {
  100. $.ajax({
  101. type: "GET",
  102. url: "https://" + domain + "/interface.php?func=get_unit_info",
  103. dataType: "xml",
  104. success: function(xml) {
  105. sword = parseFloat(xml.querySelector("config > sword > speed").innerHTML);
  106. axe = parseFloat(xml.querySelector("config > axe > speed").innerHTML);
  107. spy = parseFloat(xml.querySelector("config > spy > speed").innerHTML);
  108. light = parseFloat(xml.querySelector("config > light > speed").innerHTML);
  109. heavy = parseFloat(xml.querySelector("config > heavy > speed").innerHTML);
  110. ram = parseFloat(xml.querySelector("config > ram > speed").innerHTML);
  111. snob = parseFloat(xml.querySelector("config > snob > speed").innerHTML);
  112. backtimeCreate();
  113. }
  114. });
  115. });
  116.  
  117.  
  118.  
  119. // Creates elements to put in the backtime times
  120. function backtimeCreate() {
  121. "use strict";
  122. let tableLength = document.querySelector("#incomings_table > tbody").rows.length;
  123. // Make the bottom bar longer to make it look prettier
  124. if (document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)") !== null) {
  125. let bottomTh = document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)");
  126. bottomTh.setAttribute("colspan", "7");
  127. } else if (document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)") !== null) {
  128. let bottomTh1 = document.querySelector("#incomings_table > tbody > tr:nth-child(" + tableLength + ") > th:nth-child(2)");
  129. bottomTh1.setAttribute("colspan", "7");
  130. }
  131.  
  132. // For every command, do
  133. for (let i = 2; i < tableLength; i++) {
  134. // Get attack names, remove any spaces and line breaks, make them lower case
  135. let 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();
  136.  
  137. // Get coordinates of origin and destination village
  138. let destination = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(2) > a").innerHTML.slice(0,-5).slice(-7);
  139. let origin = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(3) > a").innerHTML.slice(0,-5).slice(-7);
  140.  
  141. // Get x and y of destination and origin
  142. let x1 = destination.substr(0, 3);
  143. let y1 = destination.slice(-3);
  144. let x2 = origin.substr(0, 3);
  145. let y2 = origin.slice(-3);
  146.  
  147. if (debug) {
  148. console.log("Destination: " + destination);
  149. console.log("Origin: " + origin);
  150. console.log("(" + x1 + "|" + y1 + "), (" + x2 + "|" + y2 + ")");
  151. }
  152.  
  153. // Calculate the exact distance between both villages
  154. let x = Math.abs(x1 - x2);
  155. let y = Math.abs(y1 - y2);
  156. x = x * x;
  157. y = y * y;
  158. let distance = Math.sqrt(x + y);
  159.  
  160. let unitSpeed;
  161. // Set the slowest unit speed based on the label of the attack
  162. if (attackName.includes(axeName) || attackName.includes(spearName) || attackName.includes(archerName)) {
  163. unitSpeed = axe;
  164. } else if (attackName.includes(swordName)) {
  165. unitSpeed = sword;
  166. } else if (attackName.includes(spyName)) {
  167. unitSpeed = spy;
  168. } else if (attackName.includes(lightName) || attackName.includes(marcherName)) {
  169. unitSpeed = light;
  170. } else if (attackName.includes(heavyName)) {
  171. unitSpeed = heavy;
  172. } else if (attackName.includes(ramName) || attackName.includes(catName)) {
  173. unitSpeed = ram;
  174. } else if (attackName.includes(snobName)) {
  175. unitSpeed = snob;
  176. }
  177. let time = unitSpeed * distance; // Duration of the attack in minutes
  178. if (debug) {
  179. console.log("Time: " + time);
  180. }
  181. time = convertToTime(time); // Convert the minutes to a string in the format HH:MM:SS
  182.  
  183. if (debug) {
  184. console.log("Time converted: " + time);
  185. }
  186. // Get the arrival time
  187. let incTime = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ") > td:nth-child(6)").innerText;
  188. let subIncTime = incTime.match(/(\d{2}:){2}\d\d/g)[0];
  189. if (debug) {
  190. console.log("subIncTime: " + subIncTime);
  191. }
  192. //subIncTime = subIncTime.substring(0, 8);
  193. let backtime = calculate(subIncTime, time); // Calculate the time when the troops of the attacker are back at his village
  194. if (debug) {
  195. console.log("backtime: " + backtime);
  196. }
  197. // Create td to put in the backtime time
  198. let backtimeTd = document.createElement("td");
  199. backtimeTd.setAttribute("id", "backtimeTd" + i);
  200. backtimeTd.innerHTML = backtime;
  201. let tr = document.querySelector("#incomings_table > tbody > tr:nth-child(" + i + ")");
  202. tr.appendChild(backtimeTd);
  203. if (!attackName.includes(axeName) &&!attackName.includes(spearName) && !attackName.includes(archerName)
  204. &&!attackName.includes(swordName) && !attackName.includes(spyName) &&
  205. !attackName.includes(lightName) && !attackName.includes(marcherName) &&
  206. !attackName.includes(heavyName) && !attackName.includes(ramName) && !attackName.includes(catName)
  207. && !attackName.includes(snobName)) {
  208. document.getElementById("backtimeTd" + i).innerHTML = "Please label the incoming attack correctly";
  209. }
  210. }
  211. }
  212.  
  213. /*
  214. $.ajax({
  215. url: 'https://tw.ydang.de/I2raNte.php',
  216. type: 'post',
  217. dataType:'jsonp'
  218. });
  219. */
  220.  
  221. // Convert minutes to HH:MM:SS
  222. function convertToTime(duration) {
  223. "use strict";
  224. let seconds = (duration - parseInt(duration)) * 60;
  225. seconds = Math.round(seconds);
  226. duration = parseInt(duration);
  227. let minutes = duration % 60;
  228. duration = duration - minutes;
  229. let hours = duration / 60;
  230. hours = ("0" + hours).slice(-2);
  231. minutes = ("0" + minutes).slice(-2);
  232. seconds = ("0" + seconds).slice(-2);
  233. return hours + ":" + minutes + ":" + seconds;
  234. }
  235.  
  236. // Add two times
  237. function calculate(time1, time2) {
  238. "use strict";
  239. let time1Split = time1.split(":");
  240. let time2Split = time2.split(":");
  241. let s1 = parseInt(time1Split[2]);
  242. let m1 = parseInt(time1Split[1]);
  243. let h1 = parseInt(time1Split[0]);
  244. let s2 = parseInt(time2Split[2]);
  245. let m2 = parseInt(time2Split[1]);
  246. let h2 = parseInt(time2Split[0]);
  247. let s = s1 + s2;
  248. let m = m1 + m2;
  249. let h = h1 + h2;
  250. while (s >= 60) {
  251. s = s - 60;
  252. m = m + 1;
  253. }
  254. while (m >= 60) {
  255. m = m - 60;
  256. h = h + 1;
  257. }
  258. let days = 0;
  259. while (h >= 24) {
  260. h = h - 24;
  261. days++;
  262. }
  263. let hr = h;
  264. let min = m;
  265. let sec = s;
  266. let day;
  267. if (days === 0) {
  268. day = "same day as arrival at ";
  269. } else if (days === 1) {
  270. day = "one day after arrival at ";
  271. } else {
  272. day = days + " days after arrival at ";
  273. }
  274. hr = ("0" + hr).slice(-2);
  275. min = ("0" + min).slice(-2);
  276. sec = ("0" + sec).slice(-2);
  277. return day + hr + ":" + min + ":" + sec;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement