(feat) optional threads argument to netscript functions hack, weaken, grow

This commit is contained in:
Mason Dechaineux 2019-05-03 18:01:43 +10:00 committed by danielyxie
parent 585e1ac7aa
commit 44c26165f4
3 changed files with 32 additions and 11 deletions

@ -155,6 +155,21 @@ export function makeRuntimeRejectMsg(workerScript, msg, exp=null) {
return "|"+workerScript.serverIp+"|"+workerScript.name+"|" + msg + lineNum;
}
export function resolveNetscriptRequestedThreads(workerScript, functionName, requestedThreads) {
const threads = workerScript.scriptRef.threads;
if (!requestedThreads) {
return isNaN(threads) || threads < 1 ? 1 : threads;
}
const requestedThreadsAsInt = requestedThreads|0;
if (isNaN(requestedThreads) || requestedThreadsAsInt < 1) {
throw makeRuntimeRejectMsg(workerScript, `Invalid thread count passed to ${functionName}: ${requestedThreads}. Threads must be a positive number.`);
}
if (requestedThreads > threads) {
throw makeRuntimeRejectMsg(workerScript, `Too many threads requested by ${functionName}. Requested: ${requestedThreads}. Has: ${threads}.`);
}
return requestedThreadsAsInt;
}
//Run a script from inside a script using run() command
export function runScriptFromScript(server, scriptname, args, workerScript, threads=1) {
//Check if the script is already running

@ -113,7 +113,8 @@ import {
import {
makeRuntimeRejectMsg,
netscriptDelay,
runScriptFromScript
resolveNetscriptRequestedThreads,
runScriptFromScript,
} from "./NetscriptEvaluator";
import { NetscriptPort } from "./NetscriptPort";
import { SleeveTaskType } from "./PersonObjects/Sleeve/SleeveTaskTypesEnum";
@ -467,7 +468,7 @@ function NetscriptFunctions(workerScript) {
}
return out;
},
hack : function(ip){
hack : function(ip, { threads: requestedThreads } = {}){
if (workerScript.checkingRam) {
return updateStaticRam("hack", CONSTANTS.ScriptHackRamCost);
}
@ -475,8 +476,7 @@ function NetscriptFunctions(workerScript) {
if (ip === undefined) {
throw makeRuntimeRejectMsg(workerScript, "Hack() call has incorrect number of arguments. Takes 1 argument");
}
var threads = workerScript.scriptRef.threads;
if (isNaN(threads) || threads < 1) {threads = 1;}
var threads = resolveNetscriptRequestedThreads(workerScript, "hack", requestedThreads);
var server = getServer(ip);
if (server == null) {
workerScript.scriptRef.log("hack() error. Invalid IP or hostname passed in: " + ip + ". Stopping...");
@ -597,13 +597,12 @@ function NetscriptFunctions(workerScript) {
return Promise.resolve(true);
});
},
grow : function(ip){
grow : function(ip, { threads: requestedThreads } = {}){
if (workerScript.checkingRam) {
return updateStaticRam("grow", CONSTANTS.ScriptGrowRamCost);
}
updateDynamicRam("grow", CONSTANTS.ScriptGrowRamCost);
var threads = workerScript.scriptRef.threads;
if (isNaN(threads) || threads < 1) {threads = 1;}
var threads = resolveNetscriptRequestedThreads(workerScript, "grow", requestedThreads);
if (ip === undefined) {
throw makeRuntimeRejectMsg(workerScript, "grow() call has incorrect number of arguments. Takes 1 argument");
}
@ -659,13 +658,12 @@ function NetscriptFunctions(workerScript) {
return numCycleForGrowth(server, Number(growth), Player);
},
weaken : function(ip) {
weaken : function(ip, { threads: requestedThreads } = {}) {
if (workerScript.checkingRam) {
return updateStaticRam("weaken", CONSTANTS.ScriptWeakenRamCost);
}
updateDynamicRam("weaken", CONSTANTS.ScriptWeakenRamCost);
var threads = workerScript.scriptRef.threads;
if (isNaN(threads) || threads < 1) {threads = 1;}
var threads = resolveNetscriptRequestedThreads(workerScript, "weaken", requestedThreads)
if (ip === undefined) {
throw makeRuntimeRejectMsg(workerScript, "weaken() call has incorrect number of arguments. Takes 1 argument");
}

@ -219,9 +219,17 @@ function startNetscript1Script(workerScript) {
name === "prompt" || name === "run" || name === "exec") {
let tempWrapper = function() {
let fnArgs = [];
//All of the Object/array elements are in JSInterpreter format, so
//we have to convert them back to native format to pass them to these fns
for (let i = 0; i < arguments.length-1; ++i) {
fnArgs.push(arguments[i]);
if (typeof arguments[i] === 'object' || arguments[i].constructor === Array) {
fnArgs.push(int.pseudoToNative(arguments[i]));
} else {
fnArgs.push(arguments[i]);
}
}
console.log(fnArgs);
let cb = arguments[arguments.length-1];
let fnPromise = entry.apply(null, fnArgs);
fnPromise.then(function(res) {