WFMario / WF Upkeep calculator

// ==UserScript==
// @name         WF Upkeep calculator
// @version      2024-06-12
// @author       Mario
// @license MIT
// @description  Calculates upkeep.
// @match        https://beta4.atmoburn.com/ship_designs.php*
// @grant        unsafeWindow
// ==/UserScript==

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

let wages = 250;
if (localStorage.getItem("wages")) {
    wages = parseInt(localStorage.getItem("wages"));
}

let wagesInput = document.createElement('input');
wagesInput.type = "text";
wagesInput.class = "darkinput";
wagesInput.id = "wagesInput";
wagesInput.size = "4";
wagesInput.maxlength = "6";
wagesInput.value = wages;
wagesInput.addEventListener("change", calcmass);

let setWages = document.createElement('button');
setWages.innerHTML = "Save wages";
setWages.id = "setWages";
setWages.className = "darkbutton";
setWages.addEventListener("click", saveWages);
setWages.title = "Saves wages in localstorage";

if (document.getElementsByClassName("highlight padding5 overauto")[0]) { // checks if we're making a new design
    document.getElementsByClassName("padding5 light margintb2 aligncenter")[0].append(wagesInput);
    document.getElementsByClassName("padding5 light margintb2 aligncenter")[0].append(setWages);

    let upkeep = document.createElement('div');
    upkeep.id = "upkeep";
    upkeep.innerHTML = "Upkeep: ";
    byId("pricespan").parentElement.append(upkeep);
} else if (document.getElementsByClassName("flex flexwrap gap2")[0]) { // we're looking at an existing design
    document.getElementsByClassName("padding5 light margintb2 aligncenter")[0].append(wagesInput);
    document.getElementsByClassName("padding5 light margintb2 aligncenter")[0].append(setWages);

    let crewText = document.getElementsByClassName("flex flexwrap gap2")[0].textContent;
    crewText = crewText.substr(crewText.indexOf("Crew")+6);
    crewText = crewText.substr(0,crewText.indexOf("\n"));

    let costText = document.getElementsByClassName("light bold padding5")[0].textContent;

    let upkeep = document.createElement('div');
    upkeep.id = "upkeep";
    upkeep.innerHTML = "Upkeep: " + numberFormat(parseInt(parseInt(costText.split(" ")[1].replaceAll(",",""))/2000 + parseInt(crewText.replaceAll(",","")) * 2 * byId("wagesInput").value)) + " cr";
    document.getElementsByClassName("light bold padding5")[0].append(upkeep);
}

function saveWages() {
    localStorage.setItem("wages", byId("wagesInput").value);
}


function calcmass() {
    var layoutmass = (drivemass + gunmass + batterymass + armormass + shieldmass);
    var totalmass = Math.round((ship.mass + (layoutmass / 1000)) * 100) / 100;
    var spaceleft = Math.round(maxmass - layoutmass);
    freemass = spaceleft;

    //Recolor lists based on layout available
    driveList.forEach(element => changeOptionColor(element.id, element.mass, ship.numdrives, freemass, maxmass, drivemass, "drvOption"));
    gunList.forEach(element => changeOptionColor(element.id, element.mass, ship.gun, freemass, maxmass, gunmass, "gunOption"));
    batteryList.forEach(element => changeOptionColor(element.id, element.mass, ship.battery, freemass, maxmass, batterymass, "batteryOption"));
    armorList.forEach(element => changeOptionColor(element.id, element.mass, 1, freemass, maxmass, armormass, "armorOption"));
    shieldList.forEach(element => changeOptionColor(element.id, element.mass, 1, freemass, maxmass, shieldmass, "shieldOption"));

    byId('totalmass').innerHTML = numberFormat(totalmass);
    byId('freelayout').innerHTML = numberFormat(spaceleft) + " kilograms";

    var dprice = 0;
    dprice += Math.round(hull.price);

    var drvindex = byId('driveselect').options[byId('driveselect').selectedIndex].value;
    if (drvindex != 'x') {
        dprice = dprice + (Math.round(driveList.find(element => element.id == drvindex).price) * ship.numdrives);
    }

    if (ship.gun > 0) {
        var gunindex = byId('gunselect').options[byId('gunselect').selectedIndex].value;
        if (gunindex != 'x') {
            dprice = dprice + Math.round(gunList.find(element => element.id == gunindex).price) * ship.gun;
        }
    }
    if (ship.battery > 0) {
        var batteryindex = byId('batteryselect').options[byId('batteryselect').selectedIndex].value;
        if (batteryindex != 'x') {
            dprice = dprice + Math.round(batteryList.find(element => element.id == batteryindex).price) * ship.battery;
        }
    }
    var armorindex = byId('armorselect').options[byId('armorselect').selectedIndex].value;
    if (armorindex != 'x') {
        dprice = dprice + Math.round(armorList.find(element => element.id == armorindex).price) * armorsld.value;
    }
    var shieldindex = byId('shieldselect').options[byId('shieldselect').selectedIndex].value;
    if (shieldindex != 'x') {
        dprice = dprice + Math.round(shieldList.find(element => element.id == shieldindex).price) * shieldsld.value;
    }

    byId('pricespan').innerHTML = numberFormat(dprice);
    byId("upkeep").innerHTML = "Upkeep: " + numberFormat(parseInt(dprice/2000 + ship.pilots * 2 * byId("wagesInput").value)) + " cr";
    // console.log((dprice/2000 + ship.pilots * 250).toString());

    var resNeeded = {};
    materials.forEach(function (material) {
        resNeeded[material] = hull[material];
        if (drvindex != 'x') {
            resNeeded[material] += Math.round(driveList.find(element => element.id == drvindex)[material] * ship.numdrives);
        }
        if (ship.gun > 0 && gunindex != 'x') {
            resNeeded[material] += Math.round(gunList.find(element => element.id == gunindex)[material] * ship.gun);
        }
        if (ship.battery > 0 && batteryindex != 'x') {
            resNeeded[material] += Math.round(batteryList.find(element => element.id == batteryindex)[material] * ship.battery);
        }
        if (armorindex != 'x') {
            resNeeded[material] += Math.round(armorList.find(element => element.id == armorindex)[material] * armorsld.value);
        }
        if (shieldindex != 'x') {
            resNeeded[material] += Math.round(shieldList.find(element => element.id == shieldindex)[material] * shieldsld.value);
        }
        byId(material.replace(/ /, "_") + 'span').innerHTML = numberFormat(resNeeded[material]);

    });

    if (spaceleft < 0 || drvindex == 'x') {
        designIsValid = false;
    } else {
        designIsValid = true;
    }
    setSubmitButton();

    return totalmass;
}