mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-01-03 03:47:35 +01:00
Resolving and rejecting at the top level now returns a reference to workerScripts. This makes killing/deleting a running script possible. The feature appears to be working now
This commit is contained in:
parent
437ebc2703
commit
8c58710495
@ -4,6 +4,7 @@
|
|||||||
function Environment(parent) {
|
function Environment(parent) {
|
||||||
this.vars = Object.create(parent ? parent.vars : null);
|
this.vars = Object.create(parent ? parent.vars : null);
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
this.stopFlag = false;
|
||||||
}
|
}
|
||||||
Environment.prototype = {
|
Environment.prototype = {
|
||||||
//Create a "subscope", which is a new new "sub-environment"
|
//Create a "subscope", which is a new new "sub-environment"
|
||||||
@ -44,7 +45,5 @@ Environment.prototype = {
|
|||||||
//Creates (or overwrites) a variable in the current scope
|
//Creates (or overwrites) a variable in the current scope
|
||||||
def: function(name, value) {
|
def: function(name, value) {
|
||||||
return this.vars[name] = value;
|
return this.vars[name] = value;
|
||||||
},
|
}
|
||||||
|
|
||||||
stopFlag: false
|
|
||||||
};
|
};
|
@ -11,13 +11,13 @@ function evaluate(exp, workerScript) {
|
|||||||
case "str":
|
case "str":
|
||||||
case "bool":
|
case "bool":
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
resolve(exp.value);
|
resolve(exp.value);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "var":
|
case "var":
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
resolve(env.get(exp.value));
|
resolve(env.get(exp.value));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -25,7 +25,7 @@ function evaluate(exp, workerScript) {
|
|||||||
case "assign":
|
case "assign":
|
||||||
console.log("Evaluating assign operation");
|
console.log("Evaluating assign operation");
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
if (exp.left.type != "var")
|
if (exp.left.type != "var")
|
||||||
throw new Error("Cannot assign to " + JSON.stringify(exp.left));
|
throw new Error("Cannot assign to " + JSON.stringify(exp.left));
|
||||||
@ -36,7 +36,7 @@ function evaluate(exp, workerScript) {
|
|||||||
expRightPromise.then(function(expRight) {
|
expRightPromise.then(function(expRight) {
|
||||||
resolve(expRight);
|
resolve(expRight);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime)
|
}, CONSTANTS.CodeInstructionRunTime)
|
||||||
});
|
});
|
||||||
@ -47,14 +47,14 @@ function evaluate(exp, workerScript) {
|
|||||||
console.log("Assign operation finished");
|
console.log("Assign operation finished");
|
||||||
resolve("assignFinished");
|
resolve("assignFinished");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
case "binary":
|
case "binary":
|
||||||
console.log("Binary operation called");
|
console.log("Binary operation called");
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
var pLeft = new Promise(function(resolve, reject) {
|
var pLeft = new Promise(function(resolve, reject) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
@ -62,7 +62,7 @@ function evaluate(exp, workerScript) {
|
|||||||
promise.then(function(valLeft) {
|
promise.then(function(valLeft) {
|
||||||
resolve(valLeft);
|
resolve(valLeft);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -74,7 +74,7 @@ function evaluate(exp, workerScript) {
|
|||||||
promise.then(function(valRight) {
|
promise.then(function(valRight) {
|
||||||
resolve([valLeft, valRight]);
|
resolve([valLeft, valRight]);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -83,10 +83,10 @@ function evaluate(exp, workerScript) {
|
|||||||
console.log("Resolving binary operation");
|
console.log("Resolving binary operation");
|
||||||
resolve(apply_op(exp.operator, args[0], args[1]));
|
resolve(apply_op(exp.operator, args[0], args[1]));
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -110,7 +110,7 @@ function evaluate(exp, workerScript) {
|
|||||||
|
|
||||||
case "for":
|
case "for":
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
console.log("for loop encountered in evaluator");
|
console.log("for loop encountered in evaluator");
|
||||||
var pInit = new Promise(function(resolve, reject) {
|
var pInit = new Promise(function(resolve, reject) {
|
||||||
@ -119,7 +119,7 @@ function evaluate(exp, workerScript) {
|
|||||||
resInit.then(function(foo) {
|
resInit.then(function(foo) {
|
||||||
resolve(resInit);
|
resolve(resInit);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -129,35 +129,35 @@ function evaluate(exp, workerScript) {
|
|||||||
pForLoop.then(function(forLoopRes) {
|
pForLoop.then(function(forLoopRes) {
|
||||||
resolve("forLoopDone");
|
resolve("forLoopDone");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "while":
|
case "while":
|
||||||
console.log("Evaluating while loop");
|
console.log("Evaluating while loop");
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
var pEvaluateWhile = evaluateWhile(exp, workerScript);
|
var pEvaluateWhile = evaluateWhile(exp, workerScript);
|
||||||
pEvaluateWhile.then(function(whileLoopRes) {
|
pEvaluateWhile.then(function(whileLoopRes) {
|
||||||
resolve("whileLoopDone");
|
resolve("whileLoopDone");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "prog":
|
case "prog":
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
var evaluateProgPromise = evaluateProg(exp, workerScript, 0);
|
var evaluateProgPromise = evaluateProg(exp, workerScript, 0);
|
||||||
evaluateProgPromise.then(function(res) {
|
evaluateProgPromise.then(function(w) {
|
||||||
resolve(res);
|
resolve(workerScript);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -175,15 +175,24 @@ function evaluate(exp, workerScript) {
|
|||||||
// return evaluate(arg, env);
|
// return evaluate(arg, env);
|
||||||
//}));
|
//}));
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
if (env.stopFlag) {reject("Stopping script");}
|
if (env.stopFlag) {reject(workerScript);}
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if (exp.func.value == "hack") {
|
if (exp.func.value == "hack") {
|
||||||
console.log("Execute hack()");
|
console.log("Execute hack()");
|
||||||
if (exp.args.length != 0) {
|
if (exp.args.length != 0) {
|
||||||
throw new Error("Hack() call has incorrect number of arguments. Takes no arguments");)
|
throw new Error("Hack() call has incorrect number of arguments. Takes no arguments");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Calculate the hacking time
|
||||||
|
var currentServer = AllServers[workerScript.serverIp];
|
||||||
|
var difficultyMult = currentServer.requiredHackingSkill * currentServer.hackDifficulty;
|
||||||
|
var skillFactor = difficultMult / Player.hacking_skill;
|
||||||
|
var hackingTime = skillFactor * Player.hacking_speed_multiplier; //This is in seconds
|
||||||
|
|
||||||
|
//TODO Add a safeguard to prevent a script from hacking a Server that the player
|
||||||
|
//cannot hack
|
||||||
|
|
||||||
var p = new Promise(function(resolve, reject) {
|
var p = new Promise(function(resolve, reject) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var hackChance = Player.calculateHackingChance();
|
var hackChance = Player.calculateHackingChance();
|
||||||
@ -202,7 +211,7 @@ function evaluate(exp, workerScript) {
|
|||||||
//Player only gains 25% exp for failure? TODO Can change this later to balance
|
//Player only gains 25% exp for failure? TODO Can change this later to balance
|
||||||
Player.hacking_exp += expGainedOnFailure;
|
Player.hacking_exp += expGainedOnFailure;
|
||||||
}
|
}
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, hackingTime * 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
p.then(function(res) {
|
p.then(function(res) {
|
||||||
@ -236,7 +245,7 @@ function evaluate(exp, workerScript) {
|
|||||||
evaluatePromise.then(function(res) {
|
evaluatePromise.then(function(res) {
|
||||||
resolve(res);
|
resolve(res);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -246,7 +255,7 @@ function evaluate(exp, workerScript) {
|
|||||||
console.log("Print call executed");
|
console.log("Print call executed");
|
||||||
resolve("printExecuted");
|
resolve("printExecuted");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
@ -269,7 +278,7 @@ function evaluateFor(exp, workerScript) {
|
|||||||
console.log("Conditional evaluated to: " + resCond);
|
console.log("Conditional evaluated to: " + resCond);
|
||||||
resolve(resCond);
|
resolve(resCond);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -285,7 +294,7 @@ function evaluateFor(exp, workerScript) {
|
|||||||
console.log("Evaluated an iteration of for loop code");
|
console.log("Evaluated an iteration of for loop code");
|
||||||
resolve(resCode);
|
resolve(resCode);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -299,7 +308,7 @@ function evaluateFor(exp, workerScript) {
|
|||||||
console.log("Evaluated for loop postloop");
|
console.log("Evaluated for loop postloop");
|
||||||
resolve("postLoopFinished");
|
resolve("postLoopFinished");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -309,21 +318,21 @@ function evaluateFor(exp, workerScript) {
|
|||||||
recursiveCall.then(function(foo) {
|
recursiveCall.then(function(foo) {
|
||||||
resolve("endForLoop");
|
resolve("endForLoop");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
|
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log("Cond is false, stopping for loop");
|
console.log("Cond is false, stopping for loop");
|
||||||
resolve("endForLoop"); //Doesn't need to resolve to any particular value
|
resolve("endForLoop"); //Doesn't need to resolve to any particular value
|
||||||
}
|
}
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -338,7 +347,7 @@ function evaluateWhile(exp, workerScript) {
|
|||||||
console.log("Conditional evaluated to: " + resCond);
|
console.log("Conditional evaluated to: " + resCond);
|
||||||
resolve(resCond);
|
resolve(resCond);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -353,7 +362,7 @@ function evaluateWhile(exp, workerScript) {
|
|||||||
console.log("Evaluated an iteration of while loop code");
|
console.log("Evaluated an iteration of while loop code");
|
||||||
resolve(resCode);
|
resolve(resCode);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -364,17 +373,17 @@ function evaluateWhile(exp, workerScript) {
|
|||||||
recursiveCall.then(function(foo) {
|
recursiveCall.then(function(foo) {
|
||||||
resolve("endWhileLoop");
|
resolve("endWhileLoop");
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log("Cond is false, stopping while loop");
|
console.log("Cond is false, stopping while loop");
|
||||||
resolve("endWhileLoop"); //Doesn't need to resolve to any particular value
|
resolve("endWhileLoop"); //Doesn't need to resolve to any particular value
|
||||||
}
|
}
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -393,7 +402,7 @@ function evaluateProg(exp, workerScript, index) {
|
|||||||
evaluatePromise.then(function(evalRes) {
|
evaluatePromise.then(function(evalRes) {
|
||||||
resolve(evalRes);
|
resolve(evalRes);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, CONSTANTS.CodeInstructionRunTime);
|
}, CONSTANTS.CodeInstructionRunTime);
|
||||||
});
|
});
|
||||||
@ -402,12 +411,12 @@ function evaluateProg(exp, workerScript, index) {
|
|||||||
code.then(function(codeRes) {
|
code.then(function(codeRes) {
|
||||||
var nextLine = evaluateProg(exp, workerScript, index + 1);
|
var nextLine = evaluateProg(exp, workerScript, index + 1);
|
||||||
nextLine.then(function(nextLineRes) {
|
nextLine.then(function(nextLineRes) {
|
||||||
resolve("progDone");
|
resolve(workerScript);
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}, function() {
|
}, function() {
|
||||||
reject("Stopping script");
|
reject(workerScript);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -23,7 +23,7 @@ function runScriptsLoop() {
|
|||||||
//Run any scripts that haven't been started
|
//Run any scripts that haven't been started
|
||||||
for (var i = 0; i < workerScripts.length; i++) {
|
for (var i = 0; i < workerScripts.length; i++) {
|
||||||
//If it isn't running, start the script
|
//If it isn't running, start the script
|
||||||
if (workerScripts[i].running == false) {
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
||||||
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
|
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
|
||||||
|
|
||||||
console.log("Starting new script: " + workerScripts[i].name);
|
console.log("Starting new script: " + workerScripts[i].name);
|
||||||
@ -32,13 +32,15 @@ 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(foo) {
|
p.then(function(w) {
|
||||||
workerScripts[i].running = false;
|
w.running = false;
|
||||||
}, function() {
|
w.env.stopFlag = true;
|
||||||
workerScripts[i].running = false;
|
}, function(w) {
|
||||||
|
w.running = false;
|
||||||
|
w.env.stopFlag = true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,12 +49,13 @@ function runScriptsLoop() {
|
|||||||
//items fucks up the indexing
|
//items fucks up the indexing
|
||||||
for (var i = workerScripts.length - 1; i >= 0; i--) {
|
for (var i = workerScripts.length - 1; i >= 0; i--) {
|
||||||
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
|
||||||
|
console.log("Deleting scripts");
|
||||||
//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 ip = workerScripts[i].serverIp;
|
||||||
var name = workerScripts[i].name;
|
var name = workerScripts[i].name;
|
||||||
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
|
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
|
||||||
if (AllServers[ip].runningScripts[j] == name) {
|
if (AllServers[ip].runningScripts[j] == name) {
|
||||||
AllServers[i].runningScripts.splice(j, 1);
|
AllServers[ip].runningScripts.splice(j, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user