From 2b9d408b55a5a166be43be1c0658d392353b1517 Mon Sep 17 00:00:00 2001 From: Undeemiss Date: Tue, 17 May 2022 13:45:30 -0500 Subject: [PATCH] First attempt at an implementation; requires script be running --- src/NetscriptFunctions.ts | 23 ++++++++++++++++++----- src/ui/React/LogBoxManager.tsx | 11 +++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index ed7e2cb8e..6c4b36637 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -55,7 +55,7 @@ import { makeRuntimeRejectMsg, netscriptDelay, resolveNetscriptRequestedThreads import { numeralWrapper } from "./ui/numeralFormat"; import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions"; -import { LogBoxEvents } from "./ui/React/LogBoxManager"; +import { LogBoxEvents, LogBoxCloserEvents } from "./ui/React/LogBoxManager"; import { arrayToString } from "./utils/helpers/arrayToString"; import { isString } from "./utils/helpers/isString"; @@ -193,7 +193,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { throw makeRuntimeRejectMsg( workerScript, `Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` + - `This is probably a bug. Please report to game developer`, + `This is probably a bug. Please report to game developer`, ); } @@ -826,8 +826,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { workerScript.log( "weaken", () => - `'${server.hostname}' security level weakened to ${ - server.hackDifficulty + `'${server.hostname}' security level weakened to ${server.hackDifficulty }. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`, ); workerScript.scriptRef.onlineExpGained += expGain; @@ -990,7 +989,21 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { }, closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void { updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); - // TODO + //Get the script object + let runningScriptObj; + if (arguments.length === 0) { + runningScriptObj = workerScript.scriptRef; + } else if (typeof fn === "number") { + runningScriptObj = getRunningScriptByPid(fn, "tail"); + } else { + runningScriptObj = getRunningScript(fn, hostname, "tail", scriptArgs); + } + if (runningScriptObj == null) { + workerScript.log("closeTail", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs)); + return; + } + //Emit an event to tell the game to close the tail window + LogBoxCloserEvents.emit(runningScriptObj); }, nuke: function (_hostname: unknown): boolean { updateDynamicRam("nuke", getRamCost(Player, "nuke")); diff --git a/src/ui/React/LogBoxManager.tsx b/src/ui/React/LogBoxManager.tsx index 023f38a19..1555df75d 100644 --- a/src/ui/React/LogBoxManager.tsx +++ b/src/ui/React/LogBoxManager.tsx @@ -23,6 +23,7 @@ import { Settings } from "../../Settings/Settings"; let layerCounter = 0; export const LogBoxEvents = new EventEmitter<[RunningScript]>(); +export const LogBoxCloserEvents = new EventEmitter<[RunningScript]>(); export const LogBoxClearEvents = new EventEmitter<[]>(); interface Log { @@ -51,6 +52,16 @@ export function LogBoxManager(): React.ReactElement { [], ); + //Event used by ns.closeTail to close tail windows + useEffect( + () => + LogBoxCloserEvents.subscribe((script: RunningScript) => { + const id = script.server + "-" + script.filename + script.args.map((x: any): string => `${x}`).join("-"); + close(id); + }), + [], + ); + useEffect(() => LogBoxClearEvents.subscribe(() => { logs = [];