watcher improvements

This commit is contained in:
Isaac Shoebottom 2023-11-03 21:53:07 -03:00
parent 677f23ea8d
commit a3bdb2ccd1
2 changed files with 25 additions and 6 deletions

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)
} }