WFMario / Colony Tweaks

// ==UserScript==
// @name         Colony Tweaks
// @version      2024-08-01
// @author       Mario
// @license MIT
// @match        https://*.atmoburn.com/view_colony.php
// @match        https://*.atmoburn.com/view_colony.php?colony=*
// @match        https://*.atmoburn.com/build_facility.php?colony=*
// @match        https://*.atmoburn.com/shipyard.php?colony=*
// @match        https://*.atmoburn.com/fleet_configuration.php?colony=*
// @match        https://*.atmoburn.com/scientists.php?colony=*
// @match        https://*.atmoburn.com/colony_trade.php?colony=*
// @match        https://*.atmoburn.com/shiptrade.php?colony=*
// @match        https://*.atmoburn.com/colony_setup.php?colony=*
// @match        https://*.atmoburn.com/building_demolish.php?colony=*
// @match        https://*.atmoburn.com/ship_junkyard.php?colony=*
// @match        https://*.atmoburn.com/colony_setup.php?colony=*
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());
unsafeWindow.eval(checkElementIndex.toString());

// functions for ColSettings Templates
unsafeWindow.eval(saveColSettingsTemplate.toString());
unsafeWindow.eval(applyColSettingsTemplate.toString());

// functions for Index Templates
unsafeWindow.eval(saveIndexTemplate.toString());
unsafeWindow.eval(applyIndexTemplate.toString());

// Calculates the price per worker
if (document.URL.includes("atmoburn.com/build_facility.php?colony=") && document.URL.includes("&type=")) {
    for (let blueprint of document.getElementsByClassName("blueprintswrap padding5")[0].children) {
        if (blueprint.className === "flexwrap marginbottom") {
            let bpstats = blueprint.getElementsByClassName("light padding5 lineheight margin2")[0].textContent;
            let workers = parseInt(bpstats.substr(bpstats.indexOf("Capacity: ")+10,bpstats.indexOf(" Workers")-(bpstats.indexOf("Capacity: ")+10)).replaceAll(",",""));
            let price = parseInt(blueprint.children[1].children[0].textContent.replaceAll("\t","").split("\n")[2].replaceAll(",",""));
            blueprint.getElementsByClassName("light padding5 lineheight margin2")[0].innerHTML = blueprint.getElementsByClassName("light padding5 lineheight margin2")[0].innerHTML + '<br> Cost per worker: ' + numberFormat(price/workers).toString()
        }
    }
}

// Building defaults
if (document.URL.includes("atmoburn.com/build_facility.php?colony=") && document.URL.includes("&type=")) {
    let type = document.URL.substr(document.URL.indexOf("&type=")+6, document.URL.indexOf("&subtype=")-(document.URL.indexOf("&type=")+6))
    let subtype = document.URL.substr(document.URL.indexOf("&subtype=")+9)
    if (localStorage.getItem(document.domain.split(".")[0] + "-" + type + "-" + subtype)) {
        document.getElementsByName("amount")[0].value = localStorage.getItem(document.domain.split(".")[0] + "-" + type + "-" + subtype)
    }

    let saveDefaultWrapper = document.createElement('div');
    saveDefaultWrapper.className = "aligncenter";

    let saveDefaultBuildNumber = document.createElement('button');
    saveDefaultBuildNumber.className = "darkbutton";
    saveDefaultBuildNumber.type = "button";
    saveDefaultBuildNumber.innerHTML = "Save Default";
    saveDefaultBuildNumber.title = "Save current number of buildings as default for this building type";
    saveDefaultBuildNumber.addEventListener("click", function() {localStorage.setItem(document.domain.split(".")[0] + "-" + type + "-" + subtype, document.getElementsByName("amount")[0].value)});
    saveDefaultWrapper.append(saveDefaultBuildNumber);
    document.getElementsByClassName("blueprintswrap padding5")[0].before(saveDefaultWrapper);
}


// ColSettings Templates
if (document.URL.includes("atmoburn.com/view_colony.php?colony=")) {
    let saveColSettingsTemplateButton = document.createElement('button');
    saveColSettingsTemplateButton.className = "darkbutton";
    saveColSettingsTemplateButton.type = "button";
    saveColSettingsTemplateButton.innerHTML = "Save Template";
    saveColSettingsTemplateButton.title = "Save current template";
    saveColSettingsTemplateButton.addEventListener("click", saveColSettingsTemplate);
    if (document.domain.includes("beta3")) {
        byId("Modify2").after(saveColSettingsTemplateButton)
    } else {
        document.getElementsByName("Modify")[0].after(saveColSettingsTemplateButton)
    }

    let applyColSettingsTemplateButton = document.createElement('button');
    applyColSettingsTemplateButton.className = "darkbutton";
    applyColSettingsTemplateButton.type = "button";
    applyColSettingsTemplateButton.innerHTML = "Apply Template";
    applyColSettingsTemplateButton.title = "Applies current template";
    applyColSettingsTemplateButton.addEventListener("click", applyColSettingsTemplate);
    saveColSettingsTemplateButton.after(applyColSettingsTemplateButton)
}


