userscripts/grepolis.user.js

219 lines
6.8 KiB
JavaScript

// ==UserScript==
// @name Grepolis Village Farmer
// @description Farms villages every 5minutes
//
// @author Jan Bader <jan@javil.eu>
// @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 <jan@javil.eu>
//
// @match http://*.grepolis.com/game/*
//
// @version 1.1.26
// @updateURL http://git.javil.eu/jan/userscripts/raw/master/grepolis.user.js
// ==/UserScript==
(function() {
farmDuration = 300;
lastAmounts = "";
console.log("Initializing...");
windows = {};
durations = {};
switch(location.host.substring(0,4)) {
case "de63":
farm_ids = {
// Insel 0
83792: [20468, 20470, 20471, 20472],
84064: [20466, 20467],
// Insel 1
85523: [20489, 20490, 20492, 20496, 20491, 20495],
// Insel 2
74298: [19577, 19579, 19583, 19584, 19582, 19581],
}
towns = {
83792: "0x00",
84064: "0x01",
85523: "0x10",
74298: "0x20"
}
break;
case "de67":
farm_ids = {
// Insel 0
4809: [1510, 1509, 1508, 1505],
}
towns = {
4809: "0x00",
}
break;
}
setTimeout(function() {
var logGPWindow = new GPWindow(99999, null);
logGPWindow.createWindow();
logWindowElement = $(logGPWindow.getElement());
var logWindowWrapper = logWindowElement.parent().parent();
logWindowWrapper.css({
"position": "absolute",
"z-index": 999999999,
"right": "16px",
"bottom": 0,
"width": "300px",
"height": "300px",
"align": "left",
"left": "auto",
"top": "auto"
});
logWindowElement.empty();
logWindowElement.append('<div id="timerwindow" /><div id="logwindow">');
logWindow = $("#logwindow");
timerWindow = $("#timerwindow");
var s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = 'function j_countdown(t, id){'
+'var h = Math.floor(t/(60*60));'
+'var m = Math.floor(t/60) %60;'
+'var s = Math.floor(t %60);'
+'var strZeit = "";'
+'if(h>0) strZeit = strZeit + h + ":";'
+'strZeit = strZeit + m + ":" + s;'
+'durations[id]=durations[id]-1;'
+'window.setTimeout(\'j_countdown(\'+durations[id]+\',\\\'\'+id+\'\\\')\',1000);'
+'document.getElementById(id).innerHTML = strZeit;}';
document.getElementsByTagName("head")[0].appendChild(s);
for(var town_id in farm_ids) {
var town_countdown = document.createElement("span");
town_countdown.textContent = "" + towns[town_id] + ":";
timerWindow.append(town_countdown);
farm_ids[town_id].forEach(function(xFarmId) {
var tmout = 20000 + rand(10);
setTimeout(getWaresTick(town_id, xFarmId), tmout);
var tmoutId = 'farm-' + xFarmId
$(town_countdown).append('<span id="' + tmoutId + '"><script>j_countdown(' + Math.floor(tmout/1000) + ', "' + tmoutId + '");</script></span>, ');
});
$(town_countdown).append('<br />');
}
$("#gpwnd_" + logGPWindow.getID()).parent().parent().find(".ui-dialog-titlebar").empty().append("<select id=choosefarmduration><option value=-1>disable</option><option value=300 selected>5min</option><option value=600>10min</option><option value=1200>20min</option><option value=2400>40min</option><option value=5400>90min</option><option value=10800>3h</option><option value=14400>4h</option><option value=28800>8h</option></select>" + GM_info.script.version);
var choose = $("#choosefarmduration")[0];
choose.onchange = function() {
farmDuration = this.value;
log("Duration changed to: " + farmDuration);
}
}, 15000);
function log(xText) {
var date = new Date(); // Timestamp to prepend
var timestamp = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toJSON();
logWindow.append('<span title="' + timestamp +'">' + xText + "</span><br />");
logWindow.scrollTop(logWindow[0].scrollHeight);
}
function rand(min, max) {
return Math.floor((Math.random() * (max-min) * 1000) + min * 1000);
}
function rand(max) {
return Math.floor((Math.random() * max * 1000));
}
function getWaresTick(xTownId, xFarmId) {
return function() {
var timeout = 60000;
try {
var amounts = $.map($(".indicator .amount"),function(v){return $(v).text()}).join(", ");
if(amounts != lastAmounts) {
log(amounts);
lastAmounts = amounts;
}
var windowId = windows[xFarmId];
if(windowId == undefined){
if(farmDuration < 300)
return;
var wnd = GPWindowMgr.Create(
GPWindowMgr.TYPE_FARM_TOWN, '',
{'action': "claim_info"},
xFarmId);
windows[xFarmId] = wnd.getID();
timeout = 500;
return;
}
var wnd = GPWindowMgr.getWindowById(windowId);
if(wnd == undefined){
windows[xFarmId] = undefined;
timeout = rand(20);
return;
}
var wndElement = wnd.getElement();
if(wndElement == undefined){
windows[xFarmId] = undefined;
try {
wnd.close();
} catch(e) {
}
timeout = rand(20);
return;
}
var mood = parseFloat($(wndElement).find('#mood1').siblings().last().text());
var ts = $(wndElement).find(".farm_next_claim_time").text();
if(ts != "bereit") {
var hours = ts.substring(0,1);
var minutes = ts.substring(2,4);
var seconds = hours * 60 * 60 + minutes * 60 + ts.substring(5,7) * 1;
log("Wait " + ts + " for Farm " + xFarmId);
wnd.close();
timeout = seconds * 1000 + 1000;
return;
}
var loots = $(wndElement).find("#farmtown_loot").children().last().text().split('/');
var loot_current = parseFloat(loots[0]);
var loot_maximum = parseFloat(loots[1]);
var count_per_loot = parseFloat($(wndElement).find(".farm_claim_res0").text());
if(loot_current + count_per_loot*3 > loot_maximum) {
log("Wait 15:00 for Farm " + xFarmId + " to reset");
wnd.close();
timeout = 15 * 60 * 1000;
return;
}
timeout = getWaresFromFarm(wnd, mood, xTownId, xFarmId) * 1000 + rand(5);
} finally {
durations["farm-" + xFarmId] = timeout / 1000;
setTimeout(getWaresTick(xTownId, xFarmId), timeout);
}
}
}
function getWaresFromFarm(xWindow, xMood, xTownId, xFarmId) {
if(Game.town_id != xTownId) {
Game.townId = xTownId;
}
if(xMood - 6 > 80) {
log("Plündere " + xFarmId + " @ Mood: " + xMood);
xWindow.call('claimLoad', xFarmId.toString(), 'double', 300);
return 300;
} else {
log("Fordere " + xFarmId + " @ Mood: " + xMood);
xWindow.call('claimLoad', xFarmId.toString(), 'normal', farmDuration);
return farmDuration;
}
}
function buttonIsInitialized() {
return typeof(btn) != "undefined" && btn != null && btn[0] != null;
}
})()