From 62f7501b43152d4a26a548149db23a3e0d7f8b14 Mon Sep 17 00:00:00 2001 From: David Walker Date: Wed, 11 Feb 2026 08:42:27 -0800 Subject: [PATCH] REFACTOR: Change getFailureResult to checkDarknetServer (#2494) --- src/DarkNet/effects/offlineServerHandling.ts | 6 +- src/NetscriptFunctions.ts | 8 +- src/NetscriptFunctions/Darknet.ts | 136 +++++++++---------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/DarkNet/effects/offlineServerHandling.ts b/src/DarkNet/effects/offlineServerHandling.ts index 9194956ca..e4e29307c 100644 --- a/src/DarkNet/effects/offlineServerHandling.ts +++ b/src/DarkNet/effects/offlineServerHandling.ts @@ -14,7 +14,7 @@ import { CompletedProgramName } from "../../Enums"; import type { DarknetResponseCode } from "@nsdefs"; import { isIPAddress } from "../../Types/strings"; -type FailureResultOptions = { +type CheckDarknetServerOptions = { requireAdminRights?: boolean; requireSession?: boolean; requireDirectConnection?: boolean; @@ -34,10 +34,10 @@ export function expectDarknetAccess(ctx: NetscriptContext): void { } } -export function getFailureResult( +export function checkDarknetServer( ctx: NetscriptContext, host: string, - options: FailureResultOptions = {}, + options: CheckDarknetServerOptions = {}, ): | { success: true; code: DarknetResponseCode; message: string; server: DarknetServer } | { success: false; code: DarknetResponseCode; message: string } { diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 821a2c9ae..964d1c055 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -110,7 +110,7 @@ import { compile } from "./NetscriptJSEvaluator"; import { Script } from "./Script/Script"; import { NetscriptFormat } from "./NetscriptFunctions/Format"; import { DarknetState } from "./DarkNet/models/DarknetState"; -import { getFailureResult } from "./DarkNet/effects/offlineServerHandling"; +import { checkDarknetServer } from "./DarkNet/effects/offlineServerHandling"; import { DarknetServer } from "./Server/DarknetServer"; import { FragmentTypeEnum } from "./CotMG/FragmentType"; import { exampleDarknetServerData, ResponseCodeEnum } from "./DarkNet/Enums"; @@ -654,7 +654,7 @@ export const ns: InternalAPI = { const runOpts = helpers.runOptions(ctx, _thread_or_opt); const args = helpers.scriptArgs(ctx, _args); if ( - !getFailureResult(ctx, host, { + !checkDarknetServer(ctx, host, { allowNonDarknet: true, requireAdminRights: true, requireSession: true, @@ -784,7 +784,7 @@ export const ns: InternalAPI = { const destination = helpers.string(ctx, "destination", _destination); const source = helpers.string(ctx, "source", _source ?? ctx.workerScript.hostname); if ( - !getFailureResult(ctx, destination, { + !checkDarknetServer(ctx, destination, { allowNonDarknet: true, requireAdminRights: true, requireSession: true, @@ -792,7 +792,7 @@ export const ns: InternalAPI = { ) { return false; } - if (!getFailureResult(ctx, source, { allowNonDarknet: true }).success) { + if (!checkDarknetServer(ctx, source, { allowNonDarknet: true }).success) { return false; } const sourceServer = helpers.getServer(ctx, source); diff --git a/src/NetscriptFunctions/Darknet.ts b/src/NetscriptFunctions/Darknet.ts index 7b9b2b6ec..3489a0504 100644 --- a/src/NetscriptFunctions/Darknet.ts +++ b/src/NetscriptFunctions/Darknet.ts @@ -32,7 +32,7 @@ import { handleRamBlockRemoved } from "../DarkNet/effects/ramblock"; import { expectDarknetAccess, expectRunningOnDarknetServer, - getFailureResult, + checkDarknetServer, getTimeoutChance, isDirectConnected, logger, @@ -108,17 +108,17 @@ export function NetscriptDarknet(): InternalAPI { }. Attempted password starts with ${password.slice(0, 100)} `, ); } - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const threads = ctx.workerScript.scriptRef.threads; const networkDelay = calculateAuthenticationTime(server, Player, threads, password) + additionalMsec; @@ -131,16 +131,16 @@ export function NetscriptDarknet(): InternalAPI { ); return helpers.netscriptDelay(ctx, networkDelay).then(() => { - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { requireDirectConnection: true }); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true }); + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; // Authentication has a chance to timeout based on darknet instability if (Math.random() < getTimeoutChance()) { logger(ctx)(`Authentication to ${server.hostname} timed out due to network instability. Please try again.`); @@ -187,17 +187,17 @@ export function NetscriptDarknet(): InternalAPI { }. Attempted password starts with ${token.slice(0, 100)} `, ); } - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireAdminRights: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return { success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, }; } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const result = checkPassword(server, token, ctx.workerScript.scriptRef.threads, ctx.workerScript.pid); if (result.code !== ResponseCodeEnum.Success) { @@ -223,18 +223,18 @@ export function NetscriptDarknet(): InternalAPI { (_host, _opts): Promise => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); const options = heartbleedOptions(ctx, _opts); - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, logs: [], })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const networkDelay = calculateAuthenticationTime(server, Player, ctx.workerScript.scriptRef.threads) * 1.5 + (options.additionalMsec ?? 0); @@ -259,12 +259,12 @@ export function NetscriptDarknet(): InternalAPI { const xpGained = Player.mults.charisma_exp * 50 * ((500 + Player.skills.charisma) / 500); Player.gainCharismaExp(xpGained); - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { requireDirectConnection: true }); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true }); + if (!serverCheck.success) { return { success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, logs: [], }; } @@ -335,15 +335,15 @@ export function NetscriptDarknet(): InternalAPI { (_shouldLink): Promise => { const shouldLink = helpers.boolean(ctx, "shouldLink", _shouldLink ?? true); const targetHost = ctx.workerScript.getServer().hostname; - const onlineConnectionCheck = getFailureResult(ctx, targetHost); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost); + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const stasisLinkCount = getStasisLinkServers().length; const stasisLinkLimit = getStasisLinkLimit(); if (shouldLink && stasisLinkCount >= stasisLinkLimit) { @@ -379,9 +379,9 @@ export function NetscriptDarknet(): InternalAPI { }, getServerAuthDetails: (ctx) => (_host) => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); - const onlineConnectionCheck = getFailureResult(ctx, targetHost); - if (!onlineConnectionCheck.success) { - logger(ctx)(onlineConnectionCheck.message); + const serverCheck = checkDarknetServer(ctx, targetHost); + if (!serverCheck.success) { + logger(ctx)(serverCheck.message); return { isOnline: false, isConnectedToCurrentServer: false, @@ -394,7 +394,7 @@ export function NetscriptDarknet(): InternalAPI { passwordFormat: "numeric", } satisfies ReturnType; } - const targetServer = onlineConnectionCheck.server; + const targetServer = serverCheck.server; const localServer = ctx.workerScript.getServer(); const isConnected = isDirectConnected(localServer, targetServer); const hasSession = isAuthenticated(targetServer, ctx.workerScript.pid); @@ -412,19 +412,19 @@ export function NetscriptDarknet(): InternalAPI { }, packetCapture: (ctx) => (_host) => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, data: "", })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const networkDelay = calculateAuthenticationTime(server, Player, ctx.workerScript.scriptRef.threads) * 4; const xp = formatNumber(calculatePasswordAttemptChaGain(server, ctx.workerScript.scriptRef.threads), 1); @@ -442,15 +442,15 @@ export function NetscriptDarknet(): InternalAPI { (ctx) => (_host): Promise => { const targetHost = helpers.string(ctx, "host", _host); - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, preventUseOnStationaryServers: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } const hostOfCurrentServer = !isIPAddress(targetHost) @@ -465,23 +465,23 @@ export function NetscriptDarknet(): InternalAPI { message: message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; logger(ctx)(`Inducing server migration of ${server.hostname}... (Est: 6s)`); // induceServerMigration's delay is hardcoded at 6s. We should skip this delay in Jest tests. return helpers.netscriptDelay(ctx, !CONSTANTS.isInTestEnvironment ? 6000 : 0).then(() => { - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, preventUseOnStationaryServers: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; const currentDepth = server.depth; const result = chargeServerMigration(server, ctx.workerScript.scriptRef.threads); @@ -538,18 +538,18 @@ export function NetscriptDarknet(): InternalAPI { (ctx) => (_host): Promise => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, requireAdminRights: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; if (server.blockedRam <= 0) { logger(ctx)(`Server ${server.hostname} has no host-owned ram left to reallocate.`); @@ -564,18 +564,18 @@ export function NetscriptDarknet(): InternalAPI { const delayTime = Math.max(8000 * (500 / (500 + Player.skills.charisma)), 200); return helpers.netscriptDelay(ctx, delayTime).then(() => { - const onlineConnectionCheck = getFailureResult(ctx, targetHost, { + const serverCheck = checkDarknetServer(ctx, targetHost, { requireDirectConnection: true, requireAdminRights: true, }); - if (!onlineConnectionCheck.success) { + if (!serverCheck.success) { return helpers.netscriptDelay(ctx, 100).then(() => ({ success: false, - code: onlineConnectionCheck.code, - message: onlineConnectionCheck.message, + code: serverCheck.code, + message: serverCheck.message, })); } - const server = onlineConnectionCheck.server; + const server = serverCheck.server; if (server.blockedRam <= 0) { logger(ctx)(`Server ${server.hostname} has no host-owned ram left to reallocate.`); return { @@ -591,21 +591,21 @@ export function NetscriptDarknet(): InternalAPI { (ctx) => (_host): number => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); - const onlineConnectionCheck = getFailureResult(ctx, targetHost); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost); + if (!serverCheck.success) { return 0; } - return onlineConnectionCheck.server.blockedRam; + return serverCheck.server.blockedRam; }, getDepth: (ctx) => (_host): number => { const targetHost = helpers.string(ctx, "host", _host ?? ctx.workerScript.hostname); - const onlineConnectionCheck = getFailureResult(ctx, targetHost); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost); + if (!serverCheck.success) { return -1; } - return onlineConnectionCheck.server.depth; + return serverCheck.server.depth; }, promoteStock: (ctx: NetscriptContext) => @@ -660,11 +660,11 @@ export function NetscriptDarknet(): InternalAPI { (ctx) => (_host): number => { const targetHost = helpers.string(ctx, "host", _host); - const onlineConnectionCheck = getFailureResult(ctx, targetHost); - if (!onlineConnectionCheck.success) { + const serverCheck = checkDarknetServer(ctx, targetHost); + if (!serverCheck.success) { return -1; } - return onlineConnectionCheck.server.requiredCharismaSkill; + return serverCheck.server.requiredCharismaSkill; }, labreport: (ctx) => async () => { expectDarknetAccess(ctx);