WFMario / Expansion Planner

// ==UserScript==
// @name         Expansion Planner
// @version      2024-09-02
// @author       Mario
// @license MIT
// @match        https://*.atmoburn.com/known_universe.*
// @match        https://*.atmoburn.com/extras/view_planet.php?planet=*
// @match        https://*.atmoburn.com/player.php
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());



if (document.URL.includes("player.php")) {
    unsafeWindow.eval(createExpansionPlannerSettings.toString());
    unsafeWindow.eval(addExpansionPlannerStatus.toString());

    createExpansionPlannerSettings();

    function createExpansionPlannerSettings() {
        let expansionPlannerSettingsHeader = document.createElement('div');
        expansionPlannerSettingsHeader.className = "margintop padding5 highlight bold centertext box";
        expansionPlannerSettingsHeader.innerHTML = "Expansion Planner Settings";

        let expansionPlannerSettingsWrapper = document.createElement('div');
        expansionPlannerSettingsWrapper.className = "light padding5";
        expansionPlannerSettingsWrapper.id = "expansionPlannerSettings";

        // current statusses
        let expansionPlannerStatusWrapper = document.createElement('div');
        expansionPlannerSettingsWrapper.append(expansionPlannerStatusWrapper)

        let expansionPlannerStatus = document.createElement('select');
        expansionPlannerStatus.className = "darkselect";
        expansionPlannerStatus.id = "expansionStatusSelector";
        let expansionPlannerStatuses = JSON.parse(localStorage.getItem("EPStatuses")) || ["Waiting","OTW","DONE"];
        expansionPlannerStatuses.forEach(status => {
            let expansionPlannerStatusOption = document.createElement('option');
            expansionPlannerStatusOption.innerHTML = status;
            expansionPlannerStatusOption.id = "EPstatus" + status;
            // if (expansionItem.status === status) {expansionItemStatusOption.selected = true;}
            expansionPlannerStatus.append(expansionPlannerStatusOption);
        })
        expansionPlannerStatusWrapper.append(expansionPlannerStatus);

        let expansionPlannerStatusDelete = document.createElement('button');
        expansionPlannerStatusDelete.innerHTML = "Delete";
        expansionPlannerStatusDelete.className = "darkbutton dangerbutton";
        expansionPlannerStatusDelete.addEventListener("click", function() {deleteExpansionPlannerStatus(expansionPlannerStatuses)});
        expansionPlannerStatusWrapper.append(expansionPlannerStatusDelete);

        // making new statusses
        let expansionPlannerInputWrapper = document.createElement('div');
        expansionPlannerSettingsWrapper.append(expansionPlannerInputWrapper)

        let expansionPlannerStatusInput = document.createElement('input');
        expansionPlannerStatusInput.value = status;
        expansionPlannerStatusInput.id = "expansionPlannerStatus";
        expansionPlannerStatusInput.className = "darkinput";
        expansionPlannerInputWrapper.append(expansionPlannerStatusInput);

        let expansionPlannerStatusSave = document.createElement('button');
        expansionPlannerStatusSave.innerHTML = "Save";
        expansionPlannerStatusSave.className = "darkbutton greenbutton";
        expansionPlannerStatusSave.addEventListener("click", function() {addExpansionPlannerStatus(expansionPlannerStatuses)});
        expansionPlannerInputWrapper.append(expansionPlannerStatusSave);

        byId("midcolumn").getElementsByClassName("margintop padding5 highlight bold box")[0].before(expansionPlannerSettingsWrapper);
        expansionPlannerSettingsWrapper.before(expansionPlannerSettingsHeader);
    }

    function addExpansionPlannerStatus(expansionPlannerStatuses) {
        let toBeAddedStatus = byId("expansionPlannerStatus").value.trim();
        if (expansionPlannerStatuses.includes(toBeAddedStatus)) {
            alert("status with this name already exists")
        } else {
            expansionPlannerStatuses.push(toBeAddedStatus);
            let expansionPlannerStatusOption = document.createElement('option');
            expansionPlannerStatusOption.innerHTML = toBeAddedStatus;
            expansionPlannerStatusOption.id = "EPstatus" + status;
            byId("expansionStatusSelector").append(expansionPlannerStatusOption);
            localStorage.setItem("EPStatuses", JSON.stringify(expansionPlannerStatuses));
        }

    }

    function deleteExpansionPlannerStatus(expansionPlannerStatuses) {
        let status = byId("expansionStatusSelector").value;
        let index = expansionPlannerStatuses.indexOf(status);
        if (index !== -1) {
            expansionPlannerStatuses.splice(index, 1);
        }
        byId("EPstatus" + status).remove();
        localStorage.setItem("EPStatuses", JSON.stringify(expansionPlannerStatuses));
    }

        // function createExpansionPlannerSettings() {
//         let expansionPlannerStatuses = JSON.parse(localStorage.getItem("EPStatuses")) || ["Waiting","OTW","DONE"];
//         expansionPlannerStatuses.forEach(status => {
//             let expansionPlannerStatusWrapper = document.createElement('div');
//             expansionPlannerStatusWrapper.className = "fullwidth";

//             let expansionPlannerStatusInput = document.createElement('input');
//             expansionPlannerStatusInput.value = status;
//             expansionPlannerStatusInput.className = "expansionPlannerStatus";
//             expansionPlannerStatusWrapper.append(expansionPlannerStatusInput);

//             let expansionPlannerStatusSave = document.createElement('button');
//             expansionPlannerStatusSave.innerHTML = "Save";
//             expansionPlannerStatusSave.className = "darkbutton greenbutton";
//             expansionPlannerStatusSave.addEventListener("click", function() {});
//             expansionPlannerStatusWrapper.append(expansionPlannerStatusSave);
//         })

//         let addExpansionPlannerStatusButton = document.createElement('button');
//         addExpansionPlannerStatusButton.className = "darkbutton greenbutton";
//         addExpansionPlannerStatusButton.id = "addExpansionPlannerStatusButton";
//         addExpansionPlannerStatusButton


    //     byId("midcolumn").getElementsByClassName("margintop padding5 highlight bold box")[0].before(expansionPlannerSettingsWrapper);
    //     expansionPlannerSettingsWrapper.before(expansionPlannerSettingsHeader);
    // }

//     function addExpansionPlannerStatus() {
//         expansionPlannerSettings()
//     }

//     function changeExpansionPlannerStatuses() {
//         let EPStatuses = [];
//         Array.from(document.getElementsByClassName("expansionPlannerStatus")).forEach(EPStatus => {EPStatuses.push(EPStatus.value)});
//         localStorage.setItem("EPStatuses", JSON.stringify(EPStatuses));
//     }
} else {
    unsafeWindow.eval(createExpansionPlannerOverview.toString());
    unsafeWindow.eval(changeExpansionItem.toString());
    unsafeWindow.eval(addExpansionPlannerOptions.toString());
    unsafeWindow.eval(addExpansionPlannerItem.toString());
    unsafeWindow.eval(deleteExpansionPlannerItem.toString());
    unsafeWindow.eval(createExpansionPlannerItem.toString());
    unsafeWindow.eval(createAddToExpansionPlannerButton.toString());


    let expansionData;
    if (localStorage.getItem("ExpansionPlanner" + document.domain.split(".")[0])) {
        expansionData = JSON.parse(localStorage.getItem("ExpansionPlanner" + document.domain.split(".")[0]));
    } else {
        expansionData = {};
    }

    if (document.URL.includes("known_universe")) {
        if (byId("midcolumn").getElementsByClassName("padding5 opacity_wrap")[0].children[0].textContent === "Worlds") {
            createExpansionPlannerOverview(expansionData);
            addExpansionPlannerOptions(expansionData);
        }
    } else if (document.URL.includes("view_planet")) {
        // console.log("we are looking at a planet");
        createAddToExpansionPlannerButton(expansionData);
    }

    function createExpansionPlannerOverview(expansionData) {
        let expansionPlannerWrapper = document.createElement('div');
        expansionPlannerWrapper.className = "padding5 opacity_wrap";
        expansionPlannerWrapper.id = "expansionPlannerWrapper";
        byId("midcolumn").append(expansionPlannerWrapper);

        let expansionPlannerHeader = document.createElement('div');
        expansionPlannerHeader.className = "midtitle bold heading titlespace";
        expansionPlannerHeader.innerHTML = "EXPANSION PLANNER";
        expansionPlannerWrapper.append(expansionPlannerHeader);

        let expansionPlannerDataWrapper = document.createElement('div');
        expansionPlannerDataWrapper.className = "padding5";
        expansionPlannerDataWrapper.id = "expansionPlannerDataWrapper";
        expansionPlannerWrapper.append(expansionPlannerDataWrapper);

        for (const [key, expansionItem] of Object.entries(expansionData)) {
            createExpansionPlannerItem(expansionData, expansionItem);
        }
    }

    function createExpansionPlannerItem(expansionData, expansionItem) {
        let expansionPlannerDataWrapper = byId("expansionPlannerDataWrapper");
        let expansionPlannerItemWrapper = document.createElement('div');
        expansionPlannerItemWrapper.className = "light centertext";
        expansionPlannerItemWrapper.id = "wrapper"+expansionItem.ID;
        // expansionPlannerItemWrapper.style.width = "100%";
        expansionPlannerDataWrapper.append(expansionPlannerItemWrapper);

        let expansionItemName = document.createElement('a');
        expansionItemName.className = "dark margin5 padding5 inlineblock";
        expansionItemName.innerHTML = expansionItem.name;
        expansionItemName.style.width = "25%";
        expansionItemName.href = "javascript:showPlanet(" + expansionItem.ID + ");"
        expansionPlannerItemWrapper.append(expansionItemName);

        let expansionItemID = document.createElement('span');
        expansionItemID.className = "dark margin5 padding5 inlineblock";
        expansionItemID.innerHTML = expansionItem.ID;
        expansionItemID.style.width = "5%";
        expansionPlannerItemWrapper.append(expansionItemID);

        let expansionItemHab = document.createElement('span');
        expansionItemHab.className = "dark margin5 padding5 inlineblock";
        expansionItemHab.innerHTML = expansionItem.hab;
        expansionItemHab.style.width = "10%";
        // expansionItemHab.style.width = "30%";
        expansionPlannerItemWrapper.append(expansionItemHab);

        // let expansionItemView = document.createElement('button');
        // expansionItemView.className = "darkbutton";
        // expansionItemView.type = "button";
        // expansionItemView.innerHTML = "View";
        // expansionItemView.addEventListener("click", function() {showPlanet(expansionItem.ID)});
        // expansionPlannerItemWrapper.append(expansionItemView);

        let expansionItemSendFleet = document.createElement('a');
        expansionItemSendFleet.className = "darkbutton";
        expansionItemSendFleet.innerHTML = "Send Fleet";
        expansionItemSendFleet.href = "/fleet.php?tworld=" + expansionItem.ID;
        expansionItemSendFleet.target = "_blank";
        expansionPlannerItemWrapper.append(expansionItemSendFleet);

        let expansionItemDelete = document.createElement('button');
        expansionItemDelete.className = "darkbutton dangerbutton";
        expansionItemDelete.type = "button";
        expansionItemDelete.innerHTML = "Delete";
        expansionItemDelete.addEventListener("click", function() {deleteExpansionPlannerItem(expansionData, expansionItem)});
        expansionPlannerItemWrapper.append(expansionItemDelete);

        let expansionItemStatus = document.createElement('select');
        expansionItemStatus.className = "darkselect";
        expansionItemStatus.id = "expansionStatus"+expansionItem.ID;
        let expansionPlannerStatuses = JSON.parse(localStorage.getItem("EPStatuses")) || ["Waiting","OTW","DONE"];
        expansionPlannerStatuses.forEach(status => {
            let expansionItemStatusOption = document.createElement('option');
            expansionItemStatusOption.innerHTML = status;
            // if (expansionItem.status === status) {expansionItemStatusOption.selected = true;}
            expansionItemStatus.append(expansionItemStatusOption);
        })
        expansionItemStatus.value = expansionItem.status;
        expansionItemStatus.addEventListener("change", function() {changeExpansionItem(expansionData, expansionItem)});
        expansionPlannerItemWrapper.append(expansionItemStatus);

        let expansionItemNote1 = document.createElement('input');
        // expansionItemNote1.type = "text";
        expansionItemNote1.id = "expansionNote1"+expansionItem.ID;
        // expansionItemNote1.size = "4";
        // expansionItemNote1.maxlength = "6";
        expansionItemNote1.value = expansionItem.note1;
        expansionItemNote1.className = "lightinput margin5 padding5 inlineblock right";
        expansionItemNote1.style.width = "30%";
        // expansionItemNote1.style.height = "60%";
        expansionItemNote1.addEventListener("change", function() {changeExpansionItem(expansionData, expansionItem)})
        expansionPlannerItemWrapper.append(expansionItemNote1);
    }

    function changeExpansionItem(expansionData, expansionItem) {
        expansionItem.note1 = byId("expansionNote1"+expansionItem.ID).value;
        //expansionItem.note2 = byId("expansionNote2"+expansionItem.ID).value;
        expansionItem.status = byId("expansionStatus"+expansionItem.ID).value;
        expansionData[expansionItem.ID] = expansionItem;
        localStorage.setItem("ExpansionPlanner" + document.domain.split(".")[0], JSON.stringify(expansionData));
    }


    function addExpansionPlannerOptions(expansionData) {
        Array.from(byId("midcolumn").getElementsByClassName("padding5 opacity_wrap")[1].children).forEach(result => {
            if (result.className === 'padding5') {
                let addToExpansionPlannerButton = document.createElement('button');
                addToExpansionPlannerButton.className = "darkbutton right nomargin";
                addToExpansionPlannerButton.type = "button";
                addToExpansionPlannerButton.innerHTML = "Add to Expansion Planner";
                addToExpansionPlannerButton.addEventListener("click", function() {addExpansionPlannerItem(expansionData, result)});
                result.children[0].append(addToExpansionPlannerButton);
                result.children[0].style.overflow = "auto";
            }
        })
    }

    function createAddToExpansionPlannerButton(expansionData) {
        let planetTitle = document.getElementsByClassName("planettitle")[0];
        let addToExpansionPlannerButton = document.createElement('button');
        addToExpansionPlannerButton.className = "darkbutton nomargin";
        addToExpansionPlannerButton.type = "button";
        addToExpansionPlannerButton.innerHTML = "Add to Expansion Planner";
        addToExpansionPlannerButton.addEventListener("click", function() {addExpansionPlannerItem(expansionData, planetTitle)});
        // result.children[0].style.overflow = "auto";

        let goToExpansionPlannerButton = document.createElement('button');
        goToExpansionPlannerButton.className = "darkbutton right nomargin";
        goToExpansionPlannerButton.type = "button";
        goToExpansionPlannerButton.innerHTML = ">> Go to Expansion Planner";
        goToExpansionPlannerButton.addEventListener("click", function() {window.top.location.href = "/known_universe.php"});

        if (document.domain.includes("beta3")) {
            planetTitle.append(addToExpansionPlannerButton);
            planetTitle.append(goToExpansionPlannerButton);
        } else {
            let buttonWrapper = document.createElement('div');
            buttonWrapper.className = "fullwidth aligncenter";
            planetTitle.parentElement.append(buttonWrapper)
            goToExpansionPlannerButton.className = "darkbutton nomargin";
            buttonWrapper.append(addToExpansionPlannerButton);
            buttonWrapper.append(goToExpansionPlannerButton);
        }
    }

    function addExpansionPlannerItem(expansionData, result) {
        let expansionItem;
        if (result.ownerDocument.URL.includes("known_universe")) { // build expansion item based on info displayed in known_universe
            expansionItem = {
                "name": result.children[0].children[0].textContent,
                "ID": result.children[0].children[0].children[0].children[0].href.split("(")[1].split(")")[0],
                "hab": result.children[1].children[6].textContent,
                "status": "Waiting",
                "note1": "",
                "note2": "",
            }
        } else if (result.ownerDocument.URL.includes("view_planet")) { // build expansion item based on info displayed in view_planet
            expansionItem = {
                "name": result.firstChild.textContent.trim(),
                "ID": result.children[0].textContent.split(" ")[1],
                "hab": result.parentElement.nextElementSibling.getElementsByClassName("flex_50 gap2  flex_small_fill flex flexvertical padding5")[0].children[3].textContent.trim().replace(":",": "),
                "status": "Waiting",
                "note1": "",
                "note2": "",
            }
        }
        if (window.top.document.URL.includes("known_universe")) { // if the top document is known_universe we can update the displayed information
            if (expansionData[expansionItem.ID]) { // if it already exists, only update the hab
                expansionData[expansionItem.ID].hab = expansionItem.hab;
                window.top.document.getElementById("wrapper"+expansionItem.ID).children[2].innerHTML = expansionItem.hab;
            } else {
                expansionData[expansionItem.ID] = expansionItem;
                createExpansionPlannerItem(expansionData, expansionItem);
            }
        } else { // if the top document isn't known_universe then only save
            if (expansionData[expansionItem.ID]) { // if it already exists, only update the hab
                expansionData[expansionItem.ID].hab = expansionItem.hab;
            } else {
                expansionData[expansionItem.ID] = expansionItem;
            }
        }
        localStorage.setItem("ExpansionPlanner" + document.domain.split(".")[0], JSON.stringify(expansionData));
    }

    function deleteExpansionPlannerItem(expansionData, expansionItem) {
        if (confirm("Are you sure you want to delete the item?")) {
            delete expansionData[expansionItem.ID];
            localStorage.setItem("ExpansionPlanner" + document.domain.split(".")[0], JSON.stringify(expansionData));
            byId("wrapper"+expansionItem.ID).remove();
        }
    }
}