unknown-ify half the basic API.

This commit is contained in:
Olivier Gagnon 2022-03-29 21:42:27 -04:00
parent cd1f01846f
commit aa0425a23d
3 changed files with 178 additions and 86 deletions

@ -232,6 +232,11 @@ interface IBitNodeMultipliers {
*/
WorldDaemonDifficulty: number;
/**
* Influences corporation dividends.
*/
CorporationSoftCap: number;
// Index signature
[key: string]: number;
}

@ -80,6 +80,12 @@ import {
Bladeburner as IBladeburner,
Stanek as IStanek,
SourceFileLvl,
BasicHGWOptions,
ProcessInfo,
HackingMultipliers,
HacknetMultipliers,
BitNodeMultipliers as IBNMults,
Server as IServerDef,
} from "./ScriptEditor/NetscriptDefinitions";
import { NetscriptSingularity } from "./NetscriptFunctions/Singularity";
@ -162,9 +168,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
* is not specified.
*/
const getRunningScript = function (
fn: any,
hostname: any,
callingFnName: any,
fn: string,
hostname: string,
callingFnName: string,
scriptArgs: any,
): RunningScript | null {
if (typeof callingFnName !== "string" || callingFnName === "") {
@ -193,7 +199,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return workerScript.scriptRef;
};
const getRunningScriptByPid = function (pid: any, callingFnName: any): RunningScript | null {
const getRunningScriptByPid = function (pid: number, callingFnName: string): RunningScript | null {
if (typeof callingFnName !== "string" || callingFnName === "") {
callingFnName = "getRunningScriptgetRunningScriptByPid";
}
@ -213,7 +219,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
* @param {any[]} scriptArgs - Running script's arguments
* @returns {string} Error message to print to logs
*/
const getCannotFindRunningScriptErrorMessage = function (fn: any, hostname: any, scriptArgs: any): string {
const getCannotFindRunningScriptErrorMessage = function (fn: string, hostname: string, scriptArgs: any): string {
if (!Array.isArray(scriptArgs)) {
scriptArgs = [];
}
@ -228,7 +234,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
* @param {string} callingFn - Name of calling function. For logging purposes
* @returns {boolean} True if the server is a Hacknet Server, false otherwise
*/
const failOnHacknetServer = function (server: any, callingFn: any = ""): boolean {
const failOnHacknetServer = function (server: BaseServer, callingFn = ""): boolean {
if (server instanceof HacknetServer) {
workerScript.log(callingFn, () => `Does not work on Hacknet Servers`);
return true;
@ -316,7 +322,11 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
};
const hack = function (hostname: any, manual: any, { threads: requestedThreads, stock }: any = {}): Promise<number> {
const hack = function (
hostname: string,
manual: boolean,
{ threads: requestedThreads, stock }: any = {},
): Promise<number> {
if (hostname === undefined) {
throw makeRuntimeErrorMsg("hack", "Takes 1 argument.");
}
@ -447,7 +457,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
getServer: safeGetServer,
checkSingularityAccess: checkSingularityAccess,
hack: hack,
getValidPort: (funcName: string, port: any): IPort => {
getValidPort: (funcName: string, port: number): IPort => {
if (isNaN(port)) {
throw makeRuntimeErrorMsg(
funcName,
@ -501,7 +511,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
hacknet: hacknet,
sprintf: sprintf,
vsprintf: vsprintf,
scan: function (hostname: any = workerScript.hostname): any {
scan: function (_hostname: unknown = workerScript.hostname): string[] {
const hostname = helper.string("scan", "hostname", _hostname);
updateDynamicRam("scan", getRamCost(Player, "scan"));
const server = safeGetServer(hostname, "scan");
const out = [];
@ -515,11 +526,14 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("scan", () => `returned ${server.serversOnNetwork.length} connections for ${server.hostname}`);
return out;
},
hack: function (hostname: any, { threads: requestedThreads, stock }: any = {}): any {
hack: function (_hostname: unknown, { threads: requestedThreads, stock }: BasicHGWOptions = {}): Promise<number> {
const hostname = helper.string("hack", "hostname", _hostname);
updateDynamicRam("hack", getRamCost(Player, "hack"));
return hack(hostname, false, { threads: requestedThreads, stock: stock });
},
hackAnalyzeThreads: function (hostname: any, hackAmount: any): any {
hackAnalyzeThreads: function (_hostname: unknown, _hackAmount: unknown): number {
const hostname = helper.string("hackAnalyzeThreads", "hostname", _hostname);
const hackAmount = helper.number("hackAnalyzeThreads", "hackAmount", _hackAmount);
updateDynamicRam("hackAnalyzeThreads", getRamCost(Player, "hackAnalyzeThreads"));
// Check argument validity
@ -549,43 +563,48 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return hackAmount / Math.floor(server.moneyAvailable * percentHacked);
},
hackAnalyze: function (hostname: any): any {
hackAnalyze: function (_hostname: unknown): number {
const hostname = helper.string("hackAnalyze", "hostname", _hostname);
updateDynamicRam("hackAnalyze", getRamCost(Player, "hackAnalyze"));
const server = safeGetServer(hostname, "hackAnalyze");
if (!(server instanceof Server)) {
workerScript.log("hackAnalyze", () => "Cannot be executed on this server.");
return false;
return 0;
}
return calculatePercentMoneyHacked(server, Player);
},
hackAnalyzeSecurity: function (threads: any): number {
hackAnalyzeSecurity: function (_threads: unknown): number {
const threads = helper.number("hackAnalyzeSecurity", "threads", _threads);
updateDynamicRam("hackAnalyzeSecurity", getRamCost(Player, "hackAnalyzeSecurity"));
return CONSTANTS.ServerFortifyAmount * threads;
},
hackAnalyzeChance: function (hostname: any): any {
hackAnalyzeChance: function (_hostname: unknown): number {
const hostname = helper.string("hackAnalyzeChance", "hostname", _hostname);
updateDynamicRam("hackAnalyzeChance", getRamCost(Player, "hackAnalyzeChance"));
const server = safeGetServer(hostname, "hackAnalyzeChance");
if (!(server instanceof Server)) {
workerScript.log("hackAnalyzeChance", () => "Cannot be executed on this server.");
return false;
return 0;
}
return calculateHackingChance(server, Player);
},
sleep: function (time: any): any {
sleep: async function (_time: unknown = 0): Promise<void> {
const time = helper.number("sleep", "time", _time);
updateDynamicRam("sleep", getRamCost(Player, "sleep"));
if (time === undefined) {
throw makeRuntimeErrorMsg("sleep", "Takes 1 argument.");
}
workerScript.log("sleep", () => `Sleeping for ${time} milliseconds`);
return netscriptDelay(time, workerScript).then(function () {
return Promise.resolve(true);
return Promise.resolve();
});
},
asleep: function (time: any): any {
asleep: function (_time: unknown = 0): Promise<void> {
const time = helper.number("asleep", "time", _time);
updateDynamicRam("asleep", getRamCost(Player, "asleep"));
if (time === undefined) {
throw makeRuntimeErrorMsg("asleep", "Takes 1 argument.");
@ -593,16 +612,24 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("asleep", () => `Sleeping for ${time} milliseconds`);
return new Promise((resolve) => setTimeout(resolve, time));
},
grow: function (hostname: any, { threads: requestedThreads, stock }: any = {}): any {
grow: async function (
_hostname: unknown,
{ threads: requestedThreads, stock }: BasicHGWOptions = {},
): Promise<number> {
const hostname = helper.string("grow", "hostname", _hostname);
updateDynamicRam("grow", getRamCost(Player, "grow"));
const threads = resolveNetscriptRequestedThreads(workerScript, "grow", requestedThreads);
const threads = resolveNetscriptRequestedThreads(
workerScript,
"grow",
requestedThreads ?? workerScript.scriptRef.threads,
);
if (hostname === undefined) {
throw makeRuntimeErrorMsg("grow", "Takes 1 argument.");
}
const server = safeGetServer(hostname, "grow");
if (!(server instanceof Server)) {
workerScript.log("grow", () => "Cannot be executed on this server.");
return false;
return Promise.resolve(0);
}
const host = GetServer(workerScript.hostname);
@ -648,14 +675,17 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return Promise.resolve(moneyAfter / moneyBefore);
});
},
growthAnalyze: function (hostname: any, growth: any, cores: any = 1): any {
growthAnalyze: function (_hostname: unknown, _growth: unknown, _cores: unknown = 1): number {
const hostname = helper.string("growthAnalyze", "hostname", _hostname);
const growth = helper.number("growthAnalyze", "growth", _growth);
const cores = helper.number("growthAnalyze", "cores", _cores);
updateDynamicRam("growthAnalyze", getRamCost(Player, "growthAnalyze"));
// Check argument validity
const server = safeGetServer(hostname, "growthAnalyze");
if (!(server instanceof Server)) {
workerScript.log("growthAnalyze", () => "Cannot be executed on this server.");
return false;
return 0;
}
if (typeof growth !== "number" || isNaN(growth) || growth < 1 || !isFinite(growth)) {
throw makeRuntimeErrorMsg("growthAnalyze", `Invalid argument: growth must be numeric and >= 1, is ${growth}.`);
@ -663,20 +693,26 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return numCycleForGrowth(server, Number(growth), Player, cores);
},
growthAnalyzeSecurity: function (threads: any): number {
growthAnalyzeSecurity: function (_threads: unknown): number {
const threads = helper.number("growthAnalyzeSecurity", "threads", _threads);
updateDynamicRam("growthAnalyzeSecurity", getRamCost(Player, "growthAnalyzeSecurity"));
return 2 * CONSTANTS.ServerFortifyAmount * threads;
},
weaken: function (hostname: any, { threads: requestedThreads }: any = {}): any {
weaken: async function (_hostname: unknown, { threads: requestedThreads }: BasicHGWOptions = {}): Promise<number> {
const hostname = helper.string("weaken", "hostname", _hostname);
updateDynamicRam("weaken", getRamCost(Player, "weaken"));
const threads = resolveNetscriptRequestedThreads(workerScript, "weaken", requestedThreads);
const threads = resolveNetscriptRequestedThreads(
workerScript,
"weaken",
requestedThreads ?? workerScript.scriptRef.threads,
);
if (hostname === undefined) {
throw makeRuntimeErrorMsg("weaken", "Takes 1 argument.");
}
const server = safeGetServer(hostname, "weaken");
if (!(server instanceof Server)) {
workerScript.log("weaken", () => "Cannot be executed on this server.");
return false;
return Promise.resolve(0);
}
// No root access or skill level too low
@ -716,12 +752,14 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return Promise.resolve(CONSTANTS.ServerWeakenAmount * threads * coreBonus);
});
},
weakenAnalyze: function (threads: any, cores: any = 1): number {
weakenAnalyze: function (_threads: unknown, _cores: unknown = 1): number {
const threads = helper.number("weakenAnalyze", "threads", _threads);
const cores = helper.number("weakenAnalyze", "cores", _cores);
updateDynamicRam("weakenAnalyze", getRamCost(Player, "weakenAnalyze"));
const coreBonus = 1 + (cores - 1) / 16;
return CONSTANTS.ServerWeakenAmount * threads * coreBonus * BitNodeMultipliers.ServerWeakenRate;
},
share: function (): Promise<void> {
share: async function (): Promise<void> {
updateDynamicRam("share", getRamCost(Player, "share"));
workerScript.log("share", () => "Sharing this computer.");
const end = StartSharing(workerScript.scriptRef.threads * calculateIntelligenceBonus(Player.intelligence, 2));
@ -741,7 +779,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
workerScript.print(argsToString(args));
},
printf: function (format: string, ...args: any[]): void {
printf: function (_format: unknown, ...args: any[]): void {
const format = helper.string("printf", "format", _format);
updateDynamicRam("printf", getRamCost(Player, "printf"));
if (typeof format !== "string") {
throw makeRuntimeErrorMsg("printf", "First argument must be string for the format.");
@ -772,7 +811,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
Terminal.print(`${workerScript.scriptRef.filename}: ${str}`);
},
tprintf: function (format: any, ...args: any): any {
tprintf: function (_format: unknown, ...args: any[]): void {
const format = helper.string("printf", "format", _format);
updateDynamicRam("tprintf", getRamCost(Player, "tprintf"));
if (typeof format !== "string") {
throw makeRuntimeErrorMsg("tprintf", "First argument must be string for the format.");
@ -797,14 +837,15 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
Terminal.print(`${str}`);
},
clearLog: function (): any {
clearLog: function (): void {
updateDynamicRam("clearLog", getRamCost(Player, "clearLog"));
workerScript.scriptRef.clearLog();
},
disableLog: function (fn: any): any {
disableLog: function (_fn: unknown): void {
const fn = helper.string("disableLog", "fn", _fn);
updateDynamicRam("disableLog", getRamCost(Player, "disableLog"));
if (fn === "ALL") {
for (fn of Object.keys(possibleLogs)) {
for (const fn of Object.keys(possibleLogs)) {
workerScript.disableLogs[fn] = true;
}
workerScript.log("disableLog", () => `Disabled logging for all functions`);
@ -815,10 +856,11 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("disableLog", () => `Disabled logging for ${fn}`);
}
},
enableLog: function (fn: any): any {
enableLog: function (_fn: unknown): void {
const fn = helper.string("enableLog", "fn", _fn);
updateDynamicRam("enableLog", getRamCost(Player, "enableLog"));
if (fn === "ALL") {
for (fn of Object.keys(possibleLogs)) {
for (const fn of Object.keys(possibleLogs)) {
delete workerScript.disableLogs[fn];
}
workerScript.log("enableLog", () => `Enabled logging for all functions`);
@ -828,24 +870,29 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
delete workerScript.disableLogs[fn];
workerScript.log("enableLog", () => `Enabled logging for ${fn}`);
},
isLogEnabled: function (fn: any): any {
isLogEnabled: function (_fn: unknown): boolean {
const fn = helper.string("isLogEnabled", "fn", _fn);
updateDynamicRam("isLogEnabled", getRamCost(Player, "isLogEnabled"));
if (possibleLogs[fn] === undefined) {
throw makeRuntimeErrorMsg("isLogEnabled", `Invalid argument: ${fn}.`);
}
return !workerScript.disableLogs[fn];
},
getScriptLogs: function (fn: any, hostname: any, ...scriptArgs: any): any {
getScriptLogs: function (_fn: unknown, _hostname: unknown, ...scriptArgs: any[]): string[] {
const fn = helper.string("getScriptLogs", "fn", _fn);
const hostname = helper.string("getScriptLogs", "hostname", _hostname);
updateDynamicRam("getScriptLogs", getRamCost(Player, "getScriptLogs"));
const runningScriptObj = getRunningScript(fn, hostname, "getScriptLogs", scriptArgs);
if (runningScriptObj == null) {
workerScript.log("getScriptLogs", () => getCannotFindRunningScriptErrorMessage(fn, hostname, scriptArgs));
return "";
return [];
}
return runningScriptObj.logs.slice();
},
tail: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any): any {
tail: function (_fn: unknown, _hostname: unknown = workerScript.hostname, ...scriptArgs: any[]): void {
const fn = helper.string("tail", "fn", _fn);
const hostname = helper.string("tail", "hostname", _hostname);
updateDynamicRam("tail", getRamCost(Player, "tail"));
let runningScriptObj;
if (arguments.length === 0) {
@ -862,7 +909,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
LogBoxEvents.emit(runningScriptObj);
},
nuke: function (hostname: any): boolean {
nuke: function (_hostname: unknown): boolean {
const hostname = helper.string("tail", "hostname", _hostname);
updateDynamicRam("nuke", getRamCost(Player, "nuke"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("nuke", "Takes 1 argument.");
@ -886,7 +934,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("nuke", () => `Executed NUKE.exe virus on '${server.hostname}' to gain root access.`);
return true;
},
brutessh: function (hostname: any): boolean {
brutessh: function (_hostname: unknown): boolean {
const hostname = helper.string("brutessh", "hostname", _hostname);
updateDynamicRam("brutessh", getRamCost(Player, "brutessh"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("brutessh", "Takes 1 argument.");
@ -908,7 +957,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return true;
},
ftpcrack: function (hostname: any): boolean {
ftpcrack: function (_hostname: unknown): boolean {
const hostname = helper.string("ftpcrack", "hostname", _hostname);
updateDynamicRam("ftpcrack", getRamCost(Player, "ftpcrack"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("ftpcrack", "Takes 1 argument.");
@ -930,7 +980,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return true;
},
relaysmtp: function (hostname: any): boolean {
relaysmtp: function (_hostname: unknown): boolean {
const hostname = helper.string("relaysmtp", "hostname", _hostname);
updateDynamicRam("relaysmtp", getRamCost(Player, "relaysmtp"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("relaysmtp", "Takes 1 argument.");
@ -952,7 +1003,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return true;
},
httpworm: function (hostname: any): boolean {
httpworm: function (_hostname: unknown): boolean {
const hostname = helper.string("httpworm", "hostname", _hostname);
updateDynamicRam("httpworm", getRamCost(Player, "httpworm"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("httpworm", "Takes 1 argument");
@ -974,7 +1026,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return true;
},
sqlinject: function (hostname: any): boolean {
sqlinject: function (_hostname: unknown): boolean {
const hostname = helper.string("sqlinject", "hostname", _hostname);
updateDynamicRam("sqlinject", getRamCost(Player, "sqlinject"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("sqlinject", "Takes 1 argument.");
@ -996,7 +1049,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return true;
},
run: function (scriptname: any, threads: any = 1, ...args: any[]): any {
run: function (_scriptname: unknown, _threads: unknown = 1, ...args: any[]): number {
const scriptname = helper.string("run", "scriptname", _scriptname);
const threads = helper.number("run", "threads", _threads);
updateDynamicRam("run", getRamCost(Player, "run"));
if (scriptname === undefined) {
throw makeRuntimeErrorMsg("run", "Usage: run(scriptname, [numThreads], [arg1], [arg2]...)");
@ -1011,7 +1066,10 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return runScriptFromScript(Player, "run", scriptServer, scriptname, args, workerScript, threads);
},
exec: function (scriptname: any, hostname: any, threads: any = 1, ...args: any[]): any {
exec: function (_scriptname: unknown, _hostname: unknown, _threads: unknown = 1, ...args: any[]): number {
const scriptname = helper.string("exec", "scriptname", _scriptname);
const hostname = helper.string("exec", "hostname", _hostname);
const threads = helper.number("exec", "threads", _threads);
updateDynamicRam("exec", getRamCost(Player, "exec"));
if (scriptname === undefined || hostname === undefined) {
throw makeRuntimeErrorMsg("exec", "Usage: exec(scriptname, server, [numThreads], [arg1], [arg2]...)");
@ -1022,7 +1080,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const server = safeGetServer(hostname, "exec");
return runScriptFromScript(Player, "exec", server, scriptname, args, workerScript, threads);
},
spawn: function (scriptname: any, threads: any = 1, ...args: any[]): any {
spawn: function (_scriptname: unknown, _threads: unknown = 1, ...args: any[]): void {
const scriptname = helper.string("spawn", "scriptname", _scriptname);
const threads = helper.number("spawn", "threads", _threads);
updateDynamicRam("spawn", getRamCost(Player, "spawn"));
if (!scriptname || !threads) {
throw makeRuntimeErrorMsg("spawn", "Usage: spawn(scriptname, threads)");
@ -1048,7 +1108,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("spawn", () => "Exiting...");
}
},
kill: function (filename: any, hostname?: any, ...scriptArgs: any): any {
kill: function (_filename: unknown, _hostname?: unknown, ...scriptArgs: any[]): boolean {
const filename = helper.string("kill", "filename", _filename);
const hostname = helper.string("kill", "hostname", _hostname);
updateDynamicRam("kill", getRamCost(Player, "kill"));
let res;
@ -1094,7 +1156,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return false;
}
},
killall: function (hostname: any = workerScript.hostname): any {
killall: function (_hostname: unknown = workerScript.hostname): boolean {
const hostname = helper.string("killall", "hostname", _hostname);
updateDynamicRam("killall", getRamCost(Player, "killall"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("killall", "Takes 1 argument");
@ -1112,7 +1175,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return scriptsRunning;
},
exit: function (): any {
exit: function (): void {
updateDynamicRam("exit", getRamCost(Player, "exit"));
workerScript.running = false; // Prevent workerScript from "finishing execution naturally"
if (killWorkerScript(workerScript)) {
@ -1121,7 +1184,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("exit", () => "Failed. This is a bug. Report to dev.");
}
},
scp: async function (scriptname: any, hostname1: any, hostname2?: any): Promise<boolean> {
scp: async function (scriptname: any, _hostname1: unknown, hostname2?: any): Promise<boolean> {
const hostname1 = helper.string("scp", "hostname1", _hostname1);
updateDynamicRam("scp", getRamCost(Player, "scp"));
if (arguments.length !== 2 && arguments.length !== 3) {
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
@ -1278,7 +1342,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
newScript.updateRamUsage(Player, destServer.scripts).then(() => resolve(true));
});
},
ls: function (hostname: any, grep: any): any {
ls: function (_hostname: unknown, _grep: unknown = ""): string[] {
const hostname = helper.string("ls", "hostname", _hostname);
const grep = helper.string("ls", "grep", _grep);
updateDynamicRam("ls", getRamCost(Player, "ls"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("ls", "Usage: ls(hostname/ip, [grep filter])");
@ -1345,7 +1411,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
allFiles.sort();
return allFiles;
},
ps: function (hostname: any = workerScript.hostname): any {
ps: function (_hostname: unknown = workerScript.hostname): ProcessInfo[] {
const hostname = helper.string("ps", "hostname", _hostname);
updateDynamicRam("ps", getRamCost(Player, "ps"));
const server = safeGetServer(hostname, "ps");
const processes = [];
@ -1359,7 +1426,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return processes;
},
hasRootAccess: function (hostname: any): any {
hasRootAccess: function (_hostname: unknown): boolean {
const hostname = helper.string("hasRootAccess", "hostname", _hostname);
updateDynamicRam("hasRootAccess", getRamCost(Player, "hasRootAccess"));
if (hostname === undefined) {
throw makeRuntimeErrorMsg("hasRootAccess", "Takes 1 argument");
@ -1367,7 +1435,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const server = safeGetServer(hostname, "hasRootAccess");
return server.hasAdminRights;
},
getHostname: function (): any {
getHostname: function (): string {
updateDynamicRam("getHostname", getRamCost(Player, "getHostname"));
const scriptServer = GetServer(workerScript.hostname);
if (scriptServer == null) {
@ -1375,13 +1443,13 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
}
return scriptServer.hostname;
},
getHackingLevel: function (): any {
getHackingLevel: function (): number {
updateDynamicRam("getHackingLevel", getRamCost(Player, "getHackingLevel"));
Player.updateSkillLevels();
workerScript.log("getHackingLevel", () => `returned ${Player.hacking}`);
return Player.hacking;
},
getHackingMultipliers: function (): any {
getHackingMultipliers: function (): HackingMultipliers {
updateDynamicRam("getHackingMultipliers", getRamCost(Player, "getHackingMultipliers"));
return {
chance: Player.hacking_chance_mult,
@ -1390,7 +1458,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
growth: Player.hacking_grow_mult,
};
},
getHacknetMultipliers: function (): any {
getHacknetMultipliers: function (): HacknetMultipliers {
updateDynamicRam("getHacknetMultipliers", getRamCost(Player, "getHacknetMultipliers"));
return {
production: Player.hacknet_node_money_mult,
@ -1400,7 +1468,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
levelCost: Player.hacknet_node_level_cost_mult,
};
},
getBitNodeMultipliers: function (): any {
getBitNodeMultipliers: function (): IBNMults {
updateDynamicRam("getBitNodeMultipliers", getRamCost(Player, "getBitNodeMultipliers"));
if (SourceFileFlags[5] <= 0 && Player.bitNodeN !== 5) {
throw makeRuntimeErrorMsg("getBitNodeMultipliers", "Requires Source-File 5 to run.");
@ -1408,7 +1476,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const copy = Object.assign({}, BitNodeMultipliers);
return copy;
},
getServer: function (hostname: any = workerScript.hostname): any {
getServer: function (_hostname: unknown = workerScript.hostname): IServerDef {
const hostname = helper.string("getServer", "hostname", _hostname);
updateDynamicRam("getServer", getRamCost(Player, "getServer"));
const server = safeGetServer(hostname, "getServer");
const copy = Object.assign({}, server) as any;
@ -1431,7 +1500,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
if (!copy.serverGrowth) copy.serverGrowth = 0;
return copy;
},
getServerMoneyAvailable: function (hostname: any): any {
getServerMoneyAvailable: function (_hostname: unknown): number {
const hostname = helper.string("getServerMoneyAvailable", "hostname", _hostname);
updateDynamicRam("getServerMoneyAvailable", getRamCost(Player, "getServerMoneyAvailable"));
const server = safeGetServer(hostname, "getServerMoneyAvailable");
if (!(server instanceof Server)) {
@ -1455,7 +1525,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.moneyAvailable;
},
getServerSecurityLevel: function (hostname: any): any {
getServerSecurityLevel: function (_hostname: unknown): number {
const hostname = helper.string("getServerSecurityLevel", "hostname", _hostname);
updateDynamicRam("getServerSecurityLevel", getRamCost(Player, "getServerSecurityLevel"));
const server = safeGetServer(hostname, "getServerSecurityLevel");
if (!(server instanceof Server)) {
@ -1471,7 +1542,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.hackDifficulty;
},
getServerBaseSecurityLevel: function (hostname: any): any {
getServerBaseSecurityLevel: function (_hostname: unknown): number {
const hostname = helper.string("getServerBaseSecurityLevel", "hostname", _hostname);
updateDynamicRam("getServerBaseSecurityLevel", getRamCost(Player, "getServerBaseSecurityLevel"));
workerScript.log(
"getServerBaseSecurityLevel",
@ -1491,7 +1563,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.baseDifficulty;
},
getServerMinSecurityLevel: function (hostname: any): any {
getServerMinSecurityLevel: function (_hostname: unknown): number {
const hostname = helper.string("getServerMinSecurityLevel", "hostname", _hostname);
updateDynamicRam("getServerMinSecurityLevel", getRamCost(Player, "getServerMinSecurityLevel"));
const server = safeGetServer(hostname, "getServerMinSecurityLevel");
if (!(server instanceof Server)) {
@ -1507,7 +1580,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.minDifficulty;
},
getServerRequiredHackingLevel: function (hostname: any): any {
getServerRequiredHackingLevel: function (_hostname: unknown): number {
const hostname = helper.string("getServerRequiredHackingLevel", "hostname", _hostname);
updateDynamicRam("getServerRequiredHackingLevel", getRamCost(Player, "getServerRequiredHackingLevel"));
const server = safeGetServer(hostname, "getServerRequiredHackingLevel");
if (!(server instanceof Server)) {
@ -1523,7 +1597,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.requiredHackingSkill;
},
getServerMaxMoney: function (hostname: any): any {
getServerMaxMoney: function (_hostname: unknown): number {
const hostname = helper.string("getServerMaxMoney", "hostname", _hostname);
updateDynamicRam("getServerMaxMoney", getRamCost(Player, "getServerMaxMoney"));
const server = safeGetServer(hostname, "getServerMaxMoney");
if (!(server instanceof Server)) {
@ -1539,7 +1614,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.moneyMax;
},
getServerGrowth: function (hostname: any): any {
getServerGrowth: function (_hostname: unknown): number {
const hostname = helper.string("getServerGrowth", "hostname", _hostname);
updateDynamicRam("getServerGrowth", getRamCost(Player, "getServerGrowth"));
const server = safeGetServer(hostname, "getServerGrowth");
if (!(server instanceof Server)) {
@ -1552,7 +1628,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
workerScript.log("getServerGrowth", () => `returned ${server.serverGrowth} for '${server.hostname}'`);
return server.serverGrowth;
},
getServerNumPortsRequired: function (hostname: any): any {
getServerNumPortsRequired: function (_hostname: unknown): number {
const hostname = helper.string("getServerNumPortsRequired", "hostname", _hostname);
updateDynamicRam("getServerNumPortsRequired", getRamCost(Player, "getServerNumPortsRequired"));
const server = safeGetServer(hostname, "getServerNumPortsRequired");
if (!(server instanceof Server)) {
@ -1568,7 +1645,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return server.numOpenPortsRequired;
},
getServerRam: function (hostname: any): any {
getServerRam: function (_hostname: unknown): [number, number] {
const hostname = helper.string("getServerRam", "hostname", _hostname);
updateDynamicRam("getServerRam", getRamCost(Player, "getServerRam"));
workerScript.log(
"getServerRam",
@ -1581,23 +1659,28 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return [server.maxRam, server.ramUsed];
},
getServerMaxRam: function (hostname: any): any {
getServerMaxRam: function (_hostname: unknown): number {
const hostname = helper.string("getServerMaxRam", "hostname", _hostname);
updateDynamicRam("getServerMaxRam", getRamCost(Player, "getServerMaxRam"));
const server = safeGetServer(hostname, "getServerMaxRam");
workerScript.log("getServerMaxRam", () => `returned ${numeralWrapper.formatRAM(server.maxRam)}`);
return server.maxRam;
},
getServerUsedRam: function (hostname: any): any {
getServerUsedRam: function (_hostname: unknown): number {
const hostname = helper.string("getServerUsedRam", "hostname", _hostname);
updateDynamicRam("getServerUsedRam", getRamCost(Player, "getServerUsedRam"));
const server = safeGetServer(hostname, "getServerUsedRam");
workerScript.log("getServerUsedRam", () => `returned ${numeralWrapper.formatRAM(server.ramUsed)}`);
return server.ramUsed;
},
serverExists: function (hostname: any): any {
serverExists: function (_hostname: unknown): boolean {
const hostname = helper.string("serverExists", "hostname", _hostname);
updateDynamicRam("serverExists", getRamCost(Player, "serverExists"));
return GetServer(hostname) !== null;
},
fileExists: function (filename: any, hostname: any = workerScript.hostname): any {
fileExists: function (_filename: unknown, _hostname: unknown = workerScript.hostname): boolean {
const filename = helper.string("fileExists", "filename", _filename);
const hostname = helper.string("fileExists", "hostname", _hostname);
updateDynamicRam("fileExists", getRamCost(Player, "fileExists"));
if (filename === undefined) {
throw makeRuntimeErrorMsg("fileExists", "Usage: fileExists(scriptname, [server])");
@ -1621,7 +1704,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const txtFile = getTextFile(filename, server);
return txtFile != null;
},
isRunning: function (fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any): any {
isRunning: function (_fn: unknown, _hostname: unknown = workerScript.hostname, ...scriptArgs: any[]): boolean {
const fn = helper.string("isRunning", "fn", _fn);
const hostname = helper.string("isRunning", "hostname", _hostname);
updateDynamicRam("isRunning", getRamCost(Player, "isRunning"));
if (fn === undefined || hostname === undefined) {
throw makeRuntimeErrorMsg("isRunning", "Usage: isRunning(scriptname, server, [arg1], [arg2]...)");
@ -1632,17 +1717,18 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return getRunningScript(fn, hostname, "isRunning", scriptArgs) != null;
}
},
getPurchasedServerLimit: function (): any {
getPurchasedServerLimit: function (): number {
updateDynamicRam("getPurchasedServerLimit", getRamCost(Player, "getPurchasedServerLimit"));
return getPurchaseServerLimit();
},
getPurchasedServerMaxRam: function (): any {
getPurchasedServerMaxRam: function (): number {
updateDynamicRam("getPurchasedServerMaxRam", getRamCost(Player, "getPurchasedServerMaxRam"));
return getPurchaseServerMaxRam();
},
getPurchasedServerCost: function (ram: any): any {
getPurchasedServerCost: function (_ram: unknown): number {
const ram = helper.number("getPurchasedServerCost", "ram", _ram);
updateDynamicRam("getPurchasedServerCost", getRamCost(Player, "getPurchasedServerCost"));
const cost = getPurchaseServerCost(ram);
@ -1653,10 +1739,10 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
return cost;
},
purchaseServer: function (aname: any, aram: any): any {
purchaseServer: function (_name: unknown, _ram: unknown): string {
const name = helper.string("purchaseServer", "name", _name);
const ram = helper.number("purchaseServer", "ram", _ram);
if (arguments.length !== 2) throw makeRuntimeErrorMsg("purchaseServer", "Takes 2 arguments");
const name = helper.string("purchaseServer", "name", aname);
const ram = helper.number("purchaseServer", "ram", aram);
updateDynamicRam("purchaseServer", getRamCost(Player, "purchaseServer"));
let hostnameStr = String(name);
hostnameStr = hostnameStr.replace(/\s+/g, "");
@ -1717,7 +1803,8 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return newServ.hostname;
},
deleteServer: function (name: any): any {
deleteServer: function (_name: unknown): boolean {
const name = helper.string("purchaseServer", "name", _name);
updateDynamicRam("deleteServer", getRamCost(Player, "deleteServer"));
let hostnameStr = String(name);
hostnameStr = hostnameStr.replace(/\s\s+/g, "");
@ -1793,7 +1880,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
);
return false;
},
getPurchasedServers: function (): any {
getPurchasedServers: function (): string[] {
updateDynamicRam("getPurchasedServers", getRamCost(Player, "getPurchasedServers"));
const res: string[] = [];
Player.purchasedServers.forEach(function (hostname) {

@ -584,7 +584,7 @@ export interface BitNodeMultipliers {
/** Influences the maximum allowed RAM for a purchased server */
PurchasedServerMaxRam: number;
/** Influences cost of any purchased server at or above 128GB */
PurchasedServerSoftCap: number;
PurchasedServerSoftcap: number;
/** Influences the minimum favor the player must have with a faction before they can donate to gain rep. */
RepToDonateToFaction: number;
/** Influences how much the money on a server can be reduced when a script performs a hack against it. */