Set Arrival Time

Set the desired arrival time in Tribal Wars and the script will automatically send the attack

Stan na 06-04-2018. Zobacz najnowsza wersja.

// ==UserScript==
// @name Set Arrival Time
// @description Set the desired arrival time in Tribal Wars and the script will automatically send the attack
// @author FunnyPocketBook
// @version 2
// @date 03-10-2017
// @namespace FunnyPocketBook
// @include https://*/game.php?village=*&screen=place&try=confirm
// ==/UserScript==

// CHANGE THESE NUMBERS TO GET THE CORRECT ARRIVAL TIME - higher number means more delay. 
// Change "windows.delayTime" before "intervalTime" and only change "intervalTime" if you 
// can't get it quite correct with only "delayTime"
window.delayTime = 11; // Set delay in ms
window.intervalTime = 10; // Set interval in ms

// Create delay input
var savedInput = localStorage.getItem("savedInput");
if(savedInput == null) {
    savedInput = "";
}
var referenceNode = document.querySelector("#place_confirm_units"); // Place after this
var delayInput = document.createElement("input");
delayInput.setAttribute("id", "delayInput");
delayInput.setAttribute("value", "savedInput");
referenceNode.parentNode.insertBefore(delayInput, referenceNode.nextSibling);

// Create okay button to save delay
var delayButton = document.createElement("a");
delayButton.setAttribute("class", "btn");
delayButton.setAttribute("id", "delayButton");
$("#delayButton").onclick(function() {
    var delayTime = $("#delayInput")
})


var arrival;
var reloadBefore = 1; // Reload page 1 minute before attack is supposed to be sent
/*
*  Save tab ID
*/
var tabID = sessionStorage.tabID ? sessionStorage.tabID : sessionStorage.tabID = Math.random(); // Generate ID for tab
console.log("tabID: " + tabID);
var storedID = JSON.parse(localStorage.getItem("storedID")); // Get tab ID
console.log("storedID: " + storedID);
if(storedID == null) { // See if any tab IDs already exist, if not, initialize the array
    storedID = [];
    console.log("storedID = " + storedID);
}
if(!storedID.includes(tabID) && !storedID.includes(parseFloat(tabID))) { // If the tab ID of the current tab is not stored yet, push the array
    storedID.push(tabID);
    console.log("storedID pushed: " + storedID);
}
localStorage.setItem("storedID", JSON.stringify(storedID));
console.log("localStorage id set to " + localStorage.getItem("storedID"));

/*
*  Reload boolean - set to true to reload, false to not reload
*/
var reload = JSON.parse(localStorage.getItem("reload")); // Get reload
if(reload == null) {
    reload = [];
}
console.log("Reload: " + reload);
localStorage.setItem("reload", JSON.stringify(reload));

/*
*  Get stored times
*/
var index = storedID.indexOf(tabID); // Get index of time of this tab
var storedTimes = JSON.parse(localStorage.getItem("storedTimes")); // Get stored times
if(storedTimes == null) { // If there are no times yet, initialize array
    storedTimes = [];
}
console.log("storedTimes = " + storedTimes);
localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
console.log("localStorage times set to " + localStorage.getItem("storedTimes"));
var fullTime = storedTimes[index];
if(fullTime != null) {
    window.input = fullTime.slice(0, 8);
    window.ms = fullTime.slice(9);
}

window.showArrTimeTr = document.createElement("tr"); 
window.showArrTimeTd = document.createElement("td");
var pEle = document.getElementById("troop_confirm_go"); // Button comes after this element
var btn = document.createElement("a"); // Create button called btn as a link because any button causes the attack to launch
btn.setAttribute("id", "arrTime"); 
btn.setAttribute("class", "btn"); 
btn.setAttribute("style", "cursor:pointer;");
pEle.parentNode.insertBefore(btn, pEle.nextElementSibling); // Place btn after pEle
var t = document.createTextNode("Set arrival time"); 
btn.appendChild(t); 

var dateArrival = document.getElementById("date_arrival"); // Cell of relative_time
window.showArrTimeTr.appendChild(window.showArrTimeTd);
dateArrival.parentNode.parentNode.insertBefore(window.showArrTimeTr, dateArrival.parentNode[1]); // Insert tablerow as last cell
// Display the set time if exist, else say "No time set"
if(window.input != "" || window.ms != "") {
    window.showArrTimeTd.innerHTML = "You set the arrival time to: ~" + window.input + ":" + window.ms;
} else {
    window.showArrTimeTd.innerHTML = "You set the arrival time to: No time set";
}
window.showArrTimeTd.setAttribute("colspan", "2");
window.showArrTimeTd.setAttribute("id", "showArrTime");

btn.onclick = function() {
	"use strict";
	var time = document.getElementsByClassName("relative_time")[0].textContent.slice(-8); // Get the time of arrival if sent now
	window.input = prompt("Please enter desired arrival time", time); // Save desired time
	window.ms = parseInt(prompt("Please enter approximate milliseconds", "000")); // Save desired ms
    var setTime = window.input + ":" + window.ms;
    window.showArrTimeTd.innerHTML = "You set the arrival time to: ~" + window.input + ":" + window.ms;
    storedTimes[index] = setTime; // Add new time to the array, same index as the tab ID
    localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
    reload[index] = true;
    localStorage.setItem("reload", JSON.stringify(reload));
};
console.log(window.input + ":" + window.ms);
console.log(window.delayTime);
var delay = window.delayTime + parseInt(window.ms); // setTimeout time
var intervalCheck = setInterval(function retime() {
    "use strict";
    arrival = document.getElementsByClassName("relative_time")[0].textContent; // Get time of arrival if sent now for comparison
    delay = window.delayTime + parseInt(window.ms); // setTimeout time
    if(arrival.slice(-8) === window.input) {
        console.log("inside");
        setTimeout(function() {
            console.log("sent");
            document.getElementById("troop_confirm_go").click();
            // Clear set time for this tab and save again in localStorage
            storedTimes[index] = ""; 
            localStorage.setItem("storedTimes", JSON.stringify(storedTimes));
        }, delay);
        clearInterval(intervalCheck);
    }
}, window.intervalTime);

var minute = window.input.substr(3,2) - reloadBefore; // Get minute of input and subtract reloadBefore from it
var arriveNow = document.getElementsByClassName("relative_time")[0].textContent.slice(-8); // Get relative_time to compare
var timeReload;
setInterval(function() {
    minute = window.input.substr(3,2) - reloadBefore;
    arriveNow = document.getElementsByClassName("relative_time")[0].textContent.slice(-8);
    timeReload = window.input.substr(0,3) + minute + window.input.substr(5,3); // Time stitched together with the minute subtracted by reloadBefore
    if(timeReload != -1 && arriveNow === timeReload && reload[index]) {
        reload[index] = false;
        localStorage.setItem("reload", JSON.stringify(reload));
        window.location.reload();
    }
}, 1000);