2017-09-01 16:12:40 +02:00
|
|
|
var ace = require('brace');
|
|
|
|
require('brace/mode/javascript');
|
2017-09-09 01:42:56 +02:00
|
|
|
require('brace/mode/netscript');
|
2017-09-05 03:03:29 +02:00
|
|
|
require('brace/theme/chaos');
|
|
|
|
require('brace/theme/chrome');
|
2017-09-01 16:12:40 +02:00
|
|
|
require('brace/theme/monokai');
|
2017-09-05 03:03:29 +02:00
|
|
|
require('brace/theme/solarized_dark');
|
|
|
|
require('brace/theme/solarized_light');
|
|
|
|
require('brace/theme/terminal');
|
|
|
|
require('brace/theme/twilight');
|
|
|
|
require('brace/theme/xcode');
|
|
|
|
require("brace/keybinding/vim");
|
|
|
|
require("brace/keybinding/emacs");
|
2017-10-04 03:50:13 +02:00
|
|
|
require("brace/ext/language_tools");
|
2017-09-01 16:12:40 +02:00
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
import {CONSTANTS} from "./Constants.js";
|
|
|
|
import {Engine} from "./engine.js";
|
|
|
|
import {iTutorialSteps, iTutorialNextStep,
|
|
|
|
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial.js";
|
2017-10-04 03:50:13 +02:00
|
|
|
import {NetscriptFunctions} from "./NetscriptFunctions.js";
|
2018-01-27 07:52:39 +01:00
|
|
|
import {addWorkerScript, killWorkerScript,
|
|
|
|
WorkerScript} from "./NetscriptWorker.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {Player} from "./Player.js";
|
|
|
|
import {AllServers, processSingleServerGrowth} from "./Server.js";
|
|
|
|
import {Settings} from "./Settings.js";
|
2018-01-20 05:47:57 +01:00
|
|
|
import {post} from "./Terminal.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
import {parse, Node} from "../utils/acorn.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
|
|
|
import {Reviver, Generic_toJSON,
|
|
|
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
2018-01-27 07:52:39 +01:00
|
|
|
import {compareArrays, createElement} from "../utils/HelperFunctions.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {formatNumber, numOccurrences,
|
|
|
|
numNetscriptOperators} from "../utils/StringHelperFunctions.js";
|
2016-11-24 23:30:33 +01:00
|
|
|
|
2017-09-05 03:03:29 +02:00
|
|
|
var keybindings = {
|
|
|
|
ace: null,
|
|
|
|
vim: "ace/keyboard/vim",
|
|
|
|
emacs: "ace/keyboard/emacs",
|
|
|
|
};
|
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
var scriptEditorRamCheck = null, scriptEditorRamText = null;
|
2017-05-16 05:06:25 +02:00
|
|
|
function scriptEditorInit() {
|
2018-01-27 07:52:39 +01:00
|
|
|
//Create buttons at the bottom of script editor
|
|
|
|
var wrapper = document.getElementById("script-editor-buttons-wrapper");
|
|
|
|
if (wrapper == null) {
|
|
|
|
console.log("Error finding 'script-editor-buttons-wrapper'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var closeButton = createElement("a", {
|
|
|
|
class:"a-link-button", display:"inline-block",
|
|
|
|
innerText:"Save & Close (Ctrl + b)",
|
|
|
|
clickListener:()=>{
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
scriptEditorRamText = createElement("p", {
|
|
|
|
display:"inline-block", margin:"10px", id:"script-editor-status-text"
|
|
|
|
});
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
var checkboxLabel = createElement("label", {
|
|
|
|
for:"script-editor-ram-check", margin:"4px", marginTop: "8px",
|
|
|
|
innerText:"Dynamic RAM Usage Checker", color:"white",
|
|
|
|
tooltip:"Enable/Disable the dynamic RAM Usage display. You may " +
|
|
|
|
"want to disable it for very long scripts because there may be " +
|
|
|
|
"performance issues"
|
2017-05-03 06:38:58 +02:00
|
|
|
});
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
scriptEditorRamCheck = createElement("input", {
|
|
|
|
type:"checkbox", name:"script-editor-ram-check", id:"script-editor-ram-check",
|
|
|
|
margin:"4px", marginTop: "8px",
|
|
|
|
});
|
|
|
|
scriptEditorRamCheck.checked = true;
|
|
|
|
|
|
|
|
var documentationButton = createElement("a", {
|
|
|
|
display:"inline-block", class:"a-link-button", innerText:"Netscript Documentation",
|
|
|
|
href:"https://bitburner.wikia.com/wiki/Netscript",
|
|
|
|
target:"_blank"
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.appendChild(closeButton);
|
|
|
|
wrapper.appendChild(scriptEditorRamText);
|
|
|
|
wrapper.appendChild(scriptEditorRamCheck);
|
|
|
|
wrapper.appendChild(checkboxLabel);
|
|
|
|
wrapper.appendChild(documentationButton);
|
|
|
|
|
2017-09-05 03:03:29 +02:00
|
|
|
//Initialize ACE Script editor
|
|
|
|
var editor = ace.edit('javascript-editor');
|
2017-09-09 01:42:56 +02:00
|
|
|
editor.getSession().setMode('ace/mode/netscript');
|
2017-09-05 03:03:29 +02:00
|
|
|
editor.setTheme('ace/theme/monokai');
|
|
|
|
document.getElementById('javascript-editor').style.fontSize='16px';
|
|
|
|
editor.setOption("showPrintMargin", false);
|
|
|
|
|
|
|
|
/* Script editor options */
|
|
|
|
//Theme
|
|
|
|
var themeDropdown = document.getElementById("script-editor-option-theme");
|
|
|
|
themeDropdown.selectedIndex = 2;
|
|
|
|
themeDropdown.onchange = function() {
|
|
|
|
var val = themeDropdown.value;
|
|
|
|
var themePath = "ace/theme/" + val.toLowerCase();
|
|
|
|
editor.setTheme(themePath);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Keybinding
|
|
|
|
var keybindingDropdown = document.getElementById("script-editor-option-keybinding");
|
|
|
|
keybindingDropdown.onchange = function() {
|
|
|
|
var val = keybindingDropdown.value;
|
|
|
|
editor.setKeyboardHandler(keybindings[val.toLowerCase()]);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Highlight Active line
|
|
|
|
var highlightActiveChkBox = document.getElementById("script-editor-option-highlightactiveline");
|
|
|
|
highlightActiveChkBox.onchange = function() {
|
|
|
|
editor.setHighlightActiveLine(highlightActiveChkBox.checked);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Show Invisibles
|
|
|
|
var showInvisiblesChkBox = document.getElementById("script-editor-option-showinvisibles");
|
|
|
|
showInvisiblesChkBox.onchange = function() {
|
|
|
|
editor.setShowInvisibles(showInvisiblesChkBox.checked);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Use Soft Tab
|
|
|
|
var softTabChkBox = document.getElementById("script-editor-option-usesofttab");
|
|
|
|
softTabChkBox.onchange = function() {
|
|
|
|
editor.getSession().setUseSoftTabs(softTabChkBox.checked);
|
|
|
|
};
|
|
|
|
|
2017-09-19 20:38:03 +02:00
|
|
|
//Configure some of the VIM keybindings
|
|
|
|
ace.config.loadModule('ace/keyboard/vim', function(module) {
|
|
|
|
var VimApi = module.CodeMirror.Vim;
|
|
|
|
VimApi.defineEx('write', 'w', function(cm, input) {
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
});
|
|
|
|
VimApi.defineEx('quit', 'q', function(cm, input) {
|
|
|
|
Engine.loadTerminalContent();
|
|
|
|
});
|
2017-10-02 04:35:22 +02:00
|
|
|
VimApi.defineEx('xwritequit', 'x', function(cm, input) {
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
});
|
|
|
|
VimApi.defineEx('wqwritequit', 'wq', function(cm, input) {
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
});
|
2017-09-19 20:38:03 +02:00
|
|
|
});
|
2017-10-04 03:50:13 +02:00
|
|
|
|
|
|
|
//Function autocompleter
|
|
|
|
editor.setOption("enableBasicAutocompletion", true);
|
|
|
|
var autocompleter = {
|
|
|
|
getCompletions: function(editor, session, pos, prefix, callback) {
|
|
|
|
if (prefix.length === 0) {callback(null, []); return;}
|
|
|
|
var words = [];
|
|
|
|
var fns = NetscriptFunctions(null);
|
|
|
|
for (var name in fns) {
|
|
|
|
if (fns.hasOwnProperty(name)) {
|
|
|
|
words.push({
|
|
|
|
name: name,
|
|
|
|
value: name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null, words);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
editor.completers = [autocompleter];
|
2017-09-19 20:38:03 +02:00
|
|
|
}
|
2017-05-16 05:06:25 +02:00
|
|
|
document.addEventListener("DOMContentLoaded", scriptEditorInit, false);
|
2017-05-03 06:38:58 +02:00
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
//Updates RAM usage in script
|
2017-08-30 19:44:29 +02:00
|
|
|
function updateScriptEditorContent() {
|
2018-01-27 07:52:39 +01:00
|
|
|
if (scriptEditorRamCheck == null || !scriptEditorRamCheck.checked) {
|
|
|
|
scriptEditorRamText.innerText = "N/A";
|
|
|
|
return;
|
|
|
|
}
|
2017-09-01 16:12:40 +02:00
|
|
|
var editor = ace.edit('javascript-editor');
|
|
|
|
var code = editor.getValue();
|
2017-07-13 18:54:29 +02:00
|
|
|
var codeCopy = code.repeat(1);
|
|
|
|
var ramUsage = calculateRamUsage(codeCopy);
|
2018-01-27 07:52:39 +01:00
|
|
|
if (ramUsage !== -1) {
|
|
|
|
scriptEditorRamText.innerText = "RAM: " + formatNumber(ramUsage, 2).toString() + "GB";
|
|
|
|
}
|
2017-07-13 18:54:29 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2017-09-01 16:12:40 +02:00
|
|
|
var editor = ace.edit('javascript-editor');
|
|
|
|
var code = editor.getValue();
|
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;
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
iTutorialNextStep();
|
2017-05-06 08:24:01 +02:00
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
if (filename == "") {
|
2017-06-02 19:18:53 +02:00
|
|
|
dialogBoxCreate("You must specify a filename!");
|
2017-05-03 06:38:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
if (checkValidFilename(filename) == false) {
|
|
|
|
dialogBoxCreate("Script filename can contain only alphanumerics, hyphens, and underscores");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
filename += ".script";
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
//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_-]+$/;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
if (filename.match(regex)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:39:56 +02: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
|
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
|
2017-09-01 16:12:40 +02:00
|
|
|
var editor = ace.edit('javascript-editor');
|
|
|
|
var code = editor.getValue();
|
2017-05-16 05:06:25 +02:00
|
|
|
this.code = code.replace(/^\s+|\s+$/g, '');
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
var filename = document.getElementById("script-editor-filename").value + ".script";
|
|
|
|
this.filename = filename;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
//Server
|
|
|
|
this.server = Player.currentServer;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
|
|
|
//Calculate/update ram usage, execution time, etc.
|
2016-12-14 21:29:40 +01:00
|
|
|
this.updateRamUsage();
|
2016-11-24 23:30:33 +01:00
|
|
|
}
|
2016-11-21 12:22:18 +01:00
|
|
|
}
|
2016-12-01 23:39:13 +01:00
|
|
|
|
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 codeCopy = this.code.repeat(1);
|
2018-01-27 07:52:39 +01:00
|
|
|
var res = calculateRamUsage(codeCopy);
|
|
|
|
if (res !== -1) {
|
|
|
|
this.ramUsage = res;
|
2017-07-13 18:54:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateRamUsage(codeCopy) {
|
2018-01-27 07:52:39 +01:00
|
|
|
//Create a temporary/mock WorkerScript and an AST from the code
|
|
|
|
var workerScript = new WorkerScript({
|
|
|
|
filename:"foo",
|
|
|
|
scriptRef: {code:""},
|
|
|
|
args:[]
|
|
|
|
});
|
|
|
|
workerScript.checkingRam = true; //Netscript functions will return RAM usage
|
|
|
|
|
|
|
|
try {
|
|
|
|
var ast = parse(codeCopy);
|
|
|
|
} catch(e) {
|
|
|
|
console.log("returning -1 bc parsing error: " + e.toString());
|
|
|
|
return -1;
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2018-01-27 07:52:39 +01:00
|
|
|
//Search through AST, scanning for any 'Identifier' nodes for functions, or While/For/If nodes
|
|
|
|
var queue = [], ramUsage = 1.4;
|
|
|
|
var whileUsed = false, forUsed = false, ifUsed = false;
|
|
|
|
queue.push(ast);
|
|
|
|
while (queue.length != 0) {
|
|
|
|
var exp = queue.shift();
|
|
|
|
switch (exp.type) {
|
|
|
|
case "BlockStatement":
|
|
|
|
case "Program":
|
|
|
|
for (var i = 0; i < exp.body.length; ++i) {
|
|
|
|
if (exp.body[i] instanceof Node) {
|
|
|
|
queue.push(exp.body[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "WhileStatement":
|
|
|
|
if (!whileUsed) {
|
|
|
|
ramUsage += CONSTANTS.ScriptWhileRamCost;
|
|
|
|
whileUsed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ForStatement":
|
|
|
|
if (!forUsed) {
|
|
|
|
ramUsage += CONSTANTS.ScriptForRamCost;
|
|
|
|
forUsed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "IfStatement":
|
|
|
|
if (!ifUsed) {
|
|
|
|
ramUsage += CONSTANTS.ScriptIfRamCost;
|
|
|
|
ifUsed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Identifier":
|
|
|
|
if (exp.name in workerScript.env.vars) {
|
|
|
|
var func = workerScript.env.get(exp.name);
|
|
|
|
if (typeof func === "function") {
|
|
|
|
try {
|
|
|
|
var res = func.apply(null, []);
|
|
|
|
if (!isNaN(res)) {ramUsage += res;}
|
|
|
|
} catch(e) {
|
|
|
|
console.log("ERROR applying function: " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var prop in exp) {
|
|
|
|
if (exp.hasOwnProperty(prop)) {
|
|
|
|
if (exp[prop] instanceof Node) {
|
|
|
|
queue.push(exp[prop]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ramUsage;
|
2016-12-14 21:29:40 +01: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
|
2017-08-30 19:44:29 +02:00
|
|
|
function loadAllRunningScripts() {
|
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];
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Reset each server's RAM usage to 0
|
|
|
|
server.ramUsed = 0;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-12-15 18:51:23 +01:00
|
|
|
for (var j = 0; j < server.runningScripts.length; ++j) {
|
|
|
|
count++;
|
2017-06-17 04:53:57 +02:00
|
|
|
addWorkerScript(server.runningScripts[j], server);
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
//Offline production
|
2017-06-17 04:53:57 +02:00
|
|
|
total += scriptCalculateOfflineProduction(server.runningScripts[j]);
|
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
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function scriptCalculateOfflineProduction(runningScriptObj) {
|
2016-12-19 21:59:13 +01:00
|
|
|
//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");
|
2017-07-25 16:39:56 +02:00
|
|
|
|
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
|
2017-06-17 04:53:57 +02:00
|
|
|
var confidence = (runningScriptObj.onlineRunningTime) / 14400;
|
2016-12-19 21:59:13 +01:00
|
|
|
if (confidence >= 1) {confidence = 1;}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-19 01:23:50 +02:00
|
|
|
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-13 17:58:31 +02:00
|
|
|
//Grow
|
2017-06-19 01:23:50 +02:00
|
|
|
for (var ip in runningScriptObj.dataMap) {
|
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) {continue;}
|
2017-06-13 17:58:31 +02:00
|
|
|
var serv = AllServers[ip];
|
|
|
|
if (serv == null) {continue;}
|
2017-06-19 01:23:50 +02:00
|
|
|
var timesGrown = Math.round(0.5 * runningScriptObj.dataMap[ip][2] / runningScriptObj.onlineRunningTime * timePassed);
|
2017-06-17 04:53:57 +02:00
|
|
|
console.log(runningScriptObj.filename + " called grow() on " + serv.hostname + " " + timesGrown + " times while offline");
|
|
|
|
runningScriptObj.log("Called grow() on " + serv.hostname + " " + timesGrown + " times while offline");
|
2017-06-13 17:58:31 +02:00
|
|
|
var growth = processSingleServerGrowth(serv, timesGrown * 450);
|
2017-06-17 04:53:57 +02:00
|
|
|
runningScriptObj.log(serv.hostname + " grown by " + formatNumber(growth * 100 - 100, 6) + "% from grow() calls made while offline");
|
2017-06-13 17:58:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-10 19:42:46 +02:00
|
|
|
var totalOfflineProduction = 0;
|
2017-06-19 01:23:50 +02:00
|
|
|
for (var ip in runningScriptObj.dataMap) {
|
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][0] == 0 || runningScriptObj.dataMap[ip][0] == 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-06-19 01:23:50 +02:00
|
|
|
var production = 0.5 * runningScriptObj.dataMap[ip][0] / runningScriptObj.onlineRunningTime * timePassed;
|
2017-05-10 19:42:46 +02:00
|
|
|
production *= confidence;
|
|
|
|
if (production > serv.moneyAvailable) {
|
|
|
|
production = serv.moneyAvailable;
|
|
|
|
}
|
|
|
|
totalOfflineProduction += production;
|
2017-07-25 16:39:56 +02:00
|
|
|
Player.gainMoney(production);
|
2017-06-17 04:53:57 +02:00
|
|
|
console.log(runningScriptObj.filename + " generated $" + production + " while offline by hacking " + serv.hostname);
|
|
|
|
runningScriptObj.log(runningScriptObj.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-20 18:11:33 +02:00
|
|
|
if (isNaN(serv.moneyAvailable)) {serv.moneyAvailable = 0;}
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
//Offline EXP gain
|
2017-07-25 16:39:56 +02:00
|
|
|
//A script's offline production will always be at most half of its online production.
|
2017-06-17 04:53:57 +02:00
|
|
|
var expGain = 0.5 * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed;
|
2016-12-19 21:59:13 +01:00
|
|
|
expGain *= confidence;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
Player.gainHackingExp(expGain);
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
//Update script stats
|
2017-06-17 04:53:57 +02:00
|
|
|
runningScriptObj.offlineMoneyMade += totalOfflineProduction;
|
|
|
|
runningScriptObj.offlineRunningTime += timePassed;
|
|
|
|
runningScriptObj.offlineExpGained += expGain;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
//Fortify a server's security based on how many times it was hacked
|
2017-06-19 01:23:50 +02:00
|
|
|
for (var ip in runningScriptObj.dataMap) {
|
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][1] == 0 || runningScriptObj.dataMap[ip][1] == null) {continue;}
|
2017-06-01 03:57:49 +02:00
|
|
|
var serv = AllServers[ip];
|
|
|
|
if (serv == null) {continue;}
|
2017-06-19 01:23:50 +02:00
|
|
|
var timesHacked = Math.round(0.5 * runningScriptObj.dataMap[ip][1] / runningScriptObj.onlineRunningTime * timePassed);
|
2017-06-17 04:53:57 +02:00
|
|
|
console.log(runningScriptObj.filename + " hacked " + serv.hostname + " " + timesHacked + " times while offline");
|
|
|
|
runningScriptObj.log("Hacked " + serv.hostname + " " + timesHacked + " times while offline");
|
2017-06-01 03:57:49 +02:00
|
|
|
serv.fortify(CONSTANTS.ServerFortifyAmount * timesHacked);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
//Weaken
|
2017-06-19 01:23:50 +02:00
|
|
|
for (var ip in runningScriptObj.dataMap) {
|
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) {continue;}
|
2017-06-01 03:57:49 +02:00
|
|
|
var serv = AllServers[ip];
|
|
|
|
if (serv == null) {continue;}
|
2017-06-19 01:23:50 +02:00
|
|
|
var timesWeakened = Math.round(0.5 * runningScriptObj.dataMap[ip][3] / runningScriptObj.onlineRunningTime * timePassed);
|
2017-06-17 04:53:57 +02:00
|
|
|
console.log(runningScriptObj.filename + " called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline");
|
|
|
|
runningScriptObj.log("Called weaken() on " + serv.hostname + " " + timesWeakened + " times while offline");
|
2017-06-01 03:57:49 +02:00
|
|
|
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-05-20 11:27:42 +02:00
|
|
|
return totalOfflineProduction;
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//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
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
this.logs = []; //Script logging. Array of strings, with each element being a log entry
|
2017-11-02 22:47:09 +01:00
|
|
|
this.logUpd = false;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//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;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
this.threads = 1;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-19 01:23:50 +02:00
|
|
|
//[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-09-19 20:38:03 +02:00
|
|
|
this.dataMap = new AllServersMap([0, 0, 0, 0], true);
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RunningScript.prototype.log = function(txt) {
|
2017-07-25 16:39:56 +02:00
|
|
|
if (this.logs.length > Settings.MaxLogCapacity) {
|
2017-06-17 04:53:57 +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
|
|
|
|
this.logs.shift();
|
|
|
|
}
|
|
|
|
this.logs.push(txt);
|
2017-11-02 22:47:09 +01:00
|
|
|
this.logUpd = true;
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RunningScript.prototype.displayLog = function() {
|
|
|
|
for (var i = 0; i < this.logs.length; ++i) {
|
|
|
|
post(this.logs[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-07-22 00:54:55 +02:00
|
|
|
|
|
|
|
RunningScript.prototype.clearLog = function() {
|
|
|
|
this.logs.length = 0;
|
|
|
|
}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//Update the moneyStolen and numTimesHack maps when hacking
|
|
|
|
RunningScript.prototype.recordHack = function(serverIp, moneyGained, n=1) {
|
2017-06-19 01:23:50 +02:00
|
|
|
if (this.dataMap == null) {
|
|
|
|
//[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-09-19 20:38:03 +02:00
|
|
|
this.dataMap = new AllServersMap([0, 0, 0, 0], true);
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
2017-06-19 01:23:50 +02:00
|
|
|
this.dataMap[serverIp][0] += moneyGained;
|
|
|
|
this.dataMap[serverIp][1] += n;
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Update the grow map when calling grow()
|
|
|
|
RunningScript.prototype.recordGrow = function(serverIp, n=1) {
|
2017-06-19 01:23:50 +02:00
|
|
|
if (this.dataMap == null) {
|
|
|
|
//[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-09-19 20:38:03 +02:00
|
|
|
this.dataMap = new AllServersMap([0, 0, 0, 0], true);
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
2017-06-19 01:23:50 +02:00
|
|
|
this.dataMap[serverIp][2] += n;
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Update the weaken map when calling weaken() {
|
|
|
|
RunningScript.prototype.recordWeaken = function(serverIp, n=1) {
|
2017-06-19 01:23:50 +02:00
|
|
|
if (this.dataMap == null) {
|
|
|
|
//[MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-09-19 20:38:03 +02:00
|
|
|
this.dataMap = new AllServersMap([0, 0, 0, 0], true);
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
2017-06-19 01:23:50 +02:00
|
|
|
this.dataMap[serverIp][3] += n;
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RunningScript.prototype.toJSON = function() {
|
|
|
|
return Generic_toJSON("RunningScript", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RunningScript.fromJSON = function(value) {
|
|
|
|
return Generic_fromJSON(RunningScript, value.data);
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
Reviver.constructors.RunningScript = RunningScript;
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//Creates an object that creates a map/dictionary with the IP of each existing server as
|
2017-06-18 23:01:23 +02:00
|
|
|
//a key. Initializes every key with a specified value that can either by a number or an array
|
2017-09-19 20:38:03 +02:00
|
|
|
function AllServersMap(arr=false, filterOwned=false) {
|
2017-05-10 19:42:46 +02:00
|
|
|
for (var ip in AllServers) {
|
|
|
|
if (AllServers.hasOwnProperty(ip)) {
|
2017-09-19 20:38:03 +02:00
|
|
|
if (filterOwned && (AllServers[ip].purchasedByPlayer || AllServers[ip].hostname === "home")) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-19 16:54:11 +02:00
|
|
|
if (arr) {
|
|
|
|
this[ip] = [0, 0, 0, 0];
|
|
|
|
} else {
|
|
|
|
this[ip] = 0;
|
|
|
|
}
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-12 20:12:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 03:57:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 06:17:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AllServersMap.prototype.toJSON = function() {
|
|
|
|
return Generic_toJSON("AllServersMap", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AllServersMap.fromJSON = function(value) {
|
|
|
|
return Generic_fromJSON(AllServersMap, value.data);
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:39:56 +02:00
|
|
|
Reviver.constructors.AllServersMap = AllServersMap;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
|
|
|
export {updateScriptEditorContent, loadAllRunningScripts, findRunningScript,
|
|
|
|
RunningScript, Script, AllServersMap};
|