// Index Templates
if (document.URL.includes("atmoburn.com/colony_setup.php?colony=")) {
    let saveIndexTemplateButton = document.createElement('button');
    saveIndexTemplateButton.className = "darkbutton";
    saveIndexTemplateButton.type = "button";
    saveIndexTemplateButton.innerHTML = "Save Template";
    saveIndexTemplateButton.title = "Save current template";
    saveIndexTemplateButton.addEventListener("click", saveIndexTemplate);
    document.getElementsByClassName("padding5 italic width50 marginauto alignleft lineheight")[0].after(saveIndexTemplateButton)

    let applyIndexTemplateButton = document.createElement('button');
    applyIndexTemplateButton.className = "darkbutton";
    applyIndexTemplateButton.type = "button";
    applyIndexTemplateButton.innerHTML = "Apply Template";
    applyIndexTemplateButton.title = "Applies current template";
    applyIndexTemplateButton.addEventListener("click", applyIndexTemplate);
    saveIndexTemplateButton.after(applyIndexTemplateButton)
}

// Next and Previous colony buttons
if (byId("colonylist").getElementsByClassName("highlight")[0].previousElementSibling && byId("colonylist").getElementsByClassName("highlight")[0].previousElementSibling.className !== "sidemenu_header") {
    let previousColony = document.createElement('a');
    previousColony.innerHTML = "Previous colony";
    previousColony.id = "previousColony";
    previousColony.className = "darkbutton";
    previousColony.style.fontSize = "10px";
    previousColony.href = byId("colonylist").getElementsByClassName("highlight")[0].previousElementSibling.getElementsByClassName("colmenu_name")[0].href;
    let elementIndex = 0;
    byId("midcolumn").children[checkElementIndex(elementIndex)].prepend(previousColony)
}

if (byId("colonylist").getElementsByClassName("highlight")[0].nextElementSibling) {
    let nextColony = document.createElement('a');
    nextColony.innerHTML = "Next colony";
    nextColony.id = "nextColony";
    nextColony.className = "darkbutton";
    nextColony.style.fontSize = "10px";
    nextColony.href = byId("colonylist").getElementsByClassName("highlight")[0].nextElementSibling.getElementsByClassName("colmenu_name")[0].href;
    let elementIndex = 0;
    byId("midcolumn").children[checkElementIndex(elementIndex)].append(nextColony)
}

function checkElementIndex(elementIndex) {
    if (byId("midcolumn").children[elementIndex].className == "toptitle home" || byId("midcolumn").children[elementIndex].className == "infomessage bold") {
        return checkElementIndex(elementIndex + 1);
    } else { return elementIndex }
}

// functions for ColSettings Templates
function saveColSettingsTemplate() {
    let colSettings = {
        "govtype": document.getElementsByName("govtype")[0].value,
        "religion": document.getElementsByName("religion")[0].value,
        "colgroup": document.getElementsByName("colgroup")[0].value,
        "wages": document.getElementsByName("wages")[0].value,
        "income": document.getElementsByName("income")[0].value,
        "wealth": document.getElementsByName("wealth")[0].value
    }
    localStorage.setItem("colSettings", JSON.stringify(colSettings));
}

function applyColSettingsTemplate() {
    let colSettings = JSON.parse(localStorage.getItem("colSettings"));
    document.getElementsByName("govtype")[0].value = colSettings.govtype;
    document.getElementsByName("religion")[0].value = colSettings.religion;
    document.getElementsByName("colgroup")[0].value = colSettings.colgroup;
    document.getElementsByName("wages")[0].value = colSettings.wages;
    document.getElementsByName("income")[0].value = colSettings.income;
    document.getElementsByName("wealth")[0].value = colSettings.wealth;
}



// functions for Index Templates
function saveIndexTemplate() {
    let indexes = {
        "urban": byId("urban").value,
        "tech": byId("tech").value,
        "industry": byId("industry").value,
        "military": byId("military").value,
    }
    localStorage.setItem("indexes", JSON.stringify(indexes));
}

function applyIndexTemplate() {
    let indexes = JSON.parse(localStorage.getItem("indexes"))
    byId("urban").value = indexes.urban;
    byId("tech").value = indexes.tech;
    byId("industry").value = indexes.industry;
    byId("military").value = indexes.military;
    updateEstimates();
}

byId("colonylist").getElementsByClassName("highlight")[0].style.backgroundColor = "darkgreen"



 //Keyboard shortcuts section
document.onkeydown = function(e) {
    e = e || window.event;
    var keycode = e.which || e.keyCode;
    var ctrlPressed = e.ctrlKey || e.metaKey; //record if Ctrl key is pressed

    if( !ctrlPressed && document.activeElement.tagName == "BODY") { //skip if Ctrl key is pressed, activeElement used to skip if user has a text input active, don't do anything before page is loaded
        try {
            if( keycode == 37 && byId("previousColony") && byId("previousColony").style.visibility != "hidden") {// '37' is the keycode for ArrowLeft, skip if previous colony button doesn't exist or is hidden
                e.preventDefault();
                byId("previousColony").click();
            } else if( keycode == 39 && byId("nextColony") && byId("nextColony").style.visibility != "hidden") {//'39' is the keycode for ArrowRight, skip if next colony button doesn't exist or is hidden
                e.preventDefault();
                byId("nextColony").click();
            }
        } catch (error) {console.log("slow down faggot")}
    }
};