// ==UserScript== // @name Grepolis Village Farmer // @description Farms villages every 5minutes // // @author Jan Bader // @downloadURL http://git.javil.eu/jan/userscripts/raw/master/grepolis.user.js // // @license GPLv3 - http://www.gnu.org/licenses/gpl-3.0.txt // @copyright Copyright (C) 2014, by Jan Bader // // @match http://*.grepolis.com/game/* // // @version 1.0.32 // @updateURL http://git.javil.eu/jan/userscripts/raw/master/grepolis.user.js // ==/UserScript== (function() { console.logCopy = console.log.bind(console); console.log = function() { var date = new Date(); // Timestamp to prepend var timestamp = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON(); if (arguments.length) { var args = Array.prototype.slice.call(arguments, 0); // If there is a format string then... it must be a string if (typeof arguments[0] === "string") { args[0] = "%o: " + arguments[0]; args.splice(1, 0, timestamp); this.logCopy.apply(this, args); } else { this.logCopy(timestamp, args); } } }; console.log("Initializing..."); windows = {}; farm_ids = [1225, 1226, 1229, 1230, 1231]; farm_ids.forEach(function(xFarmId) { console.log("Setting timer to 5s for Farm " + xFarmId); setTimeout(getWaresTick(xFarmId), 5000); }); setTimeout(getWaresTick, 5000); function getWaresTick(xFarmId) { return function() { var d = new Date(); var dformat = "[" + [d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()].join(':') + "]"; $('.town_name').text(dformat); console.log($.map($(".indicator .amount"),function(v){return $(v).text()}).join(", ")); var windowId = windows[xFarmId]; if(windowId == undefined){ console.log("Opening new Window"); var wnd = GPWindowMgr.Create( GPWindowMgr.TYPE_FARM_TOWN, '', {'action': "claim_info"}, xFarmId); windows[xFarmId] = wnd.getID(); setTimeout(getWaresTick(xFarmId), 500); return; } var wnd = GPWindowMgr.getWindowById(windowId); if(wnd == undefined){ console.log("Window not open any more"); setTimeout(getWaresTick(xFarmId), 50); return; } var wndElement = wnd.getElement(); if(windowId == undefined){ console.log("Window not reachable any more"); try { wnd.close(); } catch(e) { console.warn("Could not close Window"); } setTimeout(getWaresTick(xFarmId), 50); return; } var btn = $(wndElement).find(".farm_claim_box .farm_claim_res0 + div + a"); var mood = parseFloat($(wndElement).find('#mood1').siblings().last().text()); if(btn.hasClass("farm_claim_button_inactive")) { var ts = $(".farm_next_claim_time").text(); if(ts != "bereit") { var minutes = ts.substring(2,4); var seconds = minutes * 60 + ts.substring(5,7) * 1; $('.town_name').text(dformat + " +" + seconds + "s"); console.log("btn is inactive, trying again in " + seconds + " seconds"); wnd.close(); setTimeout(getWaresTick(xFarmId), seconds * 1000 + 1000); return; } } getWaresFromFarm(btn[0], mood, xFarmId); setTimeout(getWaresTick(xFarmId), 60 * 1000); } } function getWaresFromFarm(xGetWaresButton, xMood, xFarmId) { if(xMood - 6 > 80) { console.log("Plündere " + xFarmId + " @ Mood: " + xMood); w(xGetWaresButton).call('claimLoad', xFarmId.toString(), 'double', 300); } else { console.log("Fordere " + xFarmId + " @ Mood: " + xMood); w(xGetWaresButton).call('claimLoad', xFarmId.toString(), 'normal', 300); } } function buttonIsInitialized() { return typeof(btn) != "undefined" && btn != null && btn[0] != null; } })()