Add no-ports code

This commit is contained in:
Isaac Shoebottom 2023-11-02 11:04:03 -03:00
parent 802e69ac91
commit 8b3749d3de
9 changed files with 184 additions and 0 deletions

13
no-ports/auto-upgrade.js Normal file
View File

@ -0,0 +1,13 @@
/** @param {NS} ns */
export async function main(ns) {
let level = ns.getHackingLevel()
while (level < 999) {
let newLevel = ns.getHackingLevel()
if (newLevel !== level) {
ns.run("run.js")
}
level = newLevel
await ns.sleep(10 * 1000) // 10 seconds
}
}

4
no-ports/hack-all.js Normal file
View File

@ -0,0 +1,4 @@
/** @param {NS} ns */
export async function main(ns) {
ns.run("recursive-run.js", 1, "home", "hack-args.js")
}

15
no-ports/hack-args.js Normal file
View File

@ -0,0 +1,15 @@
/** @param {NS} ns */
export async function main(ns) {
let hostname = ns.args[0]
let cost = ns.args[1]
let moneyThresh = ns.getServerMaxMoney(hostname) * 0.75;
let securityThresh = ns.getServerMinSecurityLevel(hostname) + 5;
let numThreads = ns.getServerMaxRam(hostname) / cost
numThreads = Math.floor(numThreads)
if (numThreads < 1 || numThreads == Infinity) {
numThreads = 1
}
ns.spawn("hack.js", numThreads, hostname, moneyThresh, securityThresh)
}

15
no-ports/hack.js Normal file
View File

@ -0,0 +1,15 @@
/** @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);
}
}
}

View File

@ -0,0 +1,33 @@
/** @param {NS} ns */
export async function main(ns) {
let parent
if (ns.args[0] == undefined) {
parent = "home"
} else {
parent = ns.args[0]
}
let files = [
"hack-args.js",
"hack.js",
"root.js",
"recursive-copy.js",
"recursive-kill.js",
"recursive-run.js"
]
let script = "recursive-copy.js"
let servers = ns.scan()
for (const server of servers) {
if (server == parent) {
continue
}
if (server == "home") {
continue
}
//ns.tprint("Copied to: :", server)
ns.scp(files, server, "home")
ns.exec(script, server, 1, ns.getHostname()) // script, dest, thread count, arg1
}
}

View File

@ -0,0 +1,26 @@
/** @param {NS} ns */
export async function main(ns) {
let parent
if (ns.args[0] == undefined) {
parent = "home"
} else {
parent = ns.args[0]
}
let script = "recursive-kill.js"
let servers = ns.scan()
for (const server of servers) {
if (server == parent) {
continue
}
if (!ns.hasRootAccess(server)) {
ns.exec("root.js", ns.getHostname(), 1, server)
}
//ns.tprint("Killed all scripts on: ", server)
ns.killall(server, true)
ns.exec(script, server, 1, ns.getHostname())
}
}

30
no-ports/recursive-run.js Normal file
View File

@ -0,0 +1,30 @@
/** @param {NS} ns */
export async function main(ns) {
let parent = ns.args[0]
let script = ns.args[1]
let selfScript = "recursive-run.js"
let servers = ns.scan()
//ns.tprint("INFO Scan of ", ns.getHostname(), ": ", servers)
for (const server of servers) {
if (server === parent) {
continue;
}
ns.exec(selfScript, server, 1, ns.getHostname(), script)
await ns.sleep(50)
let procs = ns.ps(server)
if (procs.length > 0) {
//ns.tprint("INFO Ram in use on ", server, ": ", ns.getServerUsedRam(server))
let ram = ns.getServerMaxRam(server) - ns.getServerUsedRam(server)
//ns.tprint("INFO Available ram on ", server, ": ", ram)
//ns.tprint("INFO Filename of running script: ", procs[0].filename)
}
if (ns.getServerRequiredHackingLevel(server) < ns.getHackingLevel()) {
// after thread args, first arg is the server it us running on, and then the cost of the hack script
ns.tprint("RUN Started hacking server: ", server)
ns.exec(script, server, 1, server, ns.getScriptRam("hack.js", server))
}
}
}

9
no-ports/root.js Normal file
View File

@ -0,0 +1,9 @@
/** @param {NS} ns */
export async function main(ns) {
let server = ns.args[0]
if (ns.getServerNumPortsRequired(server) <= 2) {
ns.brutessh(server)
ns.ftpcrack(server)
ns.nuke(server)
}
}

39
no-ports/run.js Normal file
View File

@ -0,0 +1,39 @@
/** @param {NS} ns */
export async function main(ns) {
ns.run("recursive-kill.js")
await ns.sleep(1000)
ns.run("recursive-copy.js")
await ns.sleep(1000)
//home is first arg for root, hack-args is the script to run
ns.run("recursive-run.js", 1, "root-server", "hack-args.js")
//problem is that the scanning takes place on the same sever instead of the server that is the parent
//need last child of each node to spawn the process on parent, as spawning on on children always needs extra ram
//
//root scans nodes, saves to list
//the last execution of the exec on the last child passes in a flag to spawn on parent
//wait for the parent to finish using sleep (maybe find better way to syncronize)
//start hack-args function on parent
//
//each child becomes the "root" and repeats
// SCRATCH ALLAT
// its pretty much unsolveable, either way you go up or down the node tree, you end up with the problem
// not knowing if a server will have enough ram to run the hack args script, because the end node
// becomes circular. the only way to solve the problem would be to be able to have enough ram to call
// spawn after the for loop after recursive-run is called for all children in the for loop.
// so for now just will have to run a manual command for each sever with less than 8gb of ram
// a more elegant but more set in stone solution might be to just collect every unique hostname
// (print to console, but don't include parent, and exclude duplicates)
// and then just run an exec on each one in a for loop
//hack to get n00dles working, since it has low ram
await ns.sleep(1000)
ns.exec("hack-args.js", "n00dles", 1, "n00dles", 2.20)
// start auto upgrade if not running
if (!ns.isRunning("auto-upgrade.js")) {
ns.run("auto-upgrade.js")
}
}