Fixed UI issues. Added offline production for grow(), weaken(), and hack() in Netscript scripts

This commit is contained in:
Daniel Xie 2017-05-31 20:57:49 -05:00
parent 5d121caadc
commit ebeac47d12
6 changed files with 99 additions and 23 deletions

@ -1,5 +1,5 @@
CONSTANTS = { CONSTANTS = {
Version: "0.18", Version: "0.18.1",
//Max level for any skill, assuming no multipliers. Determined by max numerical value in javascript for experience //Max level for any skill, assuming no multipliers. Determined by max numerical value in javascript for experience
//and the skill level formula in Player.js. Note that all this means it that when experience hits MAX_INT, then //and the skill level formula in Player.js. Note that all this means it that when experience hits MAX_INT, then

@ -6,7 +6,7 @@ function factionInit() {
if (isPositiveNumber(val)) { if (isPositiveNumber(val)) {
var numMoneyDonate = Number(val); var numMoneyDonate = Number(val);
document.getElementById("faction-donate-rep-gain").innerHTML = document.getElementById("faction-donate-rep-gain").innerHTML =
"This donation will result in " + formatNumber(numMoneyDonate/1000 * Player.faction_rep_mult, 3) + " reputation gain"; "This donation will result in " + formatNumber(numMoneyDonate/1000000 * Player.faction_rep_mult, 3) + " reputation gain";
} else { } else {
document.getElementById("faction-donate-rep-gain").innerHTML = document.getElementById("faction-donate-rep-gain").innerHTML =
"This donation will result in 0 reputation gain"; "This donation will result in 0 reputation gain";

@ -282,7 +282,7 @@ function evaluate(exp, workerScript) {
server.moneyAvailable -= moneyGained; server.moneyAvailable -= moneyGained;
Player.gainMoney(moneyGained); Player.gainMoney(moneyGained);
workerScript.scriptRef.onlineMoneyMade += moneyGained; workerScript.scriptRef.onlineMoneyMade += moneyGained;
workerScript.scriptRef.moneyStolenMap[server.ip] += moneyGained; workerScript.scriptRef.recordHack(server.ip, moneyGained);
Player.gainHackingExp(expGainedOnSuccess); Player.gainHackingExp(expGainedOnSuccess);
workerScript.scriptRef.onlineExpGained += expGainedOnSuccess; workerScript.scriptRef.onlineExpGained += expGainedOnSuccess;
@ -389,6 +389,7 @@ function evaluate(exp, workerScript) {
setTimeout(function() { setTimeout(function() {
server.moneyAvailable += 1; //It can be grown even if it has no money server.moneyAvailable += 1; //It can be grown even if it has no money
var growthPercentage = processSingleServerGrowth(server, 450); var growthPercentage = processSingleServerGrowth(server, 450);
workerScript.scriptRef.recordGrow(server.ip);
resolve(growthPercentage); resolve(growthPercentage);
}, growTime); }, growTime);
}); });
@ -435,6 +436,7 @@ function evaluate(exp, workerScript) {
if (env.stopFlag) {reject(workerScript); return;} if (env.stopFlag) {reject(workerScript); return;}
setTimeout(function() { setTimeout(function() {
server.weaken(CONSTANTS.ServerWeakenAmount); server.weaken(CONSTANTS.ServerWeakenAmount);
workerScript.scriptRef.recordWeaken(server.ip);
resolve(CONSTANTS.ServerWeakenAmount); resolve(CONSTANTS.ServerWeakenAmount);
}, weakenTime); }, weakenTime);
}); });

