Implemented basic functionality for the Active Tabs menu. Works for online production

This commit is contained in:
Daniel Xie 2016-12-19 12:20:19 -06:00
parent d88237fa91
commit 962b057ff8
13 changed files with 444 additions and 189 deletions

@ -17,20 +17,35 @@ TESTING TODO:
If a script has bad syntax...it fucks everything up when you try to run it so fix that
Try catch for script?
Check that killing scripts still works fine (TESTED - LOoks to work fine)
Check that if script has bad syntax it wont run at all and everthing works normally
Check if script throws during runtime it shuts down correctly
Check that if script has bad syntax it wont run at all and everthing works normally (Seems to work fine)
Check if script throws during runtime it shuts down correctly (seems to work fine)
Adjust leveling formula. Goes up way too high at first
http://gamedev.stackexchange.com/questions/55151/rpg-logarithmic-leveling-formula
- might be too slow now?
Scripts tab that shows script stats
Seems to work, at least the basics (for online production)
Tasks TODO:
Script offline progress
ctrl+C functionality for all running command like hack(), analyze(), and tail
Scroll all the way down when something is post()ed
Scripts tab that shows script stats
Script logging functionality? Logs to internal "log file" (property of script itself)
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
Companies
Hack time formula needs rebalancing I think
Factions
Augmentations
Update CONSTANTS.HelpText
Account for Max possible int when gaining exp
Account for Max possible int when gaining exp
Text in script editor that says ("ctrl + x" to save and quit)
Companies
Add possible CompanyPositions for every Company
Applying/working for companies
OPTIMIZATION
https://gamealchemist.wordpress.com/2013/05/01/lets-get-those-javascript-arrays-to-work-fast/

@ -24,9 +24,6 @@
padding-left: 10px;
height: 100%;
margin-left: 10%;
width: 99%;
color: #66ff33;
}
#script-editor-filename-row-div {
@ -65,4 +62,30 @@
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
}
/* Active scripts */
#active-scripts-container {
position: fixed;
padding-top: 10px;
padding-left: 10px;
height: 100%;
margin-left: 10%;
width: 99%;
}
.active-scripts-list>li h2{
color: #66ff33;
padding-top: 10px;
padding-left: 10px;
background-color: #333;
text-decoration: none;
}
.active-scripts-list>li p {
color: #66ff33;
padding: 10px;
padding-left: 40px;
background-color: #333;
text-decoration: none;
}

@ -51,7 +51,6 @@ p {
color: white;
}
h1 {
padding: 8px;
}

@ -15,6 +15,7 @@
<script src="utils/IPAddress.js"></script>
<script src="utils/JSONReviver.js"></script>
<script src="utils/StringHelperFunctions.js"></script>
<script src="utils/ObjectHelperFunctions.js"></script>
<!-- Netscript -->
<script src="src/netscript/NetScriptWorker.js"></script>
@ -48,12 +49,13 @@
<a href="#" id="character-menu-link"> Character </a>
</li>
<!-- These scripts stuff should be hidden until level 2, but leave them visible for now to test -->
<li class="create-script-tab">
<a href="#" id="create-script-menu-link"> Create Script </a>
</li>
<li class="scripts-tab" style="visibility:hidden">
<a href="#" id="scripts-menu-link"> Scripts </a>
<li class="active-scripts-tab">
<a href="#" id="active-scripts-menu-link"> Active Scripts </a>
</li>
<li class="world-tab" style="visibility:hidden">
@ -101,7 +103,12 @@
<textarea id="script-editor-text" style="border: none" autofocus>
</textarea>
</div>
<!-- Active scripts info page -->
<div id="active-scripts-container">
<ul class="active-scripts-list" id="active-scripts-list">
<ul>
</div>

