/* 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 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); } Script.prototype.displayLog = function() { for (var i = 0; i < this.logs.length; ++i) { 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); } Script.fromJSON = function(value) { return Generic_fromJSON(Script, value.data); } Reviver.constructors.Script = Script; //Called when the game is loaded. Loads all running scripts (from all servers) //into worker scripts so that they will start running loadAllRunningScripts = function() { var count = 0; var total = 0; for (var property in AllServers) { if (AllServers.hasOwnProperty(property)) { var server = AllServers[property]; //Reset each server's RAM usage to 0 server.ramUsed = 0; for (var j = 0; j < server.runningScripts.length; ++j) { count++; //runningScripts array contains only names, so find the actual script object var script = server.getScript(server.runningScripts[j]); if (script == null) {continue;} addWorkerScript(script, server); //Offline production total += scriptCalculateOfflineProduction(script); } } } return total; console.log("Loaded " + count.toString() + " running scripts"); } scriptCalculateOfflineProduction = function(script) { //The Player object stores the last update time from when we were online var thisUpdate = new Date().getTime(); var lastUpdate = Player.lastUpdate; var timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds console.log("Offline for " + timePassed + " seconds"); //Calculate the "confidence" rating of the script's true production. This is based //entirely off of time. We will arbitrarily say that if a script has been running for //4 hours (14400 sec) then we are completely confident in its ability var confidence = (script.onlineRunningTime) / 14400; if (confidence >= 1) {confidence = 1;} console.log("onlineRunningTime: " + script.onlineRunningTime); console.log("Confidence: " + confidence); var totalOfflineProduction = 0; for (var ip in script.moneyStolenMap) { if (script.moneyStolenMap.hasOwnProperty(ip)) { if (script.moneyStolenMap[ip] == 0 || script.moneyStolenMap[ip] == null) {continue;} var serv = AllServers[ip]; if (serv == null) {continue;} var production = 0.5 * script.moneyStolenMap[ip] / script.onlineRunningTime * timePassed; production *= confidence; if (production > serv.moneyAvailable) { production = serv.moneyAvailable; } 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 = 0.5 * (script.onlineExpGained / script.onlineRunningTime) * timePassed; expGain *= confidence; Player.gainHackingExp(expGain); //Update script stats 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("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("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("Called grow() on " + serv.hostname + " " + timesGrown + " times while offline"); var growth = processSingleServerGrowth(serv, timesGrown * 450); script.log(serv.hostname + " grown by " + formatNumber(growth * 100 - 100, 6) + "% from grow() calls made while offline"); } } return totalOfflineProduction; } //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 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;