Add hack all servers function
This commit is contained in:
parent
abdcc8d682
commit
f34a37a3fb
15
servers/home/hack.ts
Normal file
15
servers/home/hack.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
servers/home/hackallservers.ts
Normal file
14
servers/home/hackallservers.ts
Normal 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])
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,5 +2,5 @@ import {NS} from "NetscriptDefinitions";
|
|||||||
import {executeScriptOnServerFromAnother} from "./utils";
|
import {executeScriptOnServerFromAnother} from "./utils";
|
||||||
|
|
||||||
export async function main(ns: NS) {
|
export async function main(ns: NS) {
|
||||||
executeScriptOnServerFromAnother(ns, "home", "servers/home/ramcost.ts", [])
|
executeScriptOnServerFromAnother(ns, "home", "servers/home/ramcost.ts", 1, [])
|
||||||
}
|
}
|
@ -120,11 +120,12 @@ export function performFunctionIfCapable(ns: NS, server: string, func: CallableF
|
|||||||
* @param ns Global NS object
|
* @param ns Global NS object
|
||||||
* @param server The server to execute the script on
|
* @param server The server to execute the script on
|
||||||
* @param script The file path of the script to execute
|
* @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
|
* @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);
|
ns.scp(script, server);
|
||||||
performFunctionIfCapable(ns, server, ns.exec, [script, server, ...args])
|
performFunctionIfCapable(ns, server, ns.exec, [script, server, threads, ...args])
|
||||||
ns.atExit(
|
ns.atExit(
|
||||||
() => {
|
() => {
|
||||||
ns.rm(script, server);
|
ns.rm(script, server);
|
||||||
|
Loading…
Reference in New Issue
Block a user