Make scripts work better on fresh start

This commit is contained in:
Isaac Shoebottom 2023-11-03 20:17:24 -03:00
parent 5d6063917b
commit 74aae19a82
2 changed files with 14 additions and 2 deletions

View File

@ -21,6 +21,12 @@ export async function main(ns: NS) {
let timeout: number = <number> ns.args[0]
let nodes = ns.hacknet.numNodes()
// If there are no nodes, buy one
if (nodes === 0) {
ns.hacknet.purchaseNode()
nodes = 1
}
let costs: { type: Type, cost: number }[] = []
while (true) {
costs = []

View File

@ -75,7 +75,9 @@ export function rootServer(ns: NS, server: string) {
ns.sqlinject(server)
counter++
}
ns.nuke(server)
if (ns.getServer(server).openPortCount <= counter) {
ns.nuke(server)
}
return counter
}
@ -88,17 +90,21 @@ export function rootServer(ns: NS, server: string) {
* @returns The result of the function if it is performed or true if the function does not return anything, otherwise false
*/
export function performFunctionIfCapable(ns: NS, server: string, func: CallableFunction, args: any[]) {
ns.print(`Performing function on ${server}`)
if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) {
ns.print(`Not enough hacking level to hack ${server}`)
return false
}
if (ns.getServerNumPortsRequired(server) < ns.getServer(server).openPortCount) {
ns.print(`Not enough ports, trying to root ${server}`)
if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) {
ns.print(`Need more port opening programs to root ${server}`)
return false
}
}
if (!ns.hasRootAccess(server)) {
ns.print(`Failed to root ${server}`)
return false
}
let result = func(...args)
if (result === undefined) {