Throne3d

KNReportVault

Jun 30th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 8.90 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. function fetchReportData(doc, winLocation){
  15.     if (getQueryFromHaystack(winLocation, "screen") !== "report" || getQueryFromHaystack(winLocation, "view") === "") return false;
  16.     if ($(".nopad .vis h3", doc).length === 0 || !$(".nopad .vis h3", doc).html().match(/The (attacker|defender) has won/)) return false;
  17.     var data = {};
  18.    
  19.     var temp;
  20.    
  21.     var attSection = $("#attack_info_att", doc);
  22.     var attID = ((temp = $("tr:has(th:contains('Attacker:')) th a", attSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0;
  23.     var attCoords = ((temp = $("span.village_anchor", attSection).text().trim().match(/\((\d{3}\|\d{3})\)/)) !== undefined && temp[1]) || "000|000";
  24.     var attVilID = ((temp = $("span.village_anchor a", attSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0;
  25.     var attTroops = [];
  26.     var attLoss = [];
  27.     var attRemain = [];
  28.     $("#attack_info_att_units tr:has(td:contains('Quantity')) td.unit-item", attSection).each(function() { attTroops.push($(this).text()); });
  29.     $("#attack_info_att_units tr:has(td:contains('Losses')) td.unit-item", attSection).each(function() { attLoss.push($(this).text()); });
  30.     attTroops.forEach(function(element, index, array){ attRemain[index] = attTroops[index] - attLoss[index]; });
  31.     attTroops = attTroops.join(",");
  32.     attLoss = attLoss.join(",");
  33.     attRemain = attRemain.join(",");
  34.    
  35.     var defSection = $("#attack_info_def", doc);
  36.     var defID = ((temp = $("tr:has(th:contains('Defender:')) th a", defSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0;
  37.     var defCoords = ((temp = $("span.village_anchor", defSection).text().trim().match(/\((\d{3}\|\d{3})\)/)) !== undefined && temp[1]) || "000|000";
  38.     var defVilID = ((temp = $("span.village_anchor a", defSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0;
  39.     var defTroops = [];
  40.     var defLoss = [];
  41.     var defRemain = [];
  42.     $("#attack_info_def_units tr:has(td:contains('Quantity')) td.unit-item", defSection).each(function() { defTroops.push($(this).text()); });
  43.     $("#attack_info_def_units tr:has(td:contains('Losses')) td.unit-item", defSection).each(function() { defLoss.push($(this).text()); });
  44.     defTroops.forEach(function(element, index, array){ defRemain[index] = defTroops[index] - defLoss[index]; });
  45.     defTroops = defTroops.join(",");
  46.     defLoss = defLoss.join(",");
  47.     defRemain = defRemain.join(",");
  48.    
  49.     var resources = [];
  50.     var buildings = [];
  51.     var espionage = $("#attack_spy", doc);
  52.     if (espionage.length === 0){
  53.         resources = "";
  54.         buildings = "";
  55.     } else {
  56.         resources = $("tr:has(th:contains('Resources scouted')) td", espionage);
  57.         var resz = resources.text().trim().split("  ");
  58.         if (resources.get(0).innerHTML.indexOf("wood") === -1){
  59.             resz.unshift("0");
  60.         }
  61.         if (resources.get(0).innerHTML.indexOf("stone") === -1){
  62.             if (resz[1]) { resz.push(resz[1]); resz[1] = "0"; }
  63.             else resz.push("0");
  64.         }
  65.         if (resources.get(0).innerHTML.indexOf("iron") === -1){
  66.             resz.push("0");
  67.         }
  68.         var wood = parseInt(resz[0].replace(".", "")),
  69.             clay = parseInt(resz[1].replace(".", "")),
  70.             iron = parseInt(resz[2].replace(".", ""));
  71.        
  72.         resources = wood + "," + clay + "," + iron;
  73.        
  74.         var buildz = $("tr:has(th:contains('Buildings')) td", espionage);
  75.         if (buildz.length === 0){
  76.             buildings = "";
  77.         } else {
  78.             /*Order:
  79.             hq, barracks, stable, workshop, academy, smithy, rally point, statue, market, wood, clay, iron, farm, warehouse, hiding place, wall*/
  80.             var hq, bar, sta, wor, aca, smi, pla, stt, mar, woo, cla, iro, far, wh, hp, wal;
  81.             ((hq = buildz.text().match(/Headquarters \(Level (\d+)\)/)) !== null) && (buildings.push(hq[1])) || buildings.push(0);
  82.             ((bar = buildz.text().match(/Barracks \(Level (\d+)\)/)) !== null) && (buildings.push(bar[1])) || buildings.push(0);
  83.             ((sta = buildz.text().match(/Stable \(Level (\d+)\)/)) !== null) && (buildings.push(sta[1])) || buildings.push(0);
  84.             ((wor = buildz.text().match(/Workshop \(Level (\d+)\)/)) !== null) && (buildings.push(wor[1])) || buildings.push(0);
  85.             ((aca = buildz.text().match(/Academy \(Level (\d+)\)/)) !== null) && (buildings.push(aca[1])) || buildings.push(0);
  86.             ((smi = buildz.text().match(/Smithy \(Level (\d+)\)/)) !== null) && (buildings.push(smi[1])) || buildings.push(0);
  87.             ((pla = buildz.text().match(/Rally point \(Level (\d+)\)/)) !== null) && (buildings.push(pla[1])) || buildings.push(0);
  88.             ((stt = buildz.text().match(/Statue \(Level (\d+)\)/)) !== null) && (buildings.push(stt[1])) || buildings.push(0);
  89.             ((mar = buildz.text().match(/Market \(Level (\d+)\)/)) !== null) && (buildings.push(mar[1])) || buildings.push(0);
  90.             ((woo = buildz.text().match(/Timber camp \(Level (\d+)\)/)) !== null) && (buildings.push(woo[1])) || buildings.push(0);
  91.             ((cla = buildz.text().match(/Clay pit \(Level (\d+)\)/)) !== null) && (buildings.push(cla[1])) || buildings.push(0);
  92.             ((iro = buildz.text().match(/Iron mine \(Level (\d+)\)/)) !== null) && (buildings.push(iro[1])) || buildings.push(0);
  93.             ((far = buildz.text().match(/Farm \(Level (\d+)\)/)) !== null) && (buildings.push(far[1])) || buildings.push(0);
  94.             ((wh = buildz.text().match(/Warehouse \(Level (\d+)\)/)) !== null) && (buildings.push(wh[1])) || buildings.push(0);
  95.             ((hp = buildz.text().match(/Hiding place \(Level (\d+)\)/)) !== null) && (buildings.push(hp[1])) || buildings.push(0);
  96.             ((wal = buildz.text().match(/Wall \(Level (\d+)\)/)) !== null) && (buildings.push(wal[1])) || buildings.push(0);
  97.             buildings = buildings.join(",");
  98.         }
  99.     }
  100.    
  101.     var reportTime = $($(".nopad tr:has(> td:contains('Battle time')) td", doc).get(1)).text().trim();
  102.     var reportID = getQueryFromHaystack(winLocation, "view");
  103.    
  104.     data["attID"] = attID;
  105.     data["attCoords"] = attCoords;
  106.     data["attVilID"] = attVilID;
  107.     data["attTroops"] = attTroops;
  108.     data["attLoss"] = attLoss;
  109.     data["attRemain"] = attRemain;
  110.     data["defID"] = defID;
  111.     data["defCoords"] = defCoords;
  112.     data["defVilID"] = defVilID;
  113.     data["defTroops"] = defTroops;
  114.     data["defLoss"] = defLoss;
  115.     data["defRemain"] = defRemain;
  116.     data["resources"] = resources;
  117.     data["buildings"] = buildings;
  118.     data["reportTime"] = reportTime;
  119.     data["reportID"] = reportID;
  120.    
  121.     return data;
  122. }
  123.  
  124. function uploadReport(doc, winLocation){
  125.     var data = fetchReportData(doc, winLocation);
  126.     if (data){ var a = $.ajax("http://tw.rkarthi.com/upload.php",
  127.         {
  128.             "async": false,
  129.             "data": data,
  130.             "type": "POST",
  131.             "complete": function(jqXHR, textStatus) {
  132.                 $("<div>").css("background-color", "#E3D5B3")
  133.                         .css("padding", "15px")
  134.                         .css("border", "1px solid #7D510F")
  135.                         .html(jqXHR.readyState + ": " + jqXHR.responseText)
  136.                         .appendTo(document.body);
  137.                 console.log(jqXHR.readyState + ": " + jqXHR.responseText);
  138.             }
  139.         });
  140.         return true;
  141.     } else {
  142.         return false;
  143.     }
  144. }
  145.  
  146. if (getQuery("screen") === "report") {
  147.     if (getQuery("view") !== "") uploadReport(document, window.location.href);
  148.     else {
  149.         $("#report_list tr:not(tr:has(th)) .quickedit-content a:not(a.rename-icon)").each(function(index){
  150.             $.ajax($(this).attr("href"), {
  151.                 "context" : $(this),
  152.                 "complete": function(jqXHR, textStatus) {
  153.                     if (textStatus === "success"){
  154.                         var doc = $("<div></div>");
  155.                         doc.get(0).id = "KNReport";
  156.                         doc.html(jqXHR.responseText);
  157.                         if (uploadReport(doc, this.attr("href"))){
  158.                             this.html(this.html() + " - Uploaded");
  159.                         } else {
  160.                             this.html(this.html() + " - Not Uploaded");
  161.                         }
  162.                     }
  163.                 }
  164.             });
  165.         });
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment