Ikariam Auto Builder HACK

Ikariam Auto Builder HACK lets you add your building to an upgrading queue (YES! NOW YOU CAN DO IT FOR FREE!).

اعتبارا من 05-03-2016. شاهد أحدث إصدار.

// ==UserScript==
// @name       Ikariam Auto Builder HACK
// @namespace  http://google.com/
// @version    3.0.6
// @description  Ikariam Auto Builder HACK lets you add your building to an upgrading queue (YES! NOW YOU CAN DO IT FOR FREE!). 
// @unwrap
// @include      *://*.ikariam.gameforge.com/*
// @match	*://*.ikariam.gameforge.com/*
// @copyright  2015+, You\
// @require http://code.jquery.com/jquery-latest.min.js
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==

var updateInterval = 600000; //In ms (1000 ms = 1s). How often the upgrader will make checks.
var timerUpdateInterval = 1000; //In ms (1000 ms = 1s). This indicates how often will be the timer refreshed. RECOMMENDED: 1000

//TRANSLATIONS//
var addToQueueText = "Add to queue"; //This appears on the button
var queueEmptyText = "The building queue is empty in the current city"; //This appears when the queue is empty



var queuePrefix = "IAcity_"; //do NOT change this!
var separator = ";"; ////do NOT change this!


function callFunc(f,params){
	f.apply(this, params);   
}

unsafeWindow.doAfterLoading = function doAfterLoading(f){
    if($("div#loadingPreview").css("display") == "none"){
		var params = Array.prototype.slice.call(arguments);
		params.shift();
        alert(params[0]);
        f.apply(this,params);
    }
    else setTimeout(callFunc,100, doAfterLoading, arguments);
}



function clickCityInDropDown(id){
    $("div#dropDown_js_citySelectContainer div.bg ul li")[id-1].click();
}

unsafeWindow.goToCity = function(id){
    $("div#js_citySelectContainer span").click();
    setTimeout(clickCityInDropDown, 100, id);
}

unsafeWindow.getCityName = function(){
    return $("div#js_citySelectContainer span a").text();   
}

unsafeWindow.getQueue = function getQueue(){
	var raw = getValue(queuePrefix + getCityName());
    if(raw === undefined) return [];
	return raw.split(separator);
}

unsafeWindow.addToQueue = function addToQueue(id){
	var before = getValue(queuePrefix + getCityName());
    if(before === undefined){
    	setValue(queuePrefix + getCityName(), id);   
    }
    else setValue(queuePrefix + getCityName(), before + separator + id);
    updateQueueView();
}

unsafeWindow.removeFromQueue = function removeFromQueue(number){
	var before = getValue(queuePrefix + getCityName());
    if(before !== undefined){
    	var after = "";
        var items = before.split(separator);
        for(var i = 0; i < items.length; i++){
        	if(i != number) after = after + items[i] + separator;    
        }
        
        after = after.substring(0,after.length-1);
        
        if(after == ""){
        	deleteValue(queuePrefix + getCityName());   
        }
        else {
            setValue(queuePrefix + getCityName(), after);   
        }
    }
    updateQueueView();
	
}


function upgradeBuilding(id, cityCount){
    
    if($("div.accordionContent").length == 0){
    	setTimeout(upgradeBuilding, 100, id, cityCount);
        return;
    }
    
    if($("div#buildingUpgrade ul.resources li.red").length == 0){
        $("ul.actions li.upgrade a").click();
        removeFromQueue(0); 
        setTimeout(closeWindow, 100, id, cityCount, true);
        
    }
    else setTimeout(closeWindow, 100, id, cityCount, false);
}


function closeWindow(id, cityCount, waitUntilUpgradeStarts){
    if(waitUntilUpgradeStarts){
        if($("div#upgradeProgress").length == 0){
            setTimeout(closeWindow,100,id,cityCount, waitUntilUpgradeStarts);
            return;
    	}
        else {   
        }
    }
    //alert("Upgrade started! Upgrade bars: " + $("div#upgradeProgress").length);
    
    
    $("div.close").click();
    //alert("Closed! Next ID = " + (id+1) + "; cityCount = " + cityCount);
    if(id + 1 <= cityCount) {
        console.log("Next city ID = " + (id+1) +". Calling processCity()!");
    	setTimeout(processCity, 1000, id+1, cityCount);	
    }   
    else location.reload();
    
}

function doWorkInCity(id, cityCount){

if($("div#loadingPreview").css("display") != "none") {
	setTimeout(doWorkInCity, 100, id, cityCount);
    return;
}
		
        var q = getQueue();
        if(q.length != 0){
            if($("div.timetofinish").length == 0){
                openBuilding(q[0]);
                setTimeout(upgradeBuilding, 100, id, cityCount);
            }
            else {
            	   if(id + 1 <= cityCount) setTimeout(processCity, 100, id+1, cityCount);
                        else location.reload();
            }
        	
            
        }
        else if(id + 1 <= cityCount) setTimeout(processCity, 100, id+1, cityCount);
                        else location.reload();
}

unsafeWindow.processCity = function processCity(id, cityCount){
    if($("div.accordionContent").length != 0){
		setTimeout(processCity, 100, id, cityCount);
        return;
    }
    
    goToCity(id);
    setTimeout(doWorkInCity, 1000, id, cityCount);
}

