From a461491094fa828d102c5923a4f173d2273d1916 Mon Sep 17 00:00:00 2001
From: Daniel Xie <Daniel Xie>
Date: Tue, 6 Jun 2017 21:33:50 -0500
Subject: [PATCH] Fixed bugs with new Netscript functions. Adding
 ActiveScriptsUI.js file to git

---
 src/ActiveScriptsUI.js    | 222 ++++++++++++++++++++++++++++++++++++++
 src/Constants.js          |  12 ++-
 src/NetscriptEvaluator.js |   6 +-
 src/NetscriptFunctions.js |  11 +-
 4 files changed, 237 insertions(+), 14 deletions(-)
 create mode 100644 src/ActiveScriptsUI.js

diff --git a/src/ActiveScriptsUI.js b/src/ActiveScriptsUI.js
new file mode 100644
index 000000000..12903448b
--- /dev/null
+++ b/src/ActiveScriptsUI.js
@@ -0,0 +1,222 @@
+/* Active Scripts UI*/
+
+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");
+            
+            var panel = this.nextElementSibling;
+            if (panel.style.display === "block") {
+                panel.style.display = "none";
+            } else {
+                panel.style.display = "block";
+            }
+        }
+    }
+    
+    //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");
+            
+            var panel = this.nextElementSibling;
+            if (panel.style.display === "block") {
+                panel.style.display = "none";
+            } else {
+                panel.style.display = "block";
+            } 
+        }
+    }
+}
+
+//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");
+    
+    //Div of entire Panel
+    var panelDiv = document.createElement("div");
+    panelDiv.setAttribute("id", panelname);
+    
+    //Panel Header
+    var panelHdr = document.createElement("button");
+    panelHdr.setAttribute("class", "active-scripts-server-header")
+    panelHdr.setAttribute("id", panelname + "-hdr");
+    panelHdr.innerHTML = server.hostname;
+    
+    //Panel content
+    var panelContentDiv = document.createElement("div");
+    panelContentDiv.setAttribute("class", "active-scripts-server-panel");
+    panelContentDiv.setAttribute("id", panelname + "-content");
+    
+    //List of scripts
+    var panelScriptList = document.createElement("ul");
+    panelScriptList.setAttribute("id", panelname + "-script-list");
+    
+    panelContentDiv.appendChild(panelScriptList);
+    panelDiv.appendChild(panelHdr);
+    panelDiv.appendChild(panelContentDiv);
+    activeScriptsList.appendChild(panelDiv);
+    
+    setActiveScriptsClickHandlers() //Reset click handlers
+    
+    return panelDiv;
+    //TODO Alphabetize Active Scripts list?
+}
+
+//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;
+    }
+    
+    //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;
+    
+    var panel = document.getElementById(panelname);
+    if (panel == null) {
+        panel = createActiveScriptsServerPanel(server);
+    }
+    
+    //Create the element itself. Each element is an accordion collapsible
+    var itemName = "active-scripts-" + server.hostname + "-" + workerscript.name;
+    var item = document.createElement("li");
+    item.setAttribute("id", itemName);
+    
+    var btn = document.createElement("button");
+    btn.setAttribute("class", "active-scripts-script-header");
+    btn.innerHTML = workerscript.name;
+    
+    var itemContentDiv = document.createElement("div");
+    itemContentDiv.setAttribute("class", "active-scripts-script-panel");
+    itemContentDiv.setAttribute("id", itemName + "-content");
+    
+    item.appendChild(btn);
+    item.appendChild(itemContentDiv);
+    
+    createActiveScriptsText(workerscript, itemContentDiv);
+    
+    //Append element to list
+    var list = getActiveScriptsServerList(server);
+    list.appendChild(item);
+    
+    setActiveScriptsClickHandlers() //Reset click handlers
+}
+
+function deleteActiveScriptsItem(workerscript) {
+    var server = getServer(workerscript.serverIp);
+    if (server == null) {
+        console.log("ERROR: Invalid server IP for workerscript.");
+        return;
+    }
+    var itemName = "active-scripts-" + server.hostname + "-" + workerscript.name;
+    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 =
+        "Total online production rate: $" + formatNumber(total, 2) + " / second";
+}
+
+//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;
+    }
+    var itemName = "active-scripts-" + server.hostname + "-" + workerscript.name;
+    var itemContent = document.getElementById(itemName + "-content")
+    
+    //Clear the item
+    while (itemContent.firstChild) {
+        itemContent.removeChild(itemContent.firstChild);
+    }
+    
+    //Add the updated text back. Returns the total online production rate
+    return createActiveScriptsText(workerscript, itemContent);
+}
+
+function createActiveScriptsText(workerscript, item) {
+    var itemText = document.createElement("p");
+    
+    //Server ip/hostname
+    var hostname = workerscript.getServer().hostname;
+    var serverIpHostname = "Server: " + hostname + "(" + workerscript.serverIp + ")";
+    
+    //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, "&nbsp;");
+    
+    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, "&nbsp;");
+    
+    //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, "&nbsp;");
+    
+    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, "&nbsp;");
+    
+    itemText.innerHTML = serverIpHostname + "<br>" + onlineTotalMoneyMade + "<br>" + onlineTotalExpEarned + "<br>" +
+                         onlineMpsText + "<br>" + onlineEpsText + "<br>" + offlineTotalMoneyMade + "<br>" + offlineTotalExpEarned + "<br>" +
+                         offlineMpsText + "<br>" + offlineEpsText + "<br>";
+    
+    item.appendChild(itemText);
+    
+    //Return total online production rate
+    return onlineMps;
+}
\ No newline at end of file
diff --git a/src/Constants.js b/src/Constants.js
index bfbb273bb..b04b2479a 100644
--- a/src/Constants.js
+++ b/src/Constants.js
@@ -74,8 +74,8 @@ CONSTANTS = {
     ServerWeakenAmount: 0.1,        //Amount by which server's security decreases when weakened
     
     //Augmentation Constants
-    AugmentationCostMultiplier: 4.5,  //Used for balancing costs without having to readjust every Augmentation cost
-    AugmentationRepMultiplier: 1.2, //Used for balancing rep cost without having to readjust every value
+    AugmentationCostMultiplier: 5,  //Used for balancing costs without having to readjust every Augmentation cost
+    AugmentationRepMultiplier: 1.5, //Used for balancing rep cost without having to readjust every value
     
     //Maximum number of log entries for a script
     MaxLogCapacity: 40,
@@ -560,7 +560,8 @@ CONSTANTS = {
     "-Server growth no longer happens naturally<br><br>" + 
     "-Servers now have a maximum limit to their money. This limit is 50 times it's starting money<br><br>" + 
     "-Hacking now grants 10% less hacking experience<br><br>" + 
-    "-You can now edit scripts that are running<br><br><br><br>" + 
+    "-You can now edit scripts that are running<br><br>+ " +
+    "-Augmentations cost ~11% more money and 25% more faction reputation<br><br>" + 
     "v0.19.7<br>" + 
     "-Added changelog to Options menu<br>" + 
     "-Bug fix with autocompletion (wasn't working properly for capitalized filenames/programs<br><br>" + 
@@ -644,7 +645,7 @@ CONSTANTS = {
     "-You can now see what an Augmentation does and its price even while its locked<br><br>",
     
     LatestUpdate: 
-     "v0.20.0<br>" + 
+    "v0.20.0<br>" + 
     "-Refactored Netscript Interpreter code. Operations in Netscript should now run significantly faster (Every operation " + 
     "such as a variable assignment, a function call, a binary operator, getting a variable's value, etc. used to take up to several seconds, " + 
     "now each one should only take 750 milliseconds). <br><br>" +
@@ -669,5 +670,6 @@ CONSTANTS = {
     "-Server growth no longer happens naturally<br><br>" + 
     "-Servers now have a maximum limit to their money. This limit is 50 times it's starting money<br><br>" + 
     "-Hacking now grants 10% less hacking experience<br><br>" + 
-    "-You can now edit scripts that are running<br>",
+    "-You can now edit scripts that are running<br><br>+ " +
+    "-Augmentations cost ~11% more money and 25% more faction reputation<br><br>",
 }
\ No newline at end of file
diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js
index 249ffd88c..78623ed88 100644
--- a/src/NetscriptEvaluator.js
+++ b/src/NetscriptEvaluator.js
@@ -268,7 +268,7 @@ function evaluate(exp, workerScript) {
                     Promise.all(argPromises).then(function(args) {
                         if (env.stopFlag) {return reject(workerScript);}
                         filename = args[0];
-                        if (exp.args.length == 2) {
+                        if (exp.args.length == 1) {
                             return Promise.resolve(workerScript.serverIp);
                         } else {
                             return evaluate(exp.args[1], workerScript);
@@ -448,7 +448,7 @@ function evaluate(exp, workerScript) {
                     Promise.all(argPromises).then(function(args) {
                         if (env.stopFlag) {return reject(workerScript);}
                         filename = args[0];
-                        if (exp.args.length == 2) {
+                        if (exp.args.length == 1) {
                             return Promise.resolve(workerScript.serverIp);
                         } else {
                             return evaluate(exp.args[1], workerScript);
@@ -484,7 +484,7 @@ function evaluate(exp, workerScript) {
                     Promise.all(argPromises).then(function(args) {
                         if (env.stopFlag) {return reject(workerScript);}
                         filename = args[0];
-                        if (exp.args.length == 2) {
+                        if (exp.args.length == 1) {
                             return Promise.resolve(workerScript.serverIp);
                         } else {
                             return evaluate(exp.args[1], workerScript);
diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js
index 3add329cc..9f13539b5 100644
--- a/src/NetscriptFunctions.js
+++ b/src/NetscriptFunctions.js
@@ -147,10 +147,10 @@ function netscriptGrow(exp, workerScript) {
             server.moneyAvailable += 1; //It can be grown even if it has no money
             var growthPercentage = processSingleServerGrowth(server, 450);
             workerScript.scriptRef.recordGrow(server.ip);
-            workerScript.scriptRef.log("Available money on " + server.hostname + " grown by " 
-                                       + formatNumber(growthPercentage*100 - 100, 6) + "%");
             var expGain = 0.5 * Player.hacking_exp_mult;
-            workerScript.scriptRef.log("Gained " + expGain + " hacking experience");
+            workerScript.scriptRef.log("Available money on " + server.hostname + " grown by " 
+                                       + formatNumber(growthPercentage*100 - 100, 6) + "%. Gained " + 
+                                       formatNumber(expGain, 4) + " hacking exp");            
             workerScript.scriptRef.onlineExpGained += expGain;
             Player.gainHackingExp(expGain);   
             return Promise.resolve(growthPercentage);
@@ -194,10 +194,9 @@ function netscriptWeaken(exp, workerScript) {
             if (env.stopFlag) {return Promise.reject(workerScript);}
             server.weaken(CONSTANTS.ServerWeakenAmount);
             workerScript.scriptRef.recordWeaken(server.ip);
-            workerScript.scriptRef.log("Server security level on " + server.hostname + " weakened to " + server.hackDifficulty);
-            workerScript.scriptRef.log("Gained 3 hacking experience");
             var expGain = 3 * Player.hacking_exp_mult;
-            workerScript.scriptRef.log("Gained " + expGain + " hacking experience");
+            workerScript.scriptRef.log("Server security level on " + server.hostname + " weakened to " + server.hackDifficulty + 
+                                       ". Gained " + formatNumber(expGain, 4) + " hacking exp");
             workerScript.scriptRef.onlineExpGained += expGain;
             Player.gainHackingExp(expGain); 
             return Promise.resolve(CONSTANTS.ServerWeakenAmount);