Compare commits

...

36 Commits

Author SHA1 Message Date
b99827a84b Fix root server function, file name was wrong 2023-11-14 22:33:38 -04:00
62b1d6edfd Add remove files program 2023-11-13 14:27:28 -04:00
78d8153f8e Improve comments 2023-11-13 14:16:29 -04:00
461775a92f Add comments 2023-11-13 14:15:01 -04:00
70ec4d9463 Update state for first run 2023-11-13 14:13:14 -04:00
1d3ce073f6 Minor code refactoring 2023-11-13 14:11:46 -04:00
9bacf4f97f Add check for if new programs have begun to exist 2023-11-13 14:08:16 -04:00
9b4042af40 Move filtering to after logging 2023-11-13 13:38:39 -04:00
e6aca2e998 Improve logging 2023-11-13 13:36:25 -04:00
df2d550eb6 Filter instead of remove 2023-11-13 13:32:57 -04:00
c320b31ac3 Fix case where you jump too many levels at once 2023-11-13 13:31:52 -04:00
f0d4017711 Add potential guard clause 2023-11-13 13:26:43 -04:00
2ca91f574f Change filtering logic 2023-11-13 13:19:20 -04:00
7de44a3c2f Remove unneeded code and enable sorting on server list 2023-11-13 12:58:51 -04:00
afa44d0ea4 Don't sort recursive scan 2023-11-07 15:53:52 -04:00
83ba1af0cc Refactor hacknet logic 2023-11-06 20:20:36 -04:00
fad1c1a7c4 Fix number sort 2023-11-06 19:58:40 -04:00
ab6635d00e Sort arrays 2023-11-06 19:48:27 -04:00
116daf3781 Small fix for starting conditions 2023-11-06 17:39:47 -04:00
1af46c5d6f Fix hack all bug 2023-11-04 17:14:34 -03:00
f52af72140 Improve watcher slightly 2023-11-04 16:48:17 -03:00
12d9165162 Improve watcher 2023-11-03 22:55:07 -03:00
5febd7ed3b Fix logic error 2023-11-03 22:21:54 -03:00
8dff0b71d6 Fix wrong function call 2023-11-03 22:20:50 -03:00
f7fa595d71 Remove redundant check 2023-11-03 22:17:40 -03:00
6e1b576bcf Fix logic bugs 2023-11-03 22:17:05 -03:00
6c6f8b0b70 Formatting changes 2023-11-03 21:57:09 -03:00
90fef7e53a Move call outside the scope of watcher 2023-11-03 21:56:55 -03:00
92ad2da7b7 add program 2023-11-03 21:53:18 -03:00
a3bdb2ccd1 watcher improvements 2023-11-03 21:53:07 -03:00
677f23ea8d Add script that will contain user created programs 2023-11-03 21:38:19 -03:00
29d88170ab Fix logic bug 2023-11-03 21:38:00 -03:00
74aae19a82 Make scripts work better on fresh start 2023-11-03 20:17:24 -03:00
5d6063917b Servers had no money lol 2023-11-03 19:11:52 -03:00
22ddbbde5d Reformat files 2023-11-03 18:42:37 -03:00
683daff911 Add project coding guidelines 2023-11-03 18:41:51 -03:00
11 changed files with 632 additions and 308 deletions

10
.editorconfig Normal file
View File

