Throne3d

NAIT

Aug 2nd, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 5.68 KB | None | 0 0
  1. function getQuery(name) {
  2.     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  3.     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  4.         results = regex.exec(location.search);
  5.     return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  6. }
  7.  
  8. function getQueryFromHaystack(haystack, name) {
  9.     name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  10.     var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  11.         results = regex.exec(haystack);
  12.     return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  13. }
  14.  
  15. function getXML() {
  16.     if (xmlConfig === undefined) {
  17.         if (window.XMLHttpRequest) {
  18.             xmlhttp = new XMLHttpRequest();
  19.         } else {
  20.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  21.         }
  22.         xmlhttp.open("GET", window.location.protocol + "//" + window.location.host + "/interface.php?func=get_config", false);
  23.         xmlhttp.send();
  24.         xmlConfig = xmlhttp.responseXML;
  25.     }
  26.     return xmlConfig;
  27. }
  28.  
  29. function getWorldSpeed() {
  30.     return parseFloat(getXML().getElementsByTagName("speed")[0].childNodes[0].nodeValue);
  31. }
  32.  
  33. function getUnitSpeed() {
  34.     return parseFloat(getXML().getElementsByTagName("unit_speed")[0].childNodes[0].nodeValue);
  35. }
  36.  
  37. var xmlConfig;
  38.  
  39. var speed = [[9, "Scout"], [10, "LC"], [11, "HC"], [18, "Axe"], [22, "Sword"], [30, "Ram"], [35, "Noble"]];
  40. var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  41.  
  42. function main(doc, href) {
  43.     if (getQueryFromHaystack(href, "mode") === "incomings" && getQueryFromHaystack(href, "screen") === "overview_villages") {
  44.         $("#incomings_table .row_a, #incomings_table .row_b", doc).each(function() {
  45.             $.ajax($($("a", this).get(0)).attr("href"), {
  46.                 context: $($("a", this).get(0)),
  47.                 complete: function(jqXHR, textStatus){
  48.                     if (textStatus === "success"){
  49.                         var doca = $("<div></div>");
  50.                         doca.get(0).id = "NAIT";
  51.                         a = jqXHR.responseText;
  52.                         b = this.attr("href");
  53.                         doca.html(jqXHR.responseText);
  54.                         var name = main(doca, this.attr("href"));
  55.                         this.html(name);
  56.                     }
  57.                 }
  58.             });
  59.         });
  60.     } else if (getQueryFromHaystack(href, "screen") === "info_command") {
  61.         var data = {};
  62.         var table = $("#content_value table", doc);
  63.         data["origin_player"] = $($("tr:has(td:contains('Origin')) td", table).get(2)).text();
  64.         data["origin_village"] = $($("tr:has(td:contains('Village')) td", table).get(1)).text().split("(")[1].split(")")[0];
  65.         data["dest_player"] = $($("tr:has(td:contains('Destination')) td", table).get(2)).text();
  66.         data["dest_village"] = $($("tr:has(td:contains('Village')) td", table).get(3)).text().split("(")[1].split(")")[0];
  67.         data["time_left"] = $($("tr:has(td:contains('Arrival in')) td", table).get(1)).text();
  68.         time = data["time_left"].split(":");
  69.         var seconds = parseInt(time[time.length-1]),
  70.             minutes = parseInt(time[time.length-2]),
  71.             hours = parseInt(time[time.length-3]);
  72.         data["seconds_left"] = (hours * 3600) + (minutes * 60) + seconds;
  73.         data["distance"] = Math.floor(Math.sqrt(Math.pow(data["origin_village"].split("|")[0] - data["dest_village"].split("|")[0], 2) + Math.pow(data["origin_village"].split("|")[1] - data["dest_village"].split("|")[1], 2)) * 100 + 0.5) / 100;
  74.         data["time_arrival"] = $($("tr:has(td:contains('Arrival:')) td", table).get(1)).text();
  75.        
  76.         data["slowest_unit"] = "";
  77.        
  78.         var nextThing = true;
  79.         for (var i=0; i<speed.length; i++){
  80.             var secondsForUnit = speed[i][0] / (getWorldSpeed() * getUnitSpeed()) * 60;
  81.             if (data["seconds_left"] <= secondsForUnit && nextThing){
  82.                 data["slowest_unit"] = speed[i][1];
  83.                 data["time_sent"] = new Date((new Date(data["time_arrival"].replace(/\:\d{3}/, ""))) - (speed[i][0] / (getWorldSpeed() * getUnitSpeed()) * 60000));
  84.                 timeBack = (new Date(new Date(data["time_arrival"]) + (speed[i][0] / (getWorldSpeed() * getUnitSpeed()) * 60000))).toLocaleString();
  85.                 data["time_back"] = months[timeBack.getMonth()] + " " + timeBack.getDate() + " " + timeBack.getFullYear() + " " + (timeBack.getHours() < 10 ? "0" + timeBack.getHours() : timeBack.getHours()) + ":" + (timeBack.getMinutes() < 10 ? "0" + timeBack.getMinutes() : timeBack.getMinutes()) + ":" + (timeBack.getSeconds() < 10 ? "0" + timeBack.getSeconds() : timeBack.getSeconds());
  86.                 nextThing = false;
  87.             }
  88.         }
  89.        
  90.         var name = format;
  91.         name = name.replace(/\{SlowestUnit\}/g, data["slowest_unit"]);
  92.         name = name.replace(/\{AttackingPlayer\}/g, data["origin_player"]);
  93.         name = name.replace(/\{AttackingVil\}/g, data["origin_village"]);
  94.         name = name.replace(/\{DefendingPlayer\}/g, data["dest_player"]);
  95.         name = name.replace(/\{DefendingVil\}/g, data["dest_village"]);
  96.         name = name.replace(/\{SecondsLeft\}/g, data["seconds_left"]);
  97.         name = name.replace(/\{SentTime\}/g, data["time_sent"]);
  98.         name = name.replace(/\{BackTime\}/g, data["time_back"]);
  99.        
  100.         $("th .quickedit .rename-icon", table).click();
  101.         setTimeout(function(){ $("th .quickedit input[type='text']", table).val(name); setTimeout(function(){ $("th .quickedit input[type='button']", table).click(); }, 250); }, 100);
  102.        
  103.         return name;
  104.     }
  105. }
  106.  
  107. main(document, window.location.href);
Advertisement
Add Comment
Please, Sign In to add comment