Fixed loadAllRunningScripts(), now seems to be working properly

This commit is contained in:
Daniel Xie 2016-12-15 11:51:23 -06:00
parent 7ec22480a2
commit 96fb37c6d1
9 changed files with 124 additions and 23 deletions

@ -6,14 +6,13 @@ TESTING TODO:
hack() seems to be working
Sleep() seems to be working
Creating the foreign server network doesn't seem to be (Fixed it I think? Confirm later)
Creating the foreign server network doesn't seem to be working
--Seems to be fixed
Script RAM Usage and corresponding terminal commands
If a server has no more money available it cannot be hacked anymore
Should work automatically...because your money gained percentage will be multiplied by 0
When the game is loaded re-load all of the scripts in runningScripts
- Does not seem to work. Although the addWorkerScript() function itself seems to work
- LOoks like its because AllServer is an object, so AllServers.length does not work. Have
to make a custom AllServers.size() function or something oogle it im too tired right now
- Seems to be working
Tasks TODO:
Script offline progress
If a script has bad syntax...it fucks everything up when you try to run it so fix that
@ -23,4 +22,7 @@ Tasks TODO:
Update skill level on cycle
Parse script firs tot see if there are any syntax errors, and tell user if there are (when user calls "run")
Tutorial and help
Server growth
Server growth
Companies
Factions
Update CONSTANTS.HelpText

@ -14,6 +14,7 @@
<!-- Utils -->
<script src="utils/IPAddress.js"></script>
<script src="utils/JSONReviver.js"></script>
<script src="utils/StringHelperFunctions.js"></script>
<!-- Netscript -->
<script src="src/netscript/NetScriptWorker.js"></script>