@ -0,0 +1,10 @@
# All files
[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 2
# Markup files
[*.{json,yml}]
indent_style = space

215
.gitattributes vendored Normal file
View File

@ -0,0 +1,215 @@
## GITATTRIBUTES FOR WEB PROJECTS
#
# These settings are for any web project.
#
# Details per file setting:
# text These files should be normalized (i.e. convert CRLF to LF).
# binary These files are binary and should be left untouched.
#
# Note that binary is a macro for -text -diff.
######################################################################
# Auto detect
## Handle line endings automatically for files detected as
## text and leave all files detected as binary untouched.
## This will handle all files NOT defined below.
* text=auto
# Source code
*.bash text eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
*.coffee text
*.css text diff=css
*.htm text diff=html
*.html text diff=html
*.inc text
*.ini text
*.js text
*.json text
*.jsx text
*.less text
*.ls text
*.map text -diff
*.od text
*.onlydata text
*.php text diff=php
*.pl text
*.ps1 text eol=crlf
*.py text diff=python
*.rb text diff=ruby
*.sass text
*.scm text
*.scss text diff=css
*.sh text eol=lf
.husky/* text eol=lf
*.sql text
*.styl text
*.tag text
*.ts text
*.tsx text
*.xml text
*.xhtml text diff=html
# Docker
Dockerfile text
# Documentation
*.ipynb text eol=lf
*.markdown text diff=markdown
*.md text diff=markdown
*.mdwn text diff=markdown
*.mdown text diff=markdown
*.mkd text diff=markdown
*.mkdn text diff=markdown
*.mdtxt text
*.mdtext text
*.txt text
AUTHORS text
CHANGELOG text
CHANGES text
CONTRIBUTING text
COPYING text
copyright text
*COPYRIGHT* text
INSTALL text
license text
LICENSE text
NEWS text
readme text
*README* text
TODO text
# Templates
*.dot text
*.ejs text
*.erb text
*.haml text
*.handlebars text
*.hbs text
*.hbt text
*.jade text
*.latte text
*.mustache text
*.njk text
*.phtml text
*.svelte text
*.tmpl text
*.tpl text
*.twig text
*.vue text
# Configs
*.cnf text
*.conf text
*.config text
.editorconfig text
.env text
.gitattributes text
.gitconfig text
.htaccess text
*.lock text -diff
package.json text eol=lf
package-lock.json text eol=lf -diff
pnpm-lock.yaml text eol=lf -diff
.prettierrc text
yarn.lock text -diff
*.toml text
*.yaml text
*.yml text
browserslist text
Makefile text
makefile text
# Fixes syntax highlighting on GitHub to allow comments
tsconfig.json linguist-language=JSON-with-Comments
# Heroku
Procfile text
# Graphics
*.ai binary
*.bmp binary
*.eps binary
*.gif binary
*.gifv binary
*.ico binary
*.jng binary
*.jp2 binary
*.jpg binary
*.jpeg binary
*.jpx binary
*.jxr binary
*.pdf binary
*.png binary
*.psb binary
*.psd binary
# SVG treated as an asset (binary) by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.svgz binary
*.tif binary
*.tiff binary
*.wbmp binary
*.webp binary
# Audio
*.kar binary
*.m4a binary
*.mid binary
*.midi binary
*.mp3 binary
*.ogg binary
*.ra binary
# Video
*.3gpp binary
*.3gp binary
*.as binary
*.asf binary
*.asx binary
*.avi binary
*.fla binary
*.flv binary
*.m4v binary
*.mng binary
*.mov binary
*.mp4 binary
*.mpeg binary
*.mpg binary
*.ogv binary
*.swc binary
*.swf binary
*.webm binary
# Archives
*.7z binary
*.gz binary
*.jar binary
*.rar binary
*.tar binary
*.zip binary
# Fonts
*.ttf binary
*.eot binary
*.otf binary
*.woff binary
*.woff2 binary
# Executables
*.exe binary
*.pyc binary
# Prevents massive diffs caused by vendored, minified files
**/.yarn/releases/** binary
**/.yarn/plugins/** binary
# RC files (like .babelrc or .eslintrc)
*.*rc text
# Ignore files (like .npmignore or .gitignore)
*.*ignore text
# Prevents massive diffs from built files
dist/* binary

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@ -1,4 +1,5 @@
export async function main(ns: NS) { export async function main(ns: NS) {
ns.run("watcher.js") ns.run("hackallservers.js")
ns.run("hacknet.js") ns.run("watcher.js")
} ns.run("hacknet.js")
}

View File

@ -1,23 +1,14 @@
export async function main(ns: NS) { export async function main(ns: NS) {
const server: string = <string>ns.args[0]; const server: string = <string> ns.args[0]
while (true) { while (true) {
/* This code checks conditions that are not needed with smart scheduling // Guide https://darktechnomancer.github.io/#glossary-of-terms
const moneyThresh: number = <number>ns.args[1];
const securityThresh: number = <number>ns.args[2]; if (ns.getServerSecurityLevel(server) > 5) {
if (ns.getServerSecurityLevel(server) > securityThresh) { await ns.weaken(server)
await ns.weaken(server); } else if (ns.getServerMoneyAvailable(server) < ns.getServerMaxMoney(server) * 0.95) {
} else if (ns.getServerMoneyAvailable(server) < moneyThresh) { await ns.grow(server)
await ns.grow(server); } else {
} else { await ns.hack(server)
await ns.hack(server); }
} }
*/ }
// Guide
// https://darktechnomancer.github.io/#glossary-of-terms
await ns.weaken(server);
await ns.grow(server);
await ns.weaken(server);
await ns.hack(server);
}
}

