Skip to content

Instantly share code, notes, and snippets.

@Arathi
Last active November 27, 2023 09:49
Show Gist options
  • Save Arathi/fda0b153527c764ee7ec5da5154b9c51 to your computer and use it in GitHub Desktop.
Save Arathi/fda0b153527c764ee7ec5da5154b9c51 to your computer and use it in GitHub Desktop.
Bitburner Scripts
/** @type {NS} */
var ns;
async function writeToFile(id, results) {
const path = `/responses/${id}.txt`;
const content = JSON.stringify(results);
ns.write(path, content);
}
async function list() {
const amount = ns.hacknet.numNodes();
const results = [];
for (let index = 0; index < amount; index++) {
const nodeStats = ns.hacknet.getNodeStats(index);
results.push(nodeStats);
}
return results;
}
/** @param {NS} nsp */
export async function main(nsp) {
ns = nsp;
const id = ns.args[0];
if (id == undefined) {
ns.print("无效的请求报文id");
return;
}
let method = ns.args[1];
if (method == undefined) {
method = "list";
}
let json = "";
switch (method) {
case "list":
list().then((results) => writeToFile(id, results));
break;
}
ns.write(`/responses/${id}.txt`, json, 'w');
}
/** @param {NS} nsp */
export async function main(nsp) {
const host = ns.getHostname();
const dir = "/etc/init.d/";
while (true) {
const scripts = ns.ls(host, dir);
for (const script of scripts) {
ns.print(`Running ${scriptName}`);
ns.run(script, 1);
}
}
}
/** @type {NS} */
var ns;
class ServerNode {
/**
* @param {Server} server
*/
constructor(server) {
/** @type {Server} */
this.server = server;
/** @type {ServerNode[]} */
this.connects = [];
}
/**
* @param {"dfs"|"bfs"} search
*/
toList() {
/** @type {Server[]} */
const list = [];
list.push(this.server);
for (const child of this.connects) {
const cl = child.toList(search);
list.push(...cl);
}
return list;
}
}
/**
* @param {string} host
* @param {Set<string>} scaned
*/
function scan(host, scaned = new Set()) {
if (scaned.has(host)) {
return null;
}
const server = ns.getServer(host);
const node = new ServerNode(server);
scaned.add(host);
const children = ns.scan(host);
for (const childName of children) {
const childNode = scan(childName, scaned);
if (childNode != null) {
node.connects.push(childNode);
}
}
return node;
}
/**
* @param {NS} nsp
*/
export async function main(nsp) {
ns = nsp;
let host = ns.args[0];
if (host == undefined) {
host = ns.getHostname();
}
let type = ns.args[1];
if (type == undefined) {
type = "tree";
}
let output = ns.args[2];
if (output == undefined) {
output = "terminal";
}
const server = scan(host);
let content = "";
switch (type) {
case "list":
let list = server.toList();
content = JSON.stringify(list);
break;
default:
content = JSON.stringify(server);
break;
}
switch (output) {
case "log":
ns.print(content);
break;
case "terminal":
ns.tprint(content);
break;
default:
ns.write(output, content, "w");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment