2017-08-30 19:44:29 +02:00
|
|
|
import {workerScripts,
|
|
|
|
addWorkerScript,
|
2018-05-06 22:27:47 +02:00
|
|
|
killWorkerScript} from "./NetscriptWorker.js";
|
|
|
|
import {Player} from "./Player.js";
|
|
|
|
import {getServer} from "./Server.js";
|
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
|
|
|
import {printArray, createElement,
|
|
|
|
createAccordionElement, removeElement,
|
|
|
|
removeChildrenFromElement} from "../utils/HelperFunctions.js";
|
|
|
|
import {logBoxCreate} from "../utils/LogBox.js";
|
|
|
|
import numeral from "../utils/numeral.min.js";
|
|
|
|
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
|
|
|
|
|
|
|
/* {
|
|
|
|
* serverName: {
|
|
|
|
* header: Server Header Element
|
|
|
|
* panel: Server Panel List (ul) element
|
|
|
|
* scripts: {
|
|
|
|
* script id: Ref to Script information
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
let ActiveScriptsUI = {};
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
function createActiveScriptsServerPanel(server) {
|
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] != null) {
|
|
|
|
console.log("WARNING: Tried to create already-existing Active Scripts Server panel. Aborting");
|
2017-06-07 04:33:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var activeScriptsList = document.getElementById("active-scripts-list");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
let res = createAccordionElement({hdrText:hostname});
|
|
|
|
let li = res[0];
|
|
|
|
var hdr = res[1];
|
|
|
|
let panel = res[2];
|
|
|
|
|
|
|
|
var panelScriptList = createElement("ul");
|
|
|
|
panel.appendChild(panelScriptList);
|
|
|
|
activeScriptsList.appendChild(li);
|
|
|
|
|
|
|
|
ActiveScriptsUI[hostname] = {
|
|
|
|
header: hdr,
|
|
|
|
panel: panel,
|
|
|
|
panelList: panelScriptList,
|
|
|
|
scripts: {}, //Holds references to li elements for each active script
|
|
|
|
scriptHdrs: {}, //Holds references to header elements for each active script
|
|
|
|
scriptStats: {} //Holds references to the p elements containing text for each active script
|
|
|
|
};
|
|
|
|
|
|
|
|
return li;
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Deletes the info for a particular server (Dropdown header + Panel with all info)
|
|
|
|
//in the Active Scripts page if it exists
|
|
|
|
function deleteActiveScriptsServerPanel(server) {
|
2018-05-06 22:27:47 +02:00
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] == null) {
|
|
|
|
console.log("WARNING: Tried to delete non-existent Active Scripts Server panel. Aborting");
|
2017-06-07 04:33:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
//Make sure it's empty
|
|
|
|
if (Object.keys(ActiveScriptsUI[hostname].scripts).length > 0) {
|
|
|
|
console.log("WARNING: Tried to delete Active Scripts Server panel that still has scripts. Aborting");
|
|
|
|
return;
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
|
|
|
|
removeElement(ActiveScriptsUI[hostname].panel);
|
|
|
|
removeElement(ActiveScriptsUI[hostname].header);
|
|
|
|
delete ActiveScriptsUI[hostname];
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addActiveScriptsItem(workerscript) {
|
|
|
|
//Get server panel
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
2018-05-06 22:27:47 +02:00
|
|
|
console.log("ERROR: Invalid server IP for workerscript in addActiveScriptsItem()");
|
2017-06-07 04:33:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] == null) {
|
|
|
|
createActiveScriptsServerPanel(server);
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
//Create the unique identifier (key) for this script
|
2017-06-17 04:53:57 +02:00
|
|
|
var itemNameArray = ["active", "scripts", server.hostname, workerscript.name];
|
|
|
|
for (var i = 0; i < workerscript.args.length; ++i) {
|
2018-01-09 21:48:06 +01:00
|
|
|
itemNameArray.push(String(workerscript.args[i]));
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
var itemName = itemNameArray.join("-");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
let res = createAccordionElement({hdrText:workerscript.name});
|
|
|
|
let li = res[0];
|
|
|
|
let hdr = res[1];
|
|
|
|
let panel = res[2];
|
|
|
|
|
|
|
|
hdr.classList.remove("accordion-header");
|
|
|
|
hdr.classList.add("active-scripts-script-header");
|
|
|
|
panel.classList.remove("accordion-panel");
|
|
|
|
panel.classList.add("active-scripts-script-panel");
|
|
|
|
|
|
|
|
//Handle the constant elements on the panel that don't change after creation
|
|
|
|
//Threads, args, kill/log button
|
|
|
|
panel.appendChild(createElement("p", {
|
|
|
|
innerHTML: "Threads: " + workerscript.scriptRef.threads + "<br>" +
|
|
|
|
"Args: " + printArray(workerscript.args)
|
|
|
|
}));
|
|
|
|
var panelText = createElement("p", {
|
|
|
|
innerText:"Loading...", fontSize:"14px",
|
|
|
|
});
|
|
|
|
updateActiveScriptsText(workerscript, panelText, itemName);
|
|
|
|
panel.appendChild(panelText);
|
|
|
|
panel.appendChild(createElement("br"));
|
|
|
|
panel.appendChild(createElement("span", {
|
|
|
|
innerText:"Log", class:"active-scripts-button", margin:"4px", padding:"4px",
|
|
|
|
clickListener:()=>{
|
|
|
|
logBoxCreate(workerscript.scriptRef);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
panel.appendChild(createElement("span", {
|
|
|
|
innerText:"Kill Script", class:"active-scripts-button", margin:"4px", padding:"4px",
|
|
|
|
clickListener:()=>{
|
|
|
|
killWorkerScript(workerscript.scriptRef, workerscript.scriptRef.scriptRef.server);
|
|
|
|
dialogBoxCreate("Killing script, may take a few minutes to complete...");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}));
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Append element to list
|
2018-05-06 22:27:47 +02:00
|
|
|
ActiveScriptsUI[hostname]["panelList"].appendChild(li);
|
|
|
|
ActiveScriptsUI[hostname].scripts[itemName] = li;
|
|
|
|
ActiveScriptsUI[hostname].scriptHdrs[itemName] = hdr;
|
|
|
|
ActiveScriptsUI[hostname].scriptStats[itemName] = panelText;
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteActiveScriptsItem(workerscript) {
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
|
|
|
console.log("ERROR: Invalid server IP for workerscript.");
|
|
|
|
return;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] == null) {
|
|
|
|
console.log("ERROR: Trying to delete Active Script UI Element with a hostname that cant be found in ActiveScriptsUI: " + hostname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
var itemNameArray = ["active", "scripts", server.hostname, workerscript.name];
|
|
|
|
for (var i = 0; i < workerscript.args.length; ++i) {
|
2018-01-09 21:48:06 +01:00
|
|
|
itemNameArray.push(String(workerscript.args[i]));
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
var itemName = itemNameArray.join("-");
|
2018-05-06 22:27:47 +02:00
|
|
|
|
|
|
|
let li = ActiveScriptsUI[hostname].scripts[itemName];
|
2017-06-07 04:33:50 +02:00
|
|
|
if (li == null) {
|
2018-05-06 22:27:47 +02:00
|
|
|
console.log("ERROR: Cannot find Active Script UI element for workerscript: ");
|
|
|
|
console.log(workerscript);
|
2017-06-07 04:33:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
removeElement(li);
|
|
|
|
delete ActiveScriptsUI[hostname].scripts[itemName];
|
|
|
|
delete ActiveScriptsUI[hostname].scriptHdrs[itemName];
|
|
|
|
delete ActiveScriptsUI[hostname].scriptStats[itemName];
|
|
|
|
if (Object.keys(ActiveScriptsUI[hostname].scripts).length === 0) {
|
|
|
|
deleteActiveScriptsServerPanel(server);
|
|
|
|
}
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Update the ActiveScriptsItems array
|
|
|
|
function updateActiveScriptsItems() {
|
|
|
|
var total = 0;
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
total += updateActiveScriptsItemContent(workerScripts[i]);
|
|
|
|
}
|
|
|
|
document.getElementById("active-scripts-total-prod").innerHTML =
|
2017-09-27 17:13:42 +02:00
|
|
|
"Total online production of Active Scripts: " + numeral(total).format('$0.000a') + " / sec<br>" +
|
|
|
|
"Total online production since last Aug installation: " +
|
|
|
|
numeral(Player.scriptProdSinceLastAug).format('$0.000a') + " (" +
|
|
|
|
numeral(Player.scriptProdSinceLastAug / (Player.playtimeSinceLastAug/1000)).format('$0.000a') + " / sec)";
|
2017-09-12 01:14:51 +02:00
|
|
|
return total;
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Updates the content of the given item in the Active Scripts list
|
|
|
|
function updateActiveScriptsItemContent(workerscript) {
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
|
|
|
console.log("ERROR: Invalid server IP for workerscript.");
|
|
|
|
return;
|
|
|
|
}
|
2018-05-06 22:27:47 +02:00
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] == null) {
|
|
|
|
console.log("ERROR: Trying to update Active Script UI Element with a hostname that cant be found in ActiveScriptsUI: " + hostname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
var itemNameArray = ["active", "scripts", server.hostname, workerscript.name];
|
|
|
|
for (var i = 0; i < workerscript.args.length; ++i) {
|
2018-01-09 21:48:06 +01:00
|
|
|
itemNameArray.push(String(workerscript.args[i]));
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
var itemName = itemNameArray.join("-");
|
2018-05-06 22:27:47 +02:00
|
|
|
var item = ActiveScriptsUI[hostname].scriptStats[itemName];
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
//Update the text if necessary. This fn returns the online $/s production
|
|
|
|
return updateActiveScriptsText(workerscript, item, itemName);
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
function updateActiveScriptsText(workerscript, item, itemName) {
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
|
|
|
console.log("ERROR: Invalid server IP for workerscript.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let hostname = server.hostname;
|
|
|
|
if (ActiveScriptsUI[hostname] == null) {
|
|
|
|
console.log("ERROR: Trying to update Active Script UI Element with a hostname that cant be found in ActiveScriptsUI: " + hostname);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
var onlineMps = workerscript.scriptRef.onlineMoneyMade / workerscript.scriptRef.onlineRunningTime;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
//Only update if the item is visible
|
|
|
|
if (ActiveScriptsUI[hostname].header.classList.contains("active") === false) {return onlineMps;}
|
|
|
|
if (ActiveScriptsUI[hostname].scriptHdrs[itemName].classList.contains("active") === false) {return onlineMps;}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
removeChildrenFromElement(item);
|
2017-09-21 23:27:31 +02:00
|
|
|
|
|
|
|
//Online
|
|
|
|
var onlineTotalMoneyMade = "Total online production: $" + formatNumber(workerscript.scriptRef.onlineMoneyMade, 2);
|
|
|
|
var onlineTotalExpEarned = (Array(26).join(" ") + formatNumber(workerscript.scriptRef.onlineExpGained, 2) + " hacking exp").replace( / /g, " ");
|
|
|
|
|
|
|
|
var onlineMpsText = "Online production rate: $" + formatNumber(onlineMps, 2) + "/second";
|
|
|
|
var onlineEps = workerscript.scriptRef.onlineExpGained / workerscript.scriptRef.onlineRunningTime;
|
|
|
|
var onlineEpsText = (Array(25).join(" ") + formatNumber(onlineEps, 4) + " hacking exp/second").replace( / /g, " ");
|
|
|
|
|
|
|
|
//Offline
|
|
|
|
var offlineTotalMoneyMade = "Total offline production: $" + formatNumber(workerscript.scriptRef.offlineMoneyMade, 2);
|
|
|
|
var offlineTotalExpEarned = (Array(27).join(" ") + formatNumber(workerscript.scriptRef.offlineExpGained, 2) + " hacking exp").replace( / /g, " ");
|
|
|
|
|
|
|
|
var offlineMps = workerscript.scriptRef.offlineMoneyMade / workerscript.scriptRef.offlineRunningTime;
|
|
|
|
var offlineMpsText = "Offline production rate: $" + formatNumber(offlineMps, 2) + "/second";
|
|
|
|
var offlineEps = workerscript.scriptRef.offlineExpGained / workerscript.scriptRef.offlineRunningTime;
|
|
|
|
var offlineEpsText = (Array(26).join(" ") + formatNumber(offlineEps, 4) + " hacking exp/second").replace( / /g, " ");
|
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
item.innerHTML = onlineTotalMoneyMade + "<br>" + onlineTotalExpEarned + "<br>" +
|
|
|
|
onlineMpsText + "<br>" + onlineEpsText + "<br>" + offlineTotalMoneyMade + "<br>" + offlineTotalExpEarned + "<br>" +
|
|
|
|
offlineMpsText + "<br>" + offlineEpsText + "<br>";
|
2017-09-21 23:27:31 +02:00
|
|
|
return onlineMps;
|
|
|
|
}
|
|
|
|
|
2018-05-06 22:27:47 +02:00
|
|
|
export {addActiveScriptsItem, deleteActiveScriptsItem, updateActiveScriptsItems};
|