View File

@ -1,15 +1,15 @@
import {recursiveScan} from "./utils"; import { executeScriptOnServerFromAnother, recursiveScan } from "./utils"
import {executeScriptOnServerFromAnother} from "./utils";
export async function main(ns: NS) { export async function main(ns: NS) {
let servers: string[] = recursiveScan(ns); let servers: string[] = recursiveScan(ns)
for (const server of servers) { for (const server of servers) {
// Commented out code is not needed with smart scheduling // Guard clause for story servers, but hacking xp is nice. Can probably be enabled when XP is easy to get
//let moneyThresh = ns.getServerMaxMoney(server) * 0.75; // if (ns.getServerMaxMoney(server) === 0) { continue }
//let securityThresh = ns.getServerMinSecurityLevel(server) + 5;
let numThreads = ns.getServerMaxRam(server) / ns.getScriptRam("hack.js") let numThreads = ns.getServerMaxRam(server) / ns.getScriptRam("hack.js")
numThreads = Math.floor(numThreads); numThreads = Math.floor(numThreads)
//executeScriptOnServerFromAnother(ns, server, "hack.js", numThreads, [server, moneyThresh, securityThresh]) numThreads = Math.max(numThreads, 1) // Make sure we have at least 1 thread
executeScriptOnServerFromAnother(ns, server, "hack.js", numThreads, [server]) executeScriptOnServerFromAnother(ns, server, "hack.js", numThreads, [server])
} }
} }

View File

@ -1,103 +1,103 @@
/* /*
* TODO: * TODO:
* The current solution is not optimal, as it's preferable to buy new nodes, even if upgrading is cheaper. * The current solution is not optimal, as it's preferable to buy new nodes, even if upgrading is cheaper.
* TODO: * TODO:
* It may be helpful to calculate the money per second of each node on average, (total mps / total nodes) and compare * It may be helpful to calculate the money per second of each node on average, (total mps / total nodes) and compare
* it to the time it would take to purchase the next cheapest upgrade, and if the average mps is greater than the * it to the time it would take to purchase the next cheapest upgrade, and if the average mps is greater than the
* negative mps of the upgrade cost, then buy a new node. * negative mps of the upgrade cost, then buy a new node.
* TODO: * TODO:
* Potential Ideas: * Potential Ideas:
* Find a way to calculate how much mps an individual upgrade would give, and compare it to the average mps of the node. * Find a way to calculate how much mps an individual upgrade would give, and compare it to the average mps of the node.
* ns.hacknet.getNodeStats(0).production * ns.hacknet.getNodeStats(0).production
* If the upgrade mps is greater than the average mps, then buy the upgrade. * If the upgrade mps is greater than the average mps, then buy the upgrade.
* If the upgrade mps is less than the average mps, then buy a new node. * If the upgrade mps is less than the average mps, then buy a new node.
*/ */
enum Type { enum UpgradeType {
newNode = "node", level = "level", ram = "ram", core = "code",
level = "level", }
ram = "ram",
core = "code" export async function main(ns: NS) {
} let timeout: number = <number> ns.args[0]
export async function main(ns: NS) { let nodes = ns.hacknet.numNodes()
let timeout: number = <number>ns.args[0] // If there are no nodes, buyUpgrade one
if (nodes === 0) {
let nodes = ns.hacknet.numNodes() ns.hacknet.purchaseNode()
let costs: {type: Type, cost: number}[] = [] nodes = 1
while (true) { }
costs = []
// Go through each node and get the cheapest upgrade let costs: { type: UpgradeType, cost: number }[] = []
for (let i = 0; i < nodes; i++) { while (true) {
costs.push(getCheapestCost(ns, i)) costs = []
} // Go through each node and get the cheapest upgrade
// Buy the cheapest upgrade from all nodes for (let i = 0; i < nodes; i++) {
let cheapest = Math.min(...costs.map(c => c.cost)) costs.push(getCheapestCost(ns, i))
// Find the index of the cheapest cost in the list }
let index = costs.findIndex(c => c.cost === cheapest) // Get the cheapest upgrade object
// Wait to buy the cheapest upgrade let cheapest = costs.reduce((prev, curr) => prev.cost < curr.cost ? prev : curr)
try {
await buy(ns, costs[index].type, costs[index].cost, index, timeout) // Nodes have a lot more value, so only need node price to be 1/10th the cost of the cheapest upgrade
} catch (e) { if (ns.hacknet.getPurchaseNodeCost() / 10 < cheapest.cost) {
ns.tprint(e) await buyNode(ns, ns.hacknet.getPurchaseNodeCost(), timeout)
ns.exit() } else {
} await buyUpgrade(ns, cheapest.type, cheapest.cost, costs.indexOf(cheapest), timeout)
// Make sure that the number of nodes is up-to-date }
nodes = ns.hacknet.numNodes() // Make sure that the number of nodes is up-to-date
} nodes = ns.hacknet.numNodes()
} }
}
/**
* Wait until the player has enough money to buy something /**
* @param ns Global NS object * Wait until the player has enough money to buyUpgrade something
* @param money Amount of money to wait for * @param ns Global NS object
* @param timeout=-1 Number of seconds to wait before timing out. * @param money Amount of money to wait for
* @throws Error if the timeout is reached * @param timeout=-1 Number of seconds to wait before timing out.
* @note This function will wait forever by default * @throws Error if the timeout is reached
*/ * @note This function will wait forever by default
async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) { */
while (ns.getServerMoneyAvailable("home") < money) { async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) {
await ns.sleep(1000) while (ns.getServerMoneyAvailable("home") < money) {
if (timeout == 0) { await ns.sleep(1000)
throw new Error("Timed out waiting for money") if (timeout == 0) {
} else if (timeout > -1) { throw new Error("Timed out waiting for money")
timeout--; } else if (timeout > -1) {
} timeout--
} }
} }
}
async function buy(ns: NS, type: Type, cost: number, node: number, timeout: number) {
await waitUntilMoney(ns, cost, timeout) async function buyUpgrade(ns: NS, type: UpgradeType, cost: number, node: number, timeout: number) {
switch (type) { await waitUntilMoney(ns, cost, timeout)
case Type.newNode: switch (type) {
ns.hacknet.purchaseNode() case UpgradeType.level:
break ns.hacknet.upgradeLevel(node)
case Type.level: break
ns.hacknet.upgradeLevel(node) case UpgradeType.ram:
break ns.hacknet.upgradeRam(node)
case Type.ram: break
ns.hacknet.upgradeRam(node) case UpgradeType.core:
break ns.hacknet.upgradeCore(node)
case Type.core: break
ns.hacknet.upgradeCore(node) }
break }
}
} async function buyNode(ns: NS, cost: number, timeout: number) {
await waitUntilMoney(ns, cost, timeout)
function getCheapestCost(ns: NS, node: number) { ns.hacknet.purchaseNode()
let nodeCost = ns.hacknet.getPurchaseNodeCost() }
let levelCost = ns.hacknet.getLevelUpgradeCost(node)
let ramCost = ns.hacknet.getRamUpgradeCost(node) function getCheapestCost(ns: NS, node: number) {
let coreCost = ns.hacknet.getCoreUpgradeCost(node) let levelCost = ns.hacknet.getLevelUpgradeCost(node)
let cheapest = Math.min(nodeCost, levelCost, ramCost, coreCost) let ramCost = ns.hacknet.getRamUpgradeCost(node)
switch (cheapest) { let coreCost = ns.hacknet.getCoreUpgradeCost(node)
case nodeCost: let cheapest = Math.min(levelCost, ramCost, coreCost)
return {type: Type.newNode, cost: nodeCost} switch (cheapest) {
case levelCost: case levelCost:
return {type: Type.level, cost: levelCost} return { type: UpgradeType.level, cost: levelCost }
case ramCost: case ramCost:
return {type: Type.ram, cost: ramCost} return { type: UpgradeType.ram, cost: ramCost }
case coreCost: case coreCost:
return {type: Type.core, cost: coreCost} return { type: UpgradeType.core, cost: coreCost }
} }
} }

View File

@ -1,7 +1,8 @@
import {recursiveScan} from "./utils"; import { recursiveScan } from "./utils"
export async function main(ns: NS) {
let servers: string[] = recursiveScan(ns) export async function main(ns: NS) {
for (const server of servers) { let servers: string[] = recursiveScan(ns)
ns.killall(server) for (const server of servers) {
} ns.killall(server)
} }
}

32
servers/home/programs.ts Normal file
View File

@ -0,0 +1,32 @@
import { recursiveHackingRequired, recursiveScan, removeFilesOnAllServers } from "./utils"
export async function main(ns: NS) {
switch (ns.args[0]) {
case "getServers":
getServers(ns)
break
case "getHackingLevels":
getHackingLevels(ns)
break
case "removeFiles":
removeFilesOnAllServersFromArgs(ns)
break
default:
ns.tprint("Invalid program name")
break
}
}
function getServers(ns: NS) {
ns.tprint(recursiveScan(ns))
}
function getHackingLevels(ns: NS) {
ns.tprint(recursiveHackingRequired(ns))
}
function removeFilesOnAllServersFromArgs(ns: NS) {
let args = ns.args.slice(1)
let files = args.map((file) => file.toString())
removeFilesOnAllServers(ns, files)
}

View File

@ -1,141 +1,183 @@
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
/**
* Recursively scans all servers connected to the current server, excluding home /**
* @param ns global NS object * Recursively scans all servers connected to the current server, excluding home
* @returns A list of all servers connected to the current server * @param ns global NS object
*/ * @returns A list of all servers connected to the current server
export function recursiveScan(ns: NS) { */
// Starting case export function recursiveScan(ns: NS) {
let servers = ns.scan("home"); // Starting case
// Add all servers to the list let servers = ns.scan("home")
let allServers: string[] = []; // Add all servers to the list
while (servers.length > 0) { let allServers: string[] = []
let server = servers.shift(); while (servers.length > 0) {
if(server) { let server = servers.pop()
let newServers = ns.scan(server); if (server) {
for (let newServer of newServers) { let newServers = ns.scan(server)
if (!allServers.includes(newServer)) { for (const newServer of newServers) {
allServers.push(newServer); if (!allServers.includes(newServer)) {
servers.push(newServer); allServers.push(newServer)
} servers.push(newServer)
} }
} }
} }
// Remove the current server }
allServers.splice(allServers.indexOf("home"), 1); // Remove the home server from the list
// Print all servers return allServers.filter(server => server !== "home").sort()
return allServers; }
}
/**
/** * Recursively scans all servers connected to the current server for the set of all levels that hackable servers require
* Removes files passed in from all servers * @param ns
* @param ns Global NS object * @returns A set of every hacking level required to hack a new server
* @param files The files to remove */
* @returns void export function recursiveHackingRequired(ns: NS) {
* @example Removes all files from the folder "no-ports" from all servers. let servers = recursiveScan(ns)
* removeFilesOnAllServers(ns, ns.ls("home", "no-ports")); let levels: number[] = []
* @note ns.ls() returns the full path of every file, so if the file is not in the same place in every server, for (let server of servers) {
* you will need to modify each file path to match the server you are removing it from. levels.push(ns.getServerRequiredHackingLevel(server))
*/ }
export function removeFilesOnAllServers(ns: NS, files: string[]) { // Sort and remove duplicates, need custom sort because built-in sort only works alphabetically 💀
let hosts = recursiveScan(ns); return [...new Set(levels)].sort((a, b) => a - b)
for(const host of hosts) { }
for (const file of files) {
ns.rm(file, host) /**
} * 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.
* Tries to gain root access to a server * removeFilesOnAllServers(ns, ns.ls("home", "no-ports"));
* @param ns Global NS object * @note ns.ls() returns the full path of every file, so if the file is not in the same place in every server,
* @param server The server to gain root access to * you will need to modify each file path to match the server you are removing it from.
* @returns The number of programs used to gain root access */
*/ export function removeFilesOnAllServers(ns: NS, files: string[]) {
export function rootServer(ns: NS, server: string) { let hosts = recursiveScan(ns)
let counter = 0; for (const host of hosts) {
if (ns.fileExists("BruteSSH.exe", "home")) { for (const file of files) {
ns.brutessh(server); ns.rm(file, host)
counter++; }
} }
if (ns.fileExists("FTPCrack.exe", "home")) { }
ns.ftpcrack(server);
counter++; /**
} * Tries to gain root access to a server
if (ns.fileExists("SMTPCrack.exe", "home")) { * @param ns Global NS object
ns.relaysmtp(server); * @param server The server to gain root access to
counter++; * @returns The number of programs used to gain root access
} */
if (ns.fileExists("HTTPWorm.exe", "home")) { export function rootServer(ns: NS, server: string) {
ns.httpworm(server); let counter = 0
counter++; if (ns.fileExists("BruteSSH.exe", "home")) {
} ns.brutessh(server)
if (ns.fileExists("SQLInject.exe", "home")) { counter++
ns.sqlinject(server); }
counter++; if (ns.fileExists("FTPCrack.exe", "home")) {
} ns.ftpcrack(server)
ns.nuke(server) counter++
return counter; }
} if (ns.fileExists("relaySMTP.exe", "home")) {
ns.relaysmtp(server)
/** counter++
* Performs a function on a server if the player is capable of doing so, otherwise returns false }
* @param ns Global NS object if (ns.fileExists("HTTPWorm.exe", "home")) {
* @param server The server to perform the function on ns.httpworm(server)
* @param func The function to perform counter++
* @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 if (ns.fileExists("SQLInject.exe", "home")) {
*/ ns.sqlinject(server)
export function performFunctionIfCapable(ns: NS, server: string, func: CallableFunction, args: any[]) { counter++
if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) { }
return false; if (counter >= ns.getServerNumPortsRequired(server)) {
} ns.nuke(server)
if (ns.getServerNumPortsRequired(server) < ns.getServer(server).openPortCount) { }
if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) { return counter
return false; }
}
} /**
if (!ns.hasRootAccess(server)) { * Performs a function on a server if the player is capable of doing so, otherwise returns false
return false; * @param ns Global NS object
* @param server The server to perform the function on
} * @param func The function to perform
let result = func(...args); * @param args The arguments to pass to the function
if (result === undefined) { * @returns The result of the function if it is performed or true if the function does not return anything, otherwise false
return true; */
} else { export function performFunctionIfCapable(ns: NS, server: string, func: CallableFunction, args: any[]) {
return result; ns.print(`Performing function on ${server}`)
} if (ns.getHackingLevel() < ns.getServerRequiredHackingLevel(server)) {
} ns.print(`Not enough hacking level to hack ${server}`)
return false
/** }
* Executes a script on a server from another server if (ns.getServer(server).openPortCount <= ns.getServerNumPortsRequired(server)) {
* @param ns Global NS object ns.print(`Not enough ports, trying to root ${server}`)
* @param server The server to execute the script on if (rootServer(ns, server) < ns.getServerNumPortsRequired(server)) {
* @param script The file path of the script to execute ns.print(`Failed to root ${server}`)
* @param threads The number of threads to use return false
* @param args The arguments to pass to the script }
*/ }
export function executeScriptOnServerFromAnother(ns: NS, server: string, script: string, threads: number = 1, args: any[]) { let result = func(...args)
ns.scp(script, server); if (result === undefined) {
performFunctionIfCapable(ns, server, ns.exec, [script, server, threads, ...args]) return true
ns.atExit( } else {
() => { return result
ns.rm(script, server); }
} }
)
} /**
* Executes a script on a server from another server
/** * @param ns Global NS object
* Calculates the money per second the player is making * @param server The server to execute the script on
* @param ns Global NS object * @param script The file path of the script to execute
* @param time=5 The number of seconds to calculate the MPS over * @param threads The number of threads to use
*/ * @param args The arguments to pass to the script
export async function calculateMPS(ns: NS, time: number = 5) { */
let start = ns.getServerMoneyAvailable("home"); export function executeScriptOnServerFromAnother(ns: NS, server: string, script: string, threads: number = 1, args: any[]) {
let data: number[] = []; ns.scp(script, server)
for (let i = 0; i < time; i++) { performFunctionIfCapable(ns, server, ns.exec, [script, server, threads, ...args])
await ns.sleep(1000); ns.atExit(() => {
data.push(ns.getServerMoneyAvailable("home") - start); // Remove the script from the server when the program exits
} // ns.rm(script, server)
return data.reduce((a, b) => a + b, 0) / time; })
} }
/**
* 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) {
let start = ns.getServerMoneyAvailable("home")
let data: number[] = []
for (let i = 0; i < time; i++) {
await ns.sleep(1000)
data.push(ns.getServerMoneyAvailable("home") - start)
}
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
}

View File

@ -1,15 +1,42 @@
export async function main(ns: NS) { import { checkForNewPrograms, ProgramState, recursiveHackingRequired } from "./utils"
let hackingLevel = ns.getHackingLevel();
while (hackingLevel < 9999) { export async function main(ns: NS) {
let oldHackingLevel = hackingLevel; let levels = recursiveHackingRequired(ns)
hackingLevel = ns.getHackingLevel(); let hackingLevel = ns.getHackingLevel()
if(oldHackingLevel !== hackingLevel) {
ns.tprint(`Hacking level increased from ${oldHackingLevel} to ${hackingLevel}`); let state: ProgramState[] = [
ns.run("killall.js"); { program: "BruteSSH.exe", exists: false },
await ns.sleep(1000) // 1 second { program: "FTPCrack.exe", exists: false },
ns.run("hackallservers.js"); { program: "relaySMTP.exe", exists: false },
} { program: "HTTPWorm.exe", exists: false },
// Wait 1 second before checking again { program: "SQLInject.exe", exists: false },
await ns.sleep(1000) ]
} // Update state with existing programs
} checkForNewPrograms(ns, state)
// Remove levels that are already hacked
levels = levels.filter(level => level > hackingLevel)
do {
let restart = false
if (Math.min(...levels) <= hackingLevel) {
ns.tprint(`Hacking level increased to/past ${Math.min(...levels)}, is now ${hackingLevel}`)
ns.tprint(`Remaining levels to hack: ${levels}`)
// remove the level from the list, so we don't try to hack it again
levels = levels.filter(level => level > hackingLevel)
restart = true
}
if (checkForNewPrograms(ns, state)) {
ns.tprint(`New programs found: ${state.filter(program => program.exists).map(program => program.program)}`)
restart = true
}
if (restart) {
ns.run("killall.js")
await ns.sleep(1000) // 1 second
ns.run("hackallservers.js")
}
hackingLevel = ns.getHackingLevel()
// Wait 1 second before checking again
await ns.sleep(1000)
} while (hackingLevel <= Math.max(...levels))
}