2016-11-21 07:11:14 +01:00
|
|
|
/* Script.js
|
|
|
|
* Script object
|
|
|
|
*/
|
2016-11-24 23:30:33 +01:00
|
|
|
|
2017-05-03 06:38:58 +02:00
|
|
|
//Initialize the 'save and close' button on script editor page
|
|
|
|
function scriptEditorSaveCloseInit() {
|
|
|
|
var closeButton = document.getElementById("script-editor-save-and-close-button");
|
|
|
|
|
|
|
|
closeButton.addEventListener("click", function() {
|
|
|
|
saveAndCloseScriptEditor();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", scriptEditorSaveCloseInit, false);
|
|
|
|
|
|
|
|
//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) {
|
2016-12-14 21:29:40 +01:00
|
|
|
//Ctrl + x
|
2017-05-03 06:38:58 +02:00
|
|
|
if (e.keyCode == 66 && e.ctrlKey) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-11-21 12:22:18 +01:00
|
|
|
/* Properties to calculate offline progress. Only applies for infinitely looping scripts */
|
|
|
|
|
|
|
|
//Number of instructions ("lines") in the code. Any call ending in a ;
|
2016-12-19 21:59:13 +01:00
|
|
|
//is considered one instruction. Used to calculate ramUsage
|
2016-11-21 12:22:18 +01:00
|
|
|
this.numInstructions = 0;
|
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;
|
|
|
|
|
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;
|
|
|
|
this.code = code;
|
|
|
|
|
|
|
|
var filename = document.getElementById("script-editor-filename").value + ".script";
|
|
|
|
this.filename = filename;
|
|
|
|
|
|
|
|
//Server
|
|
|
|
this.server = Player.currentServer;
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Calculate/update number of instructions, ram usage, execution time, etc.
|
2016-12-14 21:29:40 +01:00
|
|
|
this.updateNumInstructions();
|
|
|
|
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-05-05 17:50:55 +02:00
|
|
|
this.offlineRunningTime = 0.01; //Seconds
|
2016-12-19 21:59:13 +01:00
|
|
|
this.offlineMoneyMade = 0;
|
2017-05-05 17:50:55 +02:00
|
|
|
this.onlineRunningTime = 0.01; //Seconds
|
2016-12-19 21:59:13 +01:00
|
|
|
this.onlineMoneyMade = 0;
|
|
|
|
this.lastUpdate = 0;
|
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-15 18:51:23 +01:00
|
|
|
//Calculates the number of instructions, which is just determined by number of semicolons
|
2016-12-14 21:29:40 +01:00
|
|
|
Script.prototype.updateNumInstructions = function() {
|
|
|
|
var numSemicolons = this.code.split(";").length - 1;
|
|
|
|
this.numInstructions = numSemicolons;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Updates how much RAM the script uses when it is running.
|
|
|
|
//Right now, it is determined solely by the number of instructions
|
2016-12-15 18:51:23 +01:00
|
|
|
//Ideally, I would want it to be based on type of instructions as well
|
|
|
|
// (e.g. hack() costs a lot but others dont)
|
2016-12-14 21:29:40 +01:00
|
|
|
Script.prototype.updateRamUsage = function() {
|
2017-04-23 04:32:51 +02:00
|
|
|
this.ramUsage = this.numInstructions * 0.5;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
scriptCalculateOfflineProduction(script);
|
2016-12-15 18:51:23 +01:00
|
|
|
}
|
2016-12-14 22:22:12 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
console.log("Offline for " + timePassed.toString() + " 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
|
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;}
|
|
|
|
console.log("onlineRunningTime: " + script.onlineRunningTime.toString());
|
|
|
|
console.log("Confidence: " + confidence.toString());
|
|
|
|
|
|
|
|
//A script's offline production will always be at most half of its online production.
|
|
|
|
var production = (1/2) * (script.onlineMoneyMade / script.onlineRunningTime) * timePassed;
|
|
|
|
production *= confidence;
|
|
|
|
|
|
|
|
var expGain = (1/2) * (script.onlineExpGained / script.onlineRunningTime) * timePassed;
|
|
|
|
expGain *= confidence;
|
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
//Account for production in Player and server)
|
|
|
|
var server = AllServers[script.server];
|
|
|
|
if (production > server.moneyAvailable) {
|
|
|
|
production = server.moneyAvailable;
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
Player.gainMoney(production);
|
2017-05-05 03:08:44 +02:00
|
|
|
Player.gainHackingExp(expGain);
|
2016-12-19 21:59:13 +01:00
|
|
|
|
|
|
|
server.moneyAvailable -= production;
|
|
|
|
if (server.moneyAvailable < 0) {server.moneyAvailable = 0;}
|
|
|
|
|
|
|
|
//Update script stats
|
|
|
|
script.offlineMoneyMade += production;
|
|
|
|
script.offlineRunningTime += timePassed;
|
|
|
|
script.offlineExpGained += expGain;
|
2016-12-20 21:18:34 +01:00
|
|
|
|
2016-12-19 21:59:13 +01:00
|
|
|
//DEBUG
|
|
|
|
var serverName = AllServers[script.server].hostname;
|
|
|
|
console.log(script.filename + " from server " + serverName + " generated $" + production.toString() + " while offline");
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|