2016-11-17 23:25:40 +01:00
/ * E v a l u a t o r
* 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 ;
2017-05-01 07:39:48 +02:00
if ( exp == null ) {
2017-05-01 19:04:30 +02:00
return new Promise ( function ( resolve , reject ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Error: NULL expression" ) ;
} ) ;
2017-05-01 07:39:48 +02:00
}
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
return new Promise ( function ( resolve , reject ) {
2017-05-12 06:59:07 +02:00
console . log ( "Evaluating assign" ) ;
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" )
2017-05-12 06:59:07 +02:00
reject ( "|" + 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 ) {
2016-12-19 19:20:19 +01:00
try {
env . set ( exp . left . value , expRight ) ;
} catch ( e ) {
2017-05-12 06:59:07 +02:00
console . log ( "here" ) ;
2016-12-19 19:20:19 +01:00
throw new Error ( "|" + workerScript . serverIp + "|" + workerScript . name + "|" + e . toString ( ) ) ;
}
2017-05-11 06:20:17 +02:00
resolve ( false ) ; //Return false so this doesnt cause loops/ifs to evaluate
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
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 ) {
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" :
2017-05-11 06:20:17 +02:00
return new Promise ( function ( resolve , reject ) {
2017-05-12 06:59:07 +02:00
console . log ( "Evaluating if" ) ;
2017-05-11 06:20:17 +02:00
var numConds = exp . cond . length ;
var numThens = exp . then . length ;
if ( numConds == 0 || numThens == 0 || numConds != numThens ) {
2017-05-12 06:59:07 +02:00
console . log ( "Number of ifs and conds dont match. Rejecting" ) ;
2017-05-11 06:20:17 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Number of conds and thens in if structure don't match (or there are none)" ) ;
}
2017-05-12 06:59:07 +02:00
var evalIfPromise = evaluateIf ( exp , workerScript , 0 ) ;
evalIfPromise . then ( function ( res ) {
if ( res ) {
//One of the if/elif statements evaluated to true
console . log ( "done with if" ) ;
resolve ( "if statement done" ) ;
} else {
//None of the if/elif statements were true. Evaluate else if there is one
if ( exp . else ) {
var elseEval = evaluate ( exp . else , workerScript ) ;
elseEval . then ( function ( res ) {
console . log ( "if statement done with else" ) ;
resolve ( "if statement done with else" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else {
console . log ( "no else statement, resolving" ) ;
resolve ( "if statement done" ) ;
}
}
} , function ( e ) {
reject ( e ) ;
} ) ;
2017-05-11 06:20:17 +02:00
} ) ;
break ;
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" ) ;
2017-04-27 22:59:57 +02:00
workerScript . scriptRef . log ( "Entering for loop" ) ;
2016-11-30 00:07:24 +01:00
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" ) ;
2017-04-27 22:59:57 +02:00
workerScript . scriptRef . log ( "Exiting for loop" ) ;
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
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 ) {
2017-05-01 07:39:48 +02:00
workerScript . errorMessage = e . toString ( ) ;
reject ( workerScript ) ;
2016-11-30 00:07:24 +01:00
} ) ;
} ) ;
break ;
2016-11-17 23:25:40 +01:00
/ * C u r r e n t l y s u p p o r t e d f u n c t i o n c a l l s :
2017-05-11 06:20:17 +02:00
* hack ( server )
2016-11-17 23:25:40 +01:00
* sleep ( N ) - sleep N seconds
* print ( x ) - Prints a variable or constant
2017-05-11 06:20:17 +02:00
* grow ( server )
* nuke ( server )
* brutessh ( server )
* ftpcrack ( server )
* relaysmtp ( server )
* httpworm ( server )
* sqlinject ( server )
2017-05-15 08:35:09 +02:00
* getHackingLevel ( )
* run ( script ) )
2017-05-11 06:20:17 +02:00
* /
2016-11-17 23:25:40 +01:00
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" ) {
2016-12-14 00:52:32 +01:00
if ( exp . args . length != 1 ) {
2017-05-01 07:39:48 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Hack() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2016-12-05 23:31:46 +01:00
}
2016-12-14 00:52:32 +01:00
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
2017-05-11 06:20:17 +02:00
2016-12-14 00:52:32 +01:00
ipPromise . then ( function ( ip ) {
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
2017-02-16 19:52:11 +01:00
if ( server == null ) {
2017-05-01 19:04:30 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into hack() command" ) ;
2017-05-11 06:20:17 +02:00
workerScript . scriptRef . log ( "Cannot hack(). Invalid IP or hostname passed in: " + ip + ". Stopping..." ) ;
2017-05-01 19:04:30 +02:00
return ;
2017-02-16 19:52:11 +01:00
}
2016-12-14 00:52:32 +01:00
//Calculate the hacking time
var hackingTime = scriptCalculateHackingTime ( server ) ; //This is in seconds
2017-04-28 00:01:26 +02:00
//No root access or skill level too low
2016-12-14 00:52:32 +01:00
if ( server . hasAdminRights == false ) {
2017-04-28 00:01:26 +02:00
workerScript . scriptRef . log ( "Cannot hack this server (" + server . hostname + ") because user does not have root access" ) ;
2017-05-01 07:39:48 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Script crashed because it did not have root access to " + server . hostname ) ;
2017-05-01 19:04:30 +02:00
return ;
2016-12-14 00:52:32 +01:00
}
2017-04-11 15:59:48 +02:00
2017-04-28 00:01:26 +02:00
if ( server . requiredHackingSkill > Player . hacking _skill ) {
workerScript . scriptRef . log ( "Cannot hack this server (" + server . hostaname + ") because user does not have root access" ) ;
2017-05-01 07:39:48 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Script crashed because player's hacking skill is not high enough to hack " + server . hostname ) ;
2017-05-01 19:04:30 +02:00
return ;
2017-04-28 00:01:26 +02:00
}
2017-05-02 19:06:46 +02:00
workerScript . scriptRef . log ( "Attempting to hack " + ip + " in " + hackingTime . toFixed ( 3 ) + " seconds" ) ;
2016-12-14 00:52:32 +01:00
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 ( ) {
2017-05-05 16:21:08 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2016-12-14 00:52:32 +01:00
var hackChance = scriptCalculateHackingChance ( server ) ;
var rand = Math . random ( ) ;
var expGainedOnSuccess = scriptCalculateExpGain ( server ) ;
2017-05-05 17:50:55 +02:00
var expGainedOnFailure = ( expGainedOnSuccess / 4 ) ;
2016-12-14 00:52:32 +01:00
if ( rand < hackChance ) { //Success!
2017-05-07 00:19:18 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; return ; }
2016-12-14 00:52:32 +01:00
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 ;
2017-05-10 19:42:46 +02:00
workerScript . scriptRef . moneyStolenMap [ server . ip ] += moneyGained ;
2016-12-14 00:52:32 +01:00
2017-04-18 06:32:17 +02:00
Player . gainHackingExp ( expGainedOnSuccess ) ;
2016-12-19 21:59:13 +01:00
workerScript . scriptRef . onlineExpGained += expGainedOnSuccess ;
2017-05-05 17:50:55 +02:00
console . log ( "Script successfully hacked " + server . hostname + " for $" + formatNumber ( moneyGained , 2 ) + " and " + formatNumber ( expGainedOnSuccess , 4 ) + " exp" ) ;
workerScript . scriptRef . log ( "Script SUCCESSFULLY hacked " + server . hostname + " for $" + formatNumber ( moneyGained , 2 ) + " and " + formatNumber ( expGainedOnSuccess , 4 ) + " exp" ) ;
2016-12-14 00:52:32 +01:00
resolve ( "Hack success" ) ;
2017-05-07 00:19:18 +02:00
} else {
if ( env . stopFlag ) { reject ( workerScript ) ; return ; }
2016-12-14 00:52:32 +01:00
//Player only gains 25% exp for failure? TODO Can change this later to balance
2017-04-18 06:32:17 +02:00
Player . gainHackingExp ( expGainedOnFailure ) ;
2016-12-19 21:59:13 +01:00
workerScript . scriptRef . onlineExpGained += expGainedOnFailure ;
2017-05-05 17:50:55 +02:00
console . log ( "Script unsuccessful to hack " + server . hostname + ". Gained " + formatNumber ( expGainedOnFailure , 4 ) + " exp" ) ;
workerScript . scriptRef . log ( "Script FAILED to hack " + server . hostname + ". Gained " + formatNumber ( expGainedOnFailure , 4 ) + " exp" ) ;
2016-12-14 00:52:32 +01:00
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" ) {
2016-12-05 23:31:46 +01:00
if ( exp . args . length != 1 ) {
2017-05-02 18:28:54 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|sleep() call has incorrect number of arguments. Takes 1 argument." ) ;
2017-05-15 17:28:59 +02:00
return ;
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 ) {
2017-04-11 15:59:48 +02:00
workerScript . scriptRef . log ( "Sleeping for " + sleepTime + " milliseconds" ) ;
2016-12-14 00:52:32 +01:00
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-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 ) {
2017-05-02 18:28:54 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|print() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
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 ) {
2017-05-02 18:28:54 +02:00
workerScript . scriptRef . log ( res . toString ( ) ) ;
2016-11-30 00:07:24 +01:00
resolve ( "printExecuted" ) ;
2016-12-15 23:22:42 +01:00
} , function ( e ) {
reject ( e ) ;
2016-11-30 00:07:24 +01:00
} ) ;
2017-05-02 18:28:54 +02:00
} else if ( exp . func . value == "grow" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|grow() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-02 18:28:54 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
2017-05-02 18:28:54 +02:00
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into grow() command" ) ;
workerScript . scriptRef . log ( "Cannot grow(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
//No root access or skill level too low
if ( server . hasAdminRights == false ) {
workerScript . scriptRef . log ( "Cannot grow this server (" + server . hostname + ") because user does not have root access" ) ;
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Script crashed because it did not have root access to " + server . hostname ) ;
return ;
}
2017-05-03 06:38:58 +02:00
workerScript . scriptRef . log ( "Calling grow() on server " + server . hostname + " in 120 seconds" ) ;
2017-05-02 18:28:54 +02:00
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
var growthPercentage = processSingleServerGrowth ( server , 450 ) ;
resolve ( growthPercentage ) ;
} , 120 * 1000 ) ; //grow() takes flat 2 minutes right now
} ) ;
p . then ( function ( growthPercentage ) {
resolve ( "hackExecuted" ) ;
2017-05-05 17:50:55 +02:00
workerScript . scriptRef . log ( "Using grow(), the money available on " + server . hostname + " was grown by " + ( growthPercentage * 100 - 100 ) . toFixed ( 6 ) + "%. Gained 1 hacking exp" ) ;
Player . gainHackingExp ( 1 ) ;
workerScript . scriptRef . onlineExpGained += 1 ;
2017-05-02 18:28:54 +02:00
} , function ( e ) {
2017-05-11 06:20:17 +02:00
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "nuke" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|nuke() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into nuke() command" ) ;
workerScript . scriptRef . log ( "Cannot nuke(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . NukeProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have NUKE program on home computer" ) ;
return ;
}
if ( server . openPortCount < server . numOpenPortsRequired ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Not enough ports opened to use NUKE.exe virus" ) ;
return ;
}
workerScript . scriptRef . log ( "Running NUKE.exe on server " + server . hostname + " in 5 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( server . hasAdminRights ) {
workerScript . scriptRef . log ( "Already have root access to " + server . hostname ) ;
} else {
server . hasAdminRights = true ;
workerScript . scriptRef . log ( "Executed NUKE.exe virus on " + server . hostname + " to gain root access" ) ;
}
resolve ( "nuke done" ) ;
} , 5 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "nukeExecuted" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "brutessh" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|brutessh() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into brutessh() command" ) ;
workerScript . scriptRef . log ( "Cannot brutessh(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . BruteSSHProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have BruteSSH.exe program on home computer" ) ;
return ;
}
workerScript . scriptRef . log ( "Running BruteSSH.exe on server " + server . hostname + " in 10 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( ! server . sshPortOpen ) {
workerScript . scriptRef . log ( "Executed BruteSSH.exe virus on " + server . hostname + " to open SSH port (22)" ) ;
server . sshPortOpen = true ;
++ server . openPortCount ;
} else {
workerScript . scriptRef . log ( "SSH Port (22) already opened on " + server . hostname ) ;
}
resolve ( "brutessh done" ) ;
} , 10 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "bruteSSHExecuted" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "ftpcrack" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|ftpcrack() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into ftpcrack() command" ) ;
workerScript . scriptRef . log ( "Cannot ftpcrack(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . FTPCrackProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have FTPCrack.exe program on home computer" ) ;
return ;
}
workerScript . scriptRef . log ( "Running FTPCrack.exe on server " + server . hostname + " in 15 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( ! server . ftpPortOpen ) {
workerScript . scriptRef . log ( "Executed FTPCrack.exe virus on " + server . hostname + " to open FTP port (21)" ) ;
server . ftpPortOpen = true ;
++ server . openPortCount ;
} else {
workerScript . scriptRef . log ( "FTP Port (21) already opened on " + server . hostname ) ;
}
resolve ( "ftpcrack done" ) ;
} , 15 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "ftpcrackexecuted" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "relaysmtp" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|relaysmtp() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into relaysmtp() command" ) ;
workerScript . scriptRef . log ( "Cannot relaysmtp(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . RelaySMTPProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have relaySMTP.exe program on home computer" ) ;
return ;
}
workerScript . scriptRef . log ( "Running relaySMTP.exe on server " + server . hostname + " in 20 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( ! server . smtpPortOpen ) {
workerScript . scriptRef . log ( "Executed relaySMTP.exe virus on " + server . hostname + " to open SMTP port (25)" ) ;
server . smtpPortOpen = true ;
++ server . openPortCount ;
} else {
workerScript . scriptRef . log ( "SMTP Port (25) already opened on " + server . hostname ) ;
}
resolve ( "relaysmtp done" ) ;
} , 20 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "relaysmtpexecuted" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "httpworm" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|httpworm() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into relaysmtp() command" ) ;
workerScript . scriptRef . log ( "Cannot httpworm(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . HTTPWormProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have HTTPWorm.exe program on home computer" ) ;
return ;
}
workerScript . scriptRef . log ( "Running HTTPWorm.exe on server " + server . hostname + " in 25 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( ! server . httpPortOpen ) {
workerScript . scriptRef . log ( "Executed HTTPWorm.exe virus on " + server . hostname + " to open HTTP port (25)" ) ;
server . httpPortOpen = true ;
++ server . openPortCount ;
} else {
workerScript . scriptRef . log ( "HTTP Port (80) already opened on " + server . hostname ) ;
}
resolve ( "httpworm done" ) ;
} , 25 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "HTTPWormexecuted" ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "sqlinject" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|sqlinject() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-11 06:20:17 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
2017-05-15 17:13:21 +02:00
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-11 06:20:17 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into sqlinject() command" ) ;
workerScript . scriptRef . log ( "Cannot sqlinject(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
if ( ! Player . hasProgram ( Programs . SQLInjectProgram ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Player does not have SQLInject.exe program on home computer" ) ;
return ;
}
workerScript . scriptRef . log ( "Running SQLInject.exe on server " + server . hostname + " in 30 seconds" ) ;
var p = new Promise ( function ( resolve , reject ) {
if ( env . stopFlag ) { reject ( workerScript ) ; }
setTimeout ( function ( ) {
if ( ! server . sqlPortOpen ) {
workerScript . scriptRef . log ( "Executed SQLInject.exe virus on " + server . hostname + " to open SQL port (1433)" ) ;
server . sqlPortOpen = true ;
++ server . openPortCount ;
} else {
workerScript . scriptRef . log ( "SQL Port (1433) already opened on " + server . hostname ) ;
}
resolve ( "sqlinject done" ) ;
} , 30 * 1000 ) ;
} ) ;
p . then ( function ( res ) {
resolve ( "sqlinjectexecuted" ) ;
} , function ( e ) {
2017-05-02 18:28:54 +02:00
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
2017-05-15 08:35:09 +02:00
} else if ( exp . func . value == "run" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|run() call has incorrect number of arguments. Takes 1 argument" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
var scriptNamePromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
scriptNamePromise . then ( function ( scriptname ) {
var serverIp = workerScript . serverIp ;
var scriptServer = AllServers [ serverIp ] ;
if ( scriptServer == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Could not find server. This is a bug in the game. Report to game dev" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
var runScriptPromise = runScriptFromScript ( scriptServer , scriptname , workerScript ) ;
runScriptPromise . then ( function ( res ) {
resolve ( res ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else if ( exp . func . value == "getHackingLevel" ) {
if ( exp . args . length != 0 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|getHackingLevel() call has incorrect number of arguments. Takes 0 arguments" ) ;
2017-05-15 17:28:59 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
setTimeout ( function ( ) {
2017-05-15 17:13:21 +02:00
Player . updateSkillLevels ( ) ;
workerScript . scriptRef . log ( "getHackingLevel() returned " + Player . hacking _skill ) ;
2017-05-15 08:35:09 +02:00
resolve ( Player . hacking _skill ) ;
} , CONSTANTS . CodeInstructionRunTime ) ;
} else if ( exp . func . value == "getServerMoneyAvailable" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|getServerMoneyAvailable() call has incorrect number of arguments. Takes 1 arguments" ) ;
2017-05-20 11:27:42 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
var ipPromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
ipPromise . then ( function ( ip ) {
setTimeout ( function ( ) {
var server = getServer ( ip ) ;
if ( server == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid IP or hostname passed into getServerMoneyAvailable() command" ) ;
workerScript . scriptRef . log ( "Cannot getServerMoneyAvailable(). Invalid IP or hostname passed in: " + ip ) ;
return ;
}
2017-05-15 17:13:21 +02:00
workerScript . scriptRef . log ( "getServerMoneyAvailable() returned " + server . moneyAvailable ) ;
2017-05-15 08:35:09 +02:00
resolve ( server . moneyAvailable ) ;
} , CONSTANTS . CodeInstructionRunTime ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
2017-05-20 11:27:42 +02:00
} else if ( exp . func . value == "purchaseHacknetNode" ) {
if ( exp . args . length != 0 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|purchaseHacknetNode() call has incorrect number of arguments. Takes 0 arguments" ) ;
return ;
}
setTimeout ( function ( ) {
var cost = getCostOfNextHacknetNode ( ) ;
if ( isNaN ( cost ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Could not calculate cost in purchaseHacknetNode(). This is a bug please report to game dev" ) ;
return ;
}
if ( cost > Player . money ) {
workerScript . scriptRef . log ( "Could not afford to purchase new Hacknet Node" ) ;
resolve ( - 1 ) ;
}
//Auto generate a name for the node for now...TODO
var numOwned = Player . hacknetNodes . length ;
var name = "hacknet-node-" + numOwned ;
var node = new HacknetNode ( name ) ;
node . updateMoneyGainRate ( ) ;
Player . loseMoney ( cost ) ;
Player . hacknetNodes . push ( node ) ;
workerScript . scriptRef . log ( "Purchased new Hacknet Node with name: " + name ) ;
resolve ( numOwned ) ;
} , CONSTANTS . CodeInstructionRunTime ) ;
} else if ( exp . func . value == "upgradeHacknetNode" ) {
if ( exp . args . length != 1 ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|upgradeHacknetNode() call has incorrect number of arguments. Takes 1 argument" ) ;
return ;
}
var namePromise = evaluate ( exp . args [ 0 ] , workerScript ) ;
namePromise . then ( function ( name ) {
var node = getHacknetNode ( name ) ;
if ( node == null ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Invalid Hacknet Node name passed into upgradeHacknetNode()" ) ;
return ;
}
var cost = node . calculateLevelUpgradeCost ( 1 ) ;
if ( isNaN ( cost ) ) {
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Could not calculate cost in upgradeHacknetNode(). This is a bug please report to game dev" ) ;
return ;
}
if ( cost > Player . money ) {
workerScript . scriptRef . log ( "Could not afford to upgrade Hacknet Node: " + name ) ;
resolve ( false ) ;
return ;
}
if ( node . level >= CONSTANTS . HacknetNodeMaxLevel ) {
workerScript . scriptRef . log ( "Hacknet Node " + name + " already at max level" ) ;
node . level = CONSTANTS . HacknetNodeMaxLevel ;
resolve ( false ) ;
return ;
}
Player . loseMoney ( cost ) ;
node . level += 1 ;
node . updateMoneyGainRate ( ) ;
workerScript . scriptRef . log ( "Hacknet node " + name + " upgraded to level " + node . level + "!" ) ;
resolve ( true ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
2017-05-02 18:28:54 +02:00
}
2016-11-30 00:07:24 +01:00
} , CONSTANTS . CodeInstructionRunTime ) ;
} ) ;
2017-05-15 16:15:59 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Unrecognized function call" ) ;
2016-11-17 23:25:40 +01:00
break ;
default :
2017-05-02 18:28:54 +02:00
reject ( "|" + workerScript . serverIp + "|" + workerScript . name + "|Can't evaluate type " + exp . type ) ;
2016-11-17 23:25:40 +01:00
}
}
2017-05-12 06:59:07 +02:00
//Returns true if any of the if statements evaluated, false otherwise. Therefore, the else statement
//should evaluate if this returns false
function evaluateIf ( exp , workerScript , i ) {
var env = workerScript . env ;
return new Promise ( function ( resolve , reject ) {
if ( i >= exp . cond . length ) {
//Catch out of bounds errors
resolve ( false ) ;
} else {
console . log ( "Evaluating cond " + i + " in if" ) ;
var cond = evaluate ( exp . cond [ i ] , workerScript ) ;
cond . then ( function ( condRes ) {
console . log ( "cond evaluated to: " + condRes ) ;
if ( condRes ) {
console . log ( "Evaluating then: " + exp . then [ i ] ) ;
var evalThen = evaluate ( exp . then [ i ] , workerScript ) ;
evalThen . then ( function ( res ) {
console . log ( "If statement done" ) ;
resolve ( true ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
} else {
//If this if statement isnt true, go on the next elif, or recursively resolve
if ( i == exp . cond . length - 1 ) {
resolve ( false ) ;
} else {
var recursiveCall = evaluateIf ( exp , workerScript , i + 1 ) ;
recursiveCall . then ( function ( res ) {
resolve ( res ) ;
} , function ( e ) {
reject ( e ) ;
} ) ;
}
}
} , function ( e ) {
reject ( e ) ;
} ) ;
}
} ) ;
}
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
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 ) {
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 for loop code
var pCode = new Promise ( function ( resolve , reject ) {
setTimeout ( function ( ) {
var evaluatePromise = evaluate ( exp . code , workerScript ) ;
evaluatePromise . then ( function ( resCode ) {
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 ) {
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 {
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
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 ) {
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 ) {
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 {
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
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 ) {
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 ) ;
2017-05-15 08:35:09 +02:00
}
//Run a script from inside a script using run() command
function runScriptFromScript ( server , scriptname , workerScript ) {
return new Promise ( function ( resolve , reject ) {
2017-05-15 17:13:21 +02:00
var env = workerScript . env ;
if ( env . stopFlag ) { reject ( workerScript ) ; }
2017-05-15 08:35:09 +02:00
setTimeout ( function ( ) {
//Check if the script is already running
for ( var i = 0 ; i < server . runningScripts . length ; ++ i ) {
if ( server . runningScripts [ i ] == scriptname ) {
workerScript . scriptRef . log ( scriptname + " is already running on " + server . hostname ) ;
resolve ( false ) ;
2017-05-15 17:13:21 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
}
//Check if the script exists and if it does run it
for ( var i = 0 ; i < server . scripts . length ; ++ i ) {
if ( server . scripts [ i ] . filename == scriptname ) {
//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 ) {
workerScript . scriptRef . log ( "Cannot run script " + scriptname + " because you do not have root access!" ) ;
resolve ( false ) ;
return ;
} else if ( ramUsage > ramAvailable ) {
workerScript . scriptRef . log ( "Cannot run script " + scriptname + " because there is not enough available RAM!" ) ;
resolve ( false ) ;
return ;
} else {
//Able to run script
workerScript . scriptRef . log ( "Running script: " + scriptname + ". May take a few seconds to start up..." ) ;
var script = server . scripts [ i ] ;
server . runningScripts . push ( script . filename ) ; //Push onto runningScripts
addWorkerScript ( script , server ) ;
resolve ( true ) ;
2017-05-15 17:13:21 +02:00
return ;
2017-05-15 08:35:09 +02:00
}
}
}
workerScript . scriptRef . log ( "Could not find script " + scriptname + " on " + server . hostname ) ;
resolve ( false ) ;
} , CONSTANTS . CodeInstructionRunTime ) ;
} ) ;
}
2016-12-14 00:52:32 +01:00
2017-05-01 07:39:48 +02:00
function isScriptErrorMessage ( msg ) {
splitMsg = msg . split ( "|" ) ;
if ( splitMsg . length != 4 ) {
return false ;
}
var ip = splitMsg [ 1 ] ;
if ( ! isValidIPAddress ( ip ) ) {
return false ;
}
return true ;
}
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 ;
2017-05-05 03:08:44 +02:00
var skillMult = ( 2 * Player . hacking _chance _mult * Player . hacking _skill ) ;
2016-12-14 00:52:32 +01:00
var skillChance = ( skillMult - server . requiredHackingSkill ) / skillMult ;
2017-04-17 14:26:54 +02:00
var chance = skillChance * difficultyMult ;
if ( chance < 0 ) { return 0 ; }
else { return chance ; }
2016-12-14 00:52:32 +01:00
}
//The same as Player's calculateHackingTime() function but takes in the server as an argument
function scriptCalculateHackingTime ( server ) {
var difficultyMult = server . requiredHackingSkill * server . hackDifficulty ;
2017-05-03 06:38:58 +02:00
var skillFactor = ( 2.5 * difficultyMult + 500 ) / ( Player . hacking _skill + 50 ) ;
2017-05-05 03:08:44 +02:00
var hackingTime = skillFactor * Player . hacking _speed _mult * 5 ; //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 ) {
2017-05-05 17:50:55 +02:00
return ( server . hackDifficulty * Player . hacking _exp _mult ) ;
2016-12-14 00:52:32 +01:00
}
//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 ;
2017-05-05 03:08:44 +02:00
var percentMoneyHacked = difficultyMult * skillMult * Player . hacking _money _mult / 1000 ;
2017-04-18 06:32:17 +02:00
if ( percentMoneyHacked < 0 ) { return 0 ; }
if ( percentMoneyHacked > 1 ) { return 1 ; }
2016-12-14 00:52:32 +01:00
return percentMoneyHacked ;
2017-05-15 08:35:09 +02:00
}