First attempt at an implementation; requires script be running

This commit is contained in:
Undeemiss 2022-05-17 13:45:30 -05:00
parent 5526355a43
commit 2b9d408b55
2 changed files with 29 additions and 5 deletions

@ -55,7 +55,7 @@ import { makeRuntimeRejectMsg, netscriptDelay, resolveNetscriptRequestedThreads
import { numeralWrapper } from "./ui/numeralFormat"; import { numeralWrapper } from "./ui/numeralFormat";
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions"; 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 { arrayToString } from "./utils/helpers/arrayToString";
import { isString } from "./utils/helpers/isString"; import { isString } from "./utils/helpers/isString";
@ -193,7 +193,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
throw makeRuntimeRejectMsg( throw makeRuntimeRejectMsg(
workerScript, workerScript,
`Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` + `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( workerScript.log(
"weaken", "weaken",
() => () =>
`'${server.hostname}' security level weakened to ${ `'${server.hostname}' security level weakened to ${server.hackDifficulty
server.hackDifficulty
}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`, }. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`,
); );
workerScript.scriptRef.onlineExpGained += expGain; 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 { closeTail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void {
updateDynamicRam("closeTail", getRamCost(Player, "closeTail")); 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 { nuke: function (_hostname: unknown): boolean {
updateDynamicRam("nuke", getRamCost(Player, "nuke")); updateDynamicRam("nuke", getRamCost(Player, "nuke"));

@ -23,6 +23,7 @@ import { Settings } from "../../Settings/Settings";
let layerCounter = 0; let layerCounter = 0;
export const LogBoxEvents = new EventEmitter<[RunningScript]>(); export const LogBoxEvents = new EventEmitter<[RunningScript]>();
export const LogBoxCloserEvents = new EventEmitter<[RunningScript]>();
export const LogBoxClearEvents = new EventEmitter<[]>(); export const LogBoxClearEvents = new EventEmitter<[]>();
interface Log { 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(() => useEffect(() =>
LogBoxClearEvents.subscribe(() => { LogBoxClearEvents.subscribe(() => {
logs = []; logs = [];