@ -5,6 +5,9 @@ CONSTANTS = {
//Time (ms) it takes to run one operation in Netscript.
CodeInstructionRunTime: 1500,
//Time (seconds) it takes to run one operation in Netscript OFFLINE
CodeOfflineExecutionTime: 10,
//Text that is displayed when the 'help' command is ran in Terminal
HelpText: "analyze Get statistics and information about current machine\n" +

@ -85,9 +85,6 @@ function killWorkerScript(scriptName, serverIp) {
function addWorkerScript(script, server) {
var filename = script.filename;
//Add script onto server's runningScripts
server.runningScripts.push(filename);
//Update server's ram usage
server.ramUsed += script.ramUsage;
@ -98,6 +95,7 @@ function addWorkerScript(script, server) {
s.serverIp = server.ip;
workerScripts.push(s);
console.log("Pushed script onto workerScripts");
return;
}
runScriptsLoop();

@ -55,12 +55,12 @@ function PlayerObject() {
this.total_money = 0;
this.lifetime_money = 0;
//Starting (home) computer
//IP Address of Starting (home) computer
this.homeComputer = null;
//Servers
this.currentServer = null; //Server currently being accessed through terminal
this.discoveredServers = []; //Secret servers not in the network that you have discovered
this.currentServer = null; //IP address of Server currently being accessed through terminal
this.discoveredServers = []; //IP addresses of secret servers not in the network that you have discovered
this.purchasedServers = [];
//Achievements and achievement progress

@ -100,10 +100,11 @@ Script.prototype.saveScript = function() {
//TODO Calculate/update number of instructions, ram usage, execution time, etc.
this.updateNumInstructions();
this.updateRamUsage();
this.updateExecutionTime();
}
}
//Calculates the number of instructions, which is just determined by number of semicolons)
//Calculates the number of instructions, which is just determined by number of semicolons
Script.prototype.updateNumInstructions = function() {
var numSemicolons = this.code.split(";").length - 1;
this.numInstructions = numSemicolons;
@ -111,11 +112,65 @@ Script.prototype.updateNumInstructions = function() {
//Updates how much RAM the script uses when it is running.
//Right now, it is determined solely by the number of instructions
//Ideally, I would want it to be based on instructions (e.g. hack() costs a lot but others dont)
//Ideally, I would want it to be based on type of instructions as well
// (e.g. hack() costs a lot but others dont)
Script.prototype.updateRamUsage = function() {
this.ramUsage = this.numInstructions * .2;
}
//Calculate the execution time of the script. This is used to calculate how much a script
//generates when the user is offline
//This is calculated based on the number of instructions and any calls to hack().
//Every instruction takes a flat time of X seconds (defined in Constants.js)
//Every hack instructions takes an ADDITIONAl time of however long it takes to hack that server
Script.prototype.updateExecutionTime = function() {
/*
var executionTime = 0;
//Ever instruction takes CONSTANTS.CodeOfflineExecutionTime seconds
console.log("numInstructions: " + this.numInstructions.toString());
executionTime += (this.numInstrutions * CONSTANTS.CodeOfflineExecutionTime);
console.log("ExecutionTime after taking into account instructions: " + executionTime.toString());
//Search the code's text for every occurrence of hack()
hackIndices = getIndicesOf('hack("', this.code, true);
for (var i = 0; i < hackIndices.length; ++i) {
//Get the full hack() call substring
var startIndex = hackIndices[i];
console.log("startIndex: " + startIndex.toString());
var endIndex = startIndex;
while (this.code.substr(endIndex, 2) != ");") {
console.log("endIndex: " + endIndex.toString());
++endIndex;
if (endIndex == this.code.length - 1) {
//Bad code...
console.log("Could not find ending to hack call");
return;
}
}
++endIndex; // This puts endIndex at the semicolon
var hackCall = this.code.substring(startIndex, endIndex);
console.log("hackCall: " + hackCall);
var argument = hackCall.match(/"([^']+)"/)[1]; //Extract the argument, which must be btw 2 quotes
//Check if its an ip or a hostname. Then get the server and calculate hack time accordingly
var server = null;
if (isValidIPAddress(argument)) {
server = AllServers[argument];
} else {
server = GetServerByHostname(argument);
}
console.log("Server hostname: " + server.hostname);
executionTime += scriptCalculateHackingTime(server);
}
this.executionTimeMillis = executionTime * 1000;
console.log("Script calculated to have an offline execution time of " + executionTime.toString() + "seconds");
*/
}
Script.prototype.toJSON = function() {
return Generic_toJSON("Script", this);
}
@ -129,15 +184,18 @@ 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
function loadAllRunningScripts() {
loadAllRunningScripts = function() {
var count = 0;
console.log("AllServers.length: " + AllServers.length);
for (var i = 0; i < AllServers.length; i++) {
var server = AllServers[i];
console.log("Loading scripts from server " + server.hostname);
for (var j = 0; j < server.runningScripts.length; j++) {
count++;
addWorkerScript(server.runningScripts[j], server);
for (var property in AllServers) {
if (AllServers.hasOwnProperty(property)) {
var server = AllServers[property];
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);
}
}
}
console.log("Loaded " + count.toString() + " running scripts");

@ -89,6 +89,17 @@ Server.prototype.getServerOnNetwork = function(i) {
return AllServers[this.serversOnNetwork[i]];
}
//Given the name of the script, returns the corresponding
//script object on the server (if it exists)
Server.prototype.getScript = function(scriptName) {
for (var i = 0; i < this.scripts.length; i++) {
if (this.scripts[i].filename == scriptName) {
return this.scripts[i];
}
}
return null;
}
//Functions for loading and saving a Server
Server.prototype.toJSON = function() {
return Generic_toJSON("Server", this);
@ -102,7 +113,7 @@ Reviver.constructors.Server = Server;
world_daemon: new Server(), //Final server for 2nd tier prestige. Discover that the world is a simulation
//world_daemon: new Server(), //Final server for 2nd tier prestige. Discover that the world is a simulation
/* Initialization */
@ -599,6 +610,14 @@ initForeignServers = function() {
//List of all servers that exist in the game, indexed by their ip
AllServers = {};
SizeOfAllServers = function() {
var size = 0, key;
for (key in AllServers) {
if (AllServers.hasOwnProperty(key)) size++;
}
return size;
}
//Add a server onto the map of all servers in the game
AddToAllServers = function(server) {
var serverIp = server.ip;
@ -612,6 +631,19 @@ AddToAllServers = function(server) {
AllServers[serverIp] = server;
}
//Returns server object with corresponding hostname
// Relatively slow, would rather not use this a lot
GetServerByHostname = function(hostname) {
for (var ip in AllServers) {
if (AllServers.hasOwnProperty(ip)) {
if (AllServers[ip].hostname == hostname) {
return AllServers[i];
}
}
}
return null;
}
//Debugging tool
PrintAllServers = function() {
for (var ip in AllServers) {

@ -257,7 +257,11 @@ var Terminal = {
}
break;
case "help":
//TODO
if (commandArray.length != 1) {
post("Incorrect usage of help command. Usage: help"); return;
}
post(CONSTANTS.HelpText);
break;
case "home":
//TODO return to home computer
@ -510,6 +514,7 @@ var Terminal = {
}else {
//Able to run script
var script = server.scripts[i];
server.runningScripts.push(script.filename); //Push onto runningScripts
addWorkerScript(script, server);
return;
}

@ -247,7 +247,9 @@ var Engine = {
console.log("Loaded game from save");
Companies.init();
CompanyPositions.init();
console.log("Calling loadAllRunningScripts()");
loadAllRunningScripts();
console.log("Finished calling loadAllRunningScripts()");
} else {
//No save found, start new game
console.log("Initializing new game");