@ -157,9 +157,6 @@ function prestigeAugmentation() {
for (var i = 0; i < homeComp.scripts.length; ++i) { for (var i = 0; i < homeComp.scripts.length; ++i) {
var s = homeComp.scripts[i]; var s = homeComp.scripts[i];
s.reset(); s.reset();
delete s.moneyStolenMap;
s.moneyStolenMap = new AllServersToMoneyMap();
s.moneyStolenMap.printConsole();
} }
//Delete active scripts display elements //Delete active scripts display elements

@ -115,7 +115,6 @@ function Script() {
this.server = ""; //IP of server this script is on this.server = ""; //IP of server this script is on
this.logs = []; //Script logging. Array of strings, with each element being a log entry this.logs = []; //Script logging. Array of strings, with each element being a log entry
/* Properties to calculate offline progress. Only applies for infinitely looping scripts */
//Stats to display on the Scripts menu, and used to determine offline progress //Stats to display on the Scripts menu, and used to determine offline progress
this.offlineRunningTime = 0.01; //Seconds this.offlineRunningTime = 0.01; //Seconds
this.offlineMoneyMade = 0; this.offlineMoneyMade = 0;
@ -124,7 +123,10 @@ function Script() {
this.onlineMoneyMade = 0; this.onlineMoneyMade = 0;
this.onlineExpGained = 0; this.onlineExpGained = 0;
this.moneyStolenMap = new AllServersToMoneyMap(); this.moneyStolenMap = new AllServersMap();
this.numTimesHackMap = new AllServersMap();
this.numTimesGrowMap = new AllServersMap();
this.numTimesWeakenMap = new AllServersMap();
}; };
//Get the script data from the Script Editor and save it to the object //Get the script data from the Script Editor and save it to the object
@ -144,13 +146,7 @@ Script.prototype.saveScript = function() {
this.updateRamUsage(); this.updateRamUsage();
//Clear the stats when the script is updated //Clear the stats when the script is updated
this.offlineRunningTime = 0.01; //Seconds this.reset();
this.offlineMoneyMade = 0;
this.onlineRunningTime = 0.01; //Seconds
this.onlineMoneyMade = 0;
this.lastUpdate = 0;
this.logs = [];
} }
} }
@ -162,6 +158,11 @@ Script.prototype.reset = function() {
this.onlineMoneyMade = 0; this.onlineMoneyMade = 0;
this.onlineExpGained = 0; this.onlineExpGained = 0;
this.logs = []; this.logs = [];
this.moneyStolenMap.reset();
this.numTimesHackMap.reset();
this.numTimesGrowMap.reset();
this.numTimesWeakenMap.reset();
} }
//Updates how much RAM the script uses when it is running. //Updates how much RAM the script uses when it is running.
@ -246,6 +247,34 @@ Script.prototype.displayLog = function() {
} }
} }
//Update the moneyStolen and numTimesHack maps when hacking
Script.prototype.recordHack = function(serverIp, moneyGained) {
if (this.moneyStolenMap == null) {
this.moneyStolenMap = new AllServersMap();
}
if (this.numTimesHackMap == null) {
this.numTimesHackMap = new AllServersMap();
}
this.moneyStolenMap[serverIp] += moneyGained;
this.numTimesHackMap[serverIp] += 1;
}
//Update the grow map when calling grow()
Script.prototype.recordGrow = function(serverIp) {
if (this.numTimesGrowMap == null) {
this.numTimesGrowMap = new AllServersMap();
}
this.numTimesGrowMap[serverIp] += 1;
}
//Update the weaken map when calling weaken() {
Script.prototype.recordWeaken = function(serverIp) {
if (this.numTimesWeakenMap == null) {
this.numTimesWeakenMap = new AllServersMap();
}
this.numTimesWeakenMap[serverIp] += 1;
}
Script.prototype.toJSON = function() { Script.prototype.toJSON = function() {
return Generic_toJSON("Script", this); return Generic_toJSON("Script", this);
} }
@ -315,13 +344,15 @@ scriptCalculateOfflineProduction = function(script) {
totalOfflineProduction += production; totalOfflineProduction += production;
Player.gainMoney(production); Player.gainMoney(production);
console.log(script.filename + " generated $" + production + " while offline by hacking " + serv.hostname); console.log(script.filename + " generated $" + production + " while offline by hacking " + serv.hostname);
script.log(script.filename + " generated $" + production + " while offline by hacking " + serv.hostname);
serv.moneyAvailable -= production; serv.moneyAvailable -= production;
if (serv.moneyAvailable < 0) {serv.moneyAvailable = 0;} if (serv.moneyAvailable < 0) {serv.moneyAvailable = 0;}
} }
} }
//Offline EXP gain
//A script's offline production will always be at most half of its online production. //A script's offline production will always be at most half of its online production.
var expGain = (1/2) * (script.onlineExpGained / script.onlineRunningTime) * timePassed; var expGain = 0.5 * (script.onlineExpGained / script.onlineRunningTime) * timePassed;
expGain *= confidence; expGain *= confidence;
Player.gainHackingExp(expGain); Player.gainHackingExp(expGain);
@ -330,16 +361,54 @@ scriptCalculateOfflineProduction = function(script) {
script.offlineMoneyMade += totalOfflineProduction; script.offlineMoneyMade += totalOfflineProduction;
script.offlineRunningTime += timePassed; script.offlineRunningTime += timePassed;
script.offlineExpGained += expGain; script.offlineExpGained += expGain;
//Fortify a server's security based on how many times it was hacked
for (var ip in script.numTimesHackMap) {
if (script.numTimesHackMap.hasOwnProperty(ip)) {
if (script.numTimesHackMap[ip] == 0 || script.numTimesHackMap[ip] == null) {continue;}
var serv = AllServers[ip];
if (serv == null) {continue;}
var timesHacked = Math.round(0.5 * script.numTimesHackMap[ip] / script.onlineRunningTime * timePassed);
console.log(script.filename + " hacked " + serv.hostname + " " + timesHacked + " times while offline");
script.log(script.filename + " hacked " + serv.hostname + " " + timesHacked + " times while offline");
serv.fortify(CONSTANTS.ServerFortifyAmount * timesHacked);
}
}
//Weaken
for (var ip in script.numTimesWeakenMap) {
if (script.numTimesWeakenMap.hasOwnProperty(ip)) {
if (script.numTimesWeakenMap[ip] == 0 || script.numTimesWeakenMap[ip] == null) {continue;}
var serv = AllServers[ip];
if (serv == null) {continue;}
var timesWeakened = Math.round(0.5 * script.numTimesWeakenMap[ip] / script.onlineRunningTime * timePassed);
console.log(script.filename + " called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline");
script.log(script.filename + " called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline");
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened);
}
}
//Grow
for (var ip in script.numTimesGrowMap) {
if (script.numTimesGrowMap.hasOwnProperty(ip)) {
if (script.numTimesGrowMap[ip] == 0 || script.numTimesGrowMap[ip] == null) {continue;}
var serv = AllServers[ip];
if (serv == null) {continue;}
var timesGrown = Math.round(0.5 * script.numTimesGrowMap[ip] / script.onlineRunningTime * timePassed);
console.log(script.filename + " called grow() on " + serv.hostname + " " + timesGrown + " times while offline");
script.log(script.filename + " called grow() on " + serv.hostname + " " + timesGrown + " times while offline");
//TODO
}
}
return totalOfflineProduction; return totalOfflineProduction;
//DEBUG
var serverName = AllServers[script.server].hostname;
console.log(script.filename + " from server " + serverName + " generated $" + totalOfflineProduction + " TOTAL while offline");
} }
//Creates a function that creates a map/dictionary with the IP of each existing server as //Creates a function that creates a map/dictionary with the IP of each existing server as
//a key, and 0 as the value. This is used to keep track of how much money a script //a key, and 0 as the value. This is used to keep track of how much money a script
//hacks from that server //hacks from that server
function AllServersToMoneyMap() { function AllServersMap() {
for (var ip in AllServers) { for (var ip in AllServers) {
if (AllServers.hasOwnProperty(ip)) { if (AllServers.hasOwnProperty(ip)) {
this[ip] = 0; this[ip] = 0;
@ -347,7 +416,15 @@ function AllServersToMoneyMap() {
} }
} }
AllServersToMoneyMap.prototype.printConsole = function() { AllServersMap.prototype.reset = function() {
for (var ip in this) {
if (this.hasOwnProperty(ip)) {
this[ip] = 0;
}
}
}
AllServersMap.prototype.printConsole = function() {
for (var ip in this) { for (var ip in this) {
if (this.hasOwnProperty(ip)) { if (this.hasOwnProperty(ip)) {
var serv = AllServers[ip]; var serv = AllServers[ip];

@ -926,12 +926,12 @@ var Terminal = {
}, },
executeScanAnalyzeCommand: function(depth=1) { executeScanAnalyzeCommand: function(depth=1) {
//We'll use the AllServersToMoneyMap as a visited() array //We'll use the AllServersMap as a visited() array
//TODO Later refactor this to a generic name //TODO Later refactor this to a generic name
//TODO Using array as stack for now, can make more efficient //TODO Using array as stack for now, can make more efficient
post("~~~~~~~~~~ Beginning scan-analyze ~~~~~~~~~~"); post("~~~~~~~~~~ Beginning scan-analyze ~~~~~~~~~~");
post(" "); post(" ");
var visited = new AllServersToMoneyMap(); var visited = new AllServersMap();
var stack = []; var stack = [];
var depthQueue = [0]; var depthQueue = [0];
var currServ = Player.getCurrentServer(); var currServ = Player.getCurrentServer();