Bitburner-Scripts/servers/home/utils.ts

184 lines
5.5 KiB
TypeScript
Raw Normal View History

2023-11-03 18:42:37 -03:00
// noinspection JSUnusedGlobalSymbols
/**
* Recursively scans all servers connected to the current server, excluding home
* @param ns global NS object
* @returns A list of all servers connected to the current server
*/
export function recursiveScan(ns: NS) {
// Starting case
2023-11-03 21:57:09 -03:00
let servers = ns.scan("home")
2023-11-03 18:42:37 -03:00
// Add all servers to the list
let allServers: string[] = []
while (servers.length > 0) {
2023-11-13 13:19:20 -04:00
let server = servers.pop()
2023-11-03 18:42:37 -03:00
if (server) {
let newServers = ns.scan(server)
2023-11-13 13:19:20 -04:00
for (const newServer of newServers) {
2023-11-03 18:42:37 -03:00
if (!allServers.includes(newServer)) {
allServers.push(newServer)
servers.push(newServer)
}
}
}
}
// Remove the home server from the list
2023-11-13 13:19:20 -04:00
return allServers.filter(server => server !== "home").sort()
2023-11-03 18:42:37 -03:00
}
2023-11-03 21:53:07 -03:00
/**
* 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))
}
2023-11-06 19:58:40 -04:00
// Sort and remove duplicates, need custom sort because built-in sort only works alphabetically 💀
return [...new Set(levels)].sort((a, b) => a - b)
2023-11-03 21:53:07 -03:00
}
2023-11-03 18:42:37 -03:00
/**
* Removes files passed in from all servers
* @param ns Global NS object
* @param files The files to remove
* @returns void
* @example Removes all files from the folder "no-ports" from all servers.
* removeFilesOnAllServers(ns, ns.ls("home", "no-ports"));
* @note ns.ls() returns the full path of every file, so if the file is not in the same place in every server,
* you will need to modify each file path to match the server you are removing it from.
*/
export function removeFilesOnAllServers(ns: NS, files: string[]) {
let hosts = recursiveScan(ns)
for (const host of hosts) {
for (const file of files) {
ns.rm(file, host)
}
}
}
/**
* Tries to gain root access to a server
* @param ns Global NS object
* @param server The server to gain root access to
* @returns The number of programs used to gain root access
*/
export function rootServer(ns: NS, server: string) {
let counter = 0
2023-11-03 21:57:09 -03:00
if (ns.fileExists("BruteSSH.exe", "home")) {
2023-11-03 18:42:37 -03:00
ns.brutessh(server)
counter++
}
2023-11-03 21:57:09 -03:00
if (ns.fileExists("FTPCrack.exe", "home")) {
2023-11-03 18:42:37 -03:00
ns.ftpcrack(server)
counter++
}
2023-11-03 21:57:09 -03:00
if (ns.fileExists("SMTPCrack.exe", "home")) {
2023-11-03 18:42:37 -03:00
ns.relaysmtp(server)
counter++
}
2023-11-03 21:57:09 -03:00
if (ns.fileExists("HTTPWorm.exe", "home")) {
2023-11-03 18:42:37 -03:00
ns.httpworm(server)
counter++
}
2023-11-03 21:57:09 -03:00
if (ns.fileExists("SQLInject.exe", "home")) {
2023-11-03 18:42:37 -03:00
ns.sqlinject(server)
counter++
}
2023-11-03 22:21:54 -03:00
if (counter >= ns.getServerNumPortsRequired(server)) {
ns.nuke(server)
}
2023-11-03 18:42:37 -03:00
return counter
}
/**
* Performs a function on a server if the player is capable of doing so, otherwise returns false
* @param ns Global NS object
* @param server The server to perform the function on
* @param func The function to perform
* @param args The arguments to pass to the function
* @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}`)
2023-11-03 18:42:37 -03:00
if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) {
ns.print(`Not enough hacking level to hack ${server}`)
2023-11-03 18:42:37 -03:00
return false
}
2023-11-06 17:39:47 -04:00
if (ns.getServer(server).openPortCount <= ns.getServerNumPortsRequired(server)) {
ns.print(`Not enough ports, trying to root ${server}`)
2023-11-03 22:17:05 -03:00
if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) {
ns.print(`Failed to root ${server}`)
return false
}
2023-11-03 18:42:37 -03:00
}
let result = func(...args)
if (result === undefined) {
return true
} else {
return result
}
}
/**
* Executes a script on a server from another server
* @param ns Global NS object
* @param server The server to execute the script on
* @param script The file path of the script to execute
* @param threads The number of threads to use
* @param args The arguments to pass to the script
*/
export function executeScriptOnServerFromAnother(ns: NS, server: string, script: string, threads: number = 1, args: any[]) {
ns.scp(script, server)
performFunctionIfCapable(ns, server, ns.exec, [script, server, threads, ...args])
ns.atExit(() => {
// Remove the script from the server when the program exits
// ns.rm(script, server)
2023-11-03 18:42:37 -03:00
})
}
/**
* Calculates the money per second the player is making
* @param ns Global NS object
* @param time=5 The number of seconds to calculate the MPS over
*/
export async function calculateMPS(ns: NS, time: number = 5) {
2023-11-03 21:57:09 -03:00
let start = ns.getServerMoneyAvailable("home")
2023-11-03 18:42:37 -03:00
let data: number[] = []
for (let i = 0; i < time; i++) {
await ns.sleep(1000)
2023-11-03 21:57:09 -03:00
data.push(ns.getServerMoneyAvailable("home") - start)
2023-11-03 18:42:37 -03:00
}
return data.reduce((a, b) => a + b, 0) / time
}
/**
* Type that represents a program and whether it exists on the home server
*/
export type ProgramState = {
program: string
exists: boolean
}
/**
* Checks if any of the programs passed in now exist on the home server, and updates the exists property of the program
* @param ns Global NS object
* @param programs The programs to check
* @returns true if any of the programs now exist, otherwise false
*/
export function checkForNewPrograms(ns: NS, programs: ProgramState[]) {
let result = false
for (const program of programs) {
if (program.exists == false) {
program.exists = ns.fileExists(program.program)
if (program.exists) {
result = true
}
}
}
return result
}