/* Script.js * Script object */ function scriptEditorInit() { //Initialize save and close button var closeButton = document.getElementById("script-editor-save-and-close-button"); closeButton.addEventListener("click", function() { saveAndCloseScriptEditor(); return false; }); //Allow tabs (four spaces) in all textareas var textareas = document.getElementsByTagName('textarea'); var count = textareas.length; for(var i=0;i= 1) {confidence = 1;} //Grow for (var ip in runningScriptObj.numTimesGrowMap) { if (runningScriptObj.numTimesGrowMap.hasOwnProperty(ip)) { if (runningScriptObj.numTimesGrowMap[ip] == 0 || runningScriptObj.numTimesGrowMap[ip] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesGrown = Math.round(0.5 * runningScriptObj.numTimesGrowMap[ip] / runningScriptObj.onlineRunningTime * timePassed); console.log(runningScriptObj.filename + " called grow() on " + serv.hostname + " " + timesGrown + " times while offline"); runningScriptObj.log("Called grow() on " + serv.hostname + " " + timesGrown + " times while offline"); var growth = processSingleServerGrowth(serv, timesGrown * 450); runningScriptObj.log(serv.hostname + " grown by " + formatNumber(growth * 100 - 100, 6) + "% from grow() calls made while offline"); } } var totalOfflineProduction = 0; for (var ip in runningScriptObj.moneyStolenMap) { if (runningScriptObj.moneyStolenMap.hasOwnProperty(ip)) { if (runningScriptObj.moneyStolenMap[ip] == 0 || runningScriptObj.moneyStolenMap[ip] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var production = 0.5 * runningScriptObj.moneyStolenMap[ip] / runningScriptObj.onlineRunningTime * timePassed; production *= confidence; if (production > serv.moneyAvailable) { production = serv.moneyAvailable; } totalOfflineProduction += production; Player.gainMoney(production); console.log(runningScriptObj.filename + " generated $" + production + " while offline by hacking " + serv.hostname); runningScriptObj.log(runningScriptObj.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 = 0.5 * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed; expGain *= confidence; Player.gainHackingExp(expGain); //Update script stats runningScriptObj.offlineMoneyMade += totalOfflineProduction; runningScriptObj.offlineRunningTime += timePassed; runningScriptObj.offlineExpGained += expGain; //Fortify a server's security based on how many times it was hacked for (var ip in runningScriptObj.numTimesHackMap) { if (runningScriptObj.numTimesHackMap.hasOwnProperty(ip)) { if (runningScriptObj.numTimesHackMap[ip] == 0 || runningScriptObj.numTimesHackMap[ip] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesHacked = Math.round(0.5 * runningScriptObj.numTimesHackMap[ip] / runningScriptObj.onlineRunningTime * timePassed); console.log(runningScriptObj.filename + " hacked " + serv.hostname + " " + timesHacked + " times while offline"); runningScriptObj.log("Hacked " + serv.hostname + " " + timesHacked + " times while offline"); serv.fortify(CONSTANTS.ServerFortifyAmount * timesHacked); } } //Weaken for (var ip in runningScriptObj.numTimesWeakenMap) { if (runningScriptObj.numTimesWeakenMap.hasOwnProperty(ip)) { if (runningScriptObj.numTimesWeakenMap[ip] == 0 || runningScriptObj.numTimesWeakenMap[ip] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesWeakened = Math.round(0.5 * runningScriptObj.numTimesWeakenMap[ip] / runningScriptObj.onlineRunningTime * timePassed); console.log(runningScriptObj.filename + " called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline"); runningScriptObj.log("Called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline"); serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened); } } return totalOfflineProduction; } //Returns a RunningScript object matching the filename and arguments on the //designated server, and false otherwise function findRunningScript(filename, args, server) { for (var i = 0; i < server.runningScripts.length; ++i) { if (server.runningScripts[i].filename == filename && compareArrays(server.runningScripts[i].args, args)) { return server.runningScripts[i]; } } return null; } function RunningScript(script, args) { if (script == null || script == undefined) {return;} this.filename = script.filename; this.args = args; this.scriptRef = script; this.server = script.server; //IP Address only this.logs = []; //Script logging. Array of strings, with each element being a log entry //Stats to display on the Scripts menu, and used to determine offline progress this.offlineRunningTime = 0.01; //Seconds this.offlineMoneyMade = 0; this.offlineExpGained = 0; this.onlineRunningTime = 0.01; //Seconds this.onlineMoneyMade = 0; this.onlineExpGained = 0; this.threads = 1; this.moneyStolenMap = new AllServersMap(); this.numTimesHackMap = new AllServersMap(); this.numTimesGrowMap = new AllServersMap(); this.numTimesWeakenMap = new AllServersMap(); } RunningScript.prototype.reset = function() { this.scriptRef.updateRamUsage(); this.offlineRunningTime = 0.01; //Seconds this.offlineMoneyMade = 0; this.offlineExpGained = 0; this.onlineRunningTime = 0.01; //Seconds this.onlineMoneyMade = 0; this.onlineExpGained = 0; this.logs = []; } RunningScript.prototype.log = function(txt) { if (this.logs.length > CONSTANTS.MaxLogCapacity) { //Delete first element and add new log entry to the end. //TODO Eventually it might be better to replace this with circular array //to improve performance this.logs.shift(); } this.logs.push(txt); } RunningScript.prototype.displayLog = function() { for (var i = 0; i < this.logs.length; ++i) { post(this.logs[i]); } } //Update the moneyStolen and numTimesHack maps when hacking RunningScript.prototype.recordHack = function(serverIp, moneyGained, n=1) { if (this.moneyStolenMap == null) { this.moneyStolenMap = new AllServersMap(); } if (this.numTimesHackMap == null) { this.numTimesHackMap = new AllServersMap(); } this.moneyStolenMap[serverIp] += moneyGained; this.numTimesHackMap[serverIp] += n; } //Update the grow map when calling grow() RunningScript.prototype.recordGrow = function(serverIp, n=1) { if (this.numTimesGrowMap == null) { this.numTimesGrowMap = new AllServersMap(); } this.numTimesGrowMap[serverIp] += n; } //Update the weaken map when calling weaken() { RunningScript.prototype.recordWeaken = function(serverIp, n=1) { if (this.numTimesWeakenMap == null) { this.numTimesWeakenMap = new AllServersMap(); } this.numTimesWeakenMap[serverIp] += n; } RunningScript.prototype.toJSON = function() { return Generic_toJSON("RunningScript", this); } RunningScript.fromJSON = function(value) { return Generic_fromJSON(RunningScript, value.data); } //Creates an object 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 AllServersMap() { for (var ip in AllServers) { if (AllServers.hasOwnProperty(ip)) { this[ip] = 0; } } } 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]; if (serv == null) { console.log("Warning null server encountered with ip: " + ip); continue; } } } } AllServersMap.prototype.toJSON = function() { return Generic_toJSON("AllServersMap", this); } AllServersMap.fromJSON = function(value) { return Generic_fromJSON(AllServersMap, value.data); } Reviver.constructors.AllServersMap = AllServersMap;