2017-08-30 19:44:29 +02:00
|
|
|
import {workerScripts,
|
|
|
|
addWorkerScript,
|
|
|
|
killWorkerScript} from "./NetscriptWorker.js";
|
2017-09-26 04:44:33 +02:00
|
|
|
import {Player} from "./Player.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {getServer} from "./Server.js";
|
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
|
|
|
import {printArray} from "../utils/HelperFunctions.js";
|
|
|
|
import {logBoxCreate} from "../utils/LogBox.js";
|
2017-09-27 17:13:42 +02:00
|
|
|
import numeral from "../utils/numeral.min.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
/* Active Scripts UI*/
|
2017-06-07 04:33:50 +02:00
|
|
|
function setActiveScriptsClickHandlers() {
|
|
|
|
//Server panel click handlers
|
|
|
|
var serverPanels = document.getElementsByClassName("active-scripts-server-header");
|
|
|
|
if (serverPanels == null) {
|
|
|
|
console.log("ERROR: Could not find Active Scripts server panels");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < serverPanels.length; ++i) {
|
|
|
|
serverPanels[i].onclick = function() {
|
|
|
|
this.classList.toggle("active");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
var panel = this.nextElementSibling;
|
|
|
|
if (panel.style.display === "block") {
|
|
|
|
panel.style.display = "none";
|
|
|
|
} else {
|
|
|
|
panel.style.display = "block";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Script Panel click handlers
|
|
|
|
var scriptPanels = document.getElementsByClassName("active-scripts-script-header");
|
|
|
|
if (scriptPanels == null) {
|
|
|
|
console.log("ERROR: Could not find Active Scripts panels for individual scripts");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < scriptPanels.length; ++i) {
|
|
|
|
scriptPanels[i].onclick = function() {
|
|
|
|
this.classList.toggle("active");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
var panel = this.nextElementSibling;
|
|
|
|
if (panel.style.display === "block") {
|
|
|
|
panel.style.display = "none";
|
|
|
|
} else {
|
|
|
|
panel.style.display = "block";
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the ul element containins all script items for a specific server
|
|
|
|
function getActiveScriptsServerList(server) {
|
|
|
|
if (server == null) {return null;}
|
|
|
|
var panelname = "active-scripts-server-panel-" + server.hostname;
|
|
|
|
var item = document.getElementById(panelname + "-script-list");
|
|
|
|
if (item == null) {
|
|
|
|
console.log("ERROR: Cannot find list for: " + server.hostname);
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createActiveScriptsServerPanel(server) {
|
|
|
|
var panelname = "active-scripts-server-panel-" + server.hostname;
|
|
|
|
var activeScriptsList = document.getElementById("active-scripts-list");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Div of entire Panel
|
|
|
|
var panelDiv = document.createElement("div");
|
|
|
|
panelDiv.setAttribute("id", panelname);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Panel Header
|
|
|
|
var panelHdr = document.createElement("button");
|
|
|
|
panelHdr.setAttribute("class", "active-scripts-server-header")
|
|
|
|
panelHdr.setAttribute("id", panelname + "-hdr");
|
|
|
|
panelHdr.innerHTML = server.hostname;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Panel content
|
|
|
|
var panelContentDiv = document.createElement("div");
|
|
|
|
panelContentDiv.setAttribute("class", "active-scripts-server-panel");
|
|
|
|
panelContentDiv.setAttribute("id", panelname + "-content");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//List of scripts
|
|
|
|
var panelScriptList = document.createElement("ul");
|
|
|
|
panelScriptList.setAttribute("id", panelname + "-script-list");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
panelContentDiv.appendChild(panelScriptList);
|
|
|
|
panelDiv.appendChild(panelHdr);
|
|
|
|
panelDiv.appendChild(panelContentDiv);
|
|
|
|
activeScriptsList.appendChild(panelDiv);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
setActiveScriptsClickHandlers() //Reset click handlers
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
return panelDiv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Deletes the info for a particular server (Dropdown header + Panel with all info)
|
|
|
|
//in the Active Scripts page if it exists
|
|
|
|
function deleteActiveScriptsServerPanel(server) {
|
|
|
|
var panelname = "active-scripts-server-panel-" + server.hostname;
|
|
|
|
var panel = document.getElementById(panelname);
|
|
|
|
if (panel == null) {
|
|
|
|
console.log("No such panel exists: " + panelname);
|
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Remove the panel if it has no elements
|
|
|
|
var scriptList = document.getElementById(panelname + "-script-list");
|
|
|
|
if (scriptList.childNodes.length == 0) {
|
|
|
|
panel.parentNode.removeChild(panel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addActiveScriptsItem(workerscript) {
|
|
|
|
//Get server panel
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
|
|
|
console.log("ERROR: Invalid server IP for workerscript.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var panelname = "active-scripts-server-panel-" + server.hostname;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
var panel = document.getElementById(panelname);
|
|
|
|
if (panel == null) {
|
|
|
|
panel = createActiveScriptsServerPanel(server);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Create the element itself. Each element is an accordion collapsible
|
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-06-07 04:33:50 +02:00
|
|
|
var item = document.createElement("li");
|
|
|
|
item.setAttribute("id", itemName);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
var btn = document.createElement("button");
|
|
|
|
btn.setAttribute("class", "active-scripts-script-header");
|
2018-05-05 22:23:57 +02:00
|
|
|
btn.setAttribute("id", itemName + "-header");
|
2017-06-07 04:33:50 +02:00
|
|
|
btn.innerHTML = workerscript.name;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
var itemContentDiv = document.createElement("div");
|
|
|
|
itemContentDiv.setAttribute("class", "active-scripts-script-panel");
|
|
|
|
itemContentDiv.setAttribute("id", itemName + "-content");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
item.appendChild(btn);
|
|
|
|
item.appendChild(itemContentDiv);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
createActiveScriptsText(workerscript, itemContentDiv);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Append element to list
|
|
|
|
var list = getActiveScriptsServerList(server);
|
|
|
|
list.appendChild(item);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
setActiveScriptsClickHandlers() //Reset click handlers
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteActiveScriptsItem(workerscript) {
|
|
|
|
var server = getServer(workerscript.serverIp);
|
|
|
|
if (server == null) {
|
|
|
|
console.log("ERROR: Invalid server IP for workerscript.");
|
|
|
|
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("-");
|
2017-06-07 04:33:50 +02:00
|
|
|
var li = document.getElementById(itemName);
|
|
|
|
if (li == null) {
|
|
|
|
console.log("could not find Active scripts li element for: " + workerscript.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
li.parentNode.removeChild(li);
|
|
|
|
deleteActiveScriptsServerPanel(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
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-06-07 04:33:50 +02:00
|
|
|
var itemContent = document.getElementById(itemName + "-content")
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Add the updated text back. Returns the total online production rate
|
2017-09-21 23:27:31 +02:00
|
|
|
return updateActiveScriptsText(workerscript, itemContent);
|
2017-06-07 04:33:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createActiveScriptsText(workerscript, item) {
|
2017-09-21 23:27:31 +02:00
|
|
|
var itemTextHeader = document.createElement("p");
|
|
|
|
var itemTextStats = document.createElement("p");
|
|
|
|
var itemId = item.id;
|
|
|
|
itemTextStats.setAttribute("id", itemId + "-stats");
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Server ip/hostname
|
2017-06-17 04:53:57 +02:00
|
|
|
var threads = "Threads: " + workerscript.scriptRef.threads;
|
|
|
|
var args = "Args: " + printArray(workerscript.args);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
itemTextHeader.innerHTML = threads + "<br>" + args + "<br>";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
item.appendChild(itemTextHeader);
|
|
|
|
item.appendChild(itemTextStats);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
var onlineMps = updateActiveScriptsText(workerscript, item, itemTextStats);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-19 16:54:11 +02:00
|
|
|
var logButton = document.createElement("span");
|
|
|
|
logButton.innerHTML = "Log";
|
|
|
|
var killButton = document.createElement("span");
|
|
|
|
killButton.innerHTML = "Kill script";
|
|
|
|
logButton.setAttribute("class", "active-scripts-button");
|
|
|
|
killButton.setAttribute("class", "active-scripts-button");
|
|
|
|
logButton.addEventListener("click", function() {
|
|
|
|
logBoxCreate(workerscript.scriptRef);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
killButton.addEventListener("click", function() {
|
|
|
|
killWorkerScript(workerscript.scriptRef, workerscript.scriptRef.scriptRef.server);
|
|
|
|
dialogBoxCreate("Killing script, may take a few minutes to complete...");
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
item.appendChild(logButton);
|
|
|
|
item.appendChild(killButton);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-07 04:33:50 +02:00
|
|
|
//Return total online production rate
|
|
|
|
return onlineMps;
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
function updateActiveScriptsText(workerscript, item, statsEl=null) {
|
|
|
|
var itemId = item.id
|
|
|
|
var itemTextStats = document.getElementById(itemId + "-stats");
|
2017-10-26 00:05:12 +02:00
|
|
|
if (itemTextStats == null || itemTextStats === undefined) {
|
2017-09-21 23:27:31 +02:00
|
|
|
itemTextStats = statsEl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Updates statistics only
|
|
|
|
//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 onlineMps = workerscript.scriptRef.onlineMoneyMade / workerscript.scriptRef.onlineRunningTime;
|
|
|
|
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, " ");
|
|
|
|
|
|
|
|
itemTextStats.innerHTML = onlineTotalMoneyMade + "<br>" + onlineTotalExpEarned + "<br>" +
|
|
|
|
onlineMpsText + "<br>" + onlineEpsText + "<br>" + offlineTotalMoneyMade + "<br>" + offlineTotalExpEarned + "<br>" +
|
|
|
|
offlineMpsText + "<br>" + offlineEpsText + "<br>";
|
|
|
|
return onlineMps;
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
export {setActiveScriptsClickHandlers, addActiveScriptsItem, deleteActiveScriptsItem, updateActiveScriptsItems};
|