updated scan-analyze.ts to use string concatenation

This commit is contained in:
Vomitblood 2026-05-04 14:34:33 +08:00
parent f75ef84f95
commit 374d46daa1
5 changed files with 32 additions and 25 deletions

View file

@ -1,7 +1,7 @@
import { ezgame } from "@/ezgame";
import { getNormalServer } from "@/ezgame/get-normal-server"; import { getNormalServer } from "@/ezgame/get-normal-server";
import { utils } from "@/utils/utils"; import { utils } from "@/utils/utils";
import { NS } from "@ns"; import { NS } from "@ns";
import { formulas } from ".";
interface TestCase { interface TestCase {
name: string; name: string;
@ -23,7 +23,7 @@ const gangTests = (ns: NS, serverName: string): TestCase[] => [
name: "gang.ascensionMultiplier", name: "gang.ascensionMultiplier",
fn: (ns, serverName) => { fn: (ns, serverName) => {
const points = utils.randomNumber(0, 1e9); const points = utils.randomNumber(0, 1e9);
const testResult = roundTo5Decimals(ezgame.formulas.gang.ascensionMultiplier(points)); const testResult = roundTo5Decimals(formulas.gang.ascensionMultiplier(points));
const actualResult = roundTo5Decimals(ns.formulas.gang.ascensionMultiplier(points)); const actualResult = roundTo5Decimals(ns.formulas.gang.ascensionMultiplier(points));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -34,7 +34,7 @@ const gangTests = (ns: NS, serverName: string): TestCase[] => [
name: "gang.ascensionPointsGain", name: "gang.ascensionPointsGain",
fn: (ns, serverName) => { fn: (ns, serverName) => {
const exp = utils.randomNumber(0, 1e9); const exp = utils.randomNumber(0, 1e9);
const testResult = roundTo5Decimals(ezgame.formulas.gang.ascensionPointsGain(exp)); const testResult = roundTo5Decimals(formulas.gang.ascensionPointsGain(exp));
const actualResult = roundTo5Decimals(ns.formulas.gang.ascensionPointsGain(exp)); const actualResult = roundTo5Decimals(ns.formulas.gang.ascensionPointsGain(exp));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -56,7 +56,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
const player = ns.getPlayer(); const player = ns.getPlayer();
const threads = utils.randomNumber(1, 100); const threads = utils.randomNumber(1, 100);
const cores = utils.randomNumber(1, 16); const cores = utils.randomNumber(1, 16);
const testResult = roundTo5Decimals(ezgame.formulas.hacking.growAmount(server, player, threads, cores)); const testResult = roundTo5Decimals(formulas.hacking.growAmount(server, player, threads, cores));
const actualResult = roundTo5Decimals(ns.formulas.hacking.growAmount(server, player, threads, cores)); const actualResult = roundTo5Decimals(ns.formulas.hacking.growAmount(server, player, threads, cores));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -70,7 +70,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
const threads = utils.randomNumber(1, 100); const threads = utils.randomNumber(1, 100);
const player = ns.getPlayer(); const player = ns.getPlayer();
const cores = utils.randomNumber(1, 16); const cores = utils.randomNumber(1, 16);
const testResult = roundTo5Decimals(ezgame.formulas.hacking.growPercent(server, threads, player, cores)); const testResult = roundTo5Decimals(formulas.hacking.growPercent(server, threads, player, cores));
const actualResult = roundTo5Decimals(ns.formulas.hacking.growPercent(server, threads, player, cores)); const actualResult = roundTo5Decimals(ns.formulas.hacking.growPercent(server, threads, player, cores));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -86,7 +86,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
// anyways if it is above moneyMax, the function should clamp it to the max (hopefully) // anyways if it is above moneyMax, the function should clamp it to the max (hopefully)
const targetMoney = server.moneyMax ?? 1e120; const targetMoney = server.moneyMax ?? 1e120;
const cores = utils.randomNumber(1, 16); const cores = utils.randomNumber(1, 16);
const testResult = roundTo5Decimals(ezgame.formulas.hacking.growThreads(server, player, targetMoney, cores)); const testResult = roundTo5Decimals(formulas.hacking.growThreads(server, player, targetMoney, cores));
const actualResult = roundTo5Decimals(ns.formulas.hacking.growThreads(server, player, targetMoney, cores)); const actualResult = roundTo5Decimals(ns.formulas.hacking.growThreads(server, player, targetMoney, cores));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -98,7 +98,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.growTime(server, player)); const testResult = roundTo5Decimals(formulas.hacking.growTime(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.growTime(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.growTime(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -110,7 +110,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.hackChance(server, player)); const testResult = roundTo5Decimals(formulas.hacking.hackChance(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.hackChance(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.hackChance(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -122,7 +122,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.hackExp(server, player)); const testResult = roundTo5Decimals(formulas.hacking.hackExp(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.hackExp(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.hackExp(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -134,7 +134,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.hackPercent(server, player)); const testResult = roundTo5Decimals(formulas.hacking.hackPercent(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.hackPercent(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.hackPercent(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -146,7 +146,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.hackTime(server, player)); const testResult = roundTo5Decimals(formulas.hacking.hackTime(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.hackTime(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.hackTime(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);
@ -158,7 +158,7 @@ const hackingTests = (ns: NS, serverName: string): TestCase[] => [
fn: (ns, serverName) => { fn: (ns, serverName) => {
const server = getNormalServer(ns, serverName); const server = getNormalServer(ns, serverName);
const player = ns.getPlayer(); const player = ns.getPlayer();
const testResult = roundTo5Decimals(ezgame.formulas.hacking.weakenTime(server, player)); const testResult = roundTo5Decimals(formulas.hacking.weakenTime(server, player));
const actualResult = roundTo5Decimals(ns.formulas.hacking.weakenTime(server, player)); const actualResult = roundTo5Decimals(ns.formulas.hacking.weakenTime(server, player));
if (testResult !== actualResult) { if (testResult !== actualResult) {
throw new Error(`expected ${actualResult}, got ${testResult}`); throw new Error(`expected ${actualResult}, got ${testResult}`);

View file

@ -49,8 +49,10 @@ export const scanAnalyze = (ns: NS, options: ScanAnalyzeOptions = {}): Map<strin
} }
if (!options.quiet) { if (!options.quiet) {
const lines: string[] = [""];
// we are sure that home is always in the tree since its the starting point // we are sure that home is always in the tree since its the starting point
printTree(ns, tree.get("home") as ServerNode, tree); printTree(ns, lines, tree.get("home") as ServerNode, tree);
ns.tprint(lines.join("\n"));
} }
return tree; return tree;
@ -58,6 +60,7 @@ export const scanAnalyze = (ns: NS, options: ScanAnalyzeOptions = {}): Map<strin
const printTree = ( const printTree = (
ns: NS, ns: NS,
lines: string[],
node: ServerNode, node: ServerNode,
tree: Map<string, ServerNode>, tree: Map<string, ServerNode>,
prefix: string[] = [" "], prefix: string[] = [" "],
@ -71,14 +74,14 @@ const printTree = (
const titlePrefix = prefix.slice(0, prefix.length - 1).join("") + (isLast ? "┗ " : "┣ "); const titlePrefix = prefix.slice(0, prefix.length - 1).join("") + (isLast ? "┗ " : "┣ ");
const infoPrefix = prefix.join("") + (node.children.length > 0 ? "┃ " : " "); const infoPrefix = prefix.join("") + (node.children.length > 0 ? "┃ " : " ");
ns.tprint(`* ${titlePrefix}${node.hostname}`); lines.push(`${titlePrefix}${node.hostname}`);
ns.tprint( lines.push(
`* ${infoPrefix}Root Access: ${server.hasAdminRights ? "YES" : "NO"}, Required hacking skill: ${ `${infoPrefix}Root Access: ${server.hasAdminRights ? "YES" : "NO"}, Required hacking skill: ${
server.requiredHackingSkill ?? 0 server.requiredHackingSkill ?? 0
}`, }`,
); );
ns.tprint(`* ${infoPrefix}Number of open ports required to NUKE: ${server.numOpenPortsRequired ?? 0}`); lines.push(`${infoPrefix}Number of open ports required to NUKE: ${server.numOpenPortsRequired ?? 0}`);
ns.tprint(`* ${infoPrefix}RAM: ${utils.format.ram(server.maxRam)}, Player purchased: ${server.purchasedByPlayer}`); lines.push(`${infoPrefix}RAM: ${utils.format.ram(server.maxRam)}, Player purchased: ${server.purchasedByPlayer}`);
// recursive // recursive
node.children.forEach((childName, i) => { node.children.forEach((childName, i) => {
@ -88,7 +91,7 @@ const printTree = (
throw new Error(`Child node ${childName} not found in tree`); throw new Error(`Child node ${childName} not found in tree`);
} }
const childIsLast = i === node.children.length - 1; const childIsLast = i === node.children.length - 1;
printTree(ns, childNode, tree, [...prefix, childIsLast ? " " : "┃ "], childIsLast); printTree(ns, lines, childNode, tree, [...prefix, childIsLast ? " " : "┃ "], childIsLast);
}); });
}; };

View file

@ -27,6 +27,6 @@ export const scriptPropagator = (ns: NS) => {
return rootedHosts; return rootedHosts;
}; };
// export const main = (ns: NS) => { export const main = (ns: NS) => {
// scriptPropagator(ns); scriptPropagator(ns);
// }; };

View file

@ -40,7 +40,11 @@ export const startall = (ns: NS, scriptName: string, options: StartOptions = {})
} }
if (threadsToUse > 0) { if (threadsToUse > 0) {
ns.exec(scriptName, host, threadsToUse, ...args); const pid = ns.exec(scriptName, host, threadsToUse, ...args);
if (pid === 0) {
ns.tprint(`${host}: Process not started. Did you check if the script exists on the server?`);
return;
}
ns.tprint(`Started ${scriptName} on ${host} with ${threadsToUse} threads`); ns.tprint(`Started ${scriptName} on ${host} with ${threadsToUse} threads`);
totalThreadsStarted += threadsToUse; totalThreadsStarted += threadsToUse;
hostsStartedOn++; hostsStartedOn++;

View file

@ -1,6 +1,6 @@
import { NS } from "@ns"; import { NS } from "@ns";
export const main = async (ns: NS) => { export const main = async (ns: NS) => {
const bruh = await ns.prompt("hello", { type: "text", choices: ["asdf1", "asdf2"] }); const electron = eval(`require("electron")`);
console.log(bruh); ns.tprint(JSON.stringify(Object.keys(electron)));
}; };