Add hack all servers function

This commit is contained in:
Isaac Shoebottom 2023-11-03 15:07:07 -03:00
parent abdcc8d682
commit f34a37a3fb
5 changed files with 33 additions and 18 deletions

15
servers/home/hack.ts Normal file
View File

@ -0,0 +1,15 @@
import {NS} from "NetscriptDefinitions";
export async function main(ns: NS) {
const server: string = <string>ns.args[0];
const moneyThresh: number = <number>ns.args[1];
const securityThresh: number = <number>ns.args[2];
while (true) {
if (ns.getServerSecurityLevel(server) > securityThresh) {
await ns.weaken(server);
} else if (ns.getServerMoneyAvailable(server) < moneyThresh) {
await ns.grow(server);
} else {
await ns.hack(server);
}
}
}

View File

@ -0,0 +1,14 @@
import {NS} from "NetscriptDefinitions";
import {recursiveScan} from "./utils";
import {executeScriptOnServerFromAnother} from "./utils";
export async function main(ns: NS) {
let servers: string[] = recursiveScan(ns);
for (const server of servers) {
let moneyThresh = ns.getServerMaxMoney(server) * 0.75;
let securityThresh = ns.getServerMinSecurityLevel(server) + 5;
let numThreads = ns.getServerMaxRam(server) / ns.getScriptRam("hack.js")
executeScriptOnServerFromAnother(ns, server, "hack.js", numThreads, [server, moneyThresh, securityThresh])
}
}

View File

@ -1,15 +0,0 @@
/** @param {NS} ns */
export async function main(ns) {
var target = ns.args[0]
var moneyThresh = ns.args[1]
var securityThresh = ns.args[2]
while (true) {
if (ns.getServerSecurityLevel(target) > securityThresh) {
await ns.weaken(target);
} else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
await ns.grow(target);
} else {
await ns.hack(target);
}
}
}

View File

@ -2,5 +2,5 @@ import {NS} from "NetscriptDefinitions";
import {executeScriptOnServerFromAnother} from "./utils";
export async function main(ns: NS) {
executeScriptOnServerFromAnother(ns, "home", "servers/home/ramcost.ts", [])
executeScriptOnServerFromAnother(ns, "home", "servers/home/ramcost.ts", 1, [])
}

View File

@ -120,11 +120,12 @@ export function performFunctionIfCapable(ns: NS, server: string, func: CallableF
* @param ns Global NS object
* @param server The server to execute the script on
* @param script The file path of the script to execute
* @param threads The number of threads to use
* @param args The arguments to pass to the script
*/
export function executeScriptOnServerFromAnother(ns: NS, server: string, script: string, args: any[]) {
export function executeScriptOnServerFromAnother(ns: NS, server: string, script: string, threads: number = 1, args: any[]) {
ns.scp(script, server);
performFunctionIfCapable(ns, server, ns.exec, [script, server, ...args])
performFunctionIfCapable(ns, server, ns.exec, [script, server, threads, ...args])
ns.atExit(
() => {
ns.rm(script, server);