Ugrás a tartalomhoz Lépj a menübe
 


javascript_farmer

javascript: /* * @author patricier * @rewriter: kottalovag (kottalovag@gmail.com) * [Script] - Clears the attacked villages of the list; clicking on offense also eliminates the entry. */ var currentVersion = "1.3"; versioningLoaded = true; function versionCompare(v1, v2) { var v1arr = v1.split("."); var v2arr = v2.split("."); v1arr = v1arr.map( function(e) { return parseInt(e, 10); }); v2arr = v2arr.map( function(e) { return parseInt(e, 10); }); var i = 0; while (i < v1arr.length && i < v2arr.length && v1arr[i] == v2arr[i]) { ++i; } if (i >= v1arr.length) { if (i >= v2arr.length) { //v1 finished, v2 finished, equal return 0; } else { //v1 finished, v2 not finished, v2 is bigger return 1; } } else { if (i >= v2arr.length) { //v1 not finished, v2 finished, v1 is bigger return -1; } else { //v1 not finished, v2 not finished, current element decides if (v1arr[i] < v2arr[i]) { return 1; } else { return -1; } } } } function isThereNewerVersion() { if (typeof(farm_script_version) == "undefined") return true; if (versionCompare(farm_script_version, currentVersion) == 1) return true; return false; } function setupNumberObjectVariant(propertyNames, variant) { if (typeof(variant) == "number") { num = variant; variant = {}; for (var idx in propertyNames) { var name = propertyNames[idx]; variant[name] = num; } } else { for (var idx in propertyNames) { var name = propertyNames[idx]; if (typeof(variant[name]) == undefined) { variant[name] = 0; } } } return variant; } function setupUnitThreshold() { if ((typeof(unit_threshold) != "object") && (typeof(unit_threshold) != "number")){ unit_threshold = 0; } unit_threshold = setupNumberObjectVariant(unitTypes, unit_threshold); } function setupMinResources() { if ((typeof(min_resources) != "object") && (typeof(min_resources) != "number")){ min_resources = 0; } min_resources = setupNumberObjectVariant(resourceTypes, min_resources); } function setupPreliminary() { doc=document; debug = false; attacked_icon = "graphic/command/attack.png"; emptied_icon = "graphic/max_loot/0.png"; goingToFarmMessage = "Ez a script a farmkezelőben működik, ugrás..."; goingToNextPageMessage = "Elfogytak a farmok, váltás a következő oldalra..."; noMoreUnitsMessage = "Nincs több a egység a sablonhoz: "; possibleTemplateMessage = "Lehetséges sablonok: "; waitingTemplateMessage = "Elvárt sablonok továbblépés előtt: "; } function setupParameters() { unitTypes = ["spear", "sword", "axe", "archer", "spy", "light", "heavy", "marcher"]; resourceTypes = ["wood", "clay", "iron"]; if (typeof(auto_next_village) == "undefined") { auto_next_village = 0; } else if (auto_next_village != 0) { auto_next_village = 1; } if(typeof(remove_attacked) == 'undefined'){ remove_attacked = 1; //remove attacked villages by default } else if (remove_attacked != 0) { remove_attacked = 1; } if (typeof(remove_emptied) == 'undefined') { remove_emptied = 1; //remove emptied villages by default } else if (remove_emptied != 0) { remove_emptied = 1; } if (typeof(remove_c_disabled) == "undefined") { remove_c_disabled = 0; } else { remove_c_disabled = 1; } if (typeof(min_resources_sum) != "number") { min_resources_sum = 0; } setupUnitThreshold(); setupMinResources(); if (typeof(what) == "undefined") { what = "c"; } else { what = what.toLowerCase(); } if (typeof(wait) == "undefined") { if (typeof(what) != "undefined") { wait = what; } else { wait = null; } } else { wait = wait.toLowerCase(); } if (typeof(min_wall) == "undefined") { min_wall = 0; } if (typeof(max_wall) == "undefined") { max_wall = 20; } if (typeof(min_time) == "undefined") { min_time = 0; } if (typeof(max_time) == "undefined") { max_time = 0; } if (typeof(max_distance) == "undefined") { max_distance = 0; } goingToNextVillageMessage = "Nincs több egység az elvárt sablonokhoz [" + wait + "], váltás a következő falura..."; greetingMessage = ""; if(isThereNewerVersion()) { greetingMessage += "!!! Új verzió elérhető !!!"; } if (remove_attacked) { greetingMessage += "Támadottak eltávolítva; "; } if (remove_emptied) { greetingMessage += "Üresek eltávolítva; "; } if (auto_next_village) { greetingMessage += "Egységek elfogyása esetén falut vált; " } if (what) { greetingMessage += "A script további nyomkodása [" + what + "] gombnyomást eredményez; "; } } function showInfo() { var htmlToPrepend = ""; if(isThereNewerVersion()) { htmlToPrepend += "[Új verzió: Farm script]"; htmlToPrepend += "[Gyorsgombok szerkesztése]"; htmlToPrepend += "[Panasz, ötlet]"; htmlToPrepend += "
" htmlToPrepend += "A legfrissebb verzió: " + currentVersion; htmlToPrepend += "
" htmlToPrepend += "A te verziód: " + farm_script_version; } else { htmlToPrepend = "[Farm script súgó]"; htmlToPrepend += "[Panasz, ötlet]"; htmlToPrepend += " Verzió: " + currentVersion; } $("#content_value").prepend(htmlToPrepend); } function getHoursElapsed(twDateTimeStr) { var date = new Date(); var thisYear = dateNow.getFullYear(); var thisMonth = dateNow.getMonth(); //calculate year, month, day: var dateStr = twDateTimeStr.match(/\d+\.\d+\./); if (dateStr) { var nums = dateStr[0].split("."); var month = parseInt(nums[1],10)-1; date.setMonth(month); date.setDate(parseInt(nums[0],10)); if (month <= thisMonth) { date.setFullYear(thisYear); } else { date.setFullYear(thisYear-1); } } else { date.setYear(thisYear); date.setMonth(thisMonth); date.setDate(dateNow.getDate()); } //calculate time: var timeStr = twDateTimeStr.match(/\d+\:\d+\:\d+/); var nums = timeStr[0].split(":"); date.setHours(parseInt(nums[0],10)); date.setMinutes(parseInt(nums[1],10)); date.setSeconds(parseInt(nums[2],10)); return (dateNow - date)/1000/60/60; } function getResources(str) { var str = str.replace(/\./g, ""); var nums = str.match(/\d+/g); if (nums) { nums = nums.map(function(e){ return parseInt(e,10); }); } var res = {}; if (nums && nums.length > 0) { res["wood"] = nums[0]; if (nums.length > 1) { res["clay"] = nums[1]; if (nums.length > 2) { res["iron"] = nums[2]; } } } resourceTypes.forEach( function(resourceType){ if (typeof(res[resourceType]) == "undefined") { res[resourceType] = 0; } }); return res; } function hasIcon(farmRow, icon) { var imgs = $(farmRow).find("img"); for (var i = 0; i < imgs.length; ++i) { if (imgs.eq(i).attr("src").indexOf(icon) != -1) { return true; } } return false; } function Farm(farmRow) { var cells = $("td", farmRow); this.row = farmRow; this.visible = true; this.targetVillageID = parseInt(farmRow.className.match(/\d+/)[0],10); this.reportID = parseInt(cells[3].innerHTML.match(/view\=\d+/)[0].split("=")[1],10); this.isAttacked = hasIcon(farmRow, attacked_icon); this.isEmptied = hasIcon(farmRow, emptied_icon); this.hoursElapsed = getHoursElapsed(cells[4].innerHTML); this.wall = parseInt(cells[6].innerHTML, 10); this.distance = parseFloat(cells[7].innerHTML); this.resources = getResources(cells[5].innerText || cells[5].textContent); //ff uses textContent this.links = { "a": $("a", cells[8]), "b": $("a", cells[9]), "c": $("a", cells[10]) }; this.startingStatus = { "a": this.isEnabled("a"), "b": this.isEnabled("b"), "c": this.isEnabled("c") }; } Farm.prototype.hide = function() { $(this.row).hide(); this.visible = false; }; Farm.prototype.show = function() { $(this.row).show(); this.visible = true; }; Farm.prototype.click = function(nowWhat) { if (this.visible) { if (this.links[nowWhat].length != 0) { this.links[nowWhat].click(); } else { this.hide(); } } }; Farm.prototype.handleExternalClick = function(nowWhat) { if (this.isEnabled(nowWhat)) { ++numRequests; pendingList.push(this); } this.hide(); }; Farm.prototype.isEnabled = function(nowWhat) { if (this.links[nowWhat].length > 0) { return !($(this.links[nowWhat]).hasClass("farm_icon_disabled")); } else { return false; } }; Farm.prototype.isFullyDisabled = function() { return !(this.isEnabled("a") || this.isEnabled("b") || this.isEnabled("c")); }; Farm.prototype.forceEnable = function(nowWhat) { this.startingStatus["nowWhat"] = true; $(this.links[nowWhat]).removeClass("farm_icon_disabled"); }; Farm.prototype.restoreStatus = function() { if (this.startingStatus["a"]) { this.forceEnable("a"); } if (this.startingStatus["b"]) { this.forceEnable("b"); } if (this.startingStatus["c"]) { this.forceEnable("c"); } }; Farm.prototype.getSumResources = function() { return this.resources.wood + this.resources.clay + this.resources.iron; } Farm.prototype.toString = function() { var str = ""; str += "wood: " + this.resources["wood"] + "; "; str += "clay: " + this.resources["clay"] + "; "; str += "iron: " + this.resources["iron"] + "; "; return str; }; //unused: Farm.prototype.synthetizeCFarmFunction = function() { this.links.c[0].onclick = synthetizedCFarmFunction; } function getKey(farmRow) { var cells = $("td", farmRow); var link = $("a", cells[11]); return link.attr("href"); } function getFarm(farmLink) { var farmRow = $(farmLink).closest("tr"); return farmMap[getKey(farmRow)]; } //unused: function synthetizedCFarmFunction() { var farm = getFarm(this); return Accountmanager.farm.sendUnitsFromReport(this, farm.targetVillageID, farm.reportID); } function createFarms() { var rows = $("tr.row_a, tr.row_b", farmWidget); for (var idx = 0; idx < rows.length; ++idx) { var row = rows[idx]; var farmObject = new Farm(row); farmList.push(farmObject); farmMap[getKey(row)] = farmObject; pendingList = new Array(); } } function getTemplate(farmLink) { if ($(farmLink).hasClass("farm_icon_a")) { return "a"; } if ($(farmLink).hasClass("farm_icon_b")) { return "b"; } if ($(farmLink).hasClass("farm_icon_c")) { return "c"; } } function setupExternalClickHook() { $("a.farm_icon_c, a.farm_icon_b, a.farm_icon_a").click(function() { getFarm(this).handleExternalClick(getTemplate(this)); }); } function handlePending() { if (numRequests <= 0) { numRequests = 0; var succ = 0; var unSucc = 0; for (var i = 0; i < pendingList.length; ++i) { farm = pendingList[i]; if (farm.isFullyDisabled()) { ++succ; } else { farm.show(); console.log("unsuccessful: " + farm.toString()); ++unSucc; } } pendingList = new Array(); if (debug) { console.log("Successful: " + succ + "; unsuccessful: " + unSucc); } } else { if (debug) { console.log("Pending requests: " + numRequests); } } } function setupTW_handleResponseHook() { original_handleResponse = TribalWars.handleResponse; TribalWars.handleResponse = function(data, callback, error_callback) { var ret = original_handleResponse.call(TribalWars, data, callback, error_callback); --numRequests; handlePending(); return ret; }; } //result form: e.g. [{name: spy, value: 1}, {name: light, value: 30}] function getTemplateUnits(form) { var inputs = $("input[type=text]", form); units = new Array(); for (var i = 0; i < inputs.length; ++i) { var element = $(inputs[i]); var value = parseInt(element.attr("value"),10); if (value > 0) { units.push({ name: element.attr("name") ,value: value}); } } return units; } function setupLogic() { dateNow = new Date(); farmWidget = doc.getElementById("am_widget_Farm"); checkedUnits = $("#farm_units").serialize(); //e.g. "light=on&marcher=on&heavy=on" checkedUnits = checkedUnits.replace(/=on/g, ""); //e.g. "light&marcher&heavy" checkedUnits = checkedUnits.split("&"); //e.g. ["light", "marcher", "heavy"] var forms = $("form"); templates = {}; templates["a"] = getTemplateUnits(forms[0]); templates["b"] = getTemplateUnits(forms[1]); remove_attacked_prev = remove_attacked; remove_emptied_prev = remove_emptied; farmList = new Array(); farmMap = {}; createFarms(); setupExternalClickHook(); setupTW_handleResponseHook(); numRequests = 0; } function areThereEnoughResourcesPerType(farm) { var ok = true; resourceTypes.forEach( function(resourceType) { if (farm.resources[resourceType] < min_resources[resourceType]) { ok = false; } }); return ok; } function filterFarms() { farmList.forEach(function(farm) { if (remove_attacked && farm.isAttacked) { farm.hide(); return; } if (remove_emptied && farm.isEmptied) { farm.hide(); return; } if (remove_c_disabled && !farm.isEnabled("c")) { farm.hide(); return; } if (farm.wall < min_wall) { farm.hide(); return; } if (farm.wall > max_wall) { farm.hide(); return; } if (max_distance > 0 && farm.distance > max_distance) { farm.hide(); return; } if (farm.hoursElapsed < min_time) { farm.hide(); return; } if (max_time > 0 && farm.hoursElapsed > max_time) { farm.hide(); return; } if (!areThereEnoughResourcesPerType(farm)) { farm.hide(); return; } if (farm.getSumResources() < min_resources_sum) { farm.hide(); return; } }); } function trySetup() { setupPreliminary(); if (typeof(settingUp) == "undefined") { settingUp = true; } else { console.log("already setting up"); return; } if (game_data.screen!="am_farm") { UI.InfoMessage(goingToFarmMessage, 4000); self.location=game_data.link_base_pure.replace(/screen\=\w*/i, "screen=am_farm"); return false; } if (game_data.player.premium == false) { alert("Prémium kéne na."); return false; } showInfo(); setupParameters(); setupLogic(); filterFarms(); UI.InfoMessage(greetingMessage, 7000); setupDone = true; return true; } function getNextVisibleFarm() { for (var i = 0; i < farmList.length; ++i) { if (farmList[i].visible) { return farmList[i]; } } return null; } function getLastPageIdx() { var nums = $("a.paged-nav-item, strong").text().match(/\d+/g); if (nums) { return parseInt(nums[nums.length-1],10)-1; } else { return 0; } } function nextPage() { UI.InfoMessage(goingToNextPageMessage, 2000); var pageStrArr = doc.URL.match(/Farm_page[\=][\d]+/i); var lastPageIdx = getLastPageIdx(); if (!pageStrArr) { //first page if (lastPageIdx == 0) { nextVillage(); } else { self.location=doc.URL + "&Farm_page=1"; } } else { var numStr = pageStrArr[0].match(/[\d]+/)[0]; var idx = parseInt(numStr, 10); if (lastPageIdx == idx) { nextVillage(); } else { ++idx; self.location = doc.URL.replace(/Farm_page[\=][\d]+/i, "Farm_page=" + idx); } } } function getAvailableUnits(unitName) { var unitStr = $("#units_home #" + unitName).text(); return parseInt(unitStr, 10); } function areThereUnitsToSend(nowWhat) { if (nowWhat == "c") { var ok = false; checkedUnits.forEach( function(unitType) { if (getAvailableUnits(unitType) > unit_threshold[unitType]) ok = true; }); return ok; } else if (nowWhat == "a" || nowWhat == "b") { var ok = true; templates[nowWhat].forEach( function (unitDescription) { //e. g. {name: "spy", value: 1} var num = getAvailableUnits(unitDescription.name); if ( num < unitDescription.value || (num - unitDescription.value) < unit_threshold[unitDescription.name]) { ok = false; } }); return ok; } } function haveToWait(nowWhat) { if (wait.indexOf(nowWhat) != -1) { return areThereUnitsToSend(nowWhat); } } function getPossibleTemplates() { var str = ""; if (areThereUnitsToSend("a")) { str += "a"; } if (areThereUnitsToSend("b")) { str += "b"; } if (areThereUnitsToSend("c")) { str += "c"; } return str; } function canGoToNextVillage() { return auto_next_village && (!(haveToWait("a") || haveToWait("b") || haveToWait("c"))); } function nextVillage() { UI.InfoMessage(goingToNextVillageMessage, 2000); self.location = $('a#village_switch_right').attr('href').replace(/Farm_page[\=][\d]+/i, "Farm_page=0"); } function farmClick() { if (typeof(setupDone) == "undefined") { console.log("wait for setup"); return; } var nextFarm = getNextVisibleFarm(); if (!nextFarm) { if (canGoToNextVillage()) { nextVillage(); } else { nextPage(); } } else { if (canGoToNextVillage()) { nextVillage(); } else if (areThereUnitsToSend(what)) { nextFarm.click(what); } else { UI.ErrorMessage(noMoreUnitsMessage + " [" + what + "] " + possibleTemplateMessage + "[" + getPossibleTemplates() + "] " + waitingTemplateMessage + "[" + wait + "]", 3000); } } } function needsToSetup() { if (typeof(firstRun) == "undefined") return true; return false; } function trackFilterSettings() { if (remove_attacked_prev != remove_attacked) { filterFarms(); remove_attacked_prev = remove_attacked; } if (remove_emptied_prev != remove_emptied) { filterFarms(); remove_emptied_prev = remove_emptied; } } function trackBehaviorSettings() { wait = wait.toLowerCase(); what = what.toLowerCase(); setupUnitThreshold(); setupMinResources(); } function advancedFarmScript() { if (needsToSetup()) { trySetup(); firstRun = false; } else { trackFilterSettings(); trackBehaviorSettings(); if (what) { farmClick(); } } } advancedFarmScript(); void(0);