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 = {
Version: "0.18",
Version: "0.18.1",
//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

@ -6,7 +6,7 @@ function factionInit() {
if (isPositiveNumber(val)) {
var numMoneyDonate = Number(val);
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 {
document.getElementById("faction-donate-rep-gain").innerHTML =
"This donation will result in 0 reputation gain";

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

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

@ -115,7 +115,6 @@ function Script() {
this.server = ""; //IP of server this script is on
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
this.offlineRunningTime = 0.01; //Seconds
this.offlineMoneyMade = 0;
@ -124,7 +123,10 @@ function Script() {
this.onlineMoneyMade = 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
@ -144,13 +146,7 @@ Script.prototype.saveScript = function() {
this.updateRamUsage();
//Clear the stats when the script is updated
this.offlineRunningTime = 0.01; //Seconds
this.offlineMoneyMade = 0;
this.onlineRunningTime = 0.01; //Seconds
this.onlineMoneyMade = 0;
this.lastUpdate = 0;
this.logs = [];
this.reset();
}
}
@ -162,6 +158,11 @@ Script.prototype.reset = function() {
this.onlineMoneyMade = 0;
this.onlineExpGained = 0;
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.
@ -245,6 +246,34 @@ Script.prototype.displayLog = function() {
post(this.logs[i]);
}
}
//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() {
return Generic_toJSON("Script", this);
@ -315,13 +344,15 @@ scriptCalculateOfflineProduction = function(script) {
totalOfflineProduction += production;
Player.gainMoney(production);
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;
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.
var expGain = (1/2) * (script.onlineExpGained / script.onlineRunningTime) * timePassed;
var expGain = 0.5 * (script.onlineExpGained / script.onlineRunningTime) * timePassed;
expGain *= confidence;
Player.gainHackingExp(expGain);
@ -330,16 +361,54 @@ scriptCalculateOfflineProduction = function(script) {
script.offlineMoneyMade += totalOfflineProduction;
script.offlineRunningTime += timePassed;
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;
//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
//a key, and 0 as the value. This is used to keep track of how much money a script
//hacks from that server
function AllServersToMoneyMap() {
function AllServersMap() {
for (var ip in AllServers) {
if (AllServers.hasOwnProperty(ip)) {
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) {
if (this.hasOwnProperty(ip)) {
var serv = AllServers[ip];

@ -926,12 +926,12 @@ var Terminal = {
},
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 Using array as stack for now, can make more efficient
post("~~~~~~~~~~ Beginning scan-analyze ~~~~~~~~~~");
post(" ");
var visited = new AllServersToMoneyMap();
var visited = new AllServersMap();
var stack = [];
var depthQueue = [0];
var currServ = Player.getCurrentServer();