72 lines
No EOL
1.9 KiB
TypeScript
72 lines
No EOL
1.9 KiB
TypeScript
import { NS, Server } from '@ns'
|
|
|
|
/** @param {NS} ns */
|
|
export async function main(ns: NS): Promise<void> {
|
|
if (ns.args.length == 0) {
|
|
ns.tprint("argument required")
|
|
ns.tprint("search all server and print to json")
|
|
ns.tprint("run cmd [filePath]")
|
|
ns.exit()
|
|
}
|
|
const filePath = String(ns.args[0])
|
|
const collectedData = selectAllServerList(ns)
|
|
const buf = JSON.stringify(collectedData, undefined, 2)
|
|
await ns.write(filePath, buf, "w")
|
|
}
|
|
|
|
export interface ServerInfo extends Server{
|
|
relHost:string[];
|
|
}
|
|
|
|
/**
|
|
* @param {NS} ns
|
|
* @param func
|
|
*/
|
|
export function visitAllServerList(ns: NS, func:(data: ServerInfo)=>void):void {
|
|
/** @type {string[]} */
|
|
const queue: string[] = ["home"]
|
|
/** @type {Set<string>} */
|
|
const visited = new Set()
|
|
|
|
//breadth first search
|
|
while (queue.length > 0) {
|
|
const vHost = queue.pop()
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
const data = getServersInfo(ns, vHost!)
|
|
|
|
func(data)
|
|
|
|
visited.add(vHost)
|
|
for (const h of data.relHost) {
|
|
if (!visited.has(h)) {
|
|
queue.push(h)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/**@param {NS} ns */
|
|
export function selectAllServerList(ns: NS): ServerInfo[] {
|
|
const collectedData: ServerInfo[] = []
|
|
visitAllServerList(ns, (data) => {
|
|
collectedData.push(data)
|
|
})
|
|
return collectedData
|
|
}
|
|
|
|
export function isRootedServer(server:Server):boolean{
|
|
return server.hasAdminRights
|
|
}
|
|
|
|
export function selectRootedServerList(ns:NS): ServerInfo[]{
|
|
return selectAllServerList(ns).filter(isRootedServer)
|
|
}
|
|
|
|
/** @param {NS} ns
|
|
* @param {string} host
|
|
* @return {Server & {relHost: string[]}}
|
|
*/
|
|
export function getServersInfo(ns: NS, host: string): ServerInfo {
|
|
const neighbor = ns.scan(host)
|
|
const info = ns.getServer(host)
|
|
return { ...info, relHost: neighbor }
|
|
} |