unsafeWindow.getBuildingTitle = function getBuildingName(id){
    return $("a#js_CityPosition" + id + "Link").attr('title');   
}

unsafeWindow.updateQueueView = function(){
    $("div#IA_queue").text("");
    var queue = getQueue();
    if(queue.length != 0){
        for(var i = 0; i < queue.length; i++){
            $("div#IA_queue").append('<p align="center" style="cursor:default;">' + getBuildingTitle(queue[i]) + '<a onclick="removeFromQueue('+i+')" style="cursor:pointer;">&#10006;</a></p>');   
        }
    }
    else $("div#IA_queue").append('<p align="center">' + queueEmptyText + '</p>'); 
    
    
    //$("div#IA_queue").css("text-align", "center");
    //$("div#IA_queue").prepend('<img src="http://simpleicon.com/wp-content/uploads/refresh.png" alt="Refresh queue" style="cursor:pointer; height: 1em; " onclick="updateQueueView()">');
}
unsafeWindow.update = function update(){
	setValue("IA_lastUpdate", Date.now());
    var cityCount = getCityCount();
    processCity(1, cityCount);
}


unsafeWindow.openBuilding = function openBuilding(id){
	$("a#js_CityPosition" + id + "Link").click();
}    

unsafeWindow.getCityCount = function getCityCount(){
    return $("li.ownCity").length;   
}


setValue = function setValue(key,value) {
      return localStorage[key]=value;
  }

getValue = function getValue(key,def) {
      return localStorage[key] || def;
  }
 
deleteValue = function deleteValue(key) {
      return delete localStorage[key];
  }



/*if(buildingUpgradeElem !== undefined) { 
    if($("div#IAButtonContainer").length == 0) {
   		buildingUpgradeElement.append('<div style="margin: 20px;" id="IAButtonContainer"><a class="button" id="IAButton" onclick="addToQueue('+ buildingId + ');">' + addToQueueText + '</a></div>');
    }
}*/ //Repeat this to restore add to queue button after some actions. NOT recommended because of possibly huge CPU usage. Just re-enter the building except.
if(getValue("IA_lastUpdate") === undefined) setTimeout(start,5000);
else start();


function timerUpdateFunc(  ){
    var time = parseInt(getValue("IA_lastUpdate")) + parseInt(updateInterval) - parseInt(Date.now());
    time = Math.floor(time / 1000); //seconds
    var minutes = Math.floor(time / 60); 
    time -= minutes * 60; 
    if (time < 10) time = "0"+time; //replace 2:9 with 2:09
    $("li#IA_timer").text(minutes + ":" + time);



    if(minutes < 0){
        update();   
    }
}

function addButton(buildingId){
    
    
    if($("div.accordionContent").length != 0 && $("div#IAButtonContainer").length == 0) $("div#buildingUpgrade").append('<div style="margin: 20px;" id="IAButtonContainer"><a class="button" id="IAButton" onclick="addToQueue('+ buildingId + ');">' + addToQueueText + '</a></div>'); 
    else setTimeout(addButton, 100, buildingId);
}




function start(){
    if($("#btn-login").length > 0) return;
    
    
    try{
        getCityCount();
    }
    catch(e){
       alert("Sorry, Ikariam Auto Builder Hack won't work in this browser. Please use Chrome or Opera!");
    }
    
    if(getValue("IA_lastUpdate") === undefined) {
        
        alert("Welcome to Ikariam Auto Builder Hack!\nAfter you click OK, the hack will go through all your cities. It is required for the script to work!\n\nHelp on script usage can be found here:\nhttp://goo.gl/myneJb");
        update();
    }

    $("a[id^='js_CityPosition']").click(function(){
        var htmlID = $(this).attr('id');
        var id = htmlID.split("Link")[0].substring(15,htmlID.length + 1 - 4);
        setTimeout(addButton, 100, id);
    });
    
    $("head").append("<style>div.addToQueueCityButton {z-index: 9000; cursor: pointer; position: absolute;right: 0px;bottom: 0px;border-top-left-radius: 3px;border-top-right-radius: 3px;border-bottom-left-radius: 3px;border-bottom-right-radius: 3px; background-color: rgba(200,200,200, 1); -webkit-transition: background-color 300ms linear;-moz-transition: background-color 300ms linear;-o-transition: background-color 300ms linear;-ms-transition: background-color 300ms linear;transition: background-color 300ms linear;} div.addToQueueCityButton:hover { background-color: rgba(200,100,100,1);}</style>");
    
    
    $("div.city_water_bottom").append('<div id="IA_queue" style="background-color: #DDD; width: 300px; position: absolute; left: 810px;"></div>');

    $("DIV#GF_toolbar ul").append('<li id="IA_timer"></li> <img id="doCheckButton" src="http://simpleicon.com/wp-content/uploads/refresh.png" style="height: 1em; cursor:pointer;" onclick="update()">');

	updateQueueView();
    
    setInterval(updateQueueView,1000);
    
    timerUpdateFunc();
    
    setInterval(timerUpdateFunc, timerUpdateInterval);
}