Compare commits

..

2 Commits

Author SHA1 Message Date
92ad2da7b7 add program 2023-11-03 21:53:18 -03:00
a3bdb2ccd1 watcher improvements 2023-11-03 21:53:07 -03:00
3 changed files with 32 additions and 7 deletions

View File

@ -1,10 +1,13 @@
import { recursiveScan } from "./utils" import { recursiveHackingRequired, recursiveScan } from "./utils"
export async function main(ns: NS){ export async function main(ns: NS){
switch (ns.args[0]) { switch (ns.args[0]) {
case "getServers": case "getServers":
getServers(ns); getServers(ns);
break; break;
case "getHackingLevels":
getHackingLevels(ns);
break;
default: default:
ns.tprint("Invalid program name"); ns.tprint("Invalid program name");
break; break;
@ -13,3 +16,6 @@ export async function main(ns: NS){
function getServers(ns: NS) { function getServers(ns: NS) {
ns.tprint(recursiveScan(ns)); ns.tprint(recursiveScan(ns));
} }
function getHackingLevels(ns: NS) {
ns.tprint(recursiveHackingRequired(ns))
}

View File

@ -28,6 +28,21 @@ export function recursiveScan(ns: NS) {
return allServers return allServers
} }
/**
* Recursively scans all servers connected to the current server for the set of all levels that hackable servers require
* @param ns
* @returns A set of every hacking level required to hack a new server
*/
export function recursiveHackingRequired(ns: NS) {
let servers = recursiveScan(ns)
let levels: number[] = []
for (let server of servers) {
levels.push(ns.getServerRequiredHackingLevel(server))
}
// remove duplicates
return [...new Set(levels)]
}
/** /**
* Removes files passed in from all servers * Removes files passed in from all servers
* @param ns Global NS object * @param ns Global NS object

View File

@ -1,16 +1,20 @@
import { recursiveHackingRequired } from "./utils"
export async function main(ns: NS) { export async function main(ns: NS) {
ns.run('hackallservers.js') ns.run('hackallservers.js')
let hackingLevel = ns.getHackingLevel() let levels = recursiveHackingRequired(ns)
while (hackingLevel < 9999) { let hackingLevel: number
let oldHackingLevel = hackingLevel do {
hackingLevel = ns.getHackingLevel() hackingLevel = ns.getHackingLevel()
if (oldHackingLevel !== hackingLevel) { if (levels.includes(hackingLevel)) {
ns.tprint(`Hacking level increased from ${oldHackingLevel} to ${hackingLevel}`) // remove the level from the list, so we don't try to hack it again
levels.splice(levels.indexOf(hackingLevel), 1)
ns.tprint(`Hacking level increased to ${hackingLevel}`)
ns.run('killall.js') ns.run('killall.js')
await ns.sleep(1000) // 1 second await ns.sleep(1000) // 1 second
ns.run('hackallservers.js') ns.run('hackallservers.js')
} }
// Wait 1 second before checking again // Wait 1 second before checking again
await ns.sleep(1000) await ns.sleep(1000)
} } while (hackingLevel < 9999)
} }