mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-20 16:22:56 +02:00
Implement Script Editor (mostly)
This commit is contained in:
+81
-5
@@ -1,14 +1,77 @@
|
||||
/* Script.js
|
||||
* Script object
|
||||
*/
|
||||
|
||||
//Define commands in script editor (ctrl x to close, etc.)
|
||||
$(document).keydown(function(e) {
|
||||
if (Engine.currentPage == Engine.Page.ScriptEditor) {
|
||||
if (e.keyCode == 88 && e.ctrlKey) {
|
||||
var filename = document.getElementById("script-editor-filename").value;
|
||||
|
||||
if (checkValidFilename(filename) == false) {
|
||||
postScriptEditorStatus("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.currentServer.runningScripts.length; i++) {
|
||||
if (filename == Player.currentServer.runningScripts[i].filename) {
|
||||
postScriptEditorStatus("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.currentServer.scripts.length; i++) {
|
||||
if (filename == Player.currentServer.scripts[i].filename) {
|
||||
Player.currentServer.scripts[i].saveScript();
|
||||
Engine.loadTerminalContent();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//If the current script does NOT exist, create a new one
|
||||
var script = new Script();
|
||||
script.saveScript();
|
||||
Player.currentServer.scripts.push(script);
|
||||
Engine.loadTerminalContent();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
var ScriptEditorLastStatus = null;
|
||||
function postScriptEditorStatus(text) {
|
||||
document.getElementById("script-editor-status").innerHTML = text;
|
||||
|
||||
clearTimeout(ScriptEditorLastStatus);
|
||||
ScriptEditorLastStatus = setTimeout(function() {
|
||||
document.getElementById("script-editor-status").innerHTML = "";
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function Script() {
|
||||
//Function queue that holds the next functions to be
|
||||
//executed in this script. A function from this queue
|
||||
//is executed every second (this may change)
|
||||
this.functionQueue = [];
|
||||
|
||||
this.filename = "";
|
||||
this.code = "";
|
||||
this.ramUsage = 0;
|
||||
this.server = null; //Which server this script is on
|
||||
|
||||
/* Properties to calculate offline progress. Only applies for infinitely looping scripts */
|
||||
|
||||
@@ -22,18 +85,31 @@ function Script() {
|
||||
|
||||
//Which servers are hacked in one iteration of the script. May contain duplicates
|
||||
this.serversHacked = [];
|
||||
}
|
||||
};
|
||||
|
||||
//Execute the next function in the Script's function queue
|
||||
Script.prototype.executeNext() {
|
||||
Script.prototype.executeNext = function() {
|
||||
if (this.functionQueue.length <= 0) {return;}
|
||||
|
||||
//Shift the next element off ths function queue and then execute it
|
||||
(this.functionQueue.shift())();
|
||||
}
|
||||
|
||||
Script.prototype.setCode(code) {
|
||||
this.code = code;
|
||||
//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;
|
||||
this.code = code;
|
||||
|
||||
var filename = document.getElementById("script-editor-filename").value + ".script";
|
||||
this.filename = filename;
|
||||
|
||||
//Server
|
||||
this.server = Player.currentServer;
|
||||
|
||||
//TODO Calculate/update number of instructions, ram usage, execution time, etc.
|
||||
}
|
||||
}
|
||||
|
||||
/* Wrapper object that wraps a function with its arguments.
|
||||
@@ -51,7 +127,7 @@ Script.prototype.setCode(code) {
|
||||
* fooObj();
|
||||
*
|
||||
*/
|
||||
function functionObject = function(fn, context, params) {
|
||||
function functionObject(fn, context, params) {
|
||||
return function() {
|
||||
fn.apply(context, params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user