From 8733556ac2e0c90a955fb81cb57c7dc90b6a9674 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Fri, 3 Nov 2023 02:00:43 -0300 Subject: [PATCH] Add scripts --- servers/home/clear-servers.js | 29 ----------------------- servers/home/hello-world.js | 4 ---- servers/home/remove-files-from-servers.ts | 19 +++++++++++++++ servers/home/utils.ts | 23 ++++++++++++++++++ 4 files changed, 42 insertions(+), 33 deletions(-) delete mode 100644 servers/home/clear-servers.js delete mode 100644 servers/home/hello-world.js create mode 100644 servers/home/remove-files-from-servers.ts create mode 100644 servers/home/utils.ts diff --git a/servers/home/clear-servers.js b/servers/home/clear-servers.js deleted file mode 100644 index 56fb46b..0000000 --- a/servers/home/clear-servers.js +++ /dev/null @@ -1,29 +0,0 @@ -/** @param {NS} ns */ -export async function main(ns) { - let files = [ - "run-script-on-all-servers.js", - "recursive-run.js", - "recursive-copy.js", - "recursive-kill.js", - "hack-args.js", - "hack.js", - "root.js", - "recursive-root.js" - ] - let hosts = [ - "zb-institute", - "comptek", - "nectar-net", - "neo-net", - "max-hardware", - "omega-net", - "avmnite-02h", - "CSEC" - ] - - for(const host of hosts) { - for (const file of files) { - ns.rm(file, host) - } - } -} \ No newline at end of file diff --git a/servers/home/hello-world.js b/servers/home/hello-world.js deleted file mode 100644 index 5c453f5..0000000 --- a/servers/home/hello-world.js +++ /dev/null @@ -1,4 +0,0 @@ -/** @param {NS} ns */ -export async function main(ns) { - ns.tprint("Hello World!"); -} \ No newline at end of file diff --git a/servers/home/remove-files-from-servers.ts b/servers/home/remove-files-from-servers.ts new file mode 100644 index 0000000..bb58dfa --- /dev/null +++ b/servers/home/remove-files-from-servers.ts @@ -0,0 +1,19 @@ +import {NS} from "NetscriptDefinitions"; +import {recursiveScan} from "./utils"; + +export async function main(ns: NS, servers: string[]) { + let hosts = recursiveScan(ns); + + let folderPrefix = "no-ports/"; + let files = ns.ls("home", folderPrefix).map((file) => { + return file.substring(folderPrefix.length, file.length); + }); + + ns.tprint("Files to be removed: ", JSON.stringify(files)); + + for(const host of hosts) { + for (const file of files) { + ns.rm(file, host) + } + } +} \ No newline at end of file diff --git a/servers/home/utils.ts b/servers/home/utils.ts new file mode 100644 index 0000000..4e5505a --- /dev/null +++ b/servers/home/utils.ts @@ -0,0 +1,23 @@ +import {NS} from "NetscriptDefinitions"; +export function recursiveScan(ns: NS) { + // Starting case + let servers = ns.scan(ns.getHostname()); + // Add all servers to the list + let allServers: string[] = []; + while (servers.length > 0) { + let server = servers.shift(); + if(server) { + let newServers = ns.scan(server); + for (let newServer of newServers) { + if (!allServers.includes(newServer)) { + allServers.push(newServer); + servers.push(newServer); + } + } + } + } + // Remove the current server + allServers.splice(allServers.indexOf(ns.getHostname()), 1); + // Print all servers + return allServers; +} \ No newline at end of file