@ -1,13 +1,15 @@
//Netburner Company class
// Note: Company Positions can be loaded every time with init() but Company class needs
// to be saved/loaded from localStorage
function Company() {
this.companyName = "";
this.companyPositions = [];
this.companyPositions = []; //Names (only name, not object) of all company positions
this.salaryMultiplier = 1; //Multiplier for base salary
this.expMultiplier = 1; //Multiplier for base exp gain
//Player-related properties for company
this.isPlayerEmployed = false;
this.playerPosition = null;
this.playerPosition = ""; //Name (only name, not object) of the current position player holds
this.playerReputation = 0; //"Reputation" within company, gain reputation by working for company
};
@ -28,6 +30,16 @@ Company.prototype.addPositions = function(positions) {
}
}
Company.prototype.toJSON = function() {
return Generic_toJSON("Company", this);
}
Company.fromJSON = function(value) {
return Generic_fromJSON(Company, value.data);
}
Reviver.constructors.Company = Company;
//Object that defines a position within a Company and its requirements
function CompanyPosition(name, reqHack, reqStr, reqDef, reqDex, reqAgi, reqCha, reqRep, salary) {
this.positionName = name;
@ -196,116 +208,176 @@ CompanyPositions = {
}
}
Companies = {
/* Initialize all companies. Only called when creating new game. Otherwise companies are
* usually loaded from localStorage */
initCompanies = function() {
/* Companies that also have servers */
//Megacorporations
ECorp: new Company(),
MegaCorp: new Company(),
BachmanAndAssociates: new Company(),
BladeIndustries: new Company(),
NWO: new Company(),
ClarkeIncorporated: new Company(),
OmniTekIncorporated: new Company(),
FourSigma: new Company(),
KuaiGongInternational: new Company(),
var ECorp = new Company();
ECorp.init("ECorp", 3.0, 3.0);
AddToCompanies(ECorp);
//Technology and communication companies ("Large" companies)
FulcrumTechnologies: new Company(),
StormTechnologies: new Company(),
DefComm: new Company(),
HeliosLabs: new Company(),
VitaLife: new Company(),
IcarusMicrosystems: new Company(),
UniversalEnergy: new Company(),
GalacticCybersystems: new Company(),
var MegaCorp = new Company();
MegaCorp.init("MegaCorp", 3.0, 3.0);
AddToCompanies(MegaCorp);
var BachmanAndAssociates = new Company();
BachmanAndAssociates.init("Bachman & Associates", 2.6, 2.6);
AddToCompanies(BachmanAndAssociates);
var BladeIndustries = new Company();
BladeIndustries.init("Blade Industries", 2.75, 2.75);
AddToCompanies(BladeIndustries);
var NWO = new Company();
NWO.init("NWO", 2.75, 2.75);
AddToCompanies(NWO);
var ClarkeIncorporated = new Company();
ClarkeIncorporated.init("Clarke Incorporated", 2.25, 2.25);
AddToCompanies(ClarkeIncorporated);
var OmniTekIncorporated = new Company();
OmniTekIncorporated.init("OmniTek Incorporated", 2.25, 2.25);
AddToCompanies(OmniTekIncorporated);
var FourSigma = new Company();
FourSigma.init("Four Sigma", 2.5, 2.5);
AddToCompanies(FourSigma);
var KuaiGongInternational = new Company();
KuaiGongInternational.init("KuaiGong International", 2.2, 2.2);
AddToCompanies(KuaiGongInternational);
//Technology and communication companies ("Large" servers)
var FulcrumTechnologies = new Company();
FulcrumTechnologies.init("Fulcrum Technologies", 2.0, 2.0);
AddToCompanies(FulcrumTechnologies);
var StormTechnologies = new Company();
StormTechnologies.init("Storm Technologies", 1.8, 1.8);
AddToCompanies(StormTechnologies);
var DefComm = new Company();
DefComm.init("DefComm", 1.75, 1.75);
AddToCompanies(DefComm);
var HeliosLabs = new Company();
HeliosLabs.init("Helios Labs", 1.8, 1.8);
AddToCompanies(HeliosLabs);
var VitaLife = new Company();
VitaLife.init("VitaLife", 1.8, 1.8);
AddToCompanies(VitaLife);
var IcarusMicrosystems = new Company();
IcarusMicrosystems.init("Icarus Microsystems", 1.9, 1.9);
AddToCompanies(IcarusMicrosystems);
var UniversalEnergy = new Company();
UniversalEnergy.init("Universal Energy", 2.0, 2.0);
AddToCompanies(UniversalEnergy);
var GalacticCybersystems = new Company();
GalacticCybersystems.init("Galactic Cybersystems", 1.9, 1.9);
AddToCompanies(GalacticCybersystems);
//Defense Companies ("Large" Companies)
AeroCorp: new Company(),
OmniaCybersystems: new Company(),
SolarisSpaceSystems: new Company(),
DeltaOne: new Company(),
var AeroCorp = new Company();
AeroCorp.init("AeroCorp", 1.7, 1.7);
AddToCompanies(AeroCorp);
//Health, medicine, pharmaceutical companies ("Large" companies)
GlobalPharmaceuticals: new Company(),
NovaMedical: new Company(),
var OmniaCybersystems = new Company();
OmniaCybersystems.init("Omnia Cybersystems", 1.7, 1.7);
AddToCompanies(OmniaCybersystems);
var SolarisSpaceSystems = new Company();
SolarisSpaceSystems.init("Solaris Space Systems", 1.7, 1.7);
AddToCompanies(SolarisSpaceSystems);
var DeltaOne = new Company();
DeltaOne.init("Delta One", 1.6, 1.6);
AddToCompanies(DeltaOne);
//Health, medicine, pharmaceutical companies ("Large" servers)
var GlobalPharmaceuticals = new Company();
GlobalPharmaceuticals.init("Global Pharmaceuticals", 1.8, 1.8);
AddToCompanies(GlobalPharmaceuticals);
var NovaMedical = new Company();
NovaMedical.init("Nova Medical", 1.75, 1.75);
AddToCompanies(NovaMedical);
//Other large companies
CIA: new Company(),
NSA: new Company(),
WatchdogSecurity: new Company(),
var CIA = new Company();
CIA.init("Central Intelligence Agency", 2.0, 2.0);
AddToCompanies(CIA);
var NSA = new Company();
NSA.init("National Security Agency", 2.0, 2.0);
AddToCompanies(NSA);
var WatchdogSecurity = new Company();
WatchdogSecurity.init("Watchdog Security", 1.5, 1.5);
AddToCompanies(WatchdogSecurity);
//"Medium level" companies
LexoCorp: new Company(),
RhoConstruction: new Company(),
AlphaEnterprises: new Company(),
AevumPolice: new Company(),
SysCoreSecurities: new Company(),
CompuTek: new Company(),
NetLinkTechnologies: new Company(),
CarmichaelSecurity: new Company(),
var LexoCorp = new Company();
LexoCorp.init("LexoCorp", 1.4, 1.4);
AddToCompanies(LexoCorp);
var RhoConstruction = new Company();
RhoConstruction.init("Rho Construction", 1.3, 1.3);
AddToCompanies(RhoConstruction);
var AlphaEnterprises = new Company();
AlphaEnterprises.init("Alpha Enterprises", 1.5, 1.5);
AddToCompanies(AlphaEnterprises);
var AevumPolice = new Company();
AevumPolice.init("Aevum Police", 1.3, 1.3);
AddToCompanies(AevumPolice);
var SysCoreSecurities = new Company();
SysCoreSecurities.init("SysCore Securities", 1.3, 1.3);
AddToCompanies(SysCoreSecurities);
var CompuTek = new Company();
CompuTek.init("CompuTek", 1.2, 1.2);
AddToCompanies(CompuTek);
var NetLinkTechnologies = new Company();
NetLinkTechnologies.init("NetLink Technologies", 1.2, 1.2);
AddToCompanies(NetLinkTechnologies);
var CarmichaelSecurity = new Company();
CarmichaelSecurity.init("Carmichael Security", 1.2, 1.2);
AddToCompanies(CarmichaelSecurity);
//"Low level" companies
FoodNStuff: new Company(),
JoesGuns: new Company(),
OmegaSoftware: new Company(),
var FoodNStuff = new Company();
FoodNStuff.init("FoodNStuff", 1, 1);
AddToCompanies(FoodNStuff);
var JoesGuns = new Company();
JoesGuns.init("Joe's Guns", 1, 1);
AddToCompanies(JoesGuns);
var OmegaSoftware = new Company();
OmegaSoftware.init("Omega Software", 1.1, 1.1);
AddToCompanies(OmegaSoftware);
/* Companies that do not have servers */
NoodleBar: new Company(),
var NoodleBar = new Company();
NoodleBar.init("Noodle Bar", 1, 1);
AddToCompanies(NoodleBar);
}
init: function() {
/* Companies that also have servers */
//Megacorporations
Companies.ECorp.init("ECorp", 3.0, 3.0);
Companies.MegaCorp.init("MegaCorp", 3.0, 3.0);
Companies.BachmanAndAssociates.init("Bachman & Associates", 2.6, 2.6);
Companies.BladeIndustries.init("Blade Industries", 2.75, 2.75);
Companies.NWO.init("NWO", 2.75, 2.75);
Companies.ClarkeIncorporated.init("Clarke Incorporated", 2.25, 2.25);
Companies.OmniTekIncorporated.init("OmniTek Incorporated", 2.25, 2.25);
Companies.FourSigma.init("Four Sigma", 2.5, 2.5);
Companies.KuaiGongInternational.init("KuaiGong International", 2.2, 2.2);
//Technology and communication companies ("Large" servers)
Companies.FulcrumTechnologies.init("Fulcrum Technologies", 2.0, 2.0);
Companies.StormTechnologies.init("Storm Technologies", 1.8, 1.8);
Companies.DefComm.init("DefComm", 1.75, 1.75);
Companies.HeliosLabs.init("Helios Labs", 1.8, 1.8);
Companies.VitaLife.init("VitaLife", 1.8, 1.8);
Companies.IcarusMicrosystems.init("Icarus Microsystems", 1.9, 1.9);
Companies.UniversalEnergy.init("Universal Energy", 2.0, 2.0);
Companies.GalacticCybersystems.init("Galactic Cybersystems", 1.9, 1.9);
//Defense Companies ("Large" Companies)
Companies.AeroCorp.init("AeroCorp", 1.7, 1.7);
Companies.OmniaCybersystems.init("Omnia Cybersystems", 1.7, 1.7);
Companies.SolarisSpaceSystems.init("Solaris Space Systems", 1.7, 1.7);
Companies.DeltaOne.init("Delta One", 1.6, 1.6);
//Health, medicine, pharmaceutical companies ("Large" servers)
Companies.GlobalPharmaceuticals.init("Global Pharmaceuticals", 1.8, 1.8);
Companies.NovaMedical.init("Nova Medical", 1.75, 1.75);
//Map of all companies that exist in the game, indexed by their name
Companies = {}
//Other large companies
Companies.CIA.init("Central Intelligence Agency", 2.0, 2.0);
Companies.NSA.init("National Security Agency", 2.0, 2.0);
Companies.WatchdogSecurity.init("Watchdog Security", 1.5, 1.5);
//"Medium level" companies
Companies.LexoCorp.init("LexoCorp", 1.4, 1.4);
Companies.RhoConstruction.init("Rho Construction", 1.3, 1.3);
Companies.AlphaEnterprises.init("Alpha Enterprises", 1.5, 1.5);
Companies.AevumPolice.init("Aevum Police", 1.3, 1.3);
Companies.SysCoreSecurities.init("SysCore Securities", 1.3, 1.3);
Companies.CompuTek.init("CompuTek", 1.2, 1.2);
Companies.NetLinkTechnologies.init("NetLink Technologies", 1.2, 1.2);
Companies.CarmichaelSecurity.init("Carmichael Security", 1.2, 1.2);
//"Low level" companies
Companies.FoodNStuff.init("FoodNStuff", 1, 1);
Companies.JoesGuns.init("Joe's Guns", 1, 1);
Companies.OmegaSoftware.init("Omega Software", 1.1, 1.1);
/* Companies that do not have servers */
Companies.NoodleBar.init("Noodle Bar", 1, 1);
}
//Add a Company object onto the map of all Companies in the game
AddToCompanies = function (company) {
var name = company.companyName;
Companies[name] = company;
}

@ -18,7 +18,11 @@ function evaluate(exp, workerScript) {
case "var":
return new Promise(function(resolve, reject) {
if (env.stopFlag) {reject(workerScript);}
resolve(env.get(exp.value));
try {
resolve(env.get(exp.value));
} catch (e) {
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
}
});
break;
//Can currently only assign to "var"s
@ -43,7 +47,11 @@ function evaluate(exp, workerScript) {
p.then(function(expRight) {
console.log("Right side of assign operation resolved with value: " + expRight);
env.set(exp.left.value, expRight);
try {
env.set(exp.left.value, expRight);
} catch (e) {
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
}
console.log("Assign operation finished");
resolve("assignFinished");
}, function(e) {
@ -192,12 +200,10 @@ function evaluate(exp, workerScript) {
var ipPromise = evaluate(exp.args[0], workerScript);
ipPromise.then(function(ip) {
console.log("Evaluated the ip, calculating hackingTime");
//Calculate the hacking time
var server = AllServers[ip];
var hackingTime = scriptCalculateHackingTime(server); //This is in seconds
console.log("Calculated hackingTime");
if (server.hasAdminRights == false) {
console.log("Cannot hack server " + server.hostname);
resolve("Cannot hack");
@ -221,6 +227,7 @@ function evaluate(exp, workerScript) {
server.moneyAvailable -= moneyGained;
Player.money += moneyGained;
workerScript.scriptRef.onlineMoneyMade += moneyGained;
Player.hacking_exp += expGainedOnSuccess;
console.log("Script successfully hacked " + server.hostname + " for $" + moneyGained + " and " + expGainedOnSuccess + " exp");

@ -6,13 +6,20 @@
//TODO Tested For and while and generic call statements. Have not tested if statements
/* Actual Worker Code */
function WorkerScript() {
function WorkerScript(script) {
this.name = "";
this.running = false;
this.serverIp = null;
this.code = "";
this.env = new Environment();
this.output = "";
this.ramUsage = 0;
this.scriptRef = script;
}
//Returns the server on which the workerScript is running
WorkerScript.prototype.getServer = function() {
return AllServers[this.serverIp];
}
//Array containing all scripts that are running across all servers, to easily run them all
@ -28,6 +35,7 @@ function runScriptsLoop() {
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
} catch (e) {
post("Syntax error in " + workerScript[i].name + ": " + e);
continue;
}
console.log("Starting new script: " + workerScripts[i].name);
@ -44,9 +52,30 @@ function runScriptsLoop() {
w.env.stopFlag = true;
}, function(w) {
if (w instanceof Error) {
console.log("Script threw an Error during runtime.");
//TODO Get the script based on the error. Format: |serverip|scriptname|error message|
//TODO Post the script error and stop the script
//Error text format: |serverip|scriptname|error message
var errorText = w.toString();
var errorTextArray = errorText.split("|");
if (errorTextArray.length != 4) {
console.log("ERROR: Something wrong with Error text in evaluator...");
console.log("Error text: " + errorText);
}
var serverIp = errorTextArray[1];
var scriptName = errorTextArray[2];
var errorMsg = errorTextArray[3];
//Post error message to terminal
//TODO Only post this if you're on the machine the script is running on?
post("Script runtime error: " + errorMsg);
//Find the corresponding workerscript and set its flags to kill it
for (var i = 0; i < workerScripts.length; ++i) {
if (workerScripts[i].serverIp == serverIp && workerScripts[i].name == scriptName) {
workerScripts[i].running = false;
workerScripts[i].env.stopFlag = true;
return;
}
}
} else {
console.log("Stopping script" + w.name + " because it was manually stopped (rejected)")
w.running = false;
@ -61,7 +90,7 @@ function runScriptsLoop() {
for (var i = workerScripts.length - 1; i >= 0; i--) {
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
console.log("Deleting script: " + workerScripts[i].name);
//Delete script from the runningScripts array on its host serverIp
//Delete script from the runningScripts array on its host serverIp
var ip = workerScripts[i].serverIp;
var name = workerScripts[i].name;
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
@ -70,6 +99,9 @@ function runScriptsLoop() {
break;
}
}
//Free RAM
AllServers[ip].ramUsed -= workerScripts[i].ramUsage;
//Delete script from workerScripts
workerScripts.splice(i, 1);
@ -98,14 +130,28 @@ function addWorkerScript(script, server) {
//Update server's ram usage
server.ramUsed += script.ramUsage;
//Create and add workerScripts
var s = new WorkerScript();
//Create the WorkerScript
var s = new WorkerScript(script);
s.name = filename;
s.code = script.code;
s.serverIp = server.ip;
s.ramUsage = script.ramUsage;
//Add the WorkerScript to the Active Scripts list
Engine.addActiveScriptsItem(s);
//Add the WorkerScript
workerScripts.push(s);
console.log("Pushed script onto workerScripts");
return;
}
//Updates the online running time stat of all running scripts
function updateOnlineScriptTimes(numCycles = 1) {
var time = (numCycles * Engine._idleSpeed) / 1000; //seconds
for (var i = 0; i < workerScripts.length; ++i) {
workerScripts[i].scriptRef.onlineRunningTime += time;
}
}
runScriptsLoop();

@ -91,10 +91,10 @@ PlayerObject.prototype.getHomeComputer = function() {
}
//Calculates skill level based on experience. The same formula will be used for every skill
// At the maximum possible exp (MAX_INT = 9007199254740991), the hacking skill will be 1796
// Gets to level 1000 hacking skill at ~1,100,000,000 exp
// At the maximum possible exp (MAX_INT = 9007199254740991), the hacking skill will be 1796 TODO REcalculate this
// Gets to level 1000 hacking skill at (TODO Determine this)
PlayerObject.prototype.calculateSkill = function(exp) {
return Math.max(Math.floor(50 * Math.log(9007199254740991+ 2.270) - 40), 1);
return Math.max(Math.floor(7.2 * Math.log(exp + 518.013) - 44), 1);
}
PlayerObject.prototype.updateSkillLevels = function() {

@ -67,7 +67,7 @@ function Script() {
this.filename = "";
this.code = "";
this.ramUsage = 0;
this.server = null; //IP of server this script is on
this.server = ""; //IP of server this script is on
/* Properties to calculate offline progress. Only applies for infinitely looping scripts */
@ -82,6 +82,13 @@ function Script() {
//Which servers are hacked in one iteration of the script. May contain duplicates
this.serversHacked = [];
//Stats to display on the Scripts menu, and used to determine offline progress
this.offlineRunningTime = 0; //Seconds
this.offlineMoneyMade = 0;
this.onlineRunningTime = 0; //Seconds
this.onlineMoneyMade = 0;
this.lastUpdate = 0;
};
//Get the script data from the Script Editor and save it to the object
@ -97,10 +104,11 @@ Script.prototype.saveScript = function() {
//Server
this.server = Player.currentServer;
//TODO Calculate/update number of instructions, ram usage, execution time, etc.
//Calculate/update number of instructions, ram usage, execution time, etc.
this.updateNumInstructions();
this.updateRamUsage();
this.updateExecutionTime();
}
}
@ -191,6 +199,10 @@ loadAllRunningScripts = function() {
for (var property in AllServers) {
if (AllServers.hasOwnProperty(property)) {
var server = AllServers[property];
//Reset each server's RAM usage to 0
server.ramUsed = 0;
for (var j = 0; j < server.runningScripts.length; ++j) {
count++;
//runningScripts array contains only names, so find the actual script object

@ -116,7 +116,7 @@ Reviver.constructors.Server = Server;
//world_daemon: new Server(), //Final server for 2nd tier prestige. Discover that the world is a simulation
/* Initialization */
/* Initialization. Called only when loading a new game( no save file) */
initForeignServers = function() {
//MegaCorporations
var ECorpServer = new Server();

@ -1,25 +1,3 @@
function TestObj() {
this.num = 1;
}
TestObj.prototype.setValue = function(val) {
this.num = val;
}
TestObj.prototype.toJSON = function() {
console.log("toJSON() called");
return Generic_toJSON("TestObj", this);
}
TestObj.fromJSON = function(value) {
console.log("fromJSON() called");
return Generic_fromJSON(TestObj, value.data);
}
Reviver.constructors.TestObj = TestObj;
var testObj = new TestObj();
//Terminal
var post = function(input) {
$("#terminal-input").before('<tr class="posted"><td style="color: #66ff33;">' + input.replace( / /g, "&nbsp;" ) + '</td></tr>');
@ -420,31 +398,8 @@ var Terminal = {
//TODO List each's script RAM usage
break;
case "test":
post(testObj.num.toString());
testObj.setValue(testObj.num + 1);
break;
case "testSave":
var testSave = JSON.stringify(testObj);
window.localStorage.setItem("netburnerTest", testSave);
console.log("Netburner TestSave saved");
break;
case "testLoad":
if (!window.localStorage.getItem("netburnerTest")) {
console.log("No TestSave file to load");
} else {
console.log("Here");
var testSave = window.localStorage.getItem("netburnerTest");
testObj = JSON.parse(testSave, Reviver);
console.log("TestSave loaded");
}
break;
case "testDelete":
if (!window.localStorage.getItem("netburnetTest")) {
console.log("No TestSave file to delete");
} else {
window.localStorage.removeItem("netburnerTest");
console.log("TestSave deleted");
}
post("test \n this post");
break;
default:
post("Command not found");
@ -513,6 +468,7 @@ var Terminal = {
return;
}else {
//Able to run script
post("Running script. May take a few seconds to start up the process...");
var script = server.scripts[i];
server.runningScripts.push(script.filename); //Push onto runningScripts
addWorkerScript(script, server);

@ -10,11 +10,12 @@ var Engine = {
hackButton: null,
//Main menu buttons
terminalMainMenuButton: null,
characterMainMenuButton: null,
scriptEditorMainMenuButton: null,
saveMainMenuButton: null,
deleteMainMenuButton: null,
terminalMainMenuButton: null,
characterMainMenuButton: null,
scriptEditorMainMenuButton: null,
activeScriptsMainMenuButton: null,
saveMainMenuButton: null,
deleteMainMenuButton: null,
},
//Display objects
@ -31,6 +32,7 @@ var Engine = {
terminalContent: null,
characterContent: null,
scriptEditorContent: null,
activeScriptsContent: null,
//Character info
characterInfo: null,
@ -44,6 +46,7 @@ var Engine = {
Terminal: "Terminal",
CharacterInfo: "CharacterInfo",
ScriptEditor: "ScriptEditor",
ActiveScripts: "ActiveScripts",
},
currentPage: null,
@ -56,10 +59,12 @@ var Engine = {
saveGame: function() {
var PlayerSave = JSON.stringify(Player);
var AllServersSave = JSON.stringify(AllServers);
var CompaniesSave = JSON.stringify(Companies);
//TODO Add factions + companies here when they're done
window.localStorage.setItem("netburnerPlayerSave", PlayerSave);
window.localStorage.setItem("netburnerAllServersSave", AllServersSave);
window.localStorage.setItem("netburnerCompaniesSave", CompaniesSave);
console.log("Game saved to local storage");
},
@ -72,11 +77,16 @@ var Engine = {
} else if (!window.localStorage.getItem("netburnerAllServersSave")) {
console.log("No AllServers save to load");
return false;
} else if (!window.localStorage.getItem("netburnerCompaniesSave")) {
console.log("No Companies save to load");
return false;
} else {
var PlayerSave = window.localStorage.getItem("netburnerPlayerSave");
var AllServersSave = window.localStorage.getItem("netburnerAllServersSave");
var CompaniesSave = window.localStorage.getItem("netburnerCompaniesSave");
Player = JSON.parse(PlayerSave, Reviver);
AllServers = JSON.parse(AllServersSave, Reviver);
Companies = JSON.parse(CompaniesSave, Reviver);
return true;
}
},
@ -88,9 +98,14 @@ var Engine = {
return false;
} else if (!window.localStorage.getItem("netburnerAllServersSave")) {
console.log("No AllServers Save to delete");
return false;
} else if (!window.localStorage.getItem("netburnerCompaniesSave")) {
console.log("No Companies Save to delete");
return false;
} else {
window.localStorage.removeItem("netburnerPlayerSave");
window.localStorage.removeItem("netburnerAllServersSave");
window.localStorage.removeItem("netburnerCompaniesSave");
console.log("Deleted saves")
return true;
}
@ -121,12 +136,20 @@ var Engine = {
document.getElementById("script-editor-text").value = code;
Engine.currentPage = Engine.Page.ScriptEditor;
},
loadActiveScriptsContent: function() {
Engine.hideAllContent();
Engine.Display.activeScriptsContent.style.visibility = "visible";
Engine.currentPage = Engine.Page.ActiveScripts;
},
//Helper function that hides all content
hideAllContent: function() {
Engine.Display.terminalContent.style.visibility = "hidden";
Engine.Display.characterContent.style.visibility = "hidden";
Engine.Display.scriptEditorContent.style.visibility = "hidden";
Engine.Display.activeScriptsContent.style.visibility = "hidden";
},
/* Display character info */
@ -142,6 +165,73 @@ var Engine = {
'Hacking Experience: ' + Player.hacking_exp + '<br><br>';
},
/* Functions used to update information on the Active Scripts page */
ActiveScriptsList: null,
//Creates and adds the <li> object for a given workerScript
addActiveScriptsItem: function(workerscript) {
console.log("addActiveScriptsItem called");
var item = document.createElement("li");
Engine.createActiveScriptsText(workerscript, item);
//Add the li element onto the list
if (Engine.ActiveScriptsList == null) {
Engine.ActiveScriptsList = document.getElementById("active-scripts-list");
}
Engine.ActiveScriptsList.appendChild(item);
},
//Update the ActiveScriptsItems array
updateActiveScriptsItems: function() {
for (var i = 0; i < workerScripts.length; ++i) {
Engine.updateActiveScriptsItemContent(i, workerScripts[i]);
}
},
//Updates the content of the given item in the Active Scripts list
updateActiveScriptsItemContent: function(i, workerscript) {
var list = Engine.ActiveScriptsList.getElementsByTagName("li");
if (i >= list.length) {
throw new Error("Trying to update an out-of-range Active Scripts Item");
}
var item = list[i];
//Clear the item
while (item.firstChild) {
item.removeChild(item.firstChild);
}
//Add the updated text back
Engine.createActiveScriptsText(workerscript, item);
},
createActiveScriptsText(workerscript, item) {
//Script name
var scriptName = document.createElement("h2");
scriptName.appendChild(document.createTextNode(workerscript.name));
item.appendChild(scriptName);
var itemText = document.createElement("p");
//Server ip/hostname
var hostname = workerscript.getServer().hostname;
var serverIpHostname = "Server: " + hostname + "(" + workerscript.serverIp + ")";
//Online money/s
var onlineMps = workerscript.scriptRef.onlineMoneyMade / workerscript.scriptRef.onlineRunningTime;
var onlineMpsText = "Online production: $" + onlineMps.toFixed(2) + "/second";
//Offline money/s
var offlineMps = workerscript.scriptRef.offlineMoneyMade / workerscript.scriptRef.offlineRunningTime;
var offlineMpsText = "Offline production: $" + offlineMps.toFixed(2) + "/second";
itemText.innerHTML = serverIpHostname + "<br>" + onlineMpsText + "<br>" + offlineMpsText + "<br>";
item.appendChild(itemText);
},
/* Main Event Loop */
idleTimer: function() {
//Get time difference
@ -161,7 +251,6 @@ var Engine = {
window.requestAnimationFrame(Engine.idleTimer);
},
//TODO Account for numCycles in Code, hasn't been done yet
updateGame: function(numCycles = 1) {
//Manual hack
if (Player.startAction == true) {
@ -173,10 +262,18 @@ var Engine = {
Engine._actionTimeStr = "Time left: ";
Player.startAction = false;
}
//Counters
Engine.decrementAllCounters(numCycles);
Engine.checkCounters();
//Manual hacks
Engine.updateHackProgress(numCycles);
//Update the running time of all active scripts
updateOnlineScriptTimes(numCycles);
},
//Counters for the main event loop. Represent the number of game cycles are required
@ -184,6 +281,7 @@ var Engine = {
Counters: {
autoSaveCounter: 300, //Autosave every minute
updateSkillLevelsCounter: 10, //Only update skill levels every 2 seconds. Might improve performance
updateDisplays: 10, //Update displays such as Active Scripts display and character display
},
decrementAllCounters: function(numCycles = 1) {
@ -206,6 +304,16 @@ var Engine = {
Player.updateSkillLevels();
Engine.Counters.updateSkillLevelsCounter = 10;
}
if (Engine.Counters.updateDisplays <= 0) {
if (Engine.currentPage == Engine.Page.ActiveScripts) {
Engine.updateActiveScriptsItems();
} else if (Engine.currentPage == Engine.Page.CharacterInfo) {
Engine.displayCharacterInfo();
}
Engine.Counters.updateDisplays = 10;
}
},
/* Calculates the hack progress for a manual (non-scripted) hack and updates the progress bar/time accordingly */
@ -251,18 +359,15 @@ var Engine = {
//Initialization functions
if (Engine.loadSave()) {
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");
Player.init();
initForeignServers();
Companies.init();
CompanyPositions.init();
initCompanies();
}
//Main menu buttons and content
@ -283,7 +388,18 @@ var Engine = {
Engine.loadScriptEditorContent();
return false;
});
Engine.Clickables.activeScriptsMainMenuButton = document.getElementById("active-scripts-menu-link");
Engine.Clickables.activeScriptsMainMenuButton.addEventListener("click", function() {
Engine.loadActiveScriptsContent();
return false;
});
//Active scripts list
Engine.ActiveScriptsList = document.getElementById("active-scripts-list");
Engine.Clickables.saveMainMenuButton = document.getElementById("save-game-link");
Engine.Clickables.saveMainMenuButton.addEventListener("click", function() {
Engine.saveGame();
@ -302,6 +418,8 @@ var Engine = {
Engine.Display.characterContent.style.visibility = "hidden";
Engine.Display.scriptEditorContent = document.getElementById("script-editor-container");
Engine.Display.scriptEditorContent.style.visibility = "hidden";
Engine.Display.activeScriptsContent = document.getElementById("active-scripts-container");
Engine.Display.activeScriptsContent.style.visibility = "hidden";
//Character info
Engine.Display.characterInfo = document.getElementById("character-info");

@ -1,4 +1,4 @@
//Netscript String helper functions
//Netburner String helper functions
//Searches for every occurence of searchStr within str and returns an array of the indices of
//all these occurences