Fixed script hack(), should be working now. Changed some numbers to rebalance. Implemented script RAM usage. If a script uses too much ram it will not run (untested)

This commit is contained in:
Daniel Xie 2016-12-14 14:29:40 -06:00
parent c485f28e20
commit 23c036827f
6 changed files with 89 additions and 31 deletions

@ -3,14 +3,15 @@ Netburner Idle Game
TESTING TODO: TESTING TODO:
hack() and sleep() in a script hack() and sleep() in a script
Hack() not finished, need a safeguard to allow script to only hack servers that the player hack() seems to be working
has admin access to
Sleep() seems to be working Sleep() seems to be working
Creating the foreign server network doesn't seem to be working Creating the foreign server network doesn't seem to be (Fixed it I think? Confirm later)
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
Tasks TODO: Tasks TODO:
If a server has no more money available it cannot be hacked anymore
Script RAM Usage and corresponding terminal commands
Script offline progress Script offline progress
When the game is loaded re-load all of the scripts in runningScripts When the game is loaded re-load all of the scripts in runningScripts
If a script has bad syntax...it fucks everything up when you try to run it so fix that If a script has bad syntax...it fucks everything up when you try to run it so fix that
@ -19,3 +20,5 @@ Tasks TODO:
Script logging functionality? Logs to internal "log file" (property of script itself) Script logging functionality? Logs to internal "log file" (property of script itself)
Update skill level on cycle 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") 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

