/* 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;} //Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] //Grow for (var ip in runningScriptObj.dataMap) { if (runningScriptObj.dataMap.hasOwnProperty(ip)) { if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesGrown = Math.round(0.5 * runningScriptObj.dataMap[ip][2] / 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.dataMap) { if (runningScriptObj.dataMap.hasOwnProperty(ip)) { if (runningScriptObj.dataMap[ip][0] == 0 || runningScriptObj.dataMap[ip][0] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var production = 0.5 * runningScriptObj.dataMap[ip][0] / 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;} if (isNaN(serv.moneyAvailable)) {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.dataMap) { if (runningScriptObj.dataMap.hasOwnProperty(ip)) { if (runningScriptObj.dataMap[ip][1] == 0 || runningScriptObj.dataMap[ip][1] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesHacked = Math.round(0.5 * runningScriptObj.dataMap[ip][1] / 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.dataMap) { if (runningScriptObj.dataMap.hasOwnProperty(ip)) { if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var timesWeakened = Math.round(0.5 * runningScriptObj.dataMap[ip][3] / 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; //[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] this.dataMap = new AllServersMap([0, 0, 0, 0]); } 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 > Settings.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]); } } RunningScript.prototype.clearLog = function() { this.logs.length = 0; } //Update the moneyStolen and numTimesHack maps when hacking RunningScript.prototype.recordHack = function(serverIp, moneyGained, n=1) { if (this.dataMap == null) { //[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] this.dataMap = new AllServersMap([0, 0, 0, 0]); } this.dataMap[serverIp][0] += moneyGained; this.dataMap[serverIp][1] += n; } //Update the grow map when calling grow() RunningScript.prototype.recordGrow = function(serverIp, n=1) { if (this.dataMap == null) { //[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] this.dataMap = new AllServersMap([0, 0, 0, 0]); } this.dataMap[serverIp][2] += n; } //Update the weaken map when calling weaken() { RunningScript.prototype.recordWeaken = function(serverIp, n=1) { if (this.dataMap == null) { //[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] this.dataMap = new AllServersMap([0, 0, 0, 0]); } this.dataMap[serverIp][3] += 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. Initializes every key with a specified value that can either by a number or an array function AllServersMap(arr=false) { for (var ip in AllServers) { if (AllServers.hasOwnProperty(ip)) { if (arr) { this[ip] = [0, 0, 0, 0]; } else { 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;