function getQuery(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } function getQueryFromHaystack(haystack, name){ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(haystack); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } function fetchReportData(doc, winLocation){ if (getQueryFromHaystack(winLocation, "screen") !== "report" || getQueryFromHaystack(winLocation, "view") === "") return false; if ($(".nopad .vis h3", doc).length === 0 || !$(".nopad .vis h3", doc).html().match(/The (attacker|defender) has won/)) return false; var data = {}; var temp; var attSection = $("#attack_info_att", doc); var attID = ((temp = $("tr:has(th:contains('Attacker:')) th a", attSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0; var attCoords = ((temp = $("span.village_anchor", attSection).text().trim().match(/\((\d{3}\|\d{3})\)/)) !== undefined && temp[1]) || "000|000"; var attVilID = ((temp = $("span.village_anchor a", attSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0; var attTroops = []; var attLoss = []; var attRemain = []; $("#attack_info_att_units tr:has(td:contains('Quantity')) td.unit-item", attSection).each(function() { attTroops.push($(this).text()); }); $("#attack_info_att_units tr:has(td:contains('Losses')) td.unit-item", attSection).each(function() { attLoss.push($(this).text()); }); attTroops.forEach(function(element, index, array){ attRemain[index] = attTroops[index] - attLoss[index]; }); attTroops = attTroops.join(","); attLoss = attLoss.join(","); attRemain = attRemain.join(","); var defSection = $("#attack_info_def", doc); var defID = ((temp = $("tr:has(th:contains('Defender:')) th a", defSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0; var defCoords = ((temp = $("span.village_anchor", defSection).text().trim().match(/\((\d{3}\|\d{3})\)/)) !== undefined && temp[1]) || "000|000"; var defVilID = ((temp = $("span.village_anchor a", defSection).attr("href")) !== undefined && temp.match(/[?&]id=(\d+)/)[1]) || 0; var defTroops = []; var defLoss = []; var defRemain = []; $("#attack_info_def_units tr:has(td:contains('Quantity')) td.unit-item", defSection).each(function() { defTroops.push($(this).text()); }); $("#attack_info_def_units tr:has(td:contains('Losses')) td.unit-item", defSection).each(function() { defLoss.push($(this).text()); }); defTroops.forEach(function(element, index, array){ defRemain[index] = defTroops[index] - defLoss[index]; }); defTroops = defTroops.join(","); defLoss = defLoss.join(","); defRemain = defRemain.join(","); var resources = []; var buildings = []; var espionage = $("#attack_spy", doc); if (espionage.length === 0){ resources = ""; buildings = ""; } else { resources = $("tr:has(th:contains('Resources scouted')) td", espionage); var resz = resources.text().trim().split(" "); if (resources.get(0).innerHTML.indexOf("wood") === -1){ resz.unshift("0"); } if (resources.get(0).innerHTML.indexOf("stone") === -1){ if (resz[1]) { resz.push(resz[1]); resz[1] = "0"; } else resz.push("0"); } if (resources.get(0).innerHTML.indexOf("iron") === -1){ resz.push("0"); } var wood = parseInt(resz[0].replace(".", "")), clay = parseInt(resz[1].replace(".", "")), iron = parseInt(resz[2].replace(".", "")); resources = wood + "," + clay + "," + iron; var buildz = $("tr:has(th:contains('Buildings')) td", espionage); if (buildz.length === 0){ buildings = ""; } else { /*Order: hq, barracks, stable, workshop, academy, smithy, rally point, statue, market, wood, clay, iron, farm, warehouse, hiding place, wall*/ var hq, bar, sta, wor, aca, smi, pla, stt, mar, woo, cla, iro, far, wh, hp, wal; ((hq = buildz.text().match(/Headquarters \(Level (\d+)\)/)) !== null) && (buildings.push(hq[1])) || buildings.push(0); ((bar = buildz.text().match(/Barracks \(Level (\d+)\)/)) !== null) && (buildings.push(bar[1])) || buildings.push(0); ((sta = buildz.text().match(/Stable \(Level (\d+)\)/)) !== null) && (buildings.push(sta[1])) || buildings.push(0); ((wor = buildz.text().match(/Workshop \(Level (\d+)\)/)) !== null) && (buildings.push(wor[1])) || buildings.push(0); ((aca = buildz.text().match(/Academy \(Level (\d+)\)/)) !== null) && (buildings.push(aca[1])) || buildings.push(0); ((smi = buildz.text().match(/Smithy \(Level (\d+)\)/)) !== null) && (buildings.push(smi[1])) || buildings.push(0); ((pla = buildz.text().match(/Rally point \(Level (\d+)\)/)) !== null) && (buildings.push(pla[1])) || buildings.push(0); ((stt = buildz.text().match(/Statue \(Level (\d+)\)/)) !== null) && (buildings.push(stt[1])) || buildings.push(0); ((mar = buildz.text().match(/Market \(Level (\d+)\)/)) !== null) && (buildings.push(mar[1])) || buildings.push(0); ((woo = buildz.text().match(/Timber camp \(Level (\d+)\)/)) !== null) && (buildings.push(woo[1])) || buildings.push(0); ((cla = buildz.text().match(/Clay pit \(Level (\d+)\)/)) !== null) && (buildings.push(cla[1])) || buildings.push(0); ((iro = buildz.text().match(/Iron mine \(Level (\d+)\)/)) !== null) && (buildings.push(iro[1])) || buildings.push(0); ((far = buildz.text().match(/Farm \(Level (\d+)\)/)) !== null) && (buildings.push(far[1])) || buildings.push(0); ((wh = buildz.text().match(/Warehouse \(Level (\d+)\)/)) !== null) && (buildings.push(wh[1])) || buildings.push(0); ((hp = buildz.text().match(/Hiding place \(Level (\d+)\)/)) !== null) && (buildings.push(hp[1])) || buildings.push(0); ((wal = buildz.text().match(/Wall \(Level (\d+)\)/)) !== null) && (buildings.push(wal[1])) || buildings.push(0); buildings = buildings.join(","); } } var reportTime = $($(".nopad tr:has(> td:contains('Battle time')) td", doc).get(1)).text().trim(); var reportID = getQueryFromHaystack(winLocation, "view"); data["attID"] = attID; data["attCoords"] = attCoords; data["attVilID"] = attVilID; data["attTroops"] = attTroops; data["attLoss"] = attLoss; data["attRemain"] = attRemain; data["defID"] = defID; data["defCoords"] = defCoords; data["defVilID"] = defVilID; data["defTroops"] = defTroops; data["defLoss"] = defLoss; data["defRemain"] = defRemain; data["resources"] = resources; data["buildings"] = buildings; data["reportTime"] = reportTime; data["reportID"] = reportID; return data; } function uploadReport(doc, winLocation){ var data = fetchReportData(doc, winLocation); if (data){ var a = $.ajax("http://tw.rkarthi.com/upload.php", { "async": false, "data": data, "type": "POST", "complete": function(jqXHR, textStatus) { $("
").css("background-color", "#E3D5B3") .css("padding", "15px") .css("border", "1px solid #7D510F") .html(jqXHR.readyState + ": " + jqXHR.responseText) .appendTo(document.body); console.log(jqXHR.readyState + ": " + jqXHR.responseText); } }); return true; } else { return false; } } if (getQuery("screen") === "report") { if (getQuery("view") !== "") uploadReport(document, window.location.href); else { $("#report_list tr:not(tr:has(th)) .quickedit-content a:not(a.rename-icon)").each(function(index){ $.ajax($(this).attr("href"), { "context" : $(this), "complete": function(jqXHR, textStatus) { if (textStatus === "success"){ var doc = $("
"); doc.get(0).id = "KNReport"; doc.html(jqXHR.responseText); if (uploadReport(doc, this.attr("href"))){ this.html(this.html() + " - Uploaded"); } else { this.html(this.html() + " - Not Uploaded"); } } } }); }); } }