2016-11-17 23:25:40 +01:00
|
|
|
/* Evaluator
|
|
|
|
* Evaluates the Abstract Syntax Tree for Netscript
|
|
|
|
* generated by the Parser class
|
|
|
|
*/
|
2016-11-30 00:07:24 +01:00
|
|
|
// Evaluator should return a Promise, so that any call to evaluate() can just
|
|
|
|
//wait for that promise to finish before continuing
|
|
|
|
function evaluate(exp, workerScript) {
|
|
|
|
var env = workerScript.env;
|
2016-11-17 23:25:40 +01:00
|
|
|
switch (exp.type) {
|
|
|
|
case "num":
|
|
|
|
case "str":
|
|
|
|
case "bool":
|
2016-11-30 00:07:24 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-11-30 00:07:24 +01:00
|
|
|
resolve(exp.value);
|
|
|
|
});
|
|
|
|
break;
|
2016-11-17 23:25:40 +01:00
|
|
|
case "var":
|
2016-11-30 00:07:24 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-19 19:20:19 +01:00
|
|
|
try {
|
|
|
|
resolve(env.get(exp.value));
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
|
|
|
|
}
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
break;
|
2016-11-17 23:25:40 +01:00
|
|
|
//Can currently only assign to "var"s
|
|
|
|
case "assign":
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("Evaluating assign operation");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
if (exp.left.type != "var")
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "| Cannot assign to " + JSON.stringify(exp.left));
|
2016-11-30 00:07:24 +01:00
|
|
|
|
|
|
|
var p = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var expRightPromise = evaluate(exp.right, workerScript);
|
|
|
|
expRightPromise.then(function(expRight) {
|
|
|
|
resolve(expRight);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime)
|
|
|
|
});
|
|
|
|
|
|
|
|
p.then(function(expRight) {
|
|
|
|
console.log("Right side of assign operation resolved with value: " + expRight);
|
2016-12-19 19:20:19 +01:00
|
|
|
try {
|
|
|
|
env.set(exp.left.value, expRight);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
|
|
|
|
}
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("Assign operation finished");
|
|
|
|
resolve("assignFinished");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-17 23:25:40 +01:00
|
|
|
case "binary":
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("Binary operation called");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var pLeft = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var promise = evaluate(exp.left, workerScript);
|
|
|
|
promise.then(function(valLeft) {
|
|
|
|
resolve(valLeft);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pLeft.then(function(valLeft) {
|
|
|
|
var pRight = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var promise = evaluate(exp.right, workerScript);
|
|
|
|
promise.then(function(valRight) {
|
|
|
|
resolve([valLeft, valRight]);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pRight.then(function(args) {
|
|
|
|
console.log("Resolving binary operation");
|
2016-12-15 23:22:42 +01:00
|
|
|
try {
|
|
|
|
resolve(apply_op(exp.operator, args[0], args[1]));
|
|
|
|
} catch (e) {
|
|
|
|
reject("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
|
|
|
|
}
|
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
2016-11-17 23:25:40 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
//TODO
|
2016-11-17 23:25:40 +01:00
|
|
|
case "if":
|
|
|
|
var numConds = exp.cond.length;
|
|
|
|
var numThens = exp.then.length;
|
|
|
|
if (numConds == 0 || numThens == 0 || numConds != numThens) {
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error ("|" + workerScript.serverIp + "|" + workerScript.name + "|Number of conds and thens in if structure don't match (or there are none)");
|
2016-11-17 23:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < numConds; i++) {
|
2016-11-30 00:07:24 +01:00
|
|
|
var cond = evaluate(exp.cond[i], workerScript);
|
|
|
|
if (cond) return evaluate(exp.then[i], workerScript);
|
2016-11-17 23:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Evaluate else if it exists, snce none of the conditionals
|
|
|
|
//were true
|
2016-11-30 00:07:24 +01:00
|
|
|
return exp.else ? evaluate(exp.else, workerScript) : false;
|
2016-11-17 23:25:40 +01:00
|
|
|
|
|
|
|
case "for":
|
2016-11-30 00:07:24 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("for loop encountered in evaluator");
|
|
|
|
var pInit = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var resInit = evaluate(exp.init, workerScript);
|
|
|
|
resInit.then(function(foo) {
|
|
|
|
resolve(resInit);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pInit.then(function(expInit) {
|
|
|
|
var pForLoop = evaluateFor(exp, workerScript);
|
|
|
|
pForLoop.then(function(forLoopRes) {
|
|
|
|
resolve("forLoopDone");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
2016-11-17 23:25:40 +01:00
|
|
|
break;
|
|
|
|
case "while":
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("Evaluating while loop");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var pEvaluateWhile = evaluateWhile(exp, workerScript);
|
|
|
|
pEvaluateWhile.then(function(whileLoopRes) {
|
|
|
|
resolve("whileLoopDone");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
2016-11-17 23:25:40 +01:00
|
|
|
break;
|
|
|
|
case "prog":
|
2016-11-30 00:07:24 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var evaluateProgPromise = evaluateProg(exp, workerScript, 0);
|
2016-12-06 17:59:20 +01:00
|
|
|
evaluateProgPromise.then(function(w) {
|
|
|
|
resolve(workerScript);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
2016-11-17 23:25:40 +01:00
|
|
|
|
|
|
|
/* Currently supported function calls:
|
|
|
|
* hack()
|
|
|
|
* sleep(N) - sleep N seconds
|
|
|
|
* print(x) - Prints a variable or constant
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
case "call":
|
|
|
|
//Define only valid function calls here, like hack() and stuff
|
|
|
|
//var func = evaluate(exp.func, env);
|
|
|
|
//return func.apply(null, exp.args.map(function(arg){
|
|
|
|
// return evaluate(arg, env);
|
|
|
|
//}));
|
2016-11-30 00:07:24 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-06 17:59:20 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
if (exp.func.value == "hack") {
|
|
|
|
console.log("Execute hack()");
|
2016-12-14 00:52:32 +01:00
|
|
|
if (exp.args.length != 1) {
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|Hack() call has incorrect number of arguments. Takes 1 argument");
|
2016-12-05 23:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
//IP of server to hack
|
|
|
|
var ipPromise = evaluate(exp.args[0], workerScript);
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
ipPromise.then(function(ip) {
|
|
|
|
//Calculate the hacking time
|
|
|
|
var server = AllServers[ip];
|
|
|
|
var hackingTime = scriptCalculateHackingTime(server); //This is in seconds
|
|
|
|
|
|
|
|
if (server.hasAdminRights == false) {
|
|
|
|
console.log("Cannot hack server " + server.hostname);
|
|
|
|
resolve("Cannot hack");
|
|
|
|
//TODO LOG That it can't be hacked
|
|
|
|
}
|
|
|
|
|
|
|
|
var p = new Promise(function(resolve, reject) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
2016-12-14 00:52:32 +01:00
|
|
|
console.log("Hacking " + server.hostname + " after " + hackingTime.toString() + " seconds.");
|
|
|
|
setTimeout(function() {
|
|
|
|
var hackChance = scriptCalculateHackingChance(server);
|
|
|
|
var rand = Math.random();
|
|
|
|
var expGainedOnSuccess = scriptCalculateExpGain(server);
|
|
|
|
var expGainedOnFailure = Math.round(expGainedOnSuccess / 4);
|
|
|
|
if (rand < hackChance) { //Success!
|
|
|
|
var moneyGained = scriptCalculatePercentMoneyHacked(server);
|
|
|
|
moneyGained = Math.floor(server.moneyAvailable * moneyGained);
|
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
//Safety check
|
|
|
|
if (moneyGained <= 0) {moneyGained = 0;}
|
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
server.moneyAvailable -= moneyGained;
|
2016-12-19 21:59:13 +01:00
|
|
|
Player.gainMoney(moneyGained);
|
2016-12-19 19:20:19 +01:00
|
|
|
workerScript.scriptRef.onlineMoneyMade += moneyGained;
|
2016-12-14 00:52:32 +01:00
|
|
|
|
|
|
|
Player.hacking_exp += expGainedOnSuccess;
|
2016-12-19 21:59:13 +01:00
|
|
|
workerScript.scriptRef.onlineExpGained += expGainedOnSuccess;
|
2016-12-14 00:52:32 +01:00
|
|
|
console.log("Script successfully hacked " + server.hostname + " for $" + moneyGained + " and " + expGainedOnSuccess + " exp");
|
|
|
|
resolve("Hack success");
|
|
|
|
} else {
|
|
|
|
//Player only gains 25% exp for failure? TODO Can change this later to balance
|
|
|
|
Player.hacking_exp += expGainedOnFailure;
|
2016-12-19 21:59:13 +01:00
|
|
|
workerScript.scriptRef.onlineExpGained += expGainedOnFailure;
|
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
console.log("Script unsuccessful to hack " + server.hostname + ". Gained " + expGainedOnFailure + "exp");
|
|
|
|
resolve("Hack failure");
|
|
|
|
}
|
|
|
|
}, hackingTime * 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
p.then(function(res) {
|
|
|
|
resolve("hackExecuted");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-12-14 00:52:32 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-12-05 23:31:46 +01:00
|
|
|
});
|
2016-12-14 00:52:32 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
} else if (exp.func.value == "sleep") {
|
|
|
|
console.log("Execute sleep()");
|
2016-12-05 23:31:46 +01:00
|
|
|
if (exp.args.length != 1) {
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|Sleep() call has incorrect number of arguments. Takes 1 argument.");
|
2016-12-05 23:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
var sleepTimePromise = evaluate(exp.args[0], workerScript);
|
|
|
|
sleepTimePromise.then(function(sleepTime) {
|
|
|
|
console.log("Sleep time: " + sleepTime);
|
|
|
|
var p = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve("foo");
|
|
|
|
}, sleepTime);
|
|
|
|
});
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
p.then(function(res) {
|
|
|
|
resolve("sleepExecuted");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-12-14 00:52:32 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e)
|
2016-12-05 23:31:46 +01:00
|
|
|
});
|
2016-12-14 00:52:32 +01:00
|
|
|
|
2016-12-05 23:31:46 +01:00
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
} else if (exp.func.value == "print") {
|
2016-12-05 23:31:46 +01:00
|
|
|
if (exp.args.length != 1) {
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "| Print() call has incorrect number of arguments. Takes 1 argument");
|
2016-12-05 23:31:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var p = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.args[0], workerScript);
|
|
|
|
evaluatePromise.then(function(res) {
|
|
|
|
resolve(res);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
p.then(function(res) {
|
|
|
|
post(res.toString());
|
|
|
|
console.log("Print call executed");
|
|
|
|
resolve("printExecuted");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
2016-11-17 23:25:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-12-15 23:22:42 +01:00
|
|
|
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "| Can't evaluate type " + exp.type);
|
2016-11-17 23:25:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
//Evaluate the looping part of a for loop (Initialization block is NOT done in here)
|
|
|
|
function evaluateFor(exp, workerScript) {
|
2016-12-15 23:22:42 +01:00
|
|
|
var env = workerScript.env;
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("evaluateFor() called");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var pCond = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.cond, workerScript);
|
|
|
|
evaluatePromise.then(function(resCond) {
|
|
|
|
console.log("Conditional evaluated to: " + resCond);
|
|
|
|
resolve(resCond);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pCond.then(function(resCond) {
|
|
|
|
if (resCond) {
|
|
|
|
console.log("About to evaluate an iteration of for loop code");
|
|
|
|
//Run the for loop code
|
|
|
|
var pCode = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.code, workerScript);
|
|
|
|
evaluatePromise.then(function(resCode) {
|
|
|
|
console.log("Evaluated an iteration of for loop code");
|
|
|
|
resolve(resCode);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
//After the code executes make a recursive call
|
|
|
|
pCode.then(function(resCode) {
|
|
|
|
var pPostLoop = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.postloop, workerScript);
|
|
|
|
evaluatePromise.then(function(foo) {
|
|
|
|
console.log("Evaluated for loop postloop");
|
|
|
|
resolve("postLoopFinished");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pPostLoop.then(function(resPostloop) {
|
|
|
|
var recursiveCall = evaluateFor(exp, workerScript);
|
|
|
|
recursiveCall.then(function(foo) {
|
|
|
|
resolve("endForLoop");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log("Cond is false, stopping for loop");
|
|
|
|
resolve("endForLoop"); //Doesn't need to resolve to any particular value
|
|
|
|
}
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateWhile(exp, workerScript) {
|
2016-12-15 23:22:42 +01:00
|
|
|
var env = workerScript.env;
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("evaluateWhile() called");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
var pCond = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.cond, workerScript);
|
|
|
|
evaluatePromise.then(function(resCond) {
|
|
|
|
console.log("Conditional evaluated to: " + resCond);
|
|
|
|
resolve(resCond);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
pCond.then(function(resCond) {
|
|
|
|
if (resCond) {
|
|
|
|
//Run the while loop code
|
|
|
|
var pCode = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.code, workerScript);
|
|
|
|
evaluatePromise.then(function(resCode) {
|
|
|
|
console.log("Evaluated an iteration of while loop code");
|
|
|
|
resolve(resCode);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
//After the code executes make a recursive call
|
|
|
|
pCode.then(function(resCode) {
|
|
|
|
var recursiveCall = evaluateWhile(exp, workerScript);
|
|
|
|
recursiveCall.then(function(foo) {
|
|
|
|
resolve("endWhileLoop");
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log("Cond is false, stopping while loop");
|
|
|
|
resolve("endWhileLoop"); //Doesn't need to resolve to any particular value
|
|
|
|
}
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateProg(exp, workerScript, index) {
|
2016-12-15 23:22:42 +01:00
|
|
|
var env = workerScript.env;
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
console.log("evaluateProg() called");
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (env.stopFlag) {reject(workerScript);}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
if (index >= exp.prog.length) {
|
|
|
|
console.log("Prog done. Resolving recursively");
|
|
|
|
resolve("progFinished");
|
|
|
|
} else {
|
|
|
|
//Evaluate this line of code in the prog
|
|
|
|
var code = new Promise(function(resolve, reject) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var evaluatePromise = evaluate(exp.prog[index], workerScript);
|
|
|
|
evaluatePromise.then(function(evalRes) {
|
|
|
|
resolve(evalRes);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}, CONSTANTS.CodeInstructionRunTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
//After the code finishes evaluating, evaluate the next line recursively
|
|
|
|
code.then(function(codeRes) {
|
|
|
|
var nextLine = evaluateProg(exp, workerScript, index + 1);
|
|
|
|
nextLine.then(function(nextLineRes) {
|
2016-12-06 17:59:20 +01:00
|
|
|
resolve(workerScript);
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
2016-12-15 23:22:42 +01:00
|
|
|
}, function(e) {
|
|
|
|
reject(e);
|
2016-11-30 00:07:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:25:40 +01:00
|
|
|
function apply_op(op, a, b) {
|
|
|
|
function num(x) {
|
|
|
|
if (typeof x != "number")
|
|
|
|
throw new Error("Expected number but got " + x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
function div(x) {
|
|
|
|
if (num(x) == 0)
|
|
|
|
throw new Error("Divide by zero");
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
switch (op) {
|
|
|
|
case "+": return num(a) + num(b);
|
|
|
|
case "-": return num(a) - num(b);
|
|
|
|
case "*": return num(a) * num(b);
|
|
|
|
case "/": return num(a) / div(b);
|
|
|
|
case "%": return num(a) % div(b);
|
|
|
|
case "&&": return a !== false && b;
|
|
|
|
case "||": return a !== false ? a : b;
|
|
|
|
case "<": return num(a) < num(b);
|
|
|
|
case ">": return num(a) > num(b);
|
|
|
|
case "<=": return num(a) <= num(b);
|
|
|
|
case ">=": return num(a) >= num(b);
|
|
|
|
case "==": return a === b;
|
|
|
|
case "!=": return a !== b;
|
|
|
|
}
|
|
|
|
throw new Error("Can't apply operator " + op);
|
2016-11-30 00:07:24 +01:00
|
|
|
}
|
2016-12-14 00:52:32 +01:00
|
|
|
|
|
|
|
//The same as Player's calculateHackingChance() function but takes in the server as an argument
|
|
|
|
function scriptCalculateHackingChance(server) {
|
|
|
|
var difficultyMult = (100 - server.hackDifficulty) / 100;
|
|
|
|
var skillMult = (Player.hacking_chance_multiplier * Player.hacking_skill);
|
|
|
|
var skillChance = (skillMult - server.requiredHackingSkill) / skillMult;
|
|
|
|
return (skillChance * difficultyMult);
|
|
|
|
}
|
|
|
|
|
|
|
|
//The same as Player's calculateHackingTime() function but takes in the server as an argument
|
|
|
|
function scriptCalculateHackingTime(server) {
|
|
|
|
var difficultyMult = server.requiredHackingSkill * server.hackDifficulty;
|
|
|
|
var skillFactor = difficultyMult / Player.hacking_skill;
|
|
|
|
var hackingTime = skillFactor * Player.hacking_speed_multiplier; //This is in seconds
|
2016-12-14 21:29:40 +01:00
|
|
|
return hackingTime;
|
2016-12-14 00:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//The same as Player's calculateExpGain() function but takes in the server as an argument
|
|
|
|
function scriptCalculateExpGain(server) {
|
|
|
|
return Math.round(server.hackDifficulty * server.requiredHackingSkill * Player.hacking_exp_mult);
|
|
|
|
}
|
|
|
|
|
|
|
|
//The same as Player's calculatePercentMoneyHacked() function but takes in the server as an argument
|
|
|
|
function scriptCalculatePercentMoneyHacked(server) {
|
|
|
|
var difficultyMult = (100 - server.hackDifficulty) / 100;
|
|
|
|
var skillMult = (Player.hacking_skill - (server.requiredHackingSkill - 1)) / Player.hacking_skill;
|
|
|
|
var percentMoneyHacked = difficultyMult * skillMult * Player.hacking_money_multiplier;
|
|
|
|
console.log("Percent money hacked calculated to be: " + percentMoneyHacked);
|
|
|
|
return percentMoneyHacked;
|
|
|
|
}
|