@ -188,10 +188,12 @@ function evaluate(exp, workerScript) {
var ipPromise = evaluate(exp.args[0], workerScript); var ipPromise = evaluate(exp.args[0], workerScript);
ipPromise.then(function(ip) { ipPromise.then(function(ip) {
console.log("Evaluated the ip, calculating hackingTime");
//Calculate the hacking time //Calculate the hacking time
var server = AllServers[ip]; var server = AllServers[ip];
var hackingTime = scriptCalculateHackingTime(server); //This is in seconds var hackingTime = scriptCalculateHackingTime(server); //This is in seconds
console.log("Calculated hackingTime");
//TODO Add a safeguard to prevent a script from hacking a Server that the player //TODO Add a safeguard to prevent a script from hacking a Server that the player
//cannot hack //cannot hack
if (server.hasAdminRights == false) { if (server.hasAdminRights == false) {
@ -201,6 +203,7 @@ function evaluate(exp, workerScript) {
} }
var p = new Promise(function(resolve, reject) { var p = new Promise(function(resolve, reject) {
if (env.stopFlag) {console.log("rejected"); reject(workerScript);}
console.log("Hacking " + server.hostname + " after " + hackingTime.toString() + " seconds."); console.log("Hacking " + server.hostname + " after " + hackingTime.toString() + " seconds.");
setTimeout(function() { setTimeout(function() {
var hackChance = scriptCalculateHackingChance(server); var hackChance = scriptCalculateHackingChance(server);
@ -211,6 +214,9 @@ function evaluate(exp, workerScript) {
var moneyGained = scriptCalculatePercentMoneyHacked(server); var moneyGained = scriptCalculatePercentMoneyHacked(server);
moneyGained = Math.floor(server.moneyAvailable * moneyGained); moneyGained = Math.floor(server.moneyAvailable * moneyGained);
//Safety check
if (moneyGained <= 0) {moneyGained = 0;}
server.moneyAvailable -= moneyGained; server.moneyAvailable -= moneyGained;
Player.money += moneyGained; Player.money += moneyGained;
@ -228,14 +234,14 @@ function evaluate(exp, workerScript) {
p.then(function(res) { p.then(function(res) {
resolve("hackExecuted"); resolve("hackExecuted");
}, function() { }, function(reason) {
console.log("rejected. reason: " + reason);
reject(workerScript); reject(workerScript);
}); });
}, function() { }, function() {
reject(workerScript) reject(workerScript);
}); });
} else if (exp.func.value == "sleep") { } else if (exp.func.value == "sleep") {
console.log("Execute sleep()"); console.log("Execute sleep()");
if (exp.args.length != 1) { if (exp.args.length != 1) {
@ -491,6 +497,7 @@ function scriptCalculateHackingTime(server) {
var difficultyMult = server.requiredHackingSkill * server.hackDifficulty; var difficultyMult = server.requiredHackingSkill * server.hackDifficulty;
var skillFactor = difficultyMult / Player.hacking_skill; var skillFactor = difficultyMult / Player.hacking_skill;
var hackingTime = skillFactor * Player.hacking_speed_multiplier; //This is in seconds var hackingTime = skillFactor * Player.hacking_speed_multiplier; //This is in seconds
return hackingTime;
} }
//The same as Player's calculateExpGain() function but takes in the server as an argument //The same as Player's calculateExpGain() function but takes in the server as an argument

@ -32,13 +32,14 @@ function runScriptsLoop() {
workerScripts[i].running = true; workerScripts[i].running = true;
var p = evaluate(ast, workerScripts[i]); var p = evaluate(ast, workerScripts[i]);
var foo = workerScripts[i];
//Once the code finishes (either resolved or rejected, doesnt matter), set its //Once the code finishes (either resolved or rejected, doesnt matter), set its
//running status to false //running status to false
p.then(function(w) { p.then(function(w) {
console.log("Stopping script " + w.name + " because it finished running naturally");
w.running = false; w.running = false;
w.env.stopFlag = true; w.env.stopFlag = true;
}, function(w) { }, function(w) {
console.log("Stopping script" + w.name + " because it was manually stopped (rejected)")
w.running = false; w.running = false;
w.env.stopFlag = true; w.env.stopFlag = true;
}); });
@ -80,4 +81,24 @@ function killWorkerScript(scriptName, serverIp) {
} }
} }
//Queues a script to be run
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;
//Create and add workerScripts
var s = new WorkerScript();
s.name = filename;
s.code = script.code;
s.serverIp = server.ip;
workerScripts.push(s);
console.log("Pushed script onto workerScripts");
}
runScriptsLoop(); runScriptsLoop();

@ -16,7 +16,7 @@ function PlayerObject() {
this.hacking_chance_multiplier = 2; //Increase through ascensions/augmentations this.hacking_chance_multiplier = 2; //Increase through ascensions/augmentations
//this.hacking_speed_multiplier = 5; //Decrease through ascensions/augmentations //this.hacking_speed_multiplier = 5; //Decrease through ascensions/augmentations
this.hacking_speed_multiplier = 1; //Make it faster for debugging this.hacking_speed_multiplier = 1; //Make it faster for debugging
this.hacking_money_multiplier = .01; //Increase through ascensions/augmentations. Can't go above 1 this.hacking_money_multiplier = .001; //Increase through ascensions/augmentations. Can't go above 1
//Note: "Lifetime" refers to current ascension, "total" refers to the entire game history //Note: "Lifetime" refers to current ascension, "total" refers to the entire game history
//Accumulative stats and skills //Accumulative stats and skills
@ -148,7 +148,7 @@ PlayerObject.prototype.calculateExpGain = function() {
PlayerObject.prototype.hack = function() { PlayerObject.prototype.hack = function() {
this.actionTime = this.calculateHackingTime(); this.actionTime = this.calculateHackingTime();
console.log("Hacking time: " + this.actionTime); console.log("Hacking time: " + this.actionTime);
//Set the startHack flag so the engine starts the hacking process //Set the startAction flag so the engine starts the hacking process
this.startAction = true; this.startAction = true;
} }
PlayerObject.prototype.analyze = function() { PlayerObject.prototype.analyze = function() {

@ -5,6 +5,7 @@
//Define commands in script editor (ctrl x to close, etc.) //Define commands in script editor (ctrl x to close, etc.)
$(document).keydown(function(e) { $(document).keydown(function(e) {
if (Engine.currentPage == Engine.Page.ScriptEditor) { if (Engine.currentPage == Engine.Page.ScriptEditor) {
//Ctrl + x
if (e.keyCode == 88 && e.ctrlKey) { if (e.keyCode == 88 && e.ctrlKey) {
var filename = document.getElementById("script-editor-filename").value; var filename = document.getElementById("script-editor-filename").value;
@ -97,9 +98,24 @@ Script.prototype.saveScript = function() {
this.server = Player.currentServer; this.server = Player.currentServer;
//TODO Calculate/update number of instructions, ram usage, execution time, etc. //TODO Calculate/update number of instructions, ram usage, execution time, etc.
this.updateNumInstructions();
this.updateRamUsage();
} }
} }
//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;
}
//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)
Script.prototype.updateRamUsage = function() {
this.ramUsage = this.numInstructions * .2;
}
Script.prototype.toJSON = function() { Script.prototype.toJSON = function() {
return Generic_toJSON("Script", this); return Generic_toJSON("Script", this);
} }
@ -109,3 +125,11 @@ Script.fromJSON = function(value) {
} }
Reviver.constructors.Script = Script; Reviver.constructors.Script = Script;
//TODO
//Called when the game is loaded. Loads all running scripts (from all servers)
//into worker scripts so that they will start running
function loadAllRunningScripts() {
}

@ -97,6 +97,9 @@ var Terminal = {
var moneyGained = Player.calculatePercentMoneyHacked(); var moneyGained = Player.calculatePercentMoneyHacked();
moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained); moneyGained = Math.floor(Player.getCurrentServer().moneyAvailable * moneyGained);
//Safety check
if (moneyGained <= 0) {moneyGained = 0;}
Player.getCurrentServer().moneyAvailable -= moneyGained; Player.getCurrentServer().moneyAvailable -= moneyGained;
Player.money += moneyGained; Player.money += moneyGained;
@ -125,6 +128,7 @@ var Terminal = {
//TODO Change the text to sound better //TODO Change the text to sound better
post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%"); post("Estimated chance to hack: " + Math.round(Player.calculateHackingChance() * 100) + "%");
post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds"); post("Estimated time to hack: " + Math.round(Player.calculateHackingTime()) + " seconds");
post("Estimed total money available on server: $" + Player.getCurrentServer().moneyAvailable);
post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired); post("Required number of open ports for PortHack: " +Player.getCurrentServer().numOpenPortsRequired);
if (Player.getCurrentServer().sshPortOpen) { if (Player.getCurrentServer().sshPortOpen) {
post("SSH port: Open") post("SSH port: Open")
@ -481,33 +485,32 @@ var Terminal = {
}, },
runScript: function(scriptName) { runScript: function(scriptName) {
var server = Player.getCurrentServer();
//Check if this script is already running //Check if this script is already running
for (var i = 0; i < Player.getCurrentServer().runningScripts.length; i++) { for (var i = 0; i < server.runningScripts.length; i++) {
if (Player.getCurrentServer().runningScripts[i] == scriptName) { if (server.runningScripts[i] == scriptName) {
post("ERROR: This script is already running. Cannot run multiple instances"); post("ERROR: This script is already running. Cannot run multiple instances");
return; return;
} }
} }
//Check if the script exists and if it does run it //Check if the script exists and if it does run it
for (var i = 0; i < Player.getCurrentServer().scripts.length; i++) { for (var i = 0; i < server.scripts.length; i++) {
if (Player.getCurrentServer().scripts[i].filename == scriptName) { if (server.scripts[i].filename == scriptName) {
if (Player.getCurrentServer().hasAdminRights == false) { //Check for admin rights and that there is enough RAM availble to run
var ramUsage = server.scripts[i].ramUsage;
var ramAvailable = server.maxRam - server.ramUsed;
if (server.hasAdminRights == false) {
post("Need root access to run script"); post("Need root access to run script");
return; return;
} else if (ramUsage > ramAvailable){
post("This machine does not have enough RAM to run this script. Script requires " + ramUsage + "GB of RAM");
return;
}else { }else {
var filename = Player.getCurrentServer().scripts[i].filename; //Able to run script
var script = server.scripts[i];
//Add to current server's runningScripts addWorkerScript(script, server);
Player.getCurrentServer().runningScripts.push(filename)
//Create WorkerScript
var s = new WorkerScript();
s.name = filename;
s.code = Player.getCurrentServer().scripts[i].code;
s.serverIp = Player.getCurrentServer().ip;
workerScripts.push(s);
console.log("Pushed script onto workerScripts");
return; return;
} }
} }