Make scripts work better on fresh start
This commit is contained in:
parent
5d6063917b
commit
74aae19a82
@ -21,6 +21,12 @@ export async function main(ns: NS) {
|
|||||||
let timeout: number = <number> ns.args[0]
|
let timeout: number = <number> ns.args[0]
|
||||||
|
|
||||||
let nodes = ns.hacknet.numNodes()
|
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 }[] = []
|
let costs: { type: Type, cost: number }[] = []
|
||||||
while (true) {
|
while (true) {
|
||||||
costs = []
|
costs = []
|
||||||
|
@ -75,7 +75,9 @@ export function rootServer(ns: NS, server: string) {
|
|||||||
ns.sqlinject(server)
|
ns.sqlinject(server)
|
||||||
counter++
|
counter++
|
||||||
}
|
}
|
||||||
ns.nuke(server)
|
if (ns.getServer(server).openPortCount <= counter) {
|
||||||
|
ns.nuke(server)
|
||||||
|
}
|
||||||
return counter
|
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
|
* @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[]) {
|
export function performFunctionIfCapable(ns: NS, server: string, func: CallableFunction, args: any[]) {
|
||||||
|
ns.print(`Performing function on ${server}`)
|
||||||
if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) {
|
if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) {
|
||||||
|
ns.print(`Not enough hacking level to hack ${server}`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if (ns.getServerNumPortsRequired(server) < ns.getServer(server).openPortCount) {
|
if (ns.getServerNumPortsRequired(server) < ns.getServer(server).openPortCount) {
|
||||||
|
ns.print(`Not enough ports, trying to root ${server}`)
|
||||||
if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) {
|
if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) {
|
||||||
|
ns.print(`Need more port opening programs to root ${server}`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!ns.hasRootAccess(server)) {
|
if (!ns.hasRootAccess(server)) {
|
||||||
|
ns.print(`Failed to root ${server}`)
|
||||||
return false
|
return false
|
||||||
|
|
||||||
}
|
}
|
||||||
let result = func(...args)
|
let result = func(...args)
|
||||||
if (result === undefined) {
|
if (result === undefined) {
|
||||||
|
Loading…
Reference in New Issue
Block a user