2016-11-21 07:11:14 +01:00
|
|
|
/* Script.js
|
|
|
|
* Script object
|
|
|
|
*/
|
2016-11-24 23:30:33 +01:00
|
|
|
|
2017-05-16 05:06:25 +02:00
|
|
|
function scriptEditorInit() {
|
2017-05-18 06:58:49 +02:00
|
|
|
//Initialize save and close button
|
2017-05-03 06:38:58 +02:00
|
|
|
var closeButton = document.getElementById("script-editor-save-and-close-button");
|
|
|
|
|
|
|
|
closeButton.addEventListener("click", function() {
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
return false;
|
|
|
|
});
|
2017-05-16 05:06:25 +02:00
|
|
|
|
2017-05-18 06:58:49 +02:00
|
|
|
//Allow tabs (four spaces) in all textareas
|
2017-05-16 05:06:25 +02:00
|
|
|
var textareas = document.getElementsByTagName('textarea');
|
|
|
|
var count = textareas.length;
|
|
|
|
for(var i=0;i<count;i++){
|
|
|
|
textareas[i].onkeydown = function(e){
|
|
|
|
if(e.keyCode==9 || e.which==9){
|
|
|
|
e.preventDefault();
|
2017-05-16 06:37:14 +02:00
|
|
|
var start = this.selectionStart;
|
|
|
|
var end = this.selectionEnd;
|
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
//Set textarea value to: text before caret + four spaces + text after caret
|
2017-05-16 06:37:14 +02:00
|
|
|
spaces = " ";
|
|
|
|
this.value = this.value.substring(0, start) + spaces + this.value.substring(end);
|
|
|
|
|
2017-05-16 23:22:54 +02:00
|
|
|
//Put caret at after the four spaces
|
2017-05-16 06:37:14 +02:00
|
|
|
this.selectionStart = this.selectionEnd = start + spaces.length;
|
2017-05-16 05:06:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 06:38:58 +02:00
|
|
|
};
|
2017-05-16 05:06:25 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", scriptEditorInit, false);
|
2017-05-03 06:38:58 +02:00
|
|
|
|
|
|
|
//Define key commands in script editor (ctrl o to save + close, etc.)
|
2016-11-24 23:30:33 +01:00
|
|
|
$(document).keydown(function(e) {
|
|
|
|
if (Engine.currentPage == Engine.Page.ScriptEditor) {
|
2017-05-06 08:24:01 +02:00
|
|
|
//Ctrl + b
|
2017-05-30 00:37:38 +02:00
|
|
|
if (e.keyCode == 66 && e.ctrlKey) {
|
|
|
|
e.preventDefault();
|
2017-05-03 06:38:58 +02:00
|
|
|
saveAndCloseScriptEditor();
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
function saveAndCloseScriptEditor() {
|
|
|
|
var filename = document.getElementById("script-editor-filename").value;
|
2017-05-06 08:24:01 +02:00
|
|
|
if (iTutorialIsRunning && currITutorialStep == iTutorialSteps.TerminalTypeScript) {
|
|
|
|
if (filename != "foodnstuff") {
|
|
|
|
dialogBoxCreate("Leave the script name as 'foodnstuff'!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var code = document.getElementById("script-editor-text").value;
|
2017-05-16 05:06:25 +02:00
|
|
|
code = code.replace(/\s/g, "");
|
|
|
|
if (code.indexOf("while(true){hack('foodnstuff');}") == -1) {
|
2017-05-06 08:24:01 +02:00
|
|
|
dialogBoxCreate("Please copy and paste the code from the tutorial!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
iTutorialNextStep();
|
|
|
|
}
|
2017-05-03 06:38:58 +02:00
|
|
|
|
|
|
|
if (filename == "") {
|
|
|
|
//If no filename...just close and do nothing
|
|
|
|
Engine.loadTerminalContent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkValidFilename(filename) == false) {
|
|
|
|
dialogBoxCreate("Script filename can contain only alphanumerics, hyphens, and underscores");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename += ".script";
|
|
|
|
|
|
|
|
//If the current script matches one thats currently running, throw an error
|
|
|
|
for (var i = 0; i < Player.getCurrentServer().runningScripts.length; i++) {
|
|
|
|
if (filename == Player.getCurrentServer().runningScripts[i].filename) {
|
|
|
|
dialogBoxCreate("Cannot write to script that is currently running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the current script already exists on the server, overwrite it
|
|
|
|
for (var i = 0; i < Player.getCurrentServer().scripts.length; i++) {
|
|
|
|
if (filename == Player.getCurrentServer().scripts[i].filename) {
|
|
|
|
Player.getCurrentServer().scripts[i].saveScript();
|
|
|
|
Engine.loadTerminalContent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the current script does NOT exist, create a new one
|
|
|
|
var script = new Script();
|
|
|
|
script.saveScript();
|
|
|
|
Player.getCurrentServer().scripts.push(script);
|
|
|
|
Engine.loadTerminalContent();
|
|
|
|
}
|
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
//Checks that the string contains only valid characters for a filename, which are alphanumeric,
|
|
|
|
// underscores and hyphens
|
|
|
|
function checkValidFilename(filename) {
|
|
|
|
var regex = /^[a-zA-Z0-9_-]+$/;
|
|
|
|
|
|
|
|
if (filename.match(regex)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-01 23:18:18 +01:00
|
|
|
function Script() {
|
2016-11-24 23:30:33 +01:00
|
|
|
this.filename = "";
|
2016-11-21 12:22:18 +01:00
|
|
|
this.code = "";
|
|
|
|
this.ramUsage = 0;
|
2016-12-19 19:20:19 +01:00
|
|
|
this.server = ""; //IP of server this script is on
|
2017-04-13 19:33:34 +02:00
|
|
|
this.logs = []; //Script logging. Array of strings, with each element being a log entry
|
2016-11-21 07:11:14 +01:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Stats to display on the Scripts menu, and used to determine offline progress
|
2017-05-05 17:50:55 +02:00
|
|
|
this.offlineRunningTime = 0.01; //Seconds
|
2016-12-19 19:20:19 +01:00
|
|
|
this.offlineMoneyMade = 0;
|
2016-12-19 21:59:13 +01:00
|
|
|
this.offlineExpGained = 0;
|
2017-05-05 17:50:55 +02:00
|
|
|
this.onlineRunningTime = 0.01; //Seconds
|
2016-12-19 19:20:19 +01:00
|
|
|
this.onlineMoneyMade = 0;
|
2016-12-19 21:59:13 +01:00
|
|
|
this.onlineExpGained = 0;
|
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
this.moneyStolenMap = new AllServersMap();
|
|
|
|
this.numTimesHackMap = new AllServersMap();
|
|
|
|
this.numTimesGrowMap = new AllServersMap();
|
|
|
|
this.numTimesWeakenMap = new AllServersMap();
|
2016-11-24 23:30:33 +01:00
|
|
|
};
|
2016-11-21 07:11:14 +01:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
//Get the script data from the Script Editor and save it to the object
|
|
|
|
Script.prototype.saveScript = function() {
|
|
|
|
if (Engine.currentPage == Engine.Page.ScriptEditor) {
|
|
|
|
//Update code and filename
|
|
|
|
var code = document.getElementById("script-editor-text").value;
|
2017-05-16 05:06:25 +02:00
|
|
|
this.code = code.replace(/^\s+|\s+$/g, '');
|
2016-11-24 23:30:33 +01:00
|
|
|
|
|
|
|
var filename = document.getElementById("script-editor-filename").value + ".script";
|
|
|
|
this.filename = filename;
|
|
|
|
|
|
|
|
//Server
|
|
|
|
this.server = Player.currentServer;
|
|
|
|
|
2017-05-15 16:15:59 +02:00
|
|
|
//Calculate/update ram usage, execution time, etc.
|
2016-12-14 21:29:40 +01:00
|
|
|
this.updateRamUsage();
|
2016-12-19 19:20:19 +01:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
//Clear the stats when the script is updated
|
2017-06-01 03:57:49 +02:00
|
|
|
this.reset();
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-11-21 12:22:18 +01:00
|
|
|
}
|
2016-12-01 23:39:13 +01:00
|
|
|
|
2017-05-10 22:21:15 +02:00
|
|
|
Script.prototype.reset = function() {
|
|
|
|
this.offlineRunningTime = 0.01; //Seconds
|
|
|
|
this.offlineMoneyMade = 0;
|
|
|
|
this.offlineExpGained = 0;
|
|
|
|
this.onlineRunningTime = 0.01; //Seconds
|
|
|
|
this.onlineMoneyMade = 0;
|
|
|
|
this.onlineExpGained = 0;
|
2017-05-15 16:15:59 +02:00
|
|
|
this.logs = [];
|
2017-06-01 03:57:49 +02:00
|
|
|
|
|
|
|
this.moneyStolenMap.reset();
|
|
|
|
this.numTimesHackMap.reset();
|
|
|
|
this.numTimesGrowMap.reset();
|
|
|
|
this.numTimesWeakenMap.reset();
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Updates how much RAM the script uses when it is running.
|
|
|
|
Script.prototype.updateRamUsage = function() {
|
2017-05-10 23:10:06 +02:00
|
|
|
var baseRam = 1; //Each script requires 1GB to run regardless
|
|
|
|
var codeCopy = this.code.repeat(1);
|
|
|
|
codeCopy = codeCopy.replace(/\s/g,''); //Remove all whitespace
|
|
|
|
|
|
|
|
var whileCount = numOccurrences(codeCopy, "while(");
|
|
|
|
var forCount = numOccurrences(codeCopy, "for(");
|
|
|
|
var ifCount = numOccurrences(codeCopy, "if(");
|
|
|
|
var hackCount = numOccurrences(codeCopy, "hack(");
|
|
|
|
var growCount = numOccurrences(codeCopy, "grow(");
|
2017-05-31 19:37:32 +02:00
|
|
|
var weakenCount = numOccurrences(codeCopy, "weaken(");
|
2017-05-11 06:20:17 +02:00
|
|
|
var nukeCount = numOccurrences(codeCopy, "nuke(");
|
|
|
|
var brutesshCount = numOccurrences(codeCopy, "brutessh(");
|
|
|
|
var ftpcrackCount = numOccurrences(codeCopy, "ftpcrack(");
|
|
|
|
var relaysmtpCount = numOccurrences(codeCopy, "relaysmtp(");
|
|
|
|
var httpwormCount = numOccurrences(codeCopy, "httpworm(");
|
|
|
|
var sqlinjectCount = numOccurrences(codeCopy, "sqlinject(");
|
2017-05-15 16:15:59 +02:00
|
|
|
var runCount = numOccurrences(codeCopy, "run(");
|
2017-05-31 18:33:54 +02:00
|
|
|
var execCount = numOccurrences(codeCopy, "exec(");
|
2017-05-30 04:02:41 +02:00
|
|
|
var scpCount = numOccurrences(codeCopy, "scp(");
|
2017-05-30 01:12:57 +02:00
|
|
|
var hasRootAccessCount = numOccurrences(codeCopy, "hasRootAccess(");
|
|
|
|
var getHostnameCount = numOccurrences(codeCopy, "getHostname(");
|
2017-05-15 16:15:59 +02:00
|
|
|
var getHackingLevelCount = numOccurrences(codeCopy, "getHackingLevel(");
|
|
|
|
var getServerMoneyAvailableCount = numOccurrences(codeCopy, "getServerMoneyAvailable(");
|
2017-05-31 19:37:32 +02:00
|
|
|
var getServerSecurityCount = numOccurrences(codeCopy, "getServerSecurityLevel(");
|
2017-05-15 17:28:59 +02:00
|
|
|
var numOperators = numNetscriptOperators(codeCopy);
|
2017-05-21 05:45:36 +02:00
|
|
|
var purchaseHacknetCount = numOccurrences(codeCopy, "purchaseHacknetNode(");
|
2017-05-25 14:18:34 +02:00
|
|
|
var hacknetnodesArrayCount = numOccurrences(codeCopy, "hacknetnodes[");
|
|
|
|
var hnUpgLevelCount = numOccurrences(codeCopy, ".upgradeLevel(");
|
|
|
|
var hnUpgRamCount = numOccurrences(codeCopy, ".upgradeRam()");
|
|
|
|
var hnUpgCoreCount = numOccurrences(codeCopy, ".upgradeCore()");
|
2017-05-10 23:10:06 +02:00
|
|
|
|
|
|
|
this.ramUsage = baseRam +
|
|
|
|
((whileCount * CONSTANTS.ScriptWhileRamCost) +
|
|
|
|
(forCount * CONSTANTS.ScriptForRamCost) +
|
|
|
|
(ifCount * CONSTANTS.ScriptIfRamCost) +
|
|
|
|
(hackCount * CONSTANTS.ScriptHackRamCost) +
|
2017-05-11 06:20:17 +02:00
|
|
|
(growCount * CONSTANTS.ScriptGrowRamCost) +
|
2017-05-31 19:37:32 +02:00
|
|
|
(weakenCount * CONSTANTS.ScriptWeakenRamCost) +
|
2017-05-11 06:20:17 +02:00
|
|
|
(nukeCount * CONSTANTS.ScriptNukeRamCost) +
|
|
|
|
(brutesshCount * CONSTANTS.ScriptBrutesshRamCost) +
|
|
|
|
(ftpcrackCount * CONSTANTS.ScriptFtpcrackRamCost) +
|
|
|
|
(relaysmtpCount * CONSTANTS.ScriptRelaysmtpRamCost) +
|
|
|
|
(httpwormCount * CONSTANTS.ScriptHttpwormRamCost) +
|
2017-05-15 16:15:59 +02:00
|
|
|
(sqlinjectCount * CONSTANTS.ScriptSqlinjectRamCost) +
|
|
|
|
(runCount * CONSTANTS.ScriptRunRamCost) +
|
2017-05-31 18:33:54 +02:00
|
|
|
(execCount * CONSTANTS.ScriptExecRamCost) +
|
2017-05-30 04:02:41 +02:00
|
|
|
(scpCount * CONSTANTS.ScriptScpRamCost) +
|
2017-05-30 01:12:57 +02:00
|
|
|
(hasRootAccessCount * CONSTANTS.ScriptHasRootAccessRamCost) +
|
|
|
|
(getHostnameCount * CONSTANTS.ScriptGetHostnameRamCost) +
|
2017-05-15 16:15:59 +02:00
|
|
|
(getHackingLevelCount * CONSTANTS.ScriptGetHackingLevelRamCost) +
|
2017-05-15 17:38:17 +02:00
|
|
|
(getServerMoneyAvailableCount * CONSTANTS.ScriptGetServerMoneyRamCost) +
|
2017-05-31 19:37:32 +02:00
|
|
|
(getServerSecurityCount * CONSTANTS.ScriptGetServerSecurityRamCost) +
|
2017-05-21 05:45:36 +02:00
|
|
|
(numOperators * CONSTANTS.ScriptOperatorRamCost) +
|
|
|
|
(purchaseHacknetCount * CONSTANTS.ScriptPurchaseHacknetRamCost) +
|
2017-05-25 14:18:34 +02:00
|
|
|
(hacknetnodesArrayCount * CONSTANTS.ScriptHacknetNodesRamCost) +
|
|
|
|
(hnUpgLevelCount * CONSTANTS.ScriptHNUpgLevelRamCost) +
|
|
|
|
(hnUpgRamCount * CONSTANTS.ScriptHNUpgRamRamCost) +
|
|
|
|
(hnUpgCoreCount * CONSTANTS.ScriptHNUpgCoreRamCost));
|
2017-05-10 23:10:06 +02:00
|
|
|
console.log("ram usage: " + this.ramUsage);
|
2017-05-15 17:38:17 +02:00
|
|
|
if (isNaN(this.ramUsage)) {
|
|
|
|
dialogBoxCreate("ERROR in calculating ram usage. This is a bug, please report to game develoepr");
|
|
|
|
}
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 23:47:06 +02:00
|
|
|
Script.prototype.log = function(txt) {
|
2017-04-13 19:33:34 +02:00
|
|
|
if (this.logs.length > CONSTANTS.MaxLogCapacity) {
|
2017-03-31 23:47:06 +02:00
|
|
|
//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
|
2017-04-13 19:33:34 +02:00
|
|
|
this.logs.shift();
|
2017-03-31 23:47:06 +02:00
|
|
|
}
|
2017-04-13 19:33:34 +02:00
|
|
|
this.logs.push(txt);
|
2017-03-31 23:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Script.prototype.displayLog = function() {
|
2017-04-13 19:33:34 +02:00
|
|
|
for (var i = 0; i < this.logs.length; ++i) {
|
|
|
|
post(this.logs[i]);
|
2017-03-31 23:47:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-01 03:57:49 +02:00
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
2017-03-31 23:47:06 +02:00
|
|
|
|
2016-12-01 23:39:13 +01:00
|
|
|
Script.prototype.toJSON = function() {
|
|
|
|
return Generic_toJSON("Script", this);
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:47:06 +02:00
|
|
|
|
2016-12-01 23:39:13 +01:00
|
|
|
Script.fromJSON = function(value) {
|
|
|
|
return Generic_fromJSON(Script, value.data);
|
|
|
|
}
|
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
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
|
2016-12-15 18:51:23 +01:00
|
|
|
loadAllRunningScripts = function() {
|
2016-12-14 22:44:18 +01:00
|
|
|
var count = 0;
|
2017-05-20 11:27:42 +02:00
|
|
|
var total = 0;
|
2016-12-15 18:51:23 +01:00
|
|
|
for (var property in AllServers) {
|
|
|
|
if (AllServers.hasOwnProperty(property)) {
|
|
|
|
var server = AllServers[property];
|
2016-12-19 19:20:19 +01:00
|
|
|
|
|
|
|
//Reset each server's RAM usage to 0
|
|
|
|
server.ramUsed = 0;
|
|
|
|
|
2016-12-15 18:51:23 +01:00
|
|
|
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);
|
2016-12-19 21:59:13 +01:00
|
|
|
|
|
|
|
//Offline production
|
2017-05-20 11:27:42 +02:00
|
|
|
total += scriptCalculateOfflineProduction(script);
|
2016-12-15 18:51:23 +01:00
|
|
|
}
|
2016-12-14 22:22:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-20 11:27:42 +02:00
|
|
|
return total;
|
2016-12-14 22:44:18 +01:00
|
|
|
console.log("Loaded " + count.toString() + " running scripts");
|
2016-12-19 21:59:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-05-10 19:42:46 +02:00
|
|
|
console.log("Offline for " + timePassed + " seconds");
|
2016-12-19 21:59:13 +01:00
|
|
|
|
|
|
|
//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
|
2017-05-05 03:08:44 +02:00
|
|
|
//4 hours (14400 sec) then we are completely confident in its ability
|
|
|
|
var confidence = (script.onlineRunningTime) / 14400;
|
2016-12-19 21:59:13 +01:00
|
|
|
if (confidence >= 1) {confidence = 1;}
|
2017-05-10 19:42:46 +02:00
|
|
|
console.log("onlineRunningTime: " + script.onlineRunningTime);
|
|
|
|
console.log("Confidence: " + confidence);
|
|
|
|
|
|
|
|
var totalOfflineProduction = 0;
|
|
|
|
for (var ip in script.moneyStolenMap) {
|
|
|
|
if (script.moneyStolenMap.hasOwnProperty(ip)) {
|
2017-05-10 22:21:15 +02:00
|
|
|
if (script.moneyStolenMap[ip] == 0 || script.moneyStolenMap[ip] == null) {continue;}
|
2017-05-10 19:42:46 +02:00
|
|
|
var serv = AllServers[ip];
|
2017-05-10 22:21:15 +02:00
|
|
|
if (serv == null) {continue;}
|
2017-05-10 19:42:46 +02:00
|
|
|
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);
|
2017-06-01 03:57:49 +02:00
|
|
|
script.log(script.filename + " generated $" + production + " while offline by hacking " + serv.hostname);
|
2017-05-10 19:42:46 +02:00
|
|
|
serv.moneyAvailable -= production;
|
|
|
|
if (serv.moneyAvailable < 0) {serv.moneyAvailable = 0;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
//Offline EXP gain
|
2017-05-10 19:42:46 +02:00
|
|
|
//A script's offline production will always be at most half of its online production.
|
2017-06-01 03:57:49 +02:00
|
|
|
var expGain = 0.5 * (script.onlineExpGained / script.onlineRunningTime) * timePassed;
|
2016-12-19 21:59:13 +01:00
|
|
|
expGain *= confidence;
|
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
Player.gainHackingExp(expGain);
|
2016-12-19 21:59:13 +01:00
|
|
|
|
|
|
|
//Update script stats
|
2017-05-10 19:42:46 +02:00
|
|
|
script.offlineMoneyMade += totalOfflineProduction;
|
2016-12-19 21:59:13 +01:00
|
|
|
script.offlineRunningTime += timePassed;
|
|
|
|
script.offlineExpGained += expGain;
|
2017-06-01 03:57:49 +02:00
|
|
|
|
|
|
|
//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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 11:27:42 +02:00
|
|
|
return totalOfflineProduction;
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
2017-06-01 03:57:49 +02:00
|
|
|
function AllServersMap() {
|
2017-05-10 19:42:46 +02:00
|
|
|
for (var ip in AllServers) {
|
|
|
|
if (AllServers.hasOwnProperty(ip)) {
|
|
|
|
this[ip] = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 20:12:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
AllServersMap.prototype.reset = function() {
|
|
|
|
for (var ip in this) {
|
|
|
|
if (this.hasOwnProperty(ip)) {
|
|
|
|
this[ip] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AllServersMap.prototype.printConsole = function() {
|
2017-05-12 20:12:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|