Compare commits

..

2 Commits

Author SHA1 Message Date
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
7 changed files with 31 additions and 29 deletions

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,11 +1,11 @@
import { executeScriptOnServerFromAnother, recursiveScan } from './utils' import { executeScriptOnServerFromAnother, recursiveScan } 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) {
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]) executeScriptOnServerFromAnother(ns, server, "hack.js", numThreads, [server])
} }
} }

View File

@ -14,7 +14,7 @@
*/ */
enum Type { enum Type {
newNode = 'node', level = 'level', ram = 'ram', core = 'code', newNode = "node", level = "level", ram = "ram", core = "code",
} }
export async function main(ns: NS) { export async function main(ns: NS) {
@ -59,10 +59,10 @@ export async function main(ns: NS) {
* @note This function will wait forever by default * @note This function will wait forever by default
*/ */
async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) { async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) {
while (ns.getServerMoneyAvailable('home') < money) { while (ns.getServerMoneyAvailable("home") < money) {
await ns.sleep(1000) await ns.sleep(1000)
if (timeout == 0) { if (timeout == 0) {
throw new Error('Timed out waiting for money') throw new Error("Timed out waiting for money")
} else if (timeout > -1) { } else if (timeout > -1) {
timeout-- timeout--
} }

View File

@ -1,4 +1,4 @@
import { recursiveScan } from './utils' import { recursiveScan } from "./utils"
export async function main(ns: NS) { export async function main(ns: NS) {
let servers: string[] = recursiveScan(ns) let servers: string[] = recursiveScan(ns)

View File

@ -1,21 +1,23 @@
import { recursiveHackingRequired, recursiveScan } from "./utils" import { recursiveHackingRequired, recursiveScan } from "./utils"
export async function main(ns: NS){ export async function main(ns: NS) {
switch (ns.args[0]) { switch (ns.args[0]) {
case "getServers": case "getServers":
getServers(ns); getServers(ns)
break; break
case "getHackingLevels": case "getHackingLevels":
getHackingLevels(ns); getHackingLevels(ns)
break; break
default: default:
ns.tprint("Invalid program name"); ns.tprint("Invalid program name")
break; break
} }
} }
function getServers(ns: NS) { function getServers(ns: NS) {
ns.tprint(recursiveScan(ns)); ns.tprint(recursiveScan(ns))
} }
function getHackingLevels(ns: NS) { function getHackingLevels(ns: NS) {
ns.tprint(recursiveHackingRequired(ns)) ns.tprint(recursiveHackingRequired(ns))
} }

View File

@ -7,7 +7,7 @@
*/ */
export function recursiveScan(ns: NS) { export function recursiveScan(ns: NS) {
// Starting case // Starting case
let servers = ns.scan('home') let servers = ns.scan("home")
// Add all servers to the list // Add all servers to the list
let allServers: string[] = [] let allServers: string[] = []
while (servers.length > 0) { while (servers.length > 0) {
@ -23,7 +23,7 @@ export function recursiveScan(ns: NS) {
} }
} }
// Remove the current server // Remove the current server
allServers.splice(allServers.indexOf('home'), 1) allServers.splice(allServers.indexOf("home"), 1)
// Print all servers // Print all servers
return allServers return allServers
} }
@ -70,23 +70,23 @@ export function removeFilesOnAllServers(ns: NS, files: string[]) {
*/ */
export function rootServer(ns: NS, server: string) { export function rootServer(ns: NS, server: string) {
let counter = 0 let counter = 0
if (ns.fileExists('BruteSSH.exe', 'home')) { if (ns.fileExists("BruteSSH.exe", "home")) {
ns.brutessh(server) ns.brutessh(server)
counter++ counter++
} }
if (ns.fileExists('FTPCrack.exe', 'home')) { if (ns.fileExists("FTPCrack.exe", "home")) {
ns.ftpcrack(server) ns.ftpcrack(server)
counter++ counter++
} }
if (ns.fileExists('SMTPCrack.exe', 'home')) { if (ns.fileExists("SMTPCrack.exe", "home")) {
ns.relaysmtp(server) ns.relaysmtp(server)
counter++ counter++
} }
if (ns.fileExists('HTTPWorm.exe', 'home')) { if (ns.fileExists("HTTPWorm.exe", "home")) {
ns.httpworm(server) ns.httpworm(server)
counter++ counter++
} }
if (ns.fileExists('SQLInject.exe', 'home')) { if (ns.fileExists("SQLInject.exe", "home")) {
ns.sqlinject(server) ns.sqlinject(server)
counter++ counter++
} }
@ -151,11 +151,11 @@ export function executeScriptOnServerFromAnother(ns: NS, server: string, script:
* @param time=5 The number of seconds to calculate the MPS over * @param time=5 The number of seconds to calculate the MPS over
*/ */
export async function calculateMPS(ns: NS, time: number = 5) { export async function calculateMPS(ns: NS, time: number = 5) {
let start = ns.getServerMoneyAvailable('home') let start = ns.getServerMoneyAvailable("home")
let data: number[] = [] let data: number[] = []
for (let i = 0; i < time; i++) { for (let i = 0; i < time; i++) {
await ns.sleep(1000) await ns.sleep(1000)
data.push(ns.getServerMoneyAvailable('home') - start) data.push(ns.getServerMoneyAvailable("home") - start)
} }
return data.reduce((a, b) => a + b, 0) / time return data.reduce((a, b) => a + b, 0) / time
} }

View File

@ -1,7 +1,6 @@
import { recursiveHackingRequired } from "./utils" import { recursiveHackingRequired } from "./utils"
export async function main(ns: NS) { export async function main(ns: NS) {
ns.run('hackallservers.js')
let levels = recursiveHackingRequired(ns) let levels = recursiveHackingRequired(ns)
let hackingLevel: number let hackingLevel: number
do { do {
@ -10,9 +9,9 @@ export async function main(ns: NS) {
// remove the level from the list, so we don't try to hack it again // remove the level from the list, so we don't try to hack it again
levels.splice(levels.indexOf(hackingLevel), 1) levels.splice(levels.indexOf(hackingLevel), 1)
ns.tprint(`Hacking level increased to ${hackingLevel}`) ns.tprint(`Hacking level increased to ${hackingLevel}`)
ns.run('killall.js') ns.run("killall.js")
await ns.sleep(1000) // 1 second await ns.sleep(1000) // 1 second
ns.run('hackallservers.js') ns.run("hackallservers.js")
} }
// Wait 1 second before checking again // Wait 1 second before checking again
await ns.sleep(1000) await ns.sleep(1000)