WFMario / Science Utils

// ==UserScript==
// @name         Science Utils
// @version      2024-04-25
// @author       Mario
// @license MIT
// @match        https://*.atmoburn.com/sci_research.php*
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());
unsafeWindow.eval(addSci.toString());
unsafeWindow.eval(updateTeam.toString());
unsafeWindow.eval(colorTeam.toString());
unsafeWindow.eval(rmSci.toString());

function colorTeam() {
    let teamSize = byId("teamSize")
    if (parseInt(teamSize.textContent) < parseInt(byId("maxTeamSize").textContent)) {
        teamSize.parentElement.style.color = "red"
    } else {
        teamSize.parentElement.style.color = "white"
    }
}

function updateTeam() {
    if (byId('location')) {
        var elSel = byId('team');
        var id = parseInt(byId('location').value);
        if (elSel) {
            var power = (id ? labData[id].capacity : 0);
            for (var sci in scientistData) {
                var thisSciRow = byId('sciRow' + sci);
                if (thisSciRow) {
                    rmSci(sci, true);
                    if (!id || scientistData[sci].colony == labData[id].colony) {
                        thisSciRow.style.display = 'flex';
                        if (!id)
                            byId('sciButton' + sci).disabled = true;
                        else
                            byId('sciButton' + sci).disabled = false;
                    } else
                        thisSciRow.style.display = 'none';
                }
            }
            elSel.size = byId('maxTeamSize').innerHTML = power;
            elSel.length = 0;
            var sciGroupSelect = byId('sciGroupSelect');
            sciGroupSelect.innerHTML = "";
            if (id) {
                sciGroupSelect.appendChild(new Option('Select Group', 'NULL', true, true));
                var sciColonyGroups = sciGroups[labData[id].colony];
                if (sciColonyGroups) {
                    for (var groupID in sciColonyGroups) {
                        if (groupID == "NULL") continue;
                        var numGroupMembers = 0;
                        for(var sciID in scientistData)
                            if (scientistData[sciID].groupID == groupID)
                                numGroupMembers++;
                        if (numGroupMembers > 0) {
                            var option = new Option(sciGroups[labData[id].colony][groupID].name, groupID);
                            if (labData[id].capacity < numGroupMembers) option.disabled = true;
                            sciGroupSelect.appendChild(option);
                        }
                    }
                }
            } else {
                sciGroupSelect.appendChild(new Option("No lab selected", '', true, true));
            }
        }
    }
    updateEstimate();
    checkOK();
    setTeamSkills();
    colorTeam();
}

function addSci(num, defer) {
    var elSel = document.getElementById('team');
    var elOptNew = document.createElement('option');
    var isin = 0;
    for (var i = 0; i < elSel.length; i++) {
        if (elSel.options[i].value == num)
            isin = 1;
    }
    if (isin == 0 && elSel.length < elSel.size) {
        elOptNew.text = num;
        elOptNew.value = num;
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        byId('sciRow' + num).classList.remove('light');
        byId('sciRow' + num).classList.add('dark');
        byId('sciButton' + num).classList.add('dangerbutton');
        byId('sciButton' + num).innerHTML = "<span class='fa fa-user-minus'></span>";
        byId('sciButton' + num).onclick = function () {
            rmSci(num);
        };
    }
    if (!defer) {
        updateEstimate();
        checkOK();
    }
    byId('teamSize').innerHTML = elSel.length;
    colorTeam();
}

function rmSci(id, defer) {
    var elSel = document.getElementById('team');

    for (var i = elSel.length - 1; i >= 0; i--) {
        var sciID = elSel.options[i].value;
        if ((!id && elSel.options[i].selected) || (id && sciID == id)) {
            byId('sciRow' + sciID).classList.add('light');
            byId('sciRow' + sciID).classList.remove('dark');
            byId('sciButton' + sciID).classList.remove('dangerbutton');
            byId('sciButton' + sciID).innerHTML = "<span class='fa fa-user-plus'></span>";
            byId('sciButton' + sciID).onclick = function () {
                addSci(sciID);
            };
            elSel.remove(i);
            break;
        }
    }
    if (!defer) {
        updateEstimate();
        checkOK();
    }
    byId('teamSize').innerHTML = elSel.length;
    colorTeam();
}