2021-09-05 01:09:30 +02:00
|
|
|
import { vsprintf, sprintf } from "sprintf-js";
|
|
|
|
import * as libarg from "arg";
|
2017-10-12 22:10:34 +02:00
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
import { getRamCost } from "./Netscript/RamCostGenerator";
|
2019-06-22 10:12:17 +02:00
|
|
|
import { WorkerScriptStartStopEventEmitter } from "./Netscript/WorkerScriptStartStopEventEmitter";
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Augmentations } from "./Augmentation/Augmentations";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { augmentationExists, installAugmentations } from "./Augmentation/AugmentationHelpers";
|
2021-03-07 10:49:57 +01:00
|
|
|
import { prestigeAugmentation } from "./Prestige";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { AugmentationNames } from "./Augmentation/data/AugmentationNames";
|
|
|
|
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
|
|
|
|
import { findCrime } from "./Crime/CrimeHelpers";
|
2021-08-17 00:43:55 +02:00
|
|
|
import { Bladeburner } from "./Bladeburner/Bladeburner";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Company } from "./Company/Company";
|
2021-05-01 09:17:31 +02:00
|
|
|
import { Companies } from "./Company/Companies";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { CompanyPosition } from "./Company/CompanyPosition";
|
|
|
|
import { CompanyPositions } from "./Company/CompanyPositions";
|
|
|
|
import { CONSTANTS } from "./Constants";
|
|
|
|
import { DarkWebItems } from "./DarkWeb/DarkWebItems";
|
2021-09-02 04:16:48 +02:00
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
NewIndustry,
|
|
|
|
NewCity,
|
|
|
|
UnlockUpgrade,
|
|
|
|
LevelUpgrade,
|
|
|
|
IssueDividends,
|
|
|
|
SellMaterial,
|
|
|
|
SellProduct,
|
|
|
|
SetSmartSupply,
|
|
|
|
BuyMaterial,
|
2021-09-10 06:13:28 +02:00
|
|
|
AssignJob,
|
|
|
|
UpgradeOfficeSize,
|
|
|
|
ThrowParty,
|
2021-09-10 06:53:39 +02:00
|
|
|
PurchaseWarehouse,
|
|
|
|
UpgradeWarehouse,
|
|
|
|
BuyCoffee,
|
|
|
|
HireAdVert,
|
|
|
|
MakeProduct,
|
2021-09-10 08:17:55 +02:00
|
|
|
Research,
|
|
|
|
ExportMaterial,
|
|
|
|
CancelExportMaterial,
|
|
|
|
SetMaterialMarketTA1,
|
|
|
|
SetMaterialMarketTA2,
|
|
|
|
SetProductMarketTA1,
|
|
|
|
SetProductMarketTA2,
|
2021-09-05 01:09:30 +02:00
|
|
|
} from "./Corporation/Actions";
|
2021-09-02 04:16:48 +02:00
|
|
|
import { CorporationUnlockUpgrades } from "./Corporation/data/CorporationUnlockUpgrades";
|
|
|
|
import { CorporationUpgrades } from "./Corporation/data/CorporationUpgrades";
|
2019-04-11 10:37:40 +02:00
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
calculateHackingChance,
|
|
|
|
calculateHackingExpGain,
|
|
|
|
calculatePercentMoneyHacked,
|
|
|
|
calculateHackingTime,
|
|
|
|
calculateGrowTime,
|
|
|
|
calculateWeakenTime,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Hacking";
|
2021-03-31 06:45:21 +02:00
|
|
|
import { calculateServerGrowth } from "./Server/formulas/grow";
|
2021-06-16 06:28:20 +02:00
|
|
|
import { AllGangs } from "./Gang/AllGangs";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Factions, factionExists } from "./Faction/Factions";
|
|
|
|
import { joinFaction, purchaseAugmentation } from "./Faction/FactionHelpers";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { netscriptCanGrow, netscriptCanHack, netscriptCanWeaken } from "./Hacking/netscriptCanHack";
|
2019-03-30 04:01:34 +01:00
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
calculateMoneyGainRate,
|
|
|
|
calculateLevelUpgradeCost,
|
|
|
|
calculateRamUpgradeCost,
|
|
|
|
calculateCoreUpgradeCost,
|
|
|
|
calculateNodeCost,
|
2021-03-31 06:45:21 +02:00
|
|
|
} from "./Hacknet/formulas/HacknetNodes";
|
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
calculateHashGainRate as HScalculateHashGainRate,
|
|
|
|
calculateLevelUpgradeCost as HScalculateLevelUpgradeCost,
|
|
|
|
calculateRamUpgradeCost as HScalculateRamUpgradeCost,
|
|
|
|
calculateCoreUpgradeCost as HScalculateCoreUpgradeCost,
|
|
|
|
calculateCacheUpgradeCost as HScalculateCacheUpgradeCost,
|
|
|
|
calculateServerCost as HScalculateServerCost,
|
2021-03-31 06:45:21 +02:00
|
|
|
} from "./Hacknet/formulas/HacknetServers";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { HacknetNodeConstants, HacknetServerConstants } from "./Hacknet/data/Constants";
|
2021-05-01 09:17:31 +02:00
|
|
|
import { HacknetServer } from "./Hacknet/HacknetServer";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { CityName } from "./Locations/data/CityNames";
|
|
|
|
import { LocationName } from "./Locations/data/LocationNames";
|
2021-03-23 21:02:34 +01:00
|
|
|
import { Terminal } from "./Terminal";
|
2021-09-05 01:09:30 +02:00
|
|
|
import { calculateSkill, calculateExp } from "./PersonObjects/formulas/skill";
|
2019-04-04 02:12:11 +02:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { Message } from "./Message/Message";
|
|
|
|
import { Player } from "./Player";
|
|
|
|
import { Programs } from "./Programs/Programs";
|
|
|
|
import { Script } from "./Script/Script";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { findRunningScript, findRunningScriptByPid } from "./Script/ScriptHelpers";
|
2021-09-24 22:34:21 +02:00
|
|
|
import { isScriptFilename } from "./Script/isScriptFilename";
|
2021-10-02 04:53:23 +02:00
|
|
|
import { PromptEvent } from "./ui/React/PromptManager";
|
2021-09-24 22:34:21 +02:00
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
import { GetServer, GetAllServers, DeleteServer, AddToAllServers, createUniqueRandomIp } from "./Server/AllServers";
|
2021-03-12 02:37:58 +01:00
|
|
|
import { RunningScript } from "./Script/RunningScript";
|
2019-04-11 10:37:40 +02:00
|
|
|
import {
|
2021-09-05 01:09:30 +02:00
|
|
|
getServerOnNetwork,
|
|
|
|
numCycleForGrowth,
|
|
|
|
processSingleServerGrowth,
|
|
|
|
safetlyCreateUniqueServer,
|
2019-04-11 10:37:40 +02:00
|
|
|
} from "./Server/ServerHelpers";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { getPurchaseServerCost, getPurchaseServerLimit, getPurchaseServerMaxRam } from "./Server/ServerPurchases";
|
2021-09-25 03:49:49 +02:00
|
|
|
import { Server } from "./Server/Server";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { SourceFileFlags } from "./SourceFile/SourceFileFlags";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { buyStock, sellStock, shortStock, sellShort } from "./StockMarket/BuyingAndSelling";
|
|
|
|
import { influenceStockThroughServerHack, influenceStockThroughServerGrow } from "./StockMarket/PlayerInfluencing";
|
2021-09-17 08:31:19 +02:00
|
|
|
import { StockMarket, SymbolToStockMap, placeOrder, cancelOrder } from "./StockMarket/StockMarket";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { getBuyTransactionCost, getSellTransactionGain } from "./StockMarket/StockMarketHelpers";
|
2019-04-23 10:48:15 +02:00
|
|
|
import { OrderTypes } from "./StockMarket/data/OrderTypes";
|
|
|
|
import { PositionTypes } from "./StockMarket/data/PositionTypes";
|
|
|
|
import { StockSymbols } from "./StockMarket/data/StockSymbols";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { getStockMarket4SDataCost, getStockMarket4STixApiCost } from "./StockMarket/StockMarketCosts";
|
|
|
|
import { isValidFilePath, removeLeadingSlash } from "./Terminal/DirectoryHelpers";
|
2019-04-11 10:37:40 +02:00
|
|
|
import { TextFile, getTextFile, createTextFile } from "./TextFile";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
import { NetscriptPorts, runScriptFromScript, startWorkerScript } from "./NetscriptWorker";
|
2019-05-16 08:05:36 +02:00
|
|
|
import { killWorkerScript } from "./Netscript/killWorkerScript";
|
|
|
|
import { workerScripts } from "./Netscript/WorkerScripts";
|
2021-09-25 03:49:49 +02:00
|
|
|
import { WorkerScript } from "./Netscript/WorkerScript";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { makeRuntimeRejectMsg, netscriptDelay, resolveNetscriptRequestedThreads } from "./NetscriptEvaluator";
|
2021-09-25 00:42:13 +02:00
|
|
|
import { Interpreter } from "./ThirdParty/JSInterpreter";
|
2021-09-20 07:45:32 +02:00
|
|
|
import { Router } from "./ui/GameRoot";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { numeralWrapper } from "./ui/numeralFormat";
|
|
|
|
import { is2DArray } from "./utils/helpers/is2DArray";
|
2021-09-25 20:42:57 +02:00
|
|
|
import { convertTimeMsToTimeElapsedString } from "./utils/StringHelperFunctions";
|
2021-10-12 04:34:04 +02:00
|
|
|
import { SpecialServers } from "./Server/data/SpecialServers";
|
2019-02-27 03:26:29 +01:00
|
|
|
|
2021-10-01 07:00:50 +02:00
|
|
|
import { LogBoxEvents } from "./ui/React/LogBoxManager";
|
2021-09-25 20:42:57 +02:00
|
|
|
import { arrayToString } from "./utils/helpers/arrayToString";
|
|
|
|
import { isString } from "./utils/helpers/isString";
|
2017-08-21 18:59:06 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
import { OfficeSpace } from "./Corporation/OfficeSpace";
|
|
|
|
import { Employee } from "./Corporation/Employee";
|
|
|
|
import { Product } from "./Corporation/Product";
|
|
|
|
import { Material } from "./Corporation/Material";
|
|
|
|
import { Warehouse } from "./Corporation/Warehouse";
|
|
|
|
import { IIndustry } from "./Corporation/IIndustry";
|
|
|
|
|
|
|
|
import { Faction } from "./Faction/Faction";
|
|
|
|
import { Augmentation } from "./Augmentation/Augmentation";
|
2021-10-12 05:14:15 +02:00
|
|
|
import { Page } from "./ui/Router";
|
2021-09-25 03:49:49 +02:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
import { CodingContract } from "./CodingContracts";
|
|
|
|
import { Stock } from "./StockMarket/Stock";
|
|
|
|
import { BaseServer } from "./Server/BaseServer";
|
2021-10-05 03:06:55 +02:00
|
|
|
import { INetscriptGang, NetscriptGang } from "./NetscriptFunctions/Gang";
|
2021-10-05 04:25:21 +02:00
|
|
|
import { INetscriptSleeve, NetscriptSleeve } from "./NetscriptFunctions/Sleeve";
|
|
|
|
import { INetscriptExtra, NetscriptExtra } from "./NetscriptFunctions/Extra";
|
|
|
|
import { INetscriptHacknet, NetscriptHacknet } from "./NetscriptFunctions/Hacknet";
|
2021-10-13 02:02:37 +02:00
|
|
|
import { dialogBoxCreate } from "./ui/React/DialogBox";
|
2021-09-25 08:36:49 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const defaultInterpreter = new Interpreter("", () => undefined);
|
2021-04-21 14:20:26 +02:00
|
|
|
|
|
|
|
// the acorn interpreter has a bug where it doesn't convert arrays correctly.
|
|
|
|
// so we have to more or less copy it here.
|
2021-09-25 03:49:49 +02:00
|
|
|
function toNative(pseudoObj: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (pseudoObj == null) return null;
|
|
|
|
if (
|
|
|
|
!pseudoObj.hasOwnProperty("properties") ||
|
|
|
|
!pseudoObj.hasOwnProperty("getter") ||
|
|
|
|
!pseudoObj.hasOwnProperty("setter") ||
|
|
|
|
!pseudoObj.hasOwnProperty("proto")
|
|
|
|
) {
|
|
|
|
return pseudoObj; // it wasn't a pseudo object anyway.
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
let nativeObj: any;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (pseudoObj.hasOwnProperty("class") && pseudoObj.class === "Array") {
|
|
|
|
nativeObj = [];
|
|
|
|
const length = defaultInterpreter.getProperty(pseudoObj, "length");
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
if (defaultInterpreter.hasProperty(pseudoObj, i)) {
|
|
|
|
nativeObj[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i));
|
2021-04-21 14:20:26 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Object.
|
|
|
|
nativeObj = {};
|
2021-09-25 07:26:03 +02:00
|
|
|
for (const key in pseudoObj.properties) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const val = pseudoObj.properties[key];
|
|
|
|
nativeObj[key] = toNative(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nativeObj;
|
2021-04-21 14:20:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 04:25:21 +02:00
|
|
|
interface NS extends INetscriptExtra {
|
2021-09-25 03:49:49 +02:00
|
|
|
[key: string]: any;
|
2021-10-05 04:25:21 +02:00
|
|
|
hacknet: INetscriptHacknet;
|
2021-10-05 03:06:55 +02:00
|
|
|
gang: INetscriptGang;
|
2021-10-05 04:25:21 +02:00
|
|
|
sleeve: INetscriptSleeve;
|
2021-09-25 03:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function NetscriptFunctions(workerScript: WorkerScript): NS {
|
2021-09-25 08:36:49 +02:00
|
|
|
const updateDynamicRam = function (fnName: string, ramCost: number): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (workerScript.dynamicLoadedFns[fnName]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
workerScript.dynamicLoadedFns[fnName] = true;
|
|
|
|
|
|
|
|
let threads = workerScript.scriptRef.threads;
|
|
|
|
if (typeof threads !== "number") {
|
2021-10-07 23:55:49 +02:00
|
|
|
console.warn(`WorkerScript detected NaN for threadcount for ${workerScript.name} on ${workerScript.hostname}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
threads = 1;
|
2018-09-23 02:25:48 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.dynamicRamUsage += ramCost * threads;
|
|
|
|
if (workerScript.dynamicRamUsage > 1.01 * workerScript.ramUsage) {
|
|
|
|
throw makeRuntimeRejectMsg(
|
|
|
|
workerScript,
|
2021-09-05 01:27:16 +02:00
|
|
|
`Dynamic RAM usage calculated to be greater than initial RAM usage on fn: ${fnName}.
|
|
|
|
This is probably because you somehow circumvented the static RAM calculation.
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
Dynamic RAM Usage: ${numeralWrapper.formatRAM(workerScript.dynamicRamUsage)}
|
2021-09-05 01:27:16 +02:00
|
|
|
Static RAM Usage: ${numeralWrapper.formatRAM(workerScript.ramUsage)}
|
|
|
|
|
|
|
|
One of these could be the reason:
|
|
|
|
* Using eval() to get a reference to a ns function
|
|
|
|
const scan = eval('ns.scan');
|
|
|
|
|
|
|
|
* Using map access to do the same
|
|
|
|
const scan = ns['scan'];
|
|
|
|
|
|
|
|
* Saving script in the improper order.
|
|
|
|
Increase the cost of an imported script, save it, then run the
|
|
|
|
parent. To fix this just re-open & save every script in order
|
|
|
|
from most imported to least imported (parent script).
|
|
|
|
|
|
|
|
Sorry :(`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the Server for a specific hostname/ip, throwing an error
|
|
|
|
* if the server doesn't exist.
|
|
|
|
* @param {string} ip - Hostname or IP of the server
|
|
|
|
* @param {string} callingFnName - Name of calling function. For logging purposes
|
|
|
|
* @returns {Server} The specified Server
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const safeGetServer = function (ip: any, callingFnName: any = ""): BaseServer {
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg(callingFnName, `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
return server;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for and returns the RunningScript object for the specified script.
|
|
|
|
* If the 'fn' argument is not specified, this returns the current RunningScript.
|
|
|
|
* @param {string} fn - Filename of script
|
|
|
|
* @param {string} ip - Hostname/ip of the server on which the script resides
|
|
|
|
* @param {any[]} scriptArgs - Running script's arguments
|
|
|
|
* @returns {RunningScript}
|
|
|
|
* Running script identified by the parameters, or null if no such script
|
|
|
|
* exists, or the current running script if the first argument 'fn'
|
|
|
|
* is not specified.
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const getRunningScript = function (fn: any, ip: any, callingFnName: any, scriptArgs: any): RunningScript | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof callingFnName !== "string" || callingFnName === "") {
|
|
|
|
callingFnName = "getRunningScript";
|
2019-07-05 03:01:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!Array.isArray(scriptArgs)) {
|
|
|
|
throw makeRuntimeRejectMsg(
|
|
|
|
workerScript,
|
|
|
|
`Invalid scriptArgs argument passed into getRunningScript() from ${callingFnName}(). ` +
|
|
|
|
`This is probably a bug. Please report to game developer`,
|
|
|
|
);
|
2021-04-21 14:20:26 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (fn != null && typeof fn === "string") {
|
|
|
|
// Get Logs of another script
|
|
|
|
if (ip == null) {
|
2021-10-07 23:55:49 +02:00
|
|
|
ip = workerScript.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
const server = safeGetServer(ip, callingFnName);
|
2019-07-05 03:01:09 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return findRunningScript(fn, scriptArgs, server);
|
2019-07-05 03:01:09 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// If no arguments are specified, return the current RunningScript
|
|
|
|
return workerScript.scriptRef;
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const getRunningScriptByPid = function (pid: any, callingFnName: any): RunningScript | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof callingFnName !== "string" || callingFnName === "") {
|
|
|
|
callingFnName = "getRunningScriptgetRunningScriptByPid";
|
2019-04-29 08:20:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 22:04:04 +02:00
|
|
|
for (const server of GetAllServers()) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const runningScript = findRunningScriptByPid(pid, server);
|
|
|
|
if (runningScript) return runningScript;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for getting the error log message when the user specifies
|
|
|
|
* a nonexistent running script
|
|
|
|
* @param {string} fn - Filename of script
|
|
|
|
* @param {string} ip - Hostname/ip of the server on which the script resides
|
|
|
|
* @param {any[]} scriptArgs - Running script's arguments
|
|
|
|
* @returns {string} Error message to print to logs
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const getCannotFindRunningScriptErrorMessage = function (fn: any, ip: any, scriptArgs: any): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!Array.isArray(scriptArgs)) {
|
|
|
|
scriptArgs = [];
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
return `Cannot find running script ${fn} on server ${ip} with args: ${arrayToString(scriptArgs)}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the player has TIX API access. Throws an error if the player does not
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const checkTixApiAccess = function (callingFn: any = ""): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!Player.hasWseAccount) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(callingFn, `You don't have WSE Access! Cannot use ${callingFn}()`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!Player.hasTixApiAccess) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(callingFn, `You don't have TIX API Access! Cannot use ${callingFn}()`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a stock, given its symbol. Throws an error if the symbol is invalid
|
|
|
|
* @param {string} symbol - Stock's symbol
|
|
|
|
* @returns {Stock} stock object
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const getStockFromSymbol = function (symbol: any, callingFn: any = ""): Stock {
|
2021-09-05 01:09:30 +02:00
|
|
|
const stock = SymbolToStockMap[symbol];
|
|
|
|
if (stock == null) {
|
|
|
|
throw makeRuntimeErrorMsg(callingFn, `Invalid stock symbol: '${symbol}'`);
|
2019-04-29 08:20:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return stock;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to fail a function if the function's target is a Hacknet Server.
|
|
|
|
* This is used for functions that should run on normal Servers, but not Hacknet Servers
|
|
|
|
* @param {Server} server - Target server
|
|
|
|
* @param {string} callingFn - Name of calling function. For logging purposes
|
|
|
|
* @returns {boolean} True if the server is a Hacknet Server, false otherwise
|
|
|
|
*/
|
2021-09-25 08:36:49 +02:00
|
|
|
const failOnHacknetServer = function (server: any, callingFn: any = ""): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server instanceof HacknetServer) {
|
|
|
|
workerScript.log(callingFn, `Does not work on Hacknet Servers`);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const makeRuntimeErrorMsg = function (caller: string, msg: string): string {
|
2021-09-25 03:49:49 +02:00
|
|
|
const errstack = new Error().stack;
|
|
|
|
if (errstack === undefined) throw new Error("how did we not throw an error?");
|
|
|
|
const stack = errstack.split("\n").slice(1);
|
2021-09-05 01:09:30 +02:00
|
|
|
const scripts = workerScript.getServer().scripts;
|
|
|
|
const userstack = [];
|
|
|
|
for (const stackline of stack) {
|
|
|
|
let filename;
|
|
|
|
for (const script of scripts) {
|
|
|
|
if (script.url && stackline.includes(script.url)) {
|
|
|
|
filename = script.filename;
|
2018-07-15 02:25:50 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const dependency of script.dependencies) {
|
|
|
|
if (stackline.includes(dependency.url)) {
|
|
|
|
filename = dependency.filename;
|
|
|
|
}
|
2018-07-15 02:25:50 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!filename) continue;
|
2019-03-30 00:14:32 +01:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
interface ILine {
|
|
|
|
line: string;
|
|
|
|
func: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseChromeStackline(line: string): ILine | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
const lineRe = /.*:(\d+):\d+.*/;
|
|
|
|
const funcRe = /.*at (.+) \(.*/;
|
2019-03-30 00:14:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const lineMatch = line.match(lineRe);
|
|
|
|
const funcMatch = line.match(funcRe);
|
|
|
|
if (lineMatch && funcMatch) {
|
|
|
|
return { line: lineMatch[1], func: funcMatch[1] };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let call = { line: "-1", func: "unknown" };
|
|
|
|
const chromeCall = parseChromeStackline(stackline);
|
|
|
|
if (chromeCall) {
|
|
|
|
call = chromeCall;
|
|
|
|
}
|
2021-03-25 07:48:15 +01:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
function parseFirefoxStackline(line: string): ILine | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
const lineRe = /.*:(\d+):\d+$/;
|
|
|
|
const lineMatch = line.match(lineRe);
|
2021-03-10 07:27:14 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const lio = line.lastIndexOf("@");
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (lineMatch && lio !== -1) {
|
|
|
|
return { line: lineMatch[1], func: line.slice(0, lio) };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-25 07:48:15 +01:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const firefoxCall = parseFirefoxStackline(stackline);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (firefoxCall) {
|
|
|
|
call = firefoxCall;
|
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
userstack.push(`${filename}:L${call.line}@${call.func}`);
|
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log(caller, msg);
|
|
|
|
let rejectMsg = `${caller}: ${msg}`;
|
2021-09-09 05:47:34 +02:00
|
|
|
if (userstack.length !== 0) rejectMsg += `<br><br>Stack:<br>${userstack.join("<br>")}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
return makeRuntimeRejectMsg(workerScript, rejectMsg);
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const checkFormulasAccess = function (func: any, n: any): void {
|
2021-09-09 05:47:34 +02:00
|
|
|
if ((SourceFileFlags[5] < 1 && Player.bitNodeN !== 5) || (SourceFileFlags[n] < 1 && Player.bitNodeN !== n)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
let extra = "";
|
|
|
|
if (n !== 5) {
|
|
|
|
extra = ` and Source-File ${n}-1`;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(`formulas.${func}`, `Requires Source-File 5-1${extra} to run.`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const checkSingularityAccess = function (func: any, n: any): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.bitNodeN !== 4) {
|
|
|
|
if (SourceFileFlags[4] < n) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(func, `This singularity function requires Source-File 4-${n} to run.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const checkBladeburnerAccess = function (func: any, skipjoined: any = false): void {
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Must have joined bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
const apiAccess =
|
|
|
|
Player.bitNodeN === 7 ||
|
|
|
|
Player.sourceFiles.some((a) => {
|
|
|
|
return a.n === 7;
|
|
|
|
});
|
|
|
|
if (!apiAccess) {
|
|
|
|
const apiDenied = `You do not currently have access to the Bladeburner API. You must either be in BitNode-7 or have Source-File 7.`;
|
|
|
|
throw makeRuntimeErrorMsg(`bladeburner.${func}`, apiDenied);
|
|
|
|
}
|
|
|
|
if (!skipjoined) {
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburnerAccess = bladeburner instanceof Bladeburner;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!bladeburnerAccess) {
|
|
|
|
const bladeburnerDenied = `You must be a member of the Bladeburner division to use this API.`;
|
|
|
|
throw makeRuntimeErrorMsg(`bladeburner.${func}`, bladeburnerDenied);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const checkBladeburnerCity = function (func: any, city: any): void {
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Must have joined bladeburner");
|
|
|
|
if (!bladeburner.cities.hasOwnProperty(city)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
throw makeRuntimeErrorMsg(`bladeburner.${func}`, `Invalid city: ${city}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const getCodingContract = function (func: any, ip: any, fn: any): CodingContract {
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, func);
|
|
|
|
const contract = server.getContract(fn);
|
|
|
|
if (contract == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(`codingcontract.${func}`, `Cannot find contract '${fn}' on server '${ip}'`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return contract;
|
|
|
|
};
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const getBladeburnerActionObject = function (func: any, type: any, name: any): any {
|
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Must have joined bladeburner");
|
|
|
|
const actionId = bladeburner.getActionIdFromTypeAndName(type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!actionId) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(`bladeburner.${func}`, `Invalid action type='${type}', name='${name}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const actionObj = bladeburner.getActionObject(actionId);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!actionObj) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg(`bladeburner.${func}`, `Invalid action type='${type}', name='${name}'`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return actionObj;
|
|
|
|
};
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const getCompany = function (func: any, name: any): Company {
|
2021-09-05 01:09:30 +02:00
|
|
|
const company = Companies[name];
|
|
|
|
if (company == null || !(company instanceof Company)) {
|
|
|
|
throw makeRuntimeErrorMsg(func, `Invalid company name: '${name}'`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return company;
|
|
|
|
};
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const getFaction = function (func: any, name: any): Faction {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!factionExists(name)) {
|
|
|
|
throw makeRuntimeErrorMsg(func, `Invalid faction name: '${name}`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return Factions[name];
|
|
|
|
};
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const getAugmentation = function (func: any, name: any): Augmentation {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!augmentationExists(name)) {
|
|
|
|
throw makeRuntimeErrorMsg(func, `Invalid augmentation: '${name}'`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return Augmentations[name];
|
|
|
|
};
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getDivision(divisionName: any): IIndustry {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("cannot be called without a corporation");
|
|
|
|
const division = corporation.divisions.find((div) => div.name === divisionName);
|
2021-09-09 05:47:34 +02:00
|
|
|
if (division === undefined) throw new Error(`No division named '${divisionName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return division;
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getOffice(divisionName: any, cityName: any): OfficeSpace {
|
2021-09-10 06:13:28 +02:00
|
|
|
const division = getDivision(divisionName);
|
|
|
|
if (!(cityName in division.offices)) throw new Error(`Invalid city name '${cityName}'`);
|
|
|
|
const office = division.offices[cityName];
|
|
|
|
if (office === 0) throw new Error(`${division.name} has not expanded to '${cityName}'`);
|
|
|
|
return office;
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getWarehouse(divisionName: any, cityName: any): Warehouse {
|
2021-09-05 01:09:30 +02:00
|
|
|
const division = getDivision(divisionName);
|
2021-09-09 05:47:34 +02:00
|
|
|
if (!(cityName in division.warehouses)) throw new Error(`Invalid city name '${cityName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
const warehouse = division.warehouses[cityName];
|
2021-09-09 05:47:34 +02:00
|
|
|
if (warehouse === 0) throw new Error(`${division.name} has not expanded to '${cityName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return warehouse;
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getMaterial(divisionName: any, cityName: any, materialName: any): Material {
|
2021-09-05 01:09:30 +02:00
|
|
|
const warehouse = getWarehouse(divisionName, cityName);
|
|
|
|
const material = warehouse.materials[materialName];
|
2021-09-09 05:47:34 +02:00
|
|
|
if (material === undefined) throw new Error(`Invalid material name: '${materialName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getProduct(divisionName: any, productName: any): Product {
|
2021-09-05 01:09:30 +02:00
|
|
|
const division = getDivision(divisionName);
|
|
|
|
const product = division.products[productName];
|
2021-09-09 05:47:34 +02:00
|
|
|
if (product === undefined) throw new Error(`Invalid product name: '${productName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return product;
|
|
|
|
}
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
function getEmployee(divisionName: any, cityName: any, employeeName: any): Employee {
|
2021-09-10 06:13:28 +02:00
|
|
|
const office = getOffice(divisionName, cityName);
|
|
|
|
const employee = office.employees.find((e) => e.name === employeeName);
|
|
|
|
if (employee === undefined) throw new Error(`Invalid employee name: '${employeeName}'`);
|
|
|
|
return employee;
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const runAfterReset = function (cbScript = null): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
//Run a script after reset
|
|
|
|
if (cbScript && isString(cbScript)) {
|
|
|
|
const home = Player.getHomeComputer();
|
|
|
|
for (const script of home.scripts) {
|
|
|
|
if (script.filename === cbScript) {
|
|
|
|
const ramUsage = script.ramUsage;
|
|
|
|
const ramAvailable = home.maxRam - home.ramUsed;
|
|
|
|
if (ramUsage > ramAvailable) {
|
|
|
|
return; // Not enough RAM
|
|
|
|
}
|
|
|
|
const runningScriptObj = new RunningScript(script, []); // No args
|
|
|
|
runningScriptObj.threads = 1; // Only 1 thread
|
|
|
|
startWorkerScript(runningScriptObj, home);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const hack = function (ip: any, manual: any, { threads: requestedThreads, stock }: any = {}): Promise<number> {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("hack", "Takes 1 argument.");
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
const threads = resolveNetscriptRequestedThreads(workerScript, "hack", requestedThreads);
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
throw makeRuntimeErrorMsg("hack", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("hack", `Invalid IP/hostname: ${ip}.`);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Calculate the hacking time
|
2021-09-25 03:49:49 +02:00
|
|
|
const hackingTime = calculateHackingTime(server, Player); // This is in seconds
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanHack(server, Player);
|
|
|
|
if (!canHack.res) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("hack", canHack.msg || "");
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log(
|
|
|
|
"hack",
|
|
|
|
`Executing ${ip} in ${convertTimeMsToTimeElapsedString(
|
|
|
|
hackingTime * 1000,
|
|
|
|
true,
|
|
|
|
)} (t=${numeralWrapper.formatThreads(threads)})`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return netscriptDelay(hackingTime * 1000, workerScript).then(function () {
|
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
return Promise.reject(workerScript);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const hackChance = calculateHackingChance(server, Player);
|
|
|
|
const rand = Math.random();
|
|
|
|
let expGainedOnSuccess = calculateHackingExpGain(server, Player) * threads;
|
|
|
|
const expGainedOnFailure = expGainedOnSuccess / 4;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (rand < hackChance) {
|
|
|
|
// Success!
|
|
|
|
const percentHacked = calculatePercentMoneyHacked(server, Player);
|
2021-09-09 05:47:34 +02:00
|
|
|
let maxThreadNeeded = Math.ceil((1 / percentHacked) * (server.moneyAvailable / server.moneyMax));
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isNaN(maxThreadNeeded)) {
|
|
|
|
// Server has a 'max money' of 0 (probably). We'll set this to an arbitrarily large value
|
|
|
|
maxThreadNeeded = 1e6;
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
let moneyDrained = Math.floor(server.moneyAvailable * percentHacked) * threads;
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Over-the-top safety checks
|
|
|
|
if (moneyDrained <= 0) {
|
|
|
|
moneyDrained = 0;
|
|
|
|
expGainedOnSuccess = expGainedOnFailure;
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (moneyDrained > server.moneyAvailable) {
|
|
|
|
moneyDrained = server.moneyAvailable;
|
|
|
|
}
|
|
|
|
server.moneyAvailable -= moneyDrained;
|
|
|
|
if (server.moneyAvailable < 0) {
|
|
|
|
server.moneyAvailable = 0;
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
const moneyGained = moneyDrained * BitNodeMultipliers.ScriptHackMoneyGain;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
Player.gainMoney(moneyGained);
|
|
|
|
workerScript.scriptRef.onlineMoneyMade += moneyGained;
|
|
|
|
Player.scriptProdSinceLastAug += moneyGained;
|
|
|
|
Player.recordMoneySource(moneyGained, "hacking");
|
2021-10-07 23:55:49 +02:00
|
|
|
workerScript.scriptRef.recordHack(server.hostname, moneyGained, threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.gainHackingExp(expGainedOnSuccess);
|
|
|
|
workerScript.scriptRef.onlineExpGained += expGainedOnSuccess;
|
|
|
|
workerScript.log(
|
|
|
|
"hack",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Successfully hacked '${server.hostname}' for ${numeralWrapper.formatMoney(
|
2021-09-05 01:09:30 +02:00
|
|
|
moneyGained,
|
2021-09-09 05:47:34 +02:00
|
|
|
)} and ${numeralWrapper.formatExp(expGainedOnSuccess)} exp (t=${numeralWrapper.formatThreads(threads)})`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
2021-09-09 05:47:34 +02:00
|
|
|
server.fortify(CONSTANTS.ServerFortifyAmount * Math.min(threads, maxThreadNeeded));
|
2021-09-05 01:09:30 +02:00
|
|
|
if (stock) {
|
|
|
|
influenceStockThroughServerHack(server, moneyGained);
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (manual) {
|
|
|
|
server.backdoorInstalled = true;
|
|
|
|
}
|
|
|
|
return Promise.resolve(moneyGained);
|
|
|
|
} else {
|
|
|
|
// Player only gains 25% exp for failure?
|
|
|
|
Player.gainHackingExp(expGainedOnFailure);
|
|
|
|
workerScript.scriptRef.onlineExpGained += expGainedOnFailure;
|
|
|
|
workerScript.log(
|
|
|
|
"hack",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Failed to hack '${server.hostname}'. Gained ${numeralWrapper.formatExp(
|
2021-09-05 01:09:30 +02:00
|
|
|
expGainedOnFailure,
|
|
|
|
)} exp (t=${numeralWrapper.formatThreads(threads)})`,
|
|
|
|
);
|
|
|
|
return Promise.resolve(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
const argsToString = function (args: any[]): string {
|
2021-09-05 01:09:30 +02:00
|
|
|
let out = "";
|
|
|
|
for (let arg of args) {
|
|
|
|
arg = toNative(arg);
|
|
|
|
if (typeof arg === "object") {
|
|
|
|
out += JSON.stringify(arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out += `${arg}`;
|
2021-03-11 09:02:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-10-05 03:06:55 +02:00
|
|
|
const helper = {
|
|
|
|
updateDynamicRam: updateDynamicRam,
|
|
|
|
makeRuntimeErrorMsg: makeRuntimeErrorMsg,
|
2021-10-05 04:25:21 +02:00
|
|
|
string: (funcName: string, argName: string, v: any): string => {
|
|
|
|
if (typeof v === "string") return v;
|
|
|
|
if (typeof v === "number") return v + ""; // cast to string;
|
|
|
|
throw makeRuntimeErrorMsg(funcName, `${argName} should be a string`);
|
|
|
|
},
|
|
|
|
number: (funcName: string, argName: string, v: any): number => {
|
|
|
|
if (typeof v === "number") return v;
|
|
|
|
if (!isNaN(v) && !isNaN(parseFloat(v))) return parseFloat(v);
|
|
|
|
throw makeRuntimeErrorMsg(funcName, `${argName} should be a number`);
|
|
|
|
},
|
|
|
|
boolean: (v: any): boolean => {
|
|
|
|
return !!v; // Just convert it to boolean.
|
|
|
|
},
|
2021-10-05 03:06:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const gang = NetscriptGang(Player, workerScript, helper);
|
2021-10-05 04:25:21 +02:00
|
|
|
const sleeve = NetscriptSleeve(Player, workerScript, helper);
|
2021-10-11 22:38:50 +02:00
|
|
|
const extra = NetscriptExtra(Player, workerScript);
|
2021-10-05 04:25:21 +02:00
|
|
|
const hacknet = NetscriptHacknet(Player, workerScript, helper);
|
2021-10-05 03:06:55 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const functions = {
|
2021-10-05 04:25:21 +02:00
|
|
|
hacknet: hacknet,
|
2021-09-05 01:09:30 +02:00
|
|
|
sprintf: sprintf,
|
|
|
|
vsprintf: vsprintf,
|
2021-10-11 20:26:44 +02:00
|
|
|
scan: function (ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("scan", getRamCost("scan"));
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("scan", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const out = [];
|
|
|
|
for (let i = 0; i < server.serversOnNetwork.length; i++) {
|
2021-09-25 03:49:49 +02:00
|
|
|
const s = getServerOnNetwork(server, i);
|
|
|
|
if (s === null) continue;
|
2021-10-11 20:26:44 +02:00
|
|
|
const entry = s.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (entry == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.push(entry);
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scan", `returned ${server.serversOnNetwork.length} connections for ${server.hostname}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return out;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hack: function (ip: any, { threads: requestedThreads, stock }: any = {}): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hack", getRamCost("hack"));
|
|
|
|
return hack(ip, false, { threads: requestedThreads, stock: stock });
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackAnalyzeThreads: function (ip: any, hackAmount: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hackAnalyzeThreads", getRamCost("hackAnalyzeThreads"));
|
|
|
|
|
|
|
|
// Check argument validity
|
|
|
|
const server = safeGetServer(ip, "hackAnalyzeThreads");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("hackAnalyzeThreads", "Cannot be executed on this server.");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isNaN(hackAmount)) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
2021-09-25 03:49:49 +02:00
|
|
|
"hackAnalyzeThreads",
|
2021-09-05 01:09:30 +02:00
|
|
|
`Invalid growth argument passed into hackAnalyzeThreads: ${hackAmount}. Must be numeric.`,
|
|
|
|
);
|
|
|
|
}
|
2021-03-10 07:27:14 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (hackAmount < 0 || hackAmount > server.moneyAvailable) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-10 07:27:14 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const percentHacked = calculatePercentMoneyHacked(server, Player);
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return hackAmount / Math.floor(server.moneyAvailable * percentHacked);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackAnalyzePercent: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hackAnalyzePercent", getRamCost("hackAnalyzePercent"));
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "hackAnalyzePercent");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("hackAnalyzePercent", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return calculatePercentMoneyHacked(server, Player) * 100;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackChance: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hackChance", getRamCost("hackChance"));
|
2021-09-02 06:36:33 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "hackChance");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("hackChance", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return calculateHackingChance(server, Player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sleep: function (time: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
grow: function (ip: any, { threads: requestedThreads, stock }: any = {}): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("grow", getRamCost("grow"));
|
2021-09-09 05:47:34 +02:00
|
|
|
const threads = resolveNetscriptRequestedThreads(workerScript, "grow", requestedThreads);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("grow", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("grow", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("grow", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
const host = GetServer(workerScript.hostname);
|
2021-10-05 21:23:30 +02:00
|
|
|
if (host === null) {
|
|
|
|
throw new Error("Workerscript host is null");
|
2021-09-25 03:49:49 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanGrow(server);
|
|
|
|
if (!canHack.res) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("grow", canHack.msg || "");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-03-12 02:37:58 +01:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const growTime = calculateGrowTime(server, Player);
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log(
|
|
|
|
"grow",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
2021-09-05 01:09:30 +02:00
|
|
|
growTime * 1000,
|
|
|
|
true,
|
|
|
|
)} (t=${numeralWrapper.formatThreads(threads)}).`,
|
|
|
|
);
|
|
|
|
return netscriptDelay(growTime * 1000, workerScript).then(function () {
|
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
return Promise.reject(workerScript);
|
2021-03-24 02:30:15 +01:00
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable;
|
2021-09-05 01:09:30 +02:00
|
|
|
processSingleServerGrowth(server, threads, Player, host.cpuCores);
|
|
|
|
const moneyAfter = server.moneyAvailable;
|
2021-10-07 23:55:49 +02:00
|
|
|
workerScript.scriptRef.recordGrow(server.hostname, threads);
|
2021-09-25 07:26:03 +02:00
|
|
|
const expGain = calculateHackingExpGain(server, Player) * threads;
|
2021-09-05 01:09:30 +02:00
|
|
|
const logGrowPercent = moneyAfter / moneyBefore - 1;
|
|
|
|
workerScript.log(
|
|
|
|
"grow",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Available money on '${server.hostname}' grown by ${numeralWrapper.formatPercentage(
|
2021-09-05 01:09:30 +02:00
|
|
|
logGrowPercent,
|
|
|
|
6,
|
2021-09-09 05:47:34 +02:00
|
|
|
)}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp (t=${numeralWrapper.formatThreads(threads)}).`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
workerScript.scriptRef.onlineExpGained += expGain;
|
|
|
|
Player.gainHackingExp(expGain);
|
|
|
|
if (stock) {
|
|
|
|
influenceStockThroughServerGrow(server, moneyAfter - moneyBefore);
|
|
|
|
}
|
|
|
|
return Promise.resolve(moneyAfter / moneyBefore);
|
|
|
|
});
|
|
|
|
},
|
2021-10-08 18:09:44 +02:00
|
|
|
growthAnalyze: function (ip: any, growth: any, cores: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("growthAnalyze", getRamCost("growthAnalyze"));
|
|
|
|
|
|
|
|
// Check argument validity
|
|
|
|
const server = safeGetServer(ip, "growthAnalyze");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("growthAnalyze", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
if (typeof growth !== "number" || isNaN(growth) || growth < 1 || !isFinite(growth)) {
|
|
|
|
throw makeRuntimeErrorMsg("growthAnalyze", `Invalid argument: growth must be numeric and >= 1, is ${growth}.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
|
2021-10-08 18:09:44 +02:00
|
|
|
return numCycleForGrowth(server, Number(growth), Player, cores);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
weaken: function (ip: any, { threads: requestedThreads }: any = {}): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("weaken", getRamCost("weaken"));
|
2021-09-25 07:26:03 +02:00
|
|
|
const threads = resolveNetscriptRequestedThreads(workerScript, "weaken", requestedThreads);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("weaken", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("weaken", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("weaken", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanWeaken(server);
|
|
|
|
if (!canHack.res) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("weaken", canHack.msg || "");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const weakenTime = calculateWeakenTime(server, Player);
|
|
|
|
workerScript.log(
|
|
|
|
"weaken",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(
|
2021-09-05 01:09:30 +02:00
|
|
|
weakenTime * 1000,
|
|
|
|
true,
|
|
|
|
)} (t=${numeralWrapper.formatThreads(threads)})`,
|
|
|
|
);
|
|
|
|
return netscriptDelay(weakenTime * 1000, workerScript).then(function () {
|
|
|
|
if (workerScript.env.stopFlag) return Promise.reject(workerScript);
|
2021-10-07 23:55:49 +02:00
|
|
|
const host = GetServer(workerScript.hostname);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (host === null) {
|
|
|
|
workerScript.log("weaken", "Server is null, did it die?");
|
|
|
|
return Promise.resolve(0);
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
const coreBonus = 1 + (host.cpuCores - 1) / 16;
|
|
|
|
server.weaken(CONSTANTS.ServerWeakenAmount * threads * coreBonus);
|
2021-10-07 23:55:49 +02:00
|
|
|
workerScript.scriptRef.recordWeaken(server.hostname, threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
const expGain = calculateHackingExpGain(server, Player) * threads;
|
|
|
|
workerScript.log(
|
|
|
|
"weaken",
|
2021-09-09 05:47:34 +02:00
|
|
|
`'${server.hostname}' security level weakened to ${server.hackDifficulty}. Gained ${numeralWrapper.formatExp(
|
2021-09-05 01:09:30 +02:00
|
|
|
expGain,
|
|
|
|
)} hacking exp (t=${numeralWrapper.formatThreads(threads)})`,
|
|
|
|
);
|
|
|
|
workerScript.scriptRef.onlineExpGained += expGain;
|
|
|
|
Player.gainHackingExp(expGain);
|
|
|
|
return Promise.resolve(CONSTANTS.ServerWeakenAmount * threads);
|
|
|
|
});
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
print: function (...args: any[]): void {
|
|
|
|
if (args.length === 0) {
|
2021-09-05 01:09:30 +02:00
|
|
|
throw makeRuntimeErrorMsg("print", "Takes at least 1 argument.");
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
workerScript.print(argsToString(args));
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
tprint: function (...args: any[]): void {
|
|
|
|
if (args.length === 0) {
|
2021-09-05 01:09:30 +02:00
|
|
|
throw makeRuntimeErrorMsg("tprint", "Takes at least 1 argument.");
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
Terminal.print(`${workerScript.scriptRef.filename}: ${argsToString(args)}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
tprintf: function (format: any, ...args: any): any {
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.print(vsprintf(format, args));
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
clearLog: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.scriptRef.clearLog();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
disableLog: function (fn: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (fn === "ALL") {
|
|
|
|
for (fn in possibleLogs) {
|
|
|
|
workerScript.disableLogs[fn] = true;
|
2021-04-18 17:18:56 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("disableLog", `Disabled logging for all functions`);
|
|
|
|
} else if (possibleLogs[fn] === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("disableLog", `Invalid argument: ${fn}.`);
|
|
|
|
} else {
|
|
|
|
workerScript.disableLogs[fn] = true;
|
|
|
|
workerScript.log("disableLog", `Disabled logging for ${fn}`);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
enableLog: function (fn: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (possibleLogs[fn] === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("enableLog", `Invalid argument: ${fn}.`);
|
|
|
|
}
|
|
|
|
delete workerScript.disableLogs[fn];
|
|
|
|
workerScript.log("enableLog", `Enabled logging for ${fn}`);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
isLogEnabled: function (fn: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (possibleLogs[fn] === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("isLogEnabled", `Invalid argument: ${fn}.`);
|
|
|
|
}
|
|
|
|
return workerScript.disableLogs[fn] ? false : true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getScriptLogs: function (fn: any, ip: any, ...scriptArgs: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
const runningScriptObj = getRunningScript(fn, ip, "getScriptLogs", scriptArgs);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (runningScriptObj == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getScriptLogs", getCannotFindRunningScriptErrorMessage(fn, ip, scriptArgs));
|
2021-09-05 01:09:30 +02:00
|
|
|
return "";
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return runningScriptObj.logs.slice();
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
tail: function (fn: any, ip: any = workerScript.hostname, ...scriptArgs: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
let runningScriptObj;
|
|
|
|
if (arguments.length === 0) {
|
|
|
|
runningScriptObj = workerScript.scriptRef;
|
|
|
|
} else if (typeof fn === "number") {
|
|
|
|
runningScriptObj = getRunningScriptByPid(fn, "tail");
|
|
|
|
} else {
|
|
|
|
runningScriptObj = getRunningScript(fn, ip, "tail", scriptArgs);
|
|
|
|
}
|
|
|
|
if (runningScriptObj == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("tail", getCannotFindRunningScriptErrorMessage(fn, ip, scriptArgs));
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-04-18 17:18:56 +02:00
|
|
|
|
2021-10-01 07:00:50 +02:00
|
|
|
LogBoxEvents.emit(runningScriptObj);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
nuke: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("nuke", getRamCost("nuke"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("nuke", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("nuke", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("nuke", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.NukeProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("nuke", "You do not have the NUKE.exe virus!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (server.openPortCount < server.numOpenPortsRequired) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("nuke", "Not enough ports opened to use NUKE.exe virus.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (server.hasAdminRights) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("nuke", `Already have root access to '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
server.hasAdminRights = true;
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("nuke", `Executed NUKE.exe virus on '${server.hostname}' to gain root access.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
brutessh: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("brutessh", getRamCost("brutessh"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("brutessh", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("brutessh", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("brutessh", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.BruteSSHProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("brutessh", "You do not have the BruteSSH.exe program!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!server.sshPortOpen) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("brutessh", `Executed BruteSSH.exe on '${server.hostname}' to open SSH port (22).`);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.sshPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("brutessh", `SSH Port (22) already opened on '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
ftpcrack: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("ftpcrack", getRamCost("ftpcrack"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("ftpcrack", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("ftpcrack", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("ftpcrack", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.FTPCrackProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("ftpcrack", "You do not have the FTPCrack.exe program!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!server.ftpPortOpen) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("ftpcrack", `Executed FTPCrack.exe on '${server.hostname}' to open FTP port (21).`);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.ftpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("ftpcrack", `FTP Port (21) already opened on '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
relaysmtp: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("relaysmtp", getRamCost("relaysmtp"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("relaysmtp", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("relaysmtp", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("relaysmtp", `Invalid IP/hostname: ${ip}.`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.RelaySMTPProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("relaysmtp", "You do not have the relaySMTP.exe program!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!server.smtpPortOpen) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("relaysmtp", `Executed relaySMTP.exe on '${server.hostname}' to open SMTP port (25).`);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.smtpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("relaysmtp", `SMTP Port (25) already opened on '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
httpworm: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("httpworm", getRamCost("httpworm"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("httpworm", "Takes 1 argument");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("httpworm", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("httpworm", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.HTTPWormProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("httpworm", "You do not have the HTTPWorm.exe program!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!server.httpPortOpen) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("httpworm", `Executed HTTPWorm.exe on '${server.hostname}' to open HTTP port (80).`);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.httpPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("httpworm", `HTTP Port (80) already opened on '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sqlinject: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("sqlinject", getRamCost("sqlinject"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("sqlinject", "Takes 1 argument.");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("sqlinject", "Cannot be executed on this server.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("sqlinject", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
if (!Player.hasProgram(Programs.SQLInjectProgram.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("sqlinject", "You do not have the SQLInject.exe program!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (!server.sqlPortOpen) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("sqlinject", `Executed SQLInject.exe on '${server.hostname}' to open SQL port (1433).`);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.sqlPortOpen = true;
|
|
|
|
++server.openPortCount;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("sqlinject", `SQL Port (1433) already opened on '${server.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
run: function (scriptname: any, threads: any = 1, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("run", getRamCost("run"));
|
|
|
|
if (scriptname === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("run", "Usage: run(scriptname, [numThreads], [arg1], [arg2]...)");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (isNaN(threads) || threads <= 0) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("run", `Invalid thread count. Must be numeric and > 0, is ${threads}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
const scriptServer = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (scriptServer == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("run", "Could not find server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
return runScriptFromScript("run", scriptServer, scriptname, args, workerScript, threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
exec: function (scriptname: any, ip: any, threads: any = 1, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("exec", getRamCost("exec"));
|
|
|
|
if (scriptname === undefined || ip === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("exec", "Usage: exec(scriptname, server, [numThreads], [arg1], [arg2]...)");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (isNaN(threads) || threads <= 0) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("exec", `Invalid thread count. Must be numeric and > 0, is ${threads}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("exec", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
return runScriptFromScript("exec", server, scriptname, args, workerScript, threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
spawn: function (scriptname: any, threads: any, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("spawn", getRamCost("spawn"));
|
|
|
|
if (!scriptname || !threads) {
|
|
|
|
throw makeRuntimeErrorMsg("spawn", "Usage: spawn(scriptname, threads)");
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const spawnDelay = 10;
|
2021-10-05 01:58:34 +02:00
|
|
|
setTimeout(() => {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isNaN(threads) || threads <= 0) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("spawn", `Invalid thread count. Must be numeric and > 0, is ${threads}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
const scriptServer = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (scriptServer == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("spawn", "Could not find server. This is a bug. Report to dev");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
return runScriptFromScript("spawn", scriptServer, scriptname, args, workerScript, threads);
|
2021-09-05 01:09:30 +02:00
|
|
|
}, spawnDelay * 1e3);
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("spawn", `Will execute '${scriptname}' in ${spawnDelay} seconds`);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
workerScript.running = false; // Prevent workerScript from "finishing execution naturally"
|
|
|
|
if (killWorkerScript(workerScript)) {
|
|
|
|
workerScript.log("spawn", "Exiting...");
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
kill: function (filename: any, ip: any, ...scriptArgs: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("kill", getRamCost("kill"));
|
|
|
|
|
|
|
|
let res;
|
|
|
|
const killByPid = typeof filename === "number";
|
|
|
|
if (killByPid) {
|
|
|
|
// Kill by pid
|
|
|
|
res = killWorkerScript(filename);
|
|
|
|
} else {
|
|
|
|
// Kill by filename/ip
|
|
|
|
if (filename === undefined || ip === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("kill", "Usage: kill(scriptname, server, [arg1], [arg2]...)");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip);
|
2021-09-09 05:47:34 +02:00
|
|
|
const runningScriptObj = getRunningScript(filename, ip, "kill", scriptArgs);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (runningScriptObj == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("kill", getCannotFindRunningScriptErrorMessage(filename, ip, scriptArgs));
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
res = killWorkerScript(runningScriptObj, server.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (res) {
|
|
|
|
if (killByPid) {
|
|
|
|
workerScript.log("kill", `Killing script with PID ${filename}`);
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("kill", `Killing '${filename}' on '${ip}' with args: ${arrayToString(scriptArgs)}.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (killByPid) {
|
|
|
|
workerScript.log("kill", `No script with PID ${filename}`);
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("kill", `No such script '${filename}' on '${ip}' with args: ${arrayToString(scriptArgs)}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
killall: function (ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("killall", getRamCost("killall"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("killall", "Takes 1 argument");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("killall", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
const scriptsRunning = server.runningScripts.length > 0;
|
|
|
|
for (let i = server.runningScripts.length - 1; i >= 0; --i) {
|
2021-10-07 23:55:49 +02:00
|
|
|
killWorkerScript(server.runningScripts[i], server.hostname, false);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-18 21:44:39 +02:00
|
|
|
WorkerScriptStartStopEventEmitter.emit();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log(
|
|
|
|
"killall",
|
|
|
|
`Killing all scripts on '${server.hostname}'. May take a few minutes for the scripts to die.`,
|
|
|
|
);
|
|
|
|
|
|
|
|
return scriptsRunning;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
exit: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.running = false; // Prevent workerScript from "finishing execution naturally"
|
|
|
|
if (killWorkerScript(workerScript)) {
|
|
|
|
workerScript.log("exit", "Exiting...");
|
|
|
|
} else {
|
|
|
|
workerScript.log("exit", "Failed. This is a bug. Report to dev.");
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
scp: function (scriptname: any, ip1: any, ip2: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("scp", getRamCost("scp"));
|
|
|
|
if (arguments.length !== 2 && arguments.length !== 3) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
|
|
|
|
}
|
|
|
|
if (scriptname && scriptname.constructor === Array) {
|
|
|
|
// Recursively call scp on all elements of array
|
2021-09-25 07:26:03 +02:00
|
|
|
let res = false;
|
2021-09-05 01:09:30 +02:00
|
|
|
scriptname.forEach(function (script) {
|
|
|
|
if (NetscriptFunctions(workerScript).scp(script, ip1, ip2)) {
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Invalid file type
|
|
|
|
if (!isValidFilePath(scriptname)) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", `Invalid filename: '${scriptname}'`);
|
|
|
|
}
|
2019-01-03 09:39:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Invalid file name
|
2021-09-09 05:47:34 +02:00
|
|
|
if (!scriptname.endsWith(".lit") && !isScriptFilename(scriptname) && !scriptname.endsWith("txt")) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", "Only works for .script, .lit, and .txt files");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
let destServer, currServ;
|
2021-08-19 22:37:59 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip2 != null) {
|
|
|
|
// 3 Argument version: scriptname, source, destination
|
2021-09-09 05:47:34 +02:00
|
|
|
if (scriptname === undefined || ip1 === undefined || ip2 === undefined) {
|
2021-09-05 01:09:30 +02:00
|
|
|
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
destServer = GetServer(ip2);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (destServer == null) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", `Invalid IP/hostname: ${ip2}`);
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
currServ = GetServer(ip1);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (currServ == null) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", `Invalid IP/hostname: ${ip1}`);
|
|
|
|
}
|
|
|
|
} else if (ip1 != null) {
|
|
|
|
// 2 Argument version: scriptname, destination
|
|
|
|
if (scriptname === undefined || ip1 === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
destServer = GetServer(ip1);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (destServer == null) {
|
|
|
|
throw makeRuntimeErrorMsg("scp", `Invalid IP/hostname: ${ip1}`);
|
|
|
|
}
|
2018-11-20 06:54:03 +01:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
currServ = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (currServ == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("scp", "Could not find server ip for this script. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("scp", "Takes 2 or 3 arguments");
|
|
|
|
}
|
2018-11-20 06:54:03 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Scp for lit files
|
|
|
|
if (scriptname.endsWith(".lit")) {
|
2021-09-25 07:26:03 +02:00
|
|
|
let found = false;
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < currServ.messages.length; ++i) {
|
2021-09-09 05:47:34 +02:00
|
|
|
if (!(currServ.messages[i] instanceof Message) && currServ.messages[i] == scriptname) {
|
2021-09-05 01:09:30 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!found) {
|
|
|
|
workerScript.log("scp", `File '${scriptname}' does not exist.`);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < destServer.messages.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (destServer.messages[i] === scriptname) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' copied over to '${destServer.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true; // Already exists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destServer.messages.push(scriptname);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' copied over to '${destServer.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-07 07:45:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Scp for text files
|
|
|
|
if (scriptname.endsWith(".txt")) {
|
2021-09-25 03:49:49 +02:00
|
|
|
let txtFile;
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < currServ.textFiles.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (currServ.textFiles[i].fn === scriptname) {
|
|
|
|
txtFile = currServ.textFiles[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
if (txtFile === undefined) {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' does not exist.`);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < destServer.textFiles.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (destServer.textFiles[i].fn === scriptname) {
|
|
|
|
// Overwrite
|
|
|
|
destServer.textFiles[i].text = txtFile.text;
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' copied over to '${destServer.hostname}'.`);
|
2017-06-28 11:47:42 +02:00
|
|
|
return true;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const newFile = new TextFile(txtFile.fn, txtFile.text);
|
2021-09-05 01:09:30 +02:00
|
|
|
destServer.textFiles.push(newFile);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' copied over to '${destServer.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-23 02:25:48 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Scp for script files
|
|
|
|
let sourceScript = null;
|
|
|
|
for (let i = 0; i < currServ.scripts.length; ++i) {
|
|
|
|
if (scriptname == currServ.scripts[i].filename) {
|
|
|
|
sourceScript = currServ.scripts[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sourceScript == null) {
|
|
|
|
workerScript.log("scp", `File '${scriptname}' does not exist.`);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Overwrite script if it already exists
|
|
|
|
for (let i = 0; i < destServer.scripts.length; ++i) {
|
|
|
|
if (scriptname == destServer.scripts[i].filename) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `WARNING: File '${scriptname}' overwritten on '${destServer.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
const oldScript = destServer.scripts[i];
|
|
|
|
// If it's the exact same file don't actually perform the
|
|
|
|
// copy to avoid recompiling uselessly. Players tend to scp
|
|
|
|
// liberally.
|
|
|
|
if (oldScript.code === sourceScript.code) return true;
|
|
|
|
oldScript.code = sourceScript.code;
|
|
|
|
oldScript.ramUsage = sourceScript.ramUsage;
|
|
|
|
oldScript.markUpdated();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Create new script if it does not already exist
|
|
|
|
const newScript = new Script(scriptname);
|
|
|
|
newScript.code = sourceScript.code;
|
|
|
|
newScript.ramUsage = sourceScript.ramUsage;
|
2021-10-07 23:55:49 +02:00
|
|
|
newScript.server = destServer.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
destServer.scripts.push(newScript);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("scp", `File '${scriptname}' copied over to '${destServer.hostname}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
ls: function (ip: any, grep: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("ls", getRamCost("ls"));
|
|
|
|
if (ip === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("ls", "Usage: ls(ip/hostname, [grep filter])");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("ls", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Get the grep filter, if one exists
|
2021-09-25 03:49:49 +02:00
|
|
|
let filter = "";
|
2021-09-05 01:09:30 +02:00
|
|
|
if (arguments.length >= 2) {
|
|
|
|
filter = grep.toString();
|
|
|
|
}
|
2019-01-09 11:06:49 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const allFiles = [];
|
|
|
|
for (let i = 0; i < server.programs.length; i++) {
|
|
|
|
if (filter) {
|
|
|
|
if (server.programs[i].includes(filter)) {
|
|
|
|
allFiles.push(server.programs[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allFiles.push(server.programs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < server.scripts.length; i++) {
|
|
|
|
if (filter) {
|
|
|
|
if (server.scripts[i].filename.includes(filter)) {
|
|
|
|
allFiles.push(server.scripts[i].filename);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allFiles.push(server.scripts[i].filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < server.messages.length; i++) {
|
|
|
|
if (filter) {
|
2021-09-25 03:49:49 +02:00
|
|
|
const msg = server.messages[i];
|
|
|
|
if (msg instanceof Message) {
|
|
|
|
if (msg.filename.includes(filter)) {
|
|
|
|
allFiles.push(msg.filename);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
} else if (msg.includes(filter)) {
|
|
|
|
allFiles.push(msg);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-25 03:49:49 +02:00
|
|
|
const msg = server.messages[i];
|
|
|
|
if (msg instanceof Message) {
|
|
|
|
allFiles.push(msg.filename);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2021-09-25 03:49:49 +02:00
|
|
|
allFiles.push(msg);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
for (let i = 0; i < server.textFiles.length; i++) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (filter) {
|
|
|
|
if (server.textFiles[i].fn.includes(filter)) {
|
|
|
|
allFiles.push(server.textFiles[i].fn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allFiles.push(server.textFiles[i].fn);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
for (let i = 0; i < server.contracts.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (filter) {
|
|
|
|
if (server.contracts[i].fn.includes(filter)) {
|
|
|
|
allFiles.push(server.contracts[i].fn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allFiles.push(server.contracts[i].fn);
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Sort the files alphabetically then print each
|
|
|
|
allFiles.sort();
|
|
|
|
return allFiles;
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
ps: function (ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("ps", getRamCost("ps"));
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("ps", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
|
|
|
const processes = [];
|
|
|
|
for (const i in server.runningScripts) {
|
|
|
|
const script = server.runningScripts[i];
|
|
|
|
processes.push({
|
|
|
|
filename: script.filename,
|
|
|
|
threads: script.threads,
|
|
|
|
args: script.args.slice(),
|
|
|
|
pid: script.pid,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return processes;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hasRootAccess: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hasRootAccess", getRamCost("hasRootAccess"));
|
|
|
|
if (ip === undefined) {
|
|
|
|
throw makeRuntimeErrorMsg("hasRootAccess", "Takes 1 argument");
|
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("hasRootAccess", `Invalid IP/hostname: ${ip}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return server.hasAdminRights;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getIp: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getIp", getRamCost("getIp"));
|
2021-10-07 23:55:49 +02:00
|
|
|
const scriptServer = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (scriptServer == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getIp", "Could not find server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
return scriptServer.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getHostname: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getHostname", getRamCost("getHostname"));
|
2021-10-07 23:55:49 +02:00
|
|
|
const scriptServer = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (scriptServer == null) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("getHostname", "Could not find server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return scriptServer.hostname;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getHackingLevel: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getHackingLevel", getRamCost("getHackingLevel"));
|
|
|
|
Player.updateSkillLevels();
|
|
|
|
workerScript.log("getHackingLevel", `returned ${Player.hacking_skill}`);
|
|
|
|
return Player.hacking_skill;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getHackingMultipliers: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getHackingMultipliers", getRamCost("getHackingMultipliers"));
|
2021-09-05 01:09:30 +02:00
|
|
|
return {
|
|
|
|
chance: Player.hacking_chance_mult,
|
|
|
|
speed: Player.hacking_speed_mult,
|
|
|
|
money: Player.hacking_money_mult,
|
|
|
|
growth: Player.hacking_grow_mult,
|
|
|
|
};
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getHacknetMultipliers: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getHacknetMultipliers", getRamCost("getHacknetMultipliers"));
|
2021-09-05 01:09:30 +02:00
|
|
|
return {
|
|
|
|
production: Player.hacknet_node_money_mult,
|
|
|
|
purchaseCost: Player.hacknet_node_purchase_cost_mult,
|
|
|
|
ramCost: Player.hacknet_node_ram_cost_mult,
|
|
|
|
coreCost: Player.hacknet_node_core_cost_mult,
|
|
|
|
levelCost: Player.hacknet_node_level_cost_mult,
|
|
|
|
};
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getBitNodeMultipliers: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getBitNodeMultipliers", getRamCost("getBitNodeMultipliers"));
|
2021-09-05 01:09:30 +02:00
|
|
|
if (SourceFileFlags[5] <= 0 && Player.bitNodeN !== 5) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getBitNodeMultipliers", "Requires Source-File 5 to run.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const copy = Object.assign({}, BitNodeMultipliers);
|
2021-09-05 01:09:30 +02:00
|
|
|
return copy;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServer: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServer", getRamCost("getServer"));
|
|
|
|
const server = safeGetServer(ip, "getServer");
|
|
|
|
const copy = Object.assign({}, server);
|
|
|
|
// These fields should be hidden.
|
2021-09-25 03:49:49 +02:00
|
|
|
copy.contracts = [];
|
|
|
|
copy.messages = [];
|
|
|
|
copy.runningScripts = [];
|
|
|
|
copy.scripts = [];
|
|
|
|
copy.textFiles = [];
|
|
|
|
copy.programs = [];
|
|
|
|
copy.serversOnNetwork = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
return copy;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerMoneyAvailable: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerMoneyAvailable", getRamCost("getServerMoneyAvailable"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerMoneyAvailable");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerMoneyAvailable")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (server.hostname == "home") {
|
|
|
|
// Return player's money
|
|
|
|
workerScript.log(
|
|
|
|
"getServerMoneyAvailable",
|
2021-09-09 05:47:34 +02:00
|
|
|
`returned player's money: ${numeralWrapper.formatMoney(Player.money.toNumber())}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return Player.money.toNumber();
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerMoneyAvailable",
|
2021-09-09 05:47:34 +02:00
|
|
|
`returned ${numeralWrapper.formatMoney(server.moneyAvailable)} for '${server.hostname}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.moneyAvailable;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerSecurityLevel: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerSecurityLevel", getRamCost("getServerSecurityLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerSecurityLevel");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerSecurityLevel")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerSecurityLevel",
|
2021-09-25 03:49:49 +02:00
|
|
|
`returned ${numeralWrapper.formatServerSecurity(server.hackDifficulty)} for '${server.hostname}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.hackDifficulty;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerBaseSecurityLevel: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerBaseSecurityLevel", getRamCost("getServerBaseSecurityLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerBaseSecurityLevel");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerBaseSecurityLevel")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerBaseSecurityLevel",
|
2021-09-25 03:49:49 +02:00
|
|
|
`returned ${numeralWrapper.formatServerSecurity(server.baseDifficulty)} for '${server.hostname}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.baseDifficulty;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerMinSecurityLevel: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerMinSecurityLevel", getRamCost("getServerMinSecurityLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerMinSecurityLevel");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerMinSecurityLevel")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerMinSecurityLevel",
|
2021-09-25 03:49:49 +02:00
|
|
|
`returned ${numeralWrapper.formatServerSecurity(server.minDifficulty)} for ${server.hostname}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.minDifficulty;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerRequiredHackingLevel: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerRequiredHackingLevel", getRamCost("getServerRequiredHackingLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerRequiredHackingLevel");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerRequiredHackingLevel")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerRequiredHackingLevel",
|
2021-09-25 03:49:49 +02:00
|
|
|
`returned ${numeralWrapper.formatSkill(server.requiredHackingSkill)} for '${server.hostname}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.requiredHackingSkill;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerMaxMoney: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServerMaxMoney", getRamCost("getServerMaxMoney"));
|
|
|
|
const server = safeGetServer(ip, "getServerMaxMoney");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerMaxMoney")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
workerScript.log(
|
|
|
|
"getServerMaxMoney",
|
2021-09-09 05:47:34 +02:00
|
|
|
`returned ${numeralWrapper.formatMoney(server.moneyMax)} for '${server.hostname}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return server.moneyMax;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerGrowth: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServerGrowth", getRamCost("getServerGrowth"));
|
|
|
|
const server = safeGetServer(ip, "getServerGrowth");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerGrowth")) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getServerGrowth", `returned ${server.serverGrowth} for '${server.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return server.serverGrowth;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerNumPortsRequired: function (ip: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getServerNumPortsRequired", getRamCost("getServerNumPortsRequired"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = safeGetServer(ip, "getServerNumPortsRequired");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getServerNumPortsRequired", "Cannot be executed on this server.");
|
|
|
|
return 5;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getServerNumPortsRequired")) {
|
|
|
|
return 5;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getServerNumPortsRequired", `returned ${server.numOpenPortsRequired} for '${server.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return server.numOpenPortsRequired;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerRam: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServerRam", getRamCost("getServerRam"));
|
|
|
|
const server = safeGetServer(ip, "getServerRam");
|
|
|
|
workerScript.log(
|
|
|
|
"getServerRam",
|
2021-09-25 03:49:49 +02:00
|
|
|
`returned [${numeralWrapper.formatRAM(server.maxRam)}, ${numeralWrapper.formatRAM(server.ramUsed)}]`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return [server.maxRam, server.ramUsed];
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerMaxRam: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServerMaxRam", getRamCost("getServerMaxRam"));
|
|
|
|
const server = safeGetServer(ip, "getServerMaxRam");
|
2021-09-25 03:49:49 +02:00
|
|
|
workerScript.log("getServerMaxRam", `returned ${numeralWrapper.formatRAM(server.maxRam)}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return server.maxRam;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getServerUsedRam: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getServerUsedRam", getRamCost("getServerUsedRam"));
|
|
|
|
const server = safeGetServer(ip, "getServerUsedRam");
|
2021-09-25 03:49:49 +02:00
|
|
|
workerScript.log("getServerUsedRam", `returned ${numeralWrapper.formatRAM(server.ramUsed)}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return server.ramUsed;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
serverExists: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("serverExists", getRamCost("serverExists"));
|
2021-10-07 22:56:01 +02:00
|
|
|
return GetServer(ip) !== null;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
fileExists: function (filename: any, ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("fileExists", getRamCost("fileExists"));
|
|
|
|
if (filename === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("fileExists", "Usage: fileExists(scriptname, [server])");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("fileExists", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (filename == server.scripts[i].filename) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < server.programs.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (filename.toLowerCase() == server.programs[i].toLowerCase()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < server.messages.length; ++i) {
|
2021-09-09 05:47:34 +02:00
|
|
|
if (!(server.messages[i] instanceof Message) && filename.toLowerCase() === server.messages[i]) {
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const txtFile = getTextFile(filename, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (txtFile != null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
isRunning: function (fn: any, ip: any = workerScript.hostname, ...scriptArgs: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("isRunning", getRamCost("isRunning"));
|
|
|
|
if (fn === undefined || ip === undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("isRunning", "Usage: isRunning(scriptname, server, [arg1], [arg2]...)");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (typeof fn === "number") {
|
|
|
|
return getRunningScriptByPid(fn, "isRunning") != null;
|
|
|
|
} else {
|
|
|
|
return getRunningScript(fn, ip, "isRunning", scriptArgs) != null;
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockSymbols: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockSymbols", getRamCost("getStockSymbols"));
|
|
|
|
checkTixApiAccess("getStockSymbols");
|
|
|
|
return Object.values(StockSymbols);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockPrice: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockPrice", getRamCost("getStockPrice"));
|
|
|
|
checkTixApiAccess("getStockPrice");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockPrice");
|
|
|
|
|
|
|
|
return stock.price;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockAskPrice: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockAskPrice", getRamCost("getStockAskPrice"));
|
|
|
|
checkTixApiAccess("getStockAskPrice");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockAskPrice");
|
|
|
|
|
|
|
|
return stock.getAskPrice();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockBidPrice: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockBidPrice", getRamCost("getStockBidPrice"));
|
|
|
|
checkTixApiAccess("getStockBidPrice");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockBidPrice");
|
|
|
|
|
|
|
|
return stock.getBidPrice();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockPosition: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockPosition", getRamCost("getStockPosition"));
|
|
|
|
checkTixApiAccess("getStockPosition");
|
2021-09-25 07:26:03 +02:00
|
|
|
const stock = SymbolToStockMap[symbol];
|
2021-09-05 01:09:30 +02:00
|
|
|
if (stock == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getStockPosition", `Invalid stock symbol: ${symbol}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
return [stock.playerShares, stock.playerAvgPx, stock.playerShortShares, stock.playerAvgShortPx];
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockMaxShares: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockMaxShares", getRamCost("getStockMaxShares"));
|
|
|
|
checkTixApiAccess("getStockMaxShares");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockMaxShares");
|
|
|
|
|
|
|
|
return stock.maxShares;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockPurchaseCost: function (symbol: any, shares: any, posType: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getStockPurchaseCost", getRamCost("getStockPurchaseCost"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkTixApiAccess("getStockPurchaseCost");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockPurchaseCost");
|
|
|
|
shares = Math.round(shares);
|
|
|
|
|
|
|
|
let pos;
|
|
|
|
const sanitizedPosType = posType.toLowerCase();
|
|
|
|
if (sanitizedPosType.includes("l")) {
|
|
|
|
pos = PositionTypes.Long;
|
|
|
|
} else if (sanitizedPosType.includes("s")) {
|
|
|
|
pos = PositionTypes.Short;
|
|
|
|
} else {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2019-06-03 05:28:02 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const res = getBuyTransactionCost(stock, shares, pos);
|
|
|
|
if (res == null) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockSaleGain: function (symbol: any, shares: any, posType: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockSaleGain", getRamCost("getStockSaleGain"));
|
|
|
|
checkTixApiAccess("getStockSaleGain");
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockSaleGain");
|
|
|
|
shares = Math.round(shares);
|
|
|
|
|
|
|
|
let pos;
|
|
|
|
const sanitizedPosType = posType.toLowerCase();
|
|
|
|
if (sanitizedPosType.includes("l")) {
|
|
|
|
pos = PositionTypes.Long;
|
|
|
|
} else if (sanitizedPosType.includes("s")) {
|
|
|
|
pos = PositionTypes.Short;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-21 07:31:19 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const res = getSellTransactionGain(stock, shares, pos);
|
|
|
|
if (res == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-23 10:23:08 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
buyStock: function (symbol: any, shares: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("buyStock", getRamCost("buyStock"));
|
|
|
|
checkTixApiAccess("buyStock");
|
|
|
|
const stock = getStockFromSymbol(symbol, "buyStock");
|
2021-09-17 08:31:19 +02:00
|
|
|
const res = buyStock(stock, shares, workerScript, {});
|
2021-09-05 01:09:30 +02:00
|
|
|
return res ? stock.price : 0;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sellStock: function (symbol: any, shares: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("sellStock", getRamCost("sellStock"));
|
|
|
|
checkTixApiAccess("sellStock");
|
|
|
|
const stock = getStockFromSymbol(symbol, "sellStock");
|
2021-09-17 08:31:19 +02:00
|
|
|
const res = sellStock(stock, shares, workerScript, {});
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return res ? stock.price : 0;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
shortStock: function (symbol: any, shares: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("shortStock", getRamCost("shortStock"));
|
|
|
|
checkTixApiAccess("shortStock");
|
|
|
|
if (Player.bitNodeN !== 8) {
|
|
|
|
if (SourceFileFlags[8] <= 1) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
2021-09-25 03:49:49 +02:00
|
|
|
"shortStock",
|
2021-09-05 01:09:30 +02:00
|
|
|
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const stock = getStockFromSymbol(symbol, "shortStock");
|
2021-09-17 08:31:19 +02:00
|
|
|
const res = shortStock(stock, shares, workerScript, {});
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return res ? stock.price : 0;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sellShort: function (symbol: any, shares: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("sellShort", getRamCost("sellShort"));
|
|
|
|
checkTixApiAccess("sellShort");
|
|
|
|
if (Player.bitNodeN !== 8) {
|
|
|
|
if (SourceFileFlags[8] <= 1) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"sellShort",
|
|
|
|
"You must either be in BitNode-8 or you must have Source-File 8 Level 2.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const stock = getStockFromSymbol(symbol, "sellShort");
|
2021-09-17 08:31:19 +02:00
|
|
|
const res = sellShort(stock, shares, workerScript, {});
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return res ? stock.price : 0;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
placeOrder: function (symbol: any, shares: any, price: any, type: any, pos: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("placeOrder", getRamCost("placeOrder"));
|
|
|
|
checkTixApiAccess("placeOrder");
|
|
|
|
if (Player.bitNodeN !== 8) {
|
|
|
|
if (SourceFileFlags[8] <= 2) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"placeOrder",
|
|
|
|
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const stock = getStockFromSymbol(symbol, "placeOrder");
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
let orderType;
|
|
|
|
let orderPos;
|
|
|
|
const ltype = type.toLowerCase();
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ltype.includes("limit") && ltype.includes("buy")) {
|
|
|
|
orderType = OrderTypes.LimitBuy;
|
|
|
|
} else if (ltype.includes("limit") && ltype.includes("sell")) {
|
|
|
|
orderType = OrderTypes.LimitSell;
|
|
|
|
} else if (ltype.includes("stop") && ltype.includes("buy")) {
|
|
|
|
orderType = OrderTypes.StopBuy;
|
|
|
|
} else if (ltype.includes("stop") && ltype.includes("sell")) {
|
|
|
|
orderType = OrderTypes.StopSell;
|
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("placeOrder", `Invalid order type: ${type}`);
|
|
|
|
}
|
2017-10-25 21:47:47 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const lpos = pos.toLowerCase();
|
2021-09-05 01:09:30 +02:00
|
|
|
if (lpos.includes("l")) {
|
|
|
|
orderPos = PositionTypes.Long;
|
|
|
|
} else if (lpos.includes("s")) {
|
|
|
|
orderPos = PositionTypes.Short;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("placeOrder", `Invalid position type: ${pos}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-10-25 21:47:47 +02:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
return placeOrder(stock, shares, price, orderType, orderPos, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
cancelOrder: function (symbol: any, shares: any, price: any, type: any, pos: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("cancelOrder", getRamCost("cancelOrder"));
|
|
|
|
checkTixApiAccess("cancelOrder");
|
|
|
|
if (Player.bitNodeN !== 8) {
|
|
|
|
if (SourceFileFlags[8] <= 2) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"cancelOrder",
|
|
|
|
"You must either be in BitNode-8 or you must have Source-File 8 Level 3.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const stock = getStockFromSymbol(symbol, "cancelOrder");
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isNaN(shares) || isNaN(price)) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"cancelOrder",
|
|
|
|
`Invalid shares or price. Must be numeric. shares=${shares}, price=${price}`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
let orderType;
|
|
|
|
let orderPos;
|
|
|
|
const ltype = type.toLowerCase();
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ltype.includes("limit") && ltype.includes("buy")) {
|
|
|
|
orderType = OrderTypes.LimitBuy;
|
|
|
|
} else if (ltype.includes("limit") && ltype.includes("sell")) {
|
|
|
|
orderType = OrderTypes.LimitSell;
|
|
|
|
} else if (ltype.includes("stop") && ltype.includes("buy")) {
|
|
|
|
orderType = OrderTypes.StopBuy;
|
|
|
|
} else if (ltype.includes("stop") && ltype.includes("sell")) {
|
|
|
|
orderType = OrderTypes.StopSell;
|
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("cancelOrder", `Invalid order type: ${type}`);
|
|
|
|
}
|
2017-10-25 21:47:47 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const lpos = pos.toLowerCase();
|
2021-09-05 01:09:30 +02:00
|
|
|
if (lpos.includes("l")) {
|
|
|
|
orderPos = PositionTypes.Long;
|
|
|
|
} else if (lpos.includes("s")) {
|
|
|
|
orderPos = PositionTypes.Short;
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("cancelOrder", `Invalid position type: ${pos}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const params = {
|
2021-09-05 01:09:30 +02:00
|
|
|
stock: stock,
|
|
|
|
shares: shares,
|
|
|
|
price: price,
|
|
|
|
type: orderType,
|
|
|
|
pos: orderPos,
|
|
|
|
};
|
|
|
|
return cancelOrder(params, workerScript);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getOrders: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getOrders", getRamCost("getOrders"));
|
|
|
|
checkTixApiAccess("getOrders");
|
|
|
|
if (Player.bitNodeN !== 8) {
|
|
|
|
if (SourceFileFlags[8] <= 2) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("getOrders", "You must either be in BitNode-8 or have Source-File 8 Level 3.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 01:11:48 +01:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const orders: any = {};
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
const stockMarketOrders = StockMarket["Orders"];
|
2021-09-25 07:26:03 +02:00
|
|
|
for (const symbol in stockMarketOrders) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const orderBook = stockMarketOrders[symbol];
|
|
|
|
if (orderBook.constructor === Array && orderBook.length > 0) {
|
|
|
|
orders[symbol] = [];
|
|
|
|
for (let i = 0; i < orderBook.length; ++i) {
|
|
|
|
orders[symbol].push({
|
|
|
|
shares: orderBook[i].shares,
|
|
|
|
price: orderBook[i].price,
|
|
|
|
type: orderBook[i].type,
|
|
|
|
position: orderBook[i].pos,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 01:11:48 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return orders;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockVolatility: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockVolatility", getRamCost("getStockVolatility"));
|
|
|
|
if (!Player.has4SDataTixApi) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getStockVolatility", "You don't have 4S Market Data TIX API Access!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockVolatility");
|
|
|
|
|
|
|
|
return stock.mv / 100; // Convert from percentage to decimal
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStockForecast: function (symbol: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStockForecast", getRamCost("getStockForecast"));
|
|
|
|
if (!Player.has4SDataTixApi) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getStockForecast", "You don't have 4S Market Data TIX API Access!");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
const stock = getStockFromSymbol(symbol, "getStockForecast");
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
let forecast = 50;
|
2021-09-05 01:09:30 +02:00
|
|
|
stock.b ? (forecast += stock.otlkMag) : (forecast -= stock.otlkMag);
|
|
|
|
return forecast / 100; // Convert from percentage to decimal
|
|
|
|
},
|
|
|
|
purchase4SMarketData: function () {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("purchase4SMarketData", getRamCost("purchase4SMarketData"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkTixApiAccess("purchase4SMarketData");
|
|
|
|
|
|
|
|
if (Player.has4SData) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchase4SMarketData", "Already purchased 4S Market Data.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.money.lt(getStockMarket4SDataCost())) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchase4SMarketData", "Not enough money to purchase 4S Market Data.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-29 08:20:27 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.has4SData = true;
|
|
|
|
Player.loseMoney(getStockMarket4SDataCost());
|
|
|
|
workerScript.log("purchase4SMarketData", "Purchased 4S Market Data");
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
purchase4SMarketDataTixApi: function () {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("purchase4SMarketDataTixApi", getRamCost("purchase4SMarketDataTixApi"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkTixApiAccess("purchase4SMarketDataTixApi");
|
|
|
|
|
|
|
|
if (Player.has4SDataTixApi) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchase4SMarketDataTixApi", "Already purchased 4S Market Data TIX API");
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.money.lt(getStockMarket4STixApiCost())) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchase4SMarketDataTixApi", "Not enough money to purchase 4S Market Data TIX API");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.has4SDataTixApi = true;
|
|
|
|
Player.loseMoney(getStockMarket4STixApiCost());
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchase4SMarketDataTixApi", "Purchased 4S Market Data TIX API");
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPurchasedServerLimit: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getPurchasedServerLimit", getRamCost("getPurchasedServerLimit"));
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return getPurchaseServerLimit();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPurchasedServerMaxRam: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getPurchasedServerMaxRam", getRamCost("getPurchasedServerMaxRam"));
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return getPurchaseServerMaxRam();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPurchasedServerCost: function (ram: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getPurchasedServerCost", getRamCost("getPurchasedServerCost"));
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
const cost = getPurchaseServerCost(ram);
|
|
|
|
if (cost === Infinity) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getPurchasedServerCost", `Invalid argument: ram='${ram}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return Infinity;
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return cost;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
purchaseServer: function (hostname: any, ram: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("purchaseServer", getRamCost("purchaseServer"));
|
2021-09-25 07:26:03 +02:00
|
|
|
let hostnameStr = String(hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
hostnameStr = hostnameStr.replace(/\s+/g, "");
|
|
|
|
if (hostnameStr == "") {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseServer", `Invalid argument: hostname='${hostnameStr}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return "";
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.purchasedServers.length >= getPurchaseServerLimit()) {
|
|
|
|
workerScript.log(
|
|
|
|
"purchaseServer",
|
|
|
|
`You have reached the maximum limit of ${getPurchaseServerLimit()} servers. You cannot purchase any more.`,
|
|
|
|
);
|
|
|
|
return "";
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const cost = getPurchaseServerCost(ram);
|
|
|
|
if (cost === Infinity) {
|
|
|
|
workerScript.log("purchaseServer", `Invalid argument: ram='${ram}'`);
|
|
|
|
return "";
|
|
|
|
}
|
2018-11-01 19:18:32 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.money.lt(cost)) {
|
|
|
|
workerScript.log(
|
|
|
|
"purchaseServer",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Not enough money to purchase server. Need ${numeralWrapper.formatMoney(cost)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return "";
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const newServ = safetlyCreateUniqueServer({
|
2021-09-05 01:09:30 +02:00
|
|
|
ip: createUniqueRandomIp(),
|
|
|
|
hostname: hostnameStr,
|
|
|
|
organizationName: "",
|
|
|
|
isConnectedTo: false,
|
|
|
|
adminRights: true,
|
|
|
|
purchasedByPlayer: true,
|
|
|
|
maxRam: ram,
|
|
|
|
});
|
|
|
|
AddToAllServers(newServ);
|
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
Player.purchasedServers.push(newServ.hostname);
|
2021-09-25 03:49:49 +02:00
|
|
|
const homeComputer = Player.getHomeComputer();
|
2021-10-07 23:55:49 +02:00
|
|
|
homeComputer.serversOnNetwork.push(newServ.hostname);
|
|
|
|
newServ.serversOnNetwork.push(homeComputer.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.loseMoney(cost);
|
|
|
|
workerScript.log(
|
|
|
|
"purchaseServer",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Purchased new server with hostname '${newServ.hostname}' for ${numeralWrapper.formatMoney(cost)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return newServ.hostname;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
deleteServer: function (hostname: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("deleteServer", getRamCost("deleteServer"));
|
2021-09-25 03:49:49 +02:00
|
|
|
let hostnameStr = String(hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
hostnameStr = hostnameStr.replace(/\s\s+/g, "");
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(hostnameStr);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("deleteServer", `Invalid argument: hostname='${hostnameStr}'`);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("deleteServer", `Invalid argument: hostname='${hostnameStr}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-20 04:21:11 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!server.purchasedByPlayer || server.hostname === "home") {
|
|
|
|
workerScript.log("deleteServer", "Cannot delete non-purchased server.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-20 04:21:11 +02:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
const ip = server.hostname;
|
2018-06-20 18:43:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Can't delete server you're currently connected to
|
|
|
|
if (server.isConnectedTo) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("deleteServer", "You are currently connected to the server you are trying to delete.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 18:43:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// A server cannot delete itself
|
2021-10-07 23:55:49 +02:00
|
|
|
if (ip === workerScript.hostname) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("deleteServer", "Cannot delete the server this script is running on.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Delete all scripts running on server
|
|
|
|
if (server.runningScripts.length > 0) {
|
|
|
|
workerScript.log(
|
|
|
|
"deleteServer",
|
|
|
|
`Cannot delete server '${server.hostname}' because it still has scripts running.`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Delete from player's purchasedServers array
|
2021-09-25 07:26:03 +02:00
|
|
|
let found = false;
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < Player.purchasedServers.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip == Player.purchasedServers[i]) {
|
|
|
|
found = true;
|
|
|
|
Player.purchasedServers.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:06:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!found) {
|
|
|
|
workerScript.log(
|
|
|
|
"deleteServer",
|
|
|
|
`Could not identify server ${server.hostname} as a purchased server. This is a bug. Report to dev.`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Delete from all servers
|
2021-10-07 22:04:04 +02:00
|
|
|
DeleteServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Delete from home computer
|
|
|
|
found = false;
|
2021-09-25 07:26:03 +02:00
|
|
|
const homeComputer = Player.getHomeComputer();
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < homeComputer.serversOnNetwork.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip == homeComputer.serversOnNetwork[i]) {
|
|
|
|
homeComputer.serversOnNetwork.splice(i, 1);
|
|
|
|
workerScript.log("deleteServer", `Deleted server '${hostnameStr}`);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wasn't found on home computer
|
|
|
|
workerScript.log(
|
|
|
|
"deleteServer",
|
|
|
|
`Could not find server ${server.hostname} as a purchased server. This is a bug. Report to dev.`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPurchasedServers: function (hostname: any = true): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getPurchasedServers", getRamCost("getPurchasedServers"));
|
2021-09-25 07:26:03 +02:00
|
|
|
const res: string[] = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.purchasedServers.forEach(function (ip) {
|
|
|
|
if (hostname) {
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getPurchasedServers", "Could not find server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
res.push(server.hostname);
|
|
|
|
} else {
|
|
|
|
res.push(ip);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
write: function (port: any, data: any = "", mode: any = "a"): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("write", getRamCost("write"));
|
|
|
|
if (!isNaN(port)) {
|
|
|
|
// Write to port
|
|
|
|
// Port 1-10
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"write",
|
|
|
|
`Trying to write to invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
2021-09-25 03:49:49 +02:00
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("write", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
return iport.write(data);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (isString(port)) {
|
|
|
|
// Write to script or text file
|
|
|
|
let fn = port;
|
|
|
|
if (!isValidFilePath(fn)) {
|
|
|
|
throw makeRuntimeErrorMsg("write", `Invalid filepath: ${fn}`);
|
|
|
|
}
|
2017-08-03 06:21:15 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (fn.lastIndexOf("/") === 0) {
|
|
|
|
fn = removeLeadingSlash(fn);
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Coerce 'data' to be a string
|
|
|
|
try {
|
|
|
|
data = String(data);
|
|
|
|
} catch (e) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("write", `Invalid data (${e}). Data being written must be convertible to a string`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-05-23 02:09:04 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = workerScript.getServer();
|
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("write", "Error getting Server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (isScriptFilename(fn)) {
|
|
|
|
// Write to script
|
2021-09-25 03:49:49 +02:00
|
|
|
let script = workerScript.getScriptOnServer(fn, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (script == null) {
|
|
|
|
// Create a new script
|
2021-10-07 23:55:49 +02:00
|
|
|
script = new Script(fn, data, server.hostname, server.scripts);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.scripts.push(script);
|
2021-10-08 20:05:47 +02:00
|
|
|
return script.updateRamUsage(server.scripts);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
mode === "w" ? (script.code = data) : (script.code += data);
|
2021-10-08 20:05:47 +02:00
|
|
|
return script.updateRamUsage(server.scripts);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
// Write to text file
|
2021-09-25 07:26:03 +02:00
|
|
|
const txtFile = getTextFile(fn, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (txtFile == null) {
|
2021-09-25 03:49:49 +02:00
|
|
|
createTextFile(fn, data, server);
|
2021-10-08 20:05:47 +02:00
|
|
|
return Promise.resolve();
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (mode === "w") {
|
|
|
|
txtFile.write(data);
|
|
|
|
} else {
|
|
|
|
txtFile.append(data);
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 20:05:47 +02:00
|
|
|
return Promise.resolve();
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("write", `Invalid argument: ${port}`);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
tryWrite: function (port: any, data: any = ""): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("tryWrite", getRamCost("tryWrite"));
|
|
|
|
if (!isNaN(port)) {
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"tryWrite",
|
|
|
|
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("tryWrite", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
return iport.tryWrite(data);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("tryWrite", `Invalid argument: ${port}`);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
read: function (port: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("read", getRamCost("read"));
|
|
|
|
if (!isNaN(port)) {
|
|
|
|
// Read from port
|
|
|
|
// Port 1-10
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"read",
|
|
|
|
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
2021-09-25 03:49:49 +02:00
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("read", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-05 22:37:54 +02:00
|
|
|
const x = iport.read();
|
|
|
|
console.log(x);
|
|
|
|
return x;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (isString(port)) {
|
|
|
|
// Read from script or text file
|
2021-09-25 07:26:03 +02:00
|
|
|
const fn = port;
|
2021-10-07 23:55:49 +02:00
|
|
|
const server = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("read", "Error getting Server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (isScriptFilename(fn)) {
|
|
|
|
// Read from script
|
2021-09-25 07:26:03 +02:00
|
|
|
const script = workerScript.getScriptOnServer(fn, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (script == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return script.code;
|
|
|
|
} else {
|
|
|
|
// Read from text file
|
2021-09-25 07:26:03 +02:00
|
|
|
const txtFile = getTextFile(fn, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (txtFile !== null) {
|
|
|
|
return txtFile.text;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("read", `Invalid argument: ${port}`);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
peek: function (port: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("peek", getRamCost("peek"));
|
|
|
|
if (isNaN(port)) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"peek",
|
|
|
|
`Invalid argument. Must be a port number between 1 and ${CONSTANTS.NumNetscriptPorts}, is ${port}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"peek",
|
|
|
|
`Invalid argument. Must be a port number between 1 and ${CONSTANTS.NumNetscriptPorts}, is ${port}`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("peek", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-10-05 22:37:54 +02:00
|
|
|
const x = iport.peek();
|
|
|
|
console.log(x);
|
|
|
|
return x;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
clear: function (port: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("clear", getRamCost("clear"));
|
|
|
|
if (!isNaN(port)) {
|
|
|
|
// Clear port
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"clear",
|
|
|
|
`Trying to clear invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("clear", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
return iport.clear();
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (isString(port)) {
|
|
|
|
// Clear text file
|
2021-09-25 07:26:03 +02:00
|
|
|
const fn = port;
|
2021-10-07 23:55:49 +02:00
|
|
|
const server = GetServer(workerScript.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("clear", "Error getting Server. This is a bug. Report to dev.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const txtFile = getTextFile(fn, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (txtFile != null) {
|
|
|
|
txtFile.write("");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw makeRuntimeErrorMsg("clear", `Invalid argument: ${port}`);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPortHandle: function (port: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getPortHandle", getRamCost("getPortHandle"));
|
|
|
|
if (isNaN(port)) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"getPortHandle",
|
|
|
|
`Invalid port: ${port} Must be an integer between 1 and ${CONSTANTS.NumNetscriptPorts}.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
port = Math.round(port);
|
|
|
|
if (port < 1 || port > CONSTANTS.NumNetscriptPorts) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"getPortHandle",
|
|
|
|
`Invalid port: ${port}. Only ports 1-${CONSTANTS.NumNetscriptPorts} are valid.`,
|
|
|
|
);
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
const iport = NetscriptPorts[port - 1];
|
|
|
|
if (iport == null || !(iport instanceof Object)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getPortHandle", `Could not find port: ${port}. This is a bug. Report to dev.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
return iport;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
rm: function (fn: any, ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("rm", getRamCost("rm"));
|
2017-08-03 06:21:15 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ip == null || ip === "") {
|
2021-10-07 23:55:49 +02:00
|
|
|
ip = workerScript.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
const s = safeGetServer(ip, "rm");
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const status = s.removeFile(fn);
|
|
|
|
if (!status.res) {
|
2021-09-25 03:49:49 +02:00
|
|
|
workerScript.log("rm", status.msg + "");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return status.res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
scriptRunning: function (scriptname: any, ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("scriptRunning", getRamCost("scriptRunning"));
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("scriptRunning", `Invalid IP/hostname: ${ip}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
for (let i = 0; i < server.runningScripts.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server.runningScripts[i].filename == scriptname) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
scriptKill: function (scriptname: any, ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("scriptKill", getRamCost("scriptKill"));
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("scriptKill", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
let suc = false;
|
|
|
|
for (let i = 0; i < server.runningScripts.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server.runningScripts[i].filename == scriptname) {
|
2021-10-07 23:55:49 +02:00
|
|
|
killWorkerScript(server.runningScripts[i], server.hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
suc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return suc;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getScriptName: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
return workerScript.name;
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
getScriptRam: function (scriptname: any, ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getScriptRam", getRamCost("getScriptRam"));
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
|
|
|
throw makeRuntimeErrorMsg("getScriptRam", `Invalid IP/hostname: ${ip}`);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
for (let i = 0; i < server.scripts.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server.scripts[i].filename == scriptname) {
|
|
|
|
return server.scripts[i].ramUsage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
getRunningScript: function (fn: any, ip: any, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getRunningScript", getRamCost("getRunningScript"));
|
|
|
|
|
|
|
|
let runningScript;
|
2021-09-25 08:36:49 +02:00
|
|
|
if (args.length === 0) {
|
2021-09-05 01:09:30 +02:00
|
|
|
runningScript = workerScript.scriptRef;
|
|
|
|
} else if (typeof fn === "number") {
|
|
|
|
runningScript = getRunningScriptByPid(fn, "getRunningScript");
|
|
|
|
} else {
|
|
|
|
const scriptArgs = [];
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 2; i < args.length; ++i) {
|
|
|
|
scriptArgs.push(args[i]);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
runningScript = getRunningScript(fn, ip, "getRunningScript", scriptArgs);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (runningScript === null) return null;
|
|
|
|
return {
|
|
|
|
args: runningScript.args.slice(),
|
|
|
|
filename: runningScript.filename,
|
|
|
|
logs: runningScript.logs.slice(),
|
|
|
|
offlineExpGained: runningScript.offlineExpGained,
|
|
|
|
offlineMoneyMade: runningScript.offlineMoneyMade,
|
|
|
|
offlineRunningTime: runningScript.offlineRunningTime,
|
|
|
|
onlineExpGained: runningScript.onlineExpGained,
|
|
|
|
onlineMoneyMade: runningScript.onlineMoneyMade,
|
|
|
|
onlineRunningTime: runningScript.onlineRunningTime,
|
|
|
|
pid: runningScript.pid,
|
|
|
|
ramUsage: runningScript.ramUsage,
|
|
|
|
server: runningScript.server,
|
|
|
|
threads: runningScript.threads,
|
|
|
|
};
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getHackTime: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getHackTime", getRamCost("getHackTime"));
|
|
|
|
const server = safeGetServer(ip, "getHackTime");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getHackTime", "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getHackTime")) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return calculateHackingTime(server, Player); // Returns seconds
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getGrowTime: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getGrowTime", getRamCost("getGrowTime"));
|
|
|
|
const server = safeGetServer(ip, "getGrowTime");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getGrowTime", "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getGrowTime")) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2018-11-14 07:05:51 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return calculateGrowTime(server, Player); // Returns seconds
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getWeakenTime: function (ip: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getWeakenTime", getRamCost("getWeakenTime"));
|
|
|
|
const server = safeGetServer(ip, "getWeakenTime");
|
2021-09-25 03:49:49 +02:00
|
|
|
if (!(server instanceof Server)) {
|
|
|
|
workerScript.log("getWeakenTime", "invalid for this kind of server");
|
|
|
|
return Infinity;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (failOnHacknetServer(server, "getWeakenTime")) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2018-01-27 07:52:39 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return calculateWeakenTime(server, Player); // Returns seconds
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
getScriptIncome: function (scriptname: any, ip: any, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getScriptIncome", getRamCost("getScriptIncome"));
|
|
|
|
if (arguments.length === 0) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const res = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// First element is total income of all currently running scripts
|
|
|
|
let total = 0;
|
|
|
|
for (const script of workerScripts.values()) {
|
2021-09-09 05:47:34 +02:00
|
|
|
total += script.scriptRef.onlineMoneyMade / script.scriptRef.onlineRunningTime;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
res.push(total);
|
|
|
|
|
|
|
|
// Second element is total income you've earned from scripts since you installed Augs
|
2021-09-09 05:47:34 +02:00
|
|
|
res.push(Player.scriptProdSinceLastAug / (Player.playtimeSinceLastAug / 1000));
|
2021-09-05 01:09:30 +02:00
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
// Get income for a particular script
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getScriptIncome", `Invalid IP/hostnamed: ${ip}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
const runningScriptObj = findRunningScript(scriptname, args, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (runningScriptObj == null) {
|
|
|
|
workerScript.log(
|
|
|
|
"getScriptIncome",
|
2021-09-25 08:36:49 +02:00
|
|
|
`No such script '${scriptname}' on '${server.hostname}' with args: ${arrayToString(args)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
return runningScriptObj.onlineMoneyMade / runningScriptObj.onlineRunningTime;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
getScriptExpGain: function (scriptname: any, ip: any, ...args: any[]): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getScriptExpGain", getRamCost("getScriptExpGain"));
|
|
|
|
if (arguments.length === 0) {
|
2021-09-25 07:26:03 +02:00
|
|
|
let total = 0;
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const ws of workerScripts.values()) {
|
2021-09-09 05:47:34 +02:00
|
|
|
total += ws.scriptRef.onlineExpGained / ws.scriptRef.onlineRunningTime;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
return total;
|
|
|
|
} else {
|
|
|
|
// Get income for a particular script
|
2021-10-07 22:56:01 +02:00
|
|
|
const server = GetServer(ip);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (server == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getScriptExpGain", `Invalid IP/hostnamed: ${ip}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-25 08:36:49 +02:00
|
|
|
const runningScriptObj = findRunningScript(scriptname, args, server);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (runningScriptObj == null) {
|
|
|
|
workerScript.log(
|
|
|
|
"getScriptExpGain",
|
2021-09-25 08:36:49 +02:00
|
|
|
`No such script '${scriptname}' on '${server.hostname}' with args: ${arrayToString(args)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
return runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
nFormat: function (n: any, format: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (isNaN(n) || isNaN(parseFloat(n)) || typeof format !== "string") {
|
|
|
|
return "";
|
|
|
|
}
|
2019-04-10 08:07:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return numeralWrapper.format(parseFloat(n), format);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
tFormat: function (milliseconds: any, milliPrecision: any = false): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
return convertTimeMsToTimeElapsedString(milliseconds, milliPrecision);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getTimeSinceLastAug: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getTimeSinceLastAug", getRamCost("getTimeSinceLastAug"));
|
2021-09-05 01:09:30 +02:00
|
|
|
return Player.playtimeSinceLastAug;
|
|
|
|
},
|
2021-10-13 02:02:37 +02:00
|
|
|
alert: function (message: any): void {
|
|
|
|
dialogBoxCreate(message);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
prompt: function (txt: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!isString(txt)) {
|
|
|
|
txt = JSON.stringify(txt);
|
|
|
|
}
|
2019-05-14 10:35:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Promise(function (resolve) {
|
2021-10-02 04:53:23 +02:00
|
|
|
PromptEvent.emit({
|
|
|
|
txt: txt,
|
|
|
|
resolve: resolve,
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
wget: async function (url: any, target: any, ip: any = workerScript.hostname): Promise<boolean> {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!isScriptFilename(target) && !target.endsWith(".txt")) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("wget", `Invalid target file: '${target}'. Must be a script or text file.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
const s = safeGetServer(ip, "wget");
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Promise(function (resolve) {
|
|
|
|
$.get(
|
|
|
|
url,
|
|
|
|
function (data) {
|
|
|
|
let res;
|
|
|
|
if (isScriptFilename(target)) {
|
|
|
|
res = s.writeToScriptFile(target, data);
|
2017-09-15 16:06:59 +02:00
|
|
|
} else {
|
2021-09-05 01:09:30 +02:00
|
|
|
res = s.writeToTextFile(target, data);
|
|
|
|
}
|
|
|
|
if (!res.success) {
|
|
|
|
workerScript.log("wget", "Failed.");
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
if (res.overwritten) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("wget", `Successfully retrieved content and overwrote '${target}' on '${ip}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return resolve(true);
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("wget", `Successfully retrieved content to new file '${target}' on '${ip}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return resolve(true);
|
|
|
|
},
|
|
|
|
"text",
|
|
|
|
).fail(function (e) {
|
|
|
|
workerScript.log("wget", JSON.stringify(e));
|
|
|
|
return resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getFavorToDonate: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getFavorToDonate", getRamCost("getFavorToDonate"));
|
2021-09-09 05:47:34 +02:00
|
|
|
return Math.floor(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Singularity Functions */
|
2021-09-25 03:49:49 +02:00
|
|
|
universityCourse: function (universityName: any, className: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("universityCourse", getRamCost("universityCourse"));
|
|
|
|
checkSingularityAccess("universityCourse", 1);
|
|
|
|
if (Player.isWorking) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const txt = Player.singularityStopWork();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("universityCourse", txt);
|
|
|
|
}
|
2017-08-21 18:59:06 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
let costMult, expMult;
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (universityName.toLowerCase()) {
|
|
|
|
case LocationName.AevumSummitUniversity.toLowerCase():
|
|
|
|
if (Player.city != CityName.Aevum) {
|
|
|
|
workerScript.log(
|
|
|
|
"universityCourse",
|
|
|
|
"You cannot study at 'Summit University' because you are not in 'Aevum'.",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.gotoLocation(LocationName.AevumSummitUniversity);
|
|
|
|
costMult = 4;
|
|
|
|
expMult = 3;
|
|
|
|
break;
|
|
|
|
case LocationName.Sector12RothmanUniversity.toLowerCase():
|
|
|
|
if (Player.city != CityName.Sector12) {
|
|
|
|
workerScript.log(
|
|
|
|
"universityCourse",
|
|
|
|
"You cannot study at 'Rothman University' because you are not in 'Sector-12'.",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.Sector12RothmanUniversity;
|
|
|
|
costMult = 3;
|
|
|
|
expMult = 2;
|
|
|
|
break;
|
|
|
|
case LocationName.VolhavenZBInstituteOfTechnology.toLowerCase():
|
|
|
|
if (Player.city != CityName.Volhaven) {
|
|
|
|
workerScript.log(
|
|
|
|
"universityCourse",
|
|
|
|
"You cannot study at 'ZB Institute of Technology' because you are not in 'Volhaven'.",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.VolhavenZBInstituteOfTechnology;
|
|
|
|
costMult = 5;
|
|
|
|
expMult = 4;
|
|
|
|
break;
|
|
|
|
default:
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("universityCourse", `Invalid university name: '${universityName}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
let task;
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (className.toLowerCase()) {
|
|
|
|
case "Study Computer Science".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassStudyComputerScience;
|
|
|
|
break;
|
|
|
|
case "Data Structures".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassDataStructures;
|
|
|
|
break;
|
|
|
|
case "Networks".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassNetworks;
|
|
|
|
break;
|
|
|
|
case "Algorithms".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassAlgorithms;
|
|
|
|
break;
|
|
|
|
case "Management".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassManagement;
|
|
|
|
break;
|
|
|
|
case "Leadership".toLowerCase():
|
|
|
|
task = CONSTANTS.ClassLeadership;
|
|
|
|
break;
|
|
|
|
default:
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("universityCourse", `Invalid class name: ${className}.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startClass(Router, costMult, expMult, task);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("universityCourse", `Started ${task} at ${universityName}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
gymWorkout: function (gymName: any, stat: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("gymWorkout", getRamCost("gymWorkout"));
|
|
|
|
checkSingularityAccess("gymWorkout", 1);
|
|
|
|
if (Player.isWorking) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const txt = Player.singularityStopWork();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("gymWorkout", txt);
|
|
|
|
}
|
2021-09-25 07:26:03 +02:00
|
|
|
let costMult, expMult;
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (gymName.toLowerCase()) {
|
|
|
|
case LocationName.AevumCrushFitnessGym.toLowerCase():
|
|
|
|
if (Player.city != CityName.Aevum) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("gymWorkout", "You cannot workout at 'Crush Fitness' because you are not in 'Aevum'.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.AevumCrushFitnessGym;
|
|
|
|
costMult = 3;
|
|
|
|
expMult = 2;
|
|
|
|
break;
|
|
|
|
case LocationName.AevumSnapFitnessGym.toLowerCase():
|
|
|
|
if (Player.city != CityName.Aevum) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("gymWorkout", "You cannot workout at 'Snap Fitness' because you are not in 'Aevum'.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.AevumSnapFitnessGym;
|
|
|
|
costMult = 10;
|
|
|
|
expMult = 5;
|
|
|
|
break;
|
|
|
|
case LocationName.Sector12IronGym.toLowerCase():
|
|
|
|
if (Player.city != CityName.Sector12) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("gymWorkout", "You cannot workout at 'Iron Gym' because you are not in 'Sector-12'.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.Sector12IronGym;
|
|
|
|
costMult = 1;
|
|
|
|
expMult = 1;
|
|
|
|
break;
|
|
|
|
case LocationName.Sector12PowerhouseGym.toLowerCase():
|
|
|
|
if (Player.city != CityName.Sector12) {
|
|
|
|
workerScript.log(
|
|
|
|
"gymWorkout",
|
|
|
|
"You cannot workout at 'Powerhouse Gym' because you are not in 'Sector-12'.",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.Sector12PowerhouseGym;
|
|
|
|
costMult = 20;
|
|
|
|
expMult = 10;
|
|
|
|
break;
|
|
|
|
case LocationName.VolhavenMilleniumFitnessGym.toLowerCase():
|
|
|
|
if (Player.city != CityName.Volhaven) {
|
|
|
|
workerScript.log(
|
|
|
|
"gymWorkout",
|
|
|
|
"You cannot workout at 'Millenium Fitness Gym' because you are not in 'Volhaven'.",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.location = LocationName.VolhavenMilleniumFitnessGym;
|
|
|
|
costMult = 7;
|
|
|
|
expMult = 4;
|
|
|
|
break;
|
|
|
|
default:
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("gymWorkout", `Invalid gym name: ${gymName}. gymWorkout() failed`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-22 20:48:04 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (stat.toLowerCase()) {
|
|
|
|
case "strength".toLowerCase():
|
|
|
|
case "str".toLowerCase():
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startClass(Router, costMult, expMult, CONSTANTS.ClassGymStrength);
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
case "defense".toLowerCase():
|
|
|
|
case "def".toLowerCase():
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startClass(Router, costMult, expMult, CONSTANTS.ClassGymDefense);
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
case "dexterity".toLowerCase():
|
|
|
|
case "dex".toLowerCase():
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startClass(Router, costMult, expMult, CONSTANTS.ClassGymDexterity);
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
case "agility".toLowerCase():
|
|
|
|
case "agi".toLowerCase():
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startClass(Router, costMult, expMult, CONSTANTS.ClassGymAgility);
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
workerScript.log("gymWorkout", `Invalid stat: ${stat}.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
workerScript.log("gymWorkout", `Started training ${stat} at ${gymName}`);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
travelToCity: function (cityname: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("travelToCity", getRamCost("travelToCity"));
|
|
|
|
checkSingularityAccess("travelToCity", 1);
|
|
|
|
|
|
|
|
switch (cityname) {
|
|
|
|
case CityName.Aevum:
|
|
|
|
case CityName.Chongqing:
|
|
|
|
case CityName.Sector12:
|
|
|
|
case CityName.NewTokyo:
|
|
|
|
case CityName.Ishima:
|
|
|
|
case CityName.Volhaven:
|
|
|
|
if (Player.money.lt(CONSTANTS.TravelCost)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("travelToCity", "Not enough money to travel.");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
Player.loseMoney(CONSTANTS.TravelCost);
|
|
|
|
Player.city = cityname;
|
|
|
|
workerScript.log("travelToCity", `Traveled to ${cityname}`);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
workerScript.log("travelToCity", `Invalid city name: '${cityname}'.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
purchaseTor: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("purchaseTor", getRamCost("purchaseTor"));
|
|
|
|
checkSingularityAccess("purchaseTor", 1);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
if (Player.hasTorRouter()) {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("purchaseTor", "You already have a TOR router!");
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.money.lt(CONSTANTS.TorRouterCost)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseTor", "You cannot afford to purchase a Tor router.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.loseMoney(CONSTANTS.TorRouterCost);
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const darkweb = safetlyCreateUniqueServer({
|
2021-09-05 01:09:30 +02:00
|
|
|
ip: createUniqueRandomIp(),
|
|
|
|
hostname: "darkweb",
|
|
|
|
organizationName: "",
|
|
|
|
isConnectedTo: false,
|
|
|
|
adminRights: false,
|
|
|
|
purchasedByPlayer: false,
|
|
|
|
maxRam: 1,
|
|
|
|
});
|
|
|
|
AddToAllServers(darkweb);
|
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
Player.getHomeComputer().serversOnNetwork.push(darkweb.hostname);
|
|
|
|
darkweb.serversOnNetwork.push(Player.getHomeComputer().hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain);
|
|
|
|
workerScript.log("purchaseTor", "You have purchased a Tor router!");
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
purchaseProgram: function (programName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("purchaseProgram", getRamCost("purchaseProgram"));
|
|
|
|
checkSingularityAccess("purchaseProgram", 1);
|
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
if (!Player.hasTorRouter()) {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("purchaseProgram", "You do not have the TOR router.");
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
programName = programName.toLowerCase();
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
let item = null;
|
|
|
|
for (const key in DarkWebItems) {
|
|
|
|
const i = DarkWebItems[key];
|
|
|
|
if (i.program.toLowerCase() == programName) {
|
|
|
|
item = i;
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (item == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseProgram", `Invalid program name: '${programName}.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.money.lt(item.price)) {
|
|
|
|
workerScript.log(
|
|
|
|
"purchaseProgram",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Not enough money to purchase '${item.program}'. Need ${numeralWrapper.formatMoney(item.price)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.hasProgram(item.program)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseProgram", `You already have the '${item.program}' program`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.loseMoney(item.price);
|
|
|
|
Player.getHomeComputer().programs.push(item.program);
|
|
|
|
workerScript.log(
|
|
|
|
"purchaseProgram",
|
|
|
|
`You have purchased the '${item.program}' program. The new program can be found on your home computer.`,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCurrentServer: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCurrentServer", getRamCost("getCurrentServer"));
|
|
|
|
checkSingularityAccess("getCurrentServer", 1);
|
|
|
|
return Player.getCurrentServer().hostname;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
connect: function (hostname: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("connect", getRamCost("connect"));
|
|
|
|
checkSingularityAccess("connect", 1);
|
|
|
|
if (!hostname) {
|
|
|
|
throw makeRuntimeErrorMsg("connect", `Invalid hostname: '${hostname}'`);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
const target = GetServer(hostname);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (target == null) {
|
|
|
|
throw makeRuntimeErrorMsg("connect", `Invalid hostname: '${hostname}'`);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (hostname === "home") {
|
|
|
|
Player.getCurrentServer().isConnectedTo = false;
|
2021-10-07 23:55:49 +02:00
|
|
|
Player.currentServer = Player.getHomeComputer().hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.getCurrentServer().isConnectedTo = true;
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.setcwd("/");
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const server = Player.getCurrentServer();
|
|
|
|
for (let i = 0; i < server.serversOnNetwork.length; i++) {
|
|
|
|
const other = getServerOnNetwork(server, i);
|
2021-09-25 03:49:49 +02:00
|
|
|
if (other === null) continue;
|
2021-10-07 23:55:49 +02:00
|
|
|
if (other.hostname == hostname) {
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.getCurrentServer().isConnectedTo = false;
|
2021-10-07 23:55:49 +02:00
|
|
|
Player.currentServer = target.hostname;
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.getCurrentServer().isConnectedTo = true;
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.setcwd("/");
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
manualHack: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("manualHack", getRamCost("manualHack"));
|
|
|
|
checkSingularityAccess("manualHack", 1);
|
|
|
|
const server = Player.getCurrentServer();
|
|
|
|
return hack(server.hostname, true);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
installBackdoor: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("installBackdoor", getRamCost("installBackdoor"));
|
|
|
|
checkSingularityAccess("installBackdoor", 1);
|
2021-09-25 03:49:49 +02:00
|
|
|
const baseserver = Player.getCurrentServer();
|
|
|
|
if (!(baseserver instanceof Server)) {
|
|
|
|
workerScript.log("installBackdoor", "cannot backdoor this kind of server");
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
const server = baseserver as Server;
|
2021-09-05 01:09:30 +02:00
|
|
|
const installTime = (calculateHackingTime(server, Player) / 4) * 1000;
|
|
|
|
|
|
|
|
// No root access or skill level too low
|
|
|
|
const canHack = netscriptCanHack(server, Player);
|
|
|
|
if (!canHack.res) {
|
2021-09-25 03:49:49 +02:00
|
|
|
throw makeRuntimeErrorMsg("installBackdoor", canHack.msg || "");
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log(
|
|
|
|
"installBackdoor",
|
2021-09-09 05:47:34 +02:00
|
|
|
`Installing backdoor on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(installTime, true)}`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return netscriptDelay(installTime, workerScript).then(function () {
|
|
|
|
if (workerScript.env.stopFlag) {
|
|
|
|
return Promise.reject(workerScript);
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("installBackdoor", `Successfully installed backdoor on '${server.hostname}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
server.backdoorInstalled = true;
|
2021-10-12 04:34:04 +02:00
|
|
|
|
|
|
|
if (SpecialServers.WorldDaemon === server.hostname) {
|
|
|
|
Router.toBitVerse(false, false);
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStats: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStats", getRamCost("getStats"));
|
|
|
|
checkSingularityAccess("getStats", 1);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getStats", `getStats is deprecated, please use getPlayer`);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
hacking: Player.hacking_skill,
|
|
|
|
strength: Player.strength,
|
|
|
|
defense: Player.defense,
|
|
|
|
dexterity: Player.dexterity,
|
|
|
|
agility: Player.agility,
|
|
|
|
charisma: Player.charisma,
|
|
|
|
intelligence: Player.intelligence,
|
|
|
|
};
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCharacterInformation: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCharacterInformation", getRamCost("getCharacterInformation"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getCharacterInformation", 1);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("getCharacterInformation", `getCharacterInformation is deprecated, please use getPlayer`);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
bitnode: Player.bitNodeN,
|
|
|
|
city: Player.city,
|
|
|
|
factions: Player.factions.slice(),
|
|
|
|
hp: Player.hp,
|
|
|
|
jobs: Object.keys(Player.jobs),
|
|
|
|
jobTitles: Object.values(Player.jobs),
|
|
|
|
maxHp: Player.max_hp,
|
|
|
|
mult: {
|
|
|
|
agility: Player.agility_mult,
|
|
|
|
agilityExp: Player.agility_exp_mult,
|
|
|
|
companyRep: Player.company_rep_mult,
|
|
|
|
crimeMoney: Player.crime_money_mult,
|
|
|
|
crimeSuccess: Player.crime_success_mult,
|
|
|
|
defense: Player.defense_mult,
|
|
|
|
defenseExp: Player.defense_exp_mult,
|
|
|
|
dexterity: Player.dexterity_mult,
|
|
|
|
dexterityExp: Player.dexterity_exp_mult,
|
|
|
|
factionRep: Player.faction_rep_mult,
|
|
|
|
hacking: Player.hacking_mult,
|
|
|
|
hackingExp: Player.hacking_exp_mult,
|
|
|
|
strength: Player.strength_mult,
|
|
|
|
strengthExp: Player.strength_exp_mult,
|
|
|
|
workMoney: Player.work_money_mult,
|
|
|
|
},
|
|
|
|
timeWorked: Player.timeWorked,
|
2021-10-07 23:55:49 +02:00
|
|
|
tor: Player.hasTorRouter(),
|
2021-09-05 01:09:30 +02:00
|
|
|
workHackExpGain: Player.workHackExpGained,
|
|
|
|
workStrExpGain: Player.workStrExpGained,
|
|
|
|
workDefExpGain: Player.workDefExpGained,
|
|
|
|
workDexExpGain: Player.workDexExpGained,
|
|
|
|
workAgiExpGain: Player.workAgiExpGained,
|
|
|
|
workChaExpGain: Player.workChaExpGained,
|
|
|
|
workRepGain: Player.workRepGained,
|
|
|
|
workMoneyGain: Player.workMoneyGained,
|
|
|
|
hackingExp: Player.hacking_exp,
|
|
|
|
strengthExp: Player.strength_exp,
|
|
|
|
defenseExp: Player.defense_exp,
|
|
|
|
dexterityExp: Player.dexterity_exp,
|
|
|
|
agilityExp: Player.agility_exp,
|
|
|
|
charismaExp: Player.charisma_exp,
|
|
|
|
};
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getPlayer: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getPlayer", getRamCost("getPlayer"));
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
hacking_skill: Player.hacking_skill,
|
|
|
|
hp: Player.hp,
|
|
|
|
max_hp: Player.max_hp,
|
|
|
|
strength: Player.strength,
|
|
|
|
defense: Player.defense,
|
|
|
|
dexterity: Player.dexterity,
|
|
|
|
agility: Player.agility,
|
|
|
|
charisma: Player.charisma,
|
|
|
|
intelligence: Player.intelligence,
|
|
|
|
hacking_chance_mult: Player.hacking_chance_mult,
|
|
|
|
hacking_speed_mult: Player.hacking_speed_mult,
|
|
|
|
hacking_money_mult: Player.hacking_money_mult,
|
|
|
|
hacking_grow_mult: Player.hacking_grow_mult,
|
|
|
|
hacking_exp: Player.hacking_exp,
|
|
|
|
strength_exp: Player.strength_exp,
|
|
|
|
defense_exp: Player.defense_exp,
|
|
|
|
dexterity_exp: Player.dexterity_exp,
|
|
|
|
agility_exp: Player.agility_exp,
|
|
|
|
charisma_exp: Player.charisma_exp,
|
|
|
|
hacking_mult: Player.hacking_mult,
|
|
|
|
strength_mult: Player.strength_mult,
|
|
|
|
defense_mult: Player.defense_mult,
|
|
|
|
dexterity_mult: Player.dexterity_mult,
|
|
|
|
agility_mult: Player.agility_mult,
|
|
|
|
charisma_mult: Player.charisma_mult,
|
|
|
|
hacking_exp_mult: Player.hacking_exp_mult,
|
|
|
|
strength_exp_mult: Player.strength_exp_mult,
|
|
|
|
defense_exp_mult: Player.defense_exp_mult,
|
|
|
|
dexterity_exp_mult: Player.dexterity_exp_mult,
|
|
|
|
agility_exp_mult: Player.agility_exp_mult,
|
|
|
|
charisma_exp_mult: Player.charisma_exp_mult,
|
|
|
|
company_rep_mult: Player.company_rep_mult,
|
|
|
|
faction_rep_mult: Player.faction_rep_mult,
|
|
|
|
numPeopleKilled: Player.numPeopleKilled,
|
|
|
|
money: Player.money.toNumber(),
|
|
|
|
city: Player.city,
|
|
|
|
location: Player.location,
|
2021-09-13 23:11:02 +02:00
|
|
|
companyName: Player.companyName,
|
2021-09-05 01:09:30 +02:00
|
|
|
crime_money_mult: Player.crime_money_mult,
|
|
|
|
crime_success_mult: Player.crime_success_mult,
|
|
|
|
isWorking: Player.isWorking,
|
|
|
|
workType: Player.workType,
|
|
|
|
currentWorkFactionName: Player.currentWorkFactionName,
|
|
|
|
currentWorkFactionDescription: Player.currentWorkFactionDescription,
|
|
|
|
workHackExpGainRate: Player.workHackExpGainRate,
|
|
|
|
workStrExpGainRate: Player.workStrExpGainRate,
|
|
|
|
workDefExpGainRate: Player.workDefExpGainRate,
|
|
|
|
workDexExpGainRate: Player.workDexExpGainRate,
|
|
|
|
workAgiExpGainRate: Player.workAgiExpGainRate,
|
|
|
|
workChaExpGainRate: Player.workChaExpGainRate,
|
|
|
|
workRepGainRate: Player.workRepGainRate,
|
|
|
|
workMoneyGainRate: Player.workMoneyGainRate,
|
|
|
|
workMoneyLossRate: Player.workMoneyLossRate,
|
|
|
|
workHackExpGained: Player.workHackExpGained,
|
|
|
|
workStrExpGained: Player.workStrExpGained,
|
|
|
|
workDefExpGained: Player.workDefExpGained,
|
|
|
|
workDexExpGained: Player.workDexExpGained,
|
|
|
|
workAgiExpGained: Player.workAgiExpGained,
|
|
|
|
workChaExpGained: Player.workChaExpGained,
|
|
|
|
workRepGained: Player.workRepGained,
|
|
|
|
workMoneyGained: Player.workMoneyGained,
|
|
|
|
createProgramName: Player.createProgramName,
|
|
|
|
createProgramReqLvl: Player.createProgramReqLvl,
|
|
|
|
className: Player.className,
|
|
|
|
crimeType: Player.crimeType,
|
|
|
|
work_money_mult: Player.work_money_mult,
|
|
|
|
hacknet_node_money_mult: Player.hacknet_node_money_mult,
|
|
|
|
hacknet_node_purchase_cost_mult: Player.hacknet_node_purchase_cost_mult,
|
|
|
|
hacknet_node_ram_cost_mult: Player.hacknet_node_ram_cost_mult,
|
|
|
|
hacknet_node_core_cost_mult: Player.hacknet_node_core_cost_mult,
|
|
|
|
hacknet_node_level_cost_mult: Player.hacknet_node_level_cost_mult,
|
|
|
|
hasWseAccount: Player.hasWseAccount,
|
|
|
|
hasTixApiAccess: Player.hasTixApiAccess,
|
|
|
|
has4SData: Player.has4SData,
|
|
|
|
has4SDataTixApi: Player.has4SDataTixApi,
|
|
|
|
bladeburner_max_stamina_mult: Player.bladeburner_max_stamina_mult,
|
|
|
|
bladeburner_stamina_gain_mult: Player.bladeburner_stamina_gain_mult,
|
|
|
|
bladeburner_analysis_mult: Player.bladeburner_analysis_mult,
|
|
|
|
bladeburner_success_chance_mult: Player.bladeburner_success_chance_mult,
|
|
|
|
bitNodeN: Player.bitNodeN,
|
|
|
|
totalPlaytime: Player.totalPlaytime,
|
|
|
|
playtimeSinceLastAug: Player.playtimeSinceLastAug,
|
|
|
|
playtimeSinceLastBitnode: Player.playtimeSinceLastBitnode,
|
|
|
|
jobs: {},
|
|
|
|
factions: Player.factions.slice(),
|
2021-10-07 23:55:49 +02:00
|
|
|
tor: Player.hasTorRouter(),
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
Object.assign(data.jobs, Player.jobs);
|
|
|
|
return data;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hospitalize: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("hospitalize", getRamCost("hospitalize"));
|
|
|
|
checkSingularityAccess("hospitalize", 1);
|
2021-10-12 05:14:15 +02:00
|
|
|
if (Player.isWorking || Router.page() === Page.Infiltration || Router.page() === Page.BitVerse) {
|
|
|
|
workerScript.log("hospitalize", "Cannot go to the hospital because the player is busy.");
|
|
|
|
return;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return Player.hospitalize();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
isBusy: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("isBusy", getRamCost("isBusy"));
|
|
|
|
checkSingularityAccess("isBusy", 1);
|
2021-10-12 05:14:15 +02:00
|
|
|
return Player.isWorking || Router.page() === Page.Infiltration || Router.page() === Page.BitVerse;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
stopAction: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("stopAction", getRamCost("stopAction"));
|
|
|
|
checkSingularityAccess("stopAction", 1);
|
|
|
|
if (Player.isWorking) {
|
2021-09-22 17:32:04 +02:00
|
|
|
Router.toTerminal();
|
2021-09-25 07:26:03 +02:00
|
|
|
const txt = Player.singularityStopWork();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("stopAction", txt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
upgradeHomeRam: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("upgradeHomeRam", getRamCost("upgradeHomeRam"));
|
|
|
|
checkSingularityAccess("upgradeHomeRam", 2);
|
|
|
|
|
|
|
|
// Check if we're at max RAM
|
|
|
|
const homeComputer = Player.getHomeComputer();
|
|
|
|
if (homeComputer.maxRam >= CONSTANTS.HomeComputerMaxRam) {
|
|
|
|
workerScript.log("upgradeHomeRam", `Your home computer is at max RAM.`);
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-01 23:05:05 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const cost = Player.getUpgradeHomeRamCost();
|
|
|
|
if (Player.money.lt(cost)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("upgradeHomeRam", `You don't have enough money. Need ${numeralWrapper.formatMoney(cost)}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
homeComputer.maxRam *= 2;
|
|
|
|
Player.loseMoney(cost);
|
|
|
|
|
|
|
|
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain);
|
|
|
|
workerScript.log(
|
|
|
|
"upgradeHomeRam",
|
2021-10-12 00:14:10 +02:00
|
|
|
`Purchased additional RAM for home computer! It now has ${numeralWrapper.formatRAM(
|
|
|
|
homeComputer.maxRam,
|
|
|
|
)} of RAM.`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getUpgradeHomeRamCost: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getUpgradeHomeRamCost", getRamCost("getUpgradeHomeRamCost"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getUpgradeHomeRamCost", 2);
|
|
|
|
|
|
|
|
return Player.getUpgradeHomeRamCost();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
workForCompany: function (companyName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("workForCompany", getRamCost("workForCompany"));
|
|
|
|
checkSingularityAccess("workForCompany", 2);
|
|
|
|
|
|
|
|
// Sanitize input
|
|
|
|
if (companyName == null) {
|
|
|
|
companyName = Player.companyName;
|
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Make sure its a valid company
|
2021-09-09 05:47:34 +02:00
|
|
|
if (companyName == null || companyName === "" || !(Companies[companyName] instanceof Company)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("workForCompany", `Invalid company: '${companyName}'`);
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Make sure player is actually employed at the comapny
|
|
|
|
if (!Object.keys(Player.jobs).includes(companyName)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForCompany", `You do not have a job at '${companyName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-24 02:30:15 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Check to make sure company position data is valid
|
|
|
|
const companyPositionName = Player.jobs[companyName];
|
|
|
|
const companyPosition = CompanyPositions[companyPositionName];
|
2021-09-09 05:47:34 +02:00
|
|
|
if (companyPositionName === "" || !(companyPosition instanceof CompanyPosition)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("workForCompany", "You do not have a job");
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-07 08:09:30 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.isWorking) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const txt = Player.singularityStopWork();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("workForCompany", txt);
|
|
|
|
}
|
2021-05-07 08:09:30 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (companyPosition.isPartTimeJob()) {
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startWorkPartTime(Router, companyName);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startWork(Router, companyName);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForCompany", `Began working at '${Player.companyName}' as a '${companyPositionName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
applyToCompany: function (companyName: any, field: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("applyToCompany", getRamCost("applyToCompany"));
|
|
|
|
checkSingularityAccess("applyToCompany", 2);
|
|
|
|
getCompany("applyToCompany", companyName);
|
|
|
|
|
|
|
|
Player.location = companyName;
|
2021-09-25 07:26:03 +02:00
|
|
|
let res;
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (field.toLowerCase()) {
|
|
|
|
case "software":
|
|
|
|
res = Player.applyForSoftwareJob(true);
|
|
|
|
break;
|
|
|
|
case "software consultant":
|
|
|
|
res = Player.applyForSoftwareConsultantJob(true);
|
|
|
|
break;
|
|
|
|
case "it":
|
|
|
|
res = Player.applyForItJob(true);
|
|
|
|
break;
|
|
|
|
case "security engineer":
|
|
|
|
res = Player.applyForSecurityEngineerJob(true);
|
|
|
|
break;
|
|
|
|
case "network engineer":
|
|
|
|
res = Player.applyForNetworkEngineerJob(true);
|
|
|
|
break;
|
|
|
|
case "business":
|
|
|
|
res = Player.applyForBusinessJob(true);
|
|
|
|
break;
|
|
|
|
case "business consultant":
|
|
|
|
res = Player.applyForBusinessConsultantJob(true);
|
|
|
|
break;
|
|
|
|
case "security":
|
|
|
|
res = Player.applyForSecurityJob(true);
|
|
|
|
break;
|
|
|
|
case "agent":
|
|
|
|
res = Player.applyForAgentJob(true);
|
|
|
|
break;
|
|
|
|
case "employee":
|
|
|
|
res = Player.applyForEmployeeJob(true);
|
|
|
|
break;
|
|
|
|
case "part-time employee":
|
|
|
|
res = Player.applyForPartTimeEmployeeJob(true);
|
|
|
|
break;
|
|
|
|
case "waiter":
|
|
|
|
res = Player.applyForWaiterJob(true);
|
|
|
|
break;
|
|
|
|
case "part-time waiter":
|
|
|
|
res = Player.applyForPartTimeWaiterJob(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
workerScript.log("applyToCompany", `Invalid job: '${field}'.`);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-25 03:49:49 +02:00
|
|
|
// TODO https://github.com/danielyxie/bitburner/issues/1378
|
2021-09-05 01:09:30 +02:00
|
|
|
// The Player object's applyForJob function can return string with special error messages
|
2021-09-25 03:49:49 +02:00
|
|
|
// if (isString(res)) {
|
|
|
|
// workerScript.log("applyToCompany", res);
|
|
|
|
// return false;
|
|
|
|
// }
|
2021-09-05 01:09:30 +02:00
|
|
|
if (res) {
|
|
|
|
workerScript.log(
|
|
|
|
"applyToCompany",
|
|
|
|
`You were offered a new job at '${companyName}' as a '${Player.jobs[companyName]}'`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
workerScript.log(
|
|
|
|
"applyToCompany",
|
|
|
|
`You failed to get a new job/promotion at '${companyName}' in the '${field}' field.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCompanyRep: function (companyName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCompanyRep", getRamCost("getCompanyRep"));
|
|
|
|
checkSingularityAccess("getCompanyRep", 2);
|
|
|
|
const company = getCompany("getCompanyRep", companyName);
|
|
|
|
return company.playerReputation;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCompanyFavor: function (companyName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCompanyFavor", getRamCost("getCompanyFavor"));
|
|
|
|
checkSingularityAccess("getCompanyFavor", 2);
|
|
|
|
const company = getCompany("getCompanyFavor", companyName);
|
|
|
|
return company.favor;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCompanyFavorGain: function (companyName: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCompanyFavorGain", getRamCost("getCompanyFavorGain"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getCompanyFavorGain", 2);
|
|
|
|
const company = getCompany("getCompanyFavorGain", companyName);
|
|
|
|
return company.getFavorGain()[0];
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
checkFactionInvitations: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("checkFactionInvitations", getRamCost("checkFactionInvitations"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("checkFactionInvitations", 2);
|
|
|
|
// Make a copy of Player.factionInvitations
|
|
|
|
return Player.factionInvitations.slice();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
joinFaction: function (name: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("joinFaction", getRamCost("joinFaction"));
|
|
|
|
checkSingularityAccess("joinFaction", 2);
|
|
|
|
getFaction("joinFaction", name);
|
|
|
|
|
|
|
|
if (!Player.factionInvitations.includes(name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("joinFaction", `You have not been invited by faction '${name}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const fac = Factions[name];
|
|
|
|
joinFaction(fac);
|
|
|
|
|
|
|
|
// Update Faction Invitation list to account for joined + banned factions
|
|
|
|
for (let i = 0; i < Player.factionInvitations.length; ++i) {
|
2021-09-09 05:47:34 +02:00
|
|
|
if (Player.factionInvitations[i] == name || Factions[Player.factionInvitations[i]].isBanned) {
|
2021-09-05 01:09:30 +02:00
|
|
|
Player.factionInvitations.splice(i, 1);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain);
|
|
|
|
workerScript.log("joinFaction", `Joined the '${name}' faction.`);
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
workForFaction: function (name: any, type: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("workForFaction", getRamCost("workForFaction"));
|
|
|
|
checkSingularityAccess("workForFaction", 2);
|
|
|
|
getFaction("workForFaction", name);
|
|
|
|
|
|
|
|
// if the player is in a gang and the target faction is any of the gang faction, fail
|
|
|
|
if (Player.inGang() && AllGangs[name] !== undefined) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Faction '${name}' does not offer work at the moment.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!Player.factions.includes(name)) {
|
|
|
|
workerScript.log("workForFaction", `You are not a member of '${name}'`);
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.isWorking) {
|
|
|
|
const txt = Player.singularityStopWork();
|
|
|
|
workerScript.log("workForFaction", txt);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const fac = Factions[name];
|
2021-09-05 01:09:30 +02:00
|
|
|
// Arrays listing factions that allow each time of work
|
2021-09-25 07:26:03 +02:00
|
|
|
const hackAvailable = [
|
2021-09-05 01:09:30 +02:00
|
|
|
"Illuminati",
|
|
|
|
"Daedalus",
|
|
|
|
"The Covenant",
|
|
|
|
"ECorp",
|
|
|
|
"MegaCorp",
|
|
|
|
"Bachman & Associates",
|
|
|
|
"Blade Industries",
|
|
|
|
"NWO",
|
|
|
|
"Clarke Incorporated",
|
|
|
|
"OmniTek Incorporated",
|
|
|
|
"Four Sigma",
|
|
|
|
"KuaiGong International",
|
|
|
|
"Fulcrum Secret Technologies",
|
|
|
|
"BitRunners",
|
|
|
|
"The Black Hand",
|
|
|
|
"NiteSec",
|
|
|
|
"Chongqing",
|
|
|
|
"Sector-12",
|
|
|
|
"New Tokyo",
|
|
|
|
"Aevum",
|
|
|
|
"Ishima",
|
|
|
|
"Volhaven",
|
|
|
|
"Speakers for the Dead",
|
|
|
|
"The Dark Army",
|
|
|
|
"The Syndicate",
|
|
|
|
"Silhouette",
|
|
|
|
"Netburners",
|
|
|
|
"Tian Di Hui",
|
|
|
|
"CyberSec",
|
|
|
|
];
|
2021-09-25 07:26:03 +02:00
|
|
|
const fdWkAvailable = [
|
2021-09-05 01:09:30 +02:00
|
|
|
"Illuminati",
|
|
|
|
"Daedalus",
|
|
|
|
"The Covenant",
|
|
|
|
"ECorp",
|
|
|
|
"MegaCorp",
|
|
|
|
"Bachman & Associates",
|
|
|
|
"Blade Industries",
|
|
|
|
"NWO",
|
|
|
|
"Clarke Incorporated",
|
|
|
|
"OmniTek Incorporated",
|
|
|
|
"Four Sigma",
|
|
|
|
"KuaiGong International",
|
|
|
|
"The Black Hand",
|
|
|
|
"Chongqing",
|
|
|
|
"Sector-12",
|
|
|
|
"New Tokyo",
|
|
|
|
"Aevum",
|
|
|
|
"Ishima",
|
|
|
|
"Volhaven",
|
|
|
|
"Speakers for the Dead",
|
|
|
|
"The Dark Army",
|
|
|
|
"The Syndicate",
|
|
|
|
"Silhouette",
|
|
|
|
"Tetrads",
|
|
|
|
"Slum Snakes",
|
|
|
|
];
|
2021-09-25 07:26:03 +02:00
|
|
|
const scWkAvailable = [
|
2021-09-05 01:09:30 +02:00
|
|
|
"ECorp",
|
|
|
|
"MegaCorp",
|
|
|
|
"Bachman & Associates",
|
|
|
|
"Blade Industries",
|
|
|
|
"NWO",
|
|
|
|
"Clarke Incorporated",
|
|
|
|
"OmniTek Incorporated",
|
|
|
|
"Four Sigma",
|
|
|
|
"KuaiGong International",
|
|
|
|
"Fulcrum Secret Technologies",
|
|
|
|
"Chongqing",
|
|
|
|
"Sector-12",
|
|
|
|
"New Tokyo",
|
|
|
|
"Aevum",
|
|
|
|
"Ishima",
|
|
|
|
"Volhaven",
|
|
|
|
"Speakers for the Dead",
|
|
|
|
"The Syndicate",
|
|
|
|
"Tetrads",
|
|
|
|
"Slum Snakes",
|
|
|
|
"Tian Di Hui",
|
|
|
|
];
|
|
|
|
|
|
|
|
switch (type.toLowerCase()) {
|
|
|
|
case "hacking":
|
|
|
|
case "hacking contracts":
|
|
|
|
case "hackingcontracts":
|
|
|
|
if (!hackAvailable.includes(fac.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Faction '${fac.name}' do not need help with hacking contracts.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startFactionHackWork(Router, fac);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Started carrying out hacking contracts for '${fac.name}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
case "field":
|
|
|
|
case "fieldwork":
|
|
|
|
case "field work":
|
|
|
|
if (!fdWkAvailable.includes(fac.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Faction '${fac.name}' do not need help with field missions.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startFactionFieldWork(Router, fac);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Started carrying out field missions for '${fac.name}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
case "security":
|
|
|
|
case "securitywork":
|
|
|
|
case "security work":
|
|
|
|
if (!scWkAvailable.includes(fac.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Faction '${fac.name}' do not need help with security work.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-20 07:45:32 +02:00
|
|
|
Player.startFactionSecurityWork(Router, fac);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("workForFaction", `Started carrying out security work for '${fac.name}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
workerScript.log("workForFaction", `Invalid work type: '${type}`);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getFactionRep: function (name: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getFactionRep", getRamCost("getFactionRep"));
|
|
|
|
checkSingularityAccess("getFactionRep", 2);
|
|
|
|
const faction = getFaction("getFactionRep", name);
|
|
|
|
return faction.playerReputation;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getFactionFavor: function (name: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getFactionFavor", getRamCost("getFactionFavor"));
|
|
|
|
checkSingularityAccess("getFactionFavor", 2);
|
|
|
|
const faction = getFaction("getFactionFavor", name);
|
|
|
|
return faction.favor;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getFactionFavorGain: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getFactionFavorGain", getRamCost("getFactionFavorGain"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getFactionFavorGain", 2);
|
|
|
|
const faction = getFaction("getFactionFavorGain", name);
|
|
|
|
return faction.getFavorGain()[0];
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
donateToFaction: function (name: any, amt: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("donateToFaction", getRamCost("donateToFaction"));
|
|
|
|
checkSingularityAccess("donateToFaction", 3);
|
|
|
|
const faction = getFaction("donateToFaction", name);
|
|
|
|
|
|
|
|
if (typeof amt !== "number" || amt <= 0) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("donateToFaction", `Invalid donation amount: '${amt}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Player.money.lt(amt)) {
|
|
|
|
workerScript.log(
|
|
|
|
"donateToFaction",
|
2021-09-09 05:47:34 +02:00
|
|
|
`You do not have enough money to donate ${numeralWrapper.formatMoney(amt)} to '${name}'`,
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
const repNeededToDonate = Math.round(CONSTANTS.BaseFavorToDonate * BitNodeMultipliers.RepToDonateToFaction);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (faction.favor < repNeededToDonate) {
|
|
|
|
workerScript.log(
|
|
|
|
"donateToFaction",
|
|
|
|
`You do not have enough favor to donate to this faction. Have ${faction.favor}, need ${repNeededToDonate}`,
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
const repGain = (amt / CONSTANTS.DonateMoneyToRepDivisor) * Player.faction_rep_mult;
|
2021-09-05 01:09:30 +02:00
|
|
|
faction.playerReputation += repGain;
|
|
|
|
Player.loseMoney(amt);
|
|
|
|
workerScript.log(
|
|
|
|
"donateToFaction",
|
2021-09-09 05:47:34 +02:00
|
|
|
`${numeralWrapper.formatMoney(amt)} donated to '${name}' for ${numeralWrapper.formatReputation(
|
2021-09-05 01:09:30 +02:00
|
|
|
repGain,
|
|
|
|
)} reputation`,
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
createProgram: function (name: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("createProgram", getRamCost("createProgram"));
|
|
|
|
checkSingularityAccess("createProgram", 3);
|
|
|
|
|
|
|
|
if (Player.isWorking) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const txt = Player.singularityStopWork();
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("createProgram", txt);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
name = name.toLowerCase();
|
2019-01-15 04:34:04 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
let p = null;
|
|
|
|
for (const key in Programs) {
|
|
|
|
if (Programs[key].name.toLowerCase() == name) {
|
|
|
|
p = Programs[key];
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 04:34:04 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (p == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("createProgram", `The specified program does not exist: '${name}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 04:34:04 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.hasProgram(p.name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("createProgram", `You already have the '${p.name}' program`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-01 23:56:30 +01:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const create = p.create;
|
|
|
|
if (create === null) {
|
|
|
|
workerScript.log("createProgram", `You cannot create the '${p.name}' program`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create.req(Player)) {
|
|
|
|
workerScript.log("createProgram", `Hacking level is too low to create '${p.name}' (level ${create.level} req)`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
Player.startCreateProgramWork(Router, p.name, create.time, create.level);
|
2021-09-05 01:09:30 +02:00
|
|
|
workerScript.log("createProgram", `Began creating program: '${name}'`);
|
|
|
|
return true;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
commitCrime: function (crimeRoughName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("commitCrime", getRamCost("commitCrime"));
|
|
|
|
checkSingularityAccess("commitCrime", 3);
|
2021-10-01 21:26:12 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.isWorking) {
|
|
|
|
const txt = Player.singularityStopWork();
|
|
|
|
workerScript.log("commitCrime", txt);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Set Location to slums
|
|
|
|
Player.gotoLocation(LocationName.Slums);
|
2018-06-10 06:55:25 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const crime = findCrime(crimeRoughName.toLowerCase());
|
|
|
|
if (crime == null) {
|
|
|
|
// couldn't find crime
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("commitCrime", `Invalid crime: '${crimeRoughName}'`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
workerScript.log("commitCrime", `Attempting to commit ${crime.name}...`);
|
2021-09-24 00:47:43 +02:00
|
|
|
return crime.commit(Router, Player, 1, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCrimeChance: function (crimeRoughName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCrimeChance", getRamCost("getCrimeChance"));
|
|
|
|
checkSingularityAccess("getCrimeChance", 3);
|
|
|
|
|
|
|
|
const crime = findCrime(crimeRoughName.toLowerCase());
|
|
|
|
if (crime == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getCrimeChance", `Invalid crime: ${crimeRoughName}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-11-01 23:56:30 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return crime.successRate(Player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCrimeStats: function (crimeRoughName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCrimeStats", getRamCost("getCrimeStats"));
|
|
|
|
checkSingularityAccess("getCrimeStats", 3);
|
|
|
|
|
|
|
|
const crime = findCrime(crimeRoughName.toLowerCase());
|
|
|
|
if (crime == null) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("getCrimeStats", `Invalid crime: ${crimeRoughName}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-08-30 20:36:59 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return Object.assign({}, crime);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getOwnedAugmentations: function (purchased: any = false): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getOwnedAugmentations", getRamCost("getOwnedAugmentations"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getOwnedAugmentations", 3);
|
2021-09-25 07:26:03 +02:00
|
|
|
const res = [];
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < Player.augmentations.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
res.push(Player.augmentations[i].name);
|
|
|
|
}
|
|
|
|
if (purchased) {
|
2021-09-25 08:36:49 +02:00
|
|
|
for (let i = 0; i < Player.queuedAugmentations.length; ++i) {
|
2021-09-05 01:09:30 +02:00
|
|
|
res.push(Player.queuedAugmentations[i].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getOwnedSourceFiles: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getOwnedSourceFiles", getRamCost("getOwnedSourceFiles"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getOwnedSourceFiles", 3);
|
2021-09-25 07:26:03 +02:00
|
|
|
const res = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
for (let i = 0; i < Player.sourceFiles.length; ++i) {
|
|
|
|
res.push({
|
|
|
|
n: Player.sourceFiles[i].n,
|
|
|
|
lvl: Player.sourceFiles[i].lvl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationsFromFaction: function (facname: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationsFromFaction", getRamCost("getAugmentationsFromFaction"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getAugmentationsFromFaction", 3);
|
|
|
|
const faction = getFaction("getAugmentationsFromFaction", facname);
|
|
|
|
|
|
|
|
// If player has a gang with this faction, return all augmentations.
|
|
|
|
if (Player.hasGangWith(facname)) {
|
|
|
|
const res = [];
|
|
|
|
for (const augName in Augmentations) {
|
|
|
|
const aug = Augmentations[augName];
|
|
|
|
if (!aug.isSpecial) {
|
|
|
|
res.push(augName);
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 18:59:06 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return res;
|
|
|
|
}
|
2021-03-11 09:02:05 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return faction.augmentations.slice();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationCost: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationCost", getRamCost("getAugmentationCost"));
|
2021-09-08 05:33:34 +02:00
|
|
|
checkSingularityAccess("getAugmentationCost", 3);
|
|
|
|
const aug = getAugmentation("getAugmentationCost", name);
|
|
|
|
return [aug.baseRepRequirement, aug.baseCost];
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationPrereq: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationPrereq", getRamCost("getAugmentationPrereq"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getAugmentationPrereq", 3);
|
|
|
|
const aug = getAugmentation("getAugmentationPrereq", name);
|
|
|
|
return aug.prereqs.slice();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationPrice: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationPrice", getRamCost("getAugmentationPrice"));
|
2021-09-08 05:33:34 +02:00
|
|
|
checkSingularityAccess("getAugmentationPrice", 3);
|
|
|
|
const aug = getAugmentation("getAugmentationPrice", name);
|
|
|
|
return aug.baseCost;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationRepReq: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationRepReq", getRamCost("getAugmentationRepReq"));
|
2021-09-08 05:33:34 +02:00
|
|
|
checkSingularityAccess("getAugmentationRepReq", 3);
|
|
|
|
const aug = getAugmentation("getAugmentationRepReq", name);
|
|
|
|
return aug.baseRepRequirement;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getAugmentationStats: function (name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getAugmentationStats", getRamCost("getAugmentationStats"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("getAugmentationStats", 3);
|
|
|
|
const aug = getAugmentation("getAugmentationStats", name);
|
|
|
|
return Object.assign({}, aug.mults);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
purchaseAugmentation: function (faction: any, name: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("purchaseAugmentation", getRamCost("purchaseAugmentation"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("purchaseAugmentation", 3);
|
|
|
|
const fac = getFaction("purchaseAugmentation", faction);
|
|
|
|
const aug = getAugmentation("purchaseAugmentation", name);
|
|
|
|
|
|
|
|
let augs = [];
|
|
|
|
if (Player.hasGangWith(faction)) {
|
|
|
|
for (const augName in Augmentations) {
|
|
|
|
const tempAug = Augmentations[augName];
|
|
|
|
if (!tempAug.isSpecial) {
|
|
|
|
augs.push(augName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
augs = fac.augmentations;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!augs.includes(name)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseAugmentation", `Faction '${faction}' does not have the '${name}' augmentation.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-02 01:31:34 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const isNeuroflux = aug.name === AugmentationNames.NeuroFluxGovernor;
|
|
|
|
if (!isNeuroflux) {
|
|
|
|
for (let j = 0; j < Player.queuedAugmentations.length; ++j) {
|
|
|
|
if (Player.queuedAugmentations[j].name === aug.name) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseAugmentation", `You already have the '${name}' augmentation.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let j = 0; j < Player.augmentations.length; ++j) {
|
|
|
|
if (Player.augmentations[j].name === aug.name) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseAugmentation", `You already have the '${name}' augmentation.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-02 01:31:34 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (fac.playerReputation < aug.baseRepRequirement) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("purchaseAugmentation", `You do not have enough reputation with '${fac.name}'.`);
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-02 01:31:34 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const res = purchaseAugmentation(aug, fac, true);
|
|
|
|
workerScript.log("purchaseAugmentation", res);
|
|
|
|
if (isString(res) && res.startsWith("You purchased")) {
|
|
|
|
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
softReset: function (cbScript: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("softReset", getRamCost("softReset"));
|
|
|
|
checkSingularityAccess("softReset", 3);
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("softReset", "Soft resetting. This will cause this script to be killed");
|
2021-10-05 01:58:34 +02:00
|
|
|
setTimeout(() => {
|
2021-09-05 01:09:30 +02:00
|
|
|
prestigeAugmentation();
|
|
|
|
runAfterReset(cbScript);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
// Prevent workerScript from "finishing execution naturally"
|
|
|
|
workerScript.running = false;
|
|
|
|
killWorkerScript(workerScript);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
installAugmentations: function (cbScript: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("installAugmentations", getRamCost("installAugmentations"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkSingularityAccess("installAugmentations", 3);
|
|
|
|
|
|
|
|
if (Player.queuedAugmentations.length === 0) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("installAugmentations", "You do not have any Augmentations to be installed.");
|
2021-09-05 01:09:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Player.gainIntelligenceExp(CONSTANTS.IntelligenceSingFnBaseExpGain);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("installAugmentations", "Installing Augmentations. This will cause this script to be killed");
|
2021-10-05 01:58:34 +02:00
|
|
|
setTimeout(() => {
|
2021-09-05 01:09:30 +02:00
|
|
|
installAugmentations();
|
|
|
|
runAfterReset(cbScript);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
workerScript.running = false; // Prevent workerScript from "finishing execution naturally"
|
|
|
|
killWorkerScript(workerScript);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Gang API
|
2021-10-05 03:06:55 +02:00
|
|
|
gang: gang,
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Bladeburner API
|
|
|
|
bladeburner: {
|
2021-09-25 03:49:49 +02:00
|
|
|
getContractNames: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getContractNames", getRamCost("bladeburner", "getContractNames"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getContractNames");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getContractNamesNetscriptFn();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getOperationNames: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getOperationNames", getRamCost("bladeburner", "getOperationNames"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getOperationNames");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getOperationNamesNetscriptFn();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getBlackOpNames: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getBlackOpNames", getRamCost("bladeburner", "getBlackOpNames"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getBlackOpNames");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getBlackOpNamesNetscriptFn();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getBlackOpRank: function (name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getBlackOpRank", getRamCost("bladeburner", "getBlackOpRank"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getBlackOpRank");
|
2021-09-25 03:49:49 +02:00
|
|
|
const action: any = getBladeburnerActionObject("getBlackOpRank", "blackops", name);
|
2021-09-05 01:09:30 +02:00
|
|
|
return action.reqdRank;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getGeneralActionNames: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getGeneralActionNames", getRamCost("bladeburner", "getGeneralActionNames"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getGeneralActionNames");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getGeneralActionNamesNetscriptFn();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getSkillNames: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getSkillNames", getRamCost("bladeburner", "getSkillNames"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getSkillNames");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getSkillNamesNetscriptFn();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
startAction: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("startAction", getRamCost("bladeburner", "startAction"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("startAction");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.startActionNetscriptFn(Player, type, name, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.startAction", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
stopBladeburnerAction: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("stopBladeburnerAction", getRamCost("bladeburner", "stopBladeburnerAction"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("stopBladeburnerAction");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.resetAction();
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCurrentAction: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCurrentAction", getRamCost("bladeburner", "getCurrentAction"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getCurrentAction");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.getTypeAndNameFromActionId(bladeburner.action);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionTime: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionTime", getRamCost("bladeburner", "getActionTime"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionTime");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getActionTimeNetscriptFn(Player, type, name, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getActionTime", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionEstimatedSuccessChance: function (type: any = "", name: any = ""): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam(
|
|
|
|
"getActionEstimatedSuccessChance",
|
|
|
|
getRamCost("bladeburner", "getActionEstimatedSuccessChance"),
|
|
|
|
);
|
|
|
|
checkBladeburnerAccess("getActionEstimatedSuccessChance");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getActionEstimatedSuccessChanceNetscriptFn(Player, type, name, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
2021-09-09 05:47:34 +02:00
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getActionEstimatedSuccessChance", e);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionRepGain: function (type: any = "", name: any = "", level: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionRepGain", getRamCost("bladeburner", "getActionRepGain"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionRepGain");
|
2021-09-09 05:47:34 +02:00
|
|
|
const action = getBladeburnerActionObject("getActionRepGain", type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
let rewardMultiplier;
|
|
|
|
if (level == null || isNaN(level)) {
|
|
|
|
rewardMultiplier = Math.pow(action.rewardFac, action.level - 1);
|
|
|
|
} else {
|
|
|
|
rewardMultiplier = Math.pow(action.rewardFac, level - 1);
|
|
|
|
}
|
2018-05-29 22:36:52 +02:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
return action.rankGain * rewardMultiplier * BitNodeMultipliers.BladeburnerRank;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionCountRemaining: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionCountRemaining", getRamCost("bladeburner", "getActionCountRemaining"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionCountRemaining");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getActionCountRemainingNetscriptFn(type, name, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getActionCountRemaining", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionMaxLevel: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionMaxLevel", getRamCost("bladeburner", "getActionMaxLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionMaxLevel");
|
2021-09-09 05:47:34 +02:00
|
|
|
const action = getBladeburnerActionObject("getActionMaxLevel", type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
return action.maxLevel;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionCurrentLevel: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionCurrentLevel", getRamCost("bladeburner", "getActionCurrentLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionCurrentLevel");
|
2021-09-09 05:47:34 +02:00
|
|
|
const action = getBladeburnerActionObject("getActionCurrentLevel", type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
return action.level;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getActionAutolevel: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getActionAutolevel", getRamCost("bladeburner", "getActionAutolevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getActionAutolevel");
|
2021-09-09 05:47:34 +02:00
|
|
|
const action = getBladeburnerActionObject("getActionCurrentLevel", type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
return action.autoLevel;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setActionAutolevel: function (type: any = "", name: any = "", autoLevel: any = true): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("setActionAutolevel", getRamCost("bladeburner", "setActionAutolevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("setActionAutolevel");
|
2021-09-09 05:47:34 +02:00
|
|
|
const action = getBladeburnerActionObject("setActionAutolevel", type, name);
|
2021-09-05 01:09:30 +02:00
|
|
|
action.autoLevel = autoLevel;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setActionLevel: function (type: any = "", name: any = "", level: any = 1): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("setActionLevel", getRamCost("bladeburner", "setActionLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("setActionLevel");
|
|
|
|
const action = getBladeburnerActionObject("setActionLevel", type, name);
|
|
|
|
if (level < 1 || level > action.maxLevel) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"bladeburner.setActionLevel",
|
|
|
|
`Level must be between 1 and ${action.maxLevel}, is ${level}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
action.level = level;
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getRank: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getRank", getRamCost("bladeburner", "getRank"));
|
|
|
|
checkBladeburnerAccess("getRank");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.rank;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getSkillPoints: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getSkillPoints", getRamCost("bladeburner", "getSkillPoints"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getSkillPoints");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.skillPoints;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getSkillLevel: function (skillName: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getSkillLevel", getRamCost("bladeburner", "getSkillLevel"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getSkillLevel");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getSkillLevelNetscriptFn(skillName, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getSkillLevel", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getSkillUpgradeCost: function (skillName: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getSkillUpgradeCost", getRamCost("bladeburner", "getSkillUpgradeCost"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getSkillUpgradeCost");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getSkillUpgradeCostNetscriptFn(skillName, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getSkillUpgradeCost", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
upgradeSkill: function (skillName: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("upgradeSkill", getRamCost("bladeburner", "upgradeSkill"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("upgradeSkill");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.upgradeSkillNetscriptFn(skillName, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.upgradeSkill", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getTeamSize: function (type: any = "", name: any = ""): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getTeamSize", getRamCost("bladeburner", "getTeamSize"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getTeamSize");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.getTeamSizeNetscriptFn(type, name, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.getTeamSize", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setTeamSize: function (type: any = "", name: any = "", size: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("setTeamSize", getRamCost("bladeburner", "setTeamSize"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("setTeamSize");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-25 03:49:49 +02:00
|
|
|
return bladeburner.setTeamSizeNetscriptFn(type, name, size, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw makeRuntimeErrorMsg("bladeburner.setTeamSize", e);
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCityEstimatedPopulation: function (cityName: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCityEstimatedPopulation", getRamCost("bladeburner", "getCityEstimatedPopulation"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getCityEstimatedPopulation");
|
|
|
|
checkBladeburnerCity("getCityEstimatedPopulation", cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.cities[cityName].popEst;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCityEstimatedCommunities: function (cityName: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCityEstimatedCommunities", getRamCost("bladeburner", "getCityEstimatedCommunities"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getCityEstimatedCommunities");
|
|
|
|
checkBladeburnerCity("getCityEstimatedCommunities", cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.cities[cityName].commsEst;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCityChaos: function (cityName: any): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getCityChaos", getRamCost("bladeburner", "getCityChaos"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getCityChaos");
|
|
|
|
checkBladeburnerCity("getCityChaos", cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.cities[cityName].chaos;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getCity: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getCity", getRamCost("bladeburner", "getCity"));
|
|
|
|
checkBladeburnerAccess("getCityChaos");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.city;
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
switchCity: function (cityName: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("switchCity", getRamCost("bladeburner", "switchCity"));
|
|
|
|
checkBladeburnerAccess("switchCity");
|
|
|
|
checkBladeburnerCity("switchCity", cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return (bladeburner.city = cityName);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getStamina: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getStamina", getRamCost("bladeburner", "getStamina"));
|
|
|
|
checkBladeburnerAccess("getStamina");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return [bladeburner.stamina, bladeburner.maxStamina];
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
joinBladeburnerFaction: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("joinBladeburnerFaction", getRamCost("bladeburner", "joinBladeburnerFaction"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("joinBladeburnerFaction", true);
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return bladeburner.joinBladeburnerFactionNetscriptFn(workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
joinBladeburnerDivision: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("joinBladeburnerDivision", getRamCost("bladeburner", "joinBladeburnerDivision"));
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Player.bitNodeN === 7 || SourceFileFlags[7] > 0) {
|
|
|
|
if (Player.bitNodeN === 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Player.bladeburner instanceof Bladeburner) {
|
|
|
|
return true; // Already member
|
|
|
|
} else if (
|
|
|
|
Player.strength >= 100 &&
|
|
|
|
Player.defense >= 100 &&
|
|
|
|
Player.dexterity >= 100 &&
|
|
|
|
Player.agility >= 100
|
|
|
|
) {
|
|
|
|
Player.bladeburner = new Bladeburner(Player);
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("joinBladeburnerDivision", "You have been accepted into the Bladeburner division");
|
2021-03-08 04:22:23 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
workerScript.log(
|
|
|
|
"joinBladeburnerDivision",
|
|
|
|
"You do not meet the requirements for joining the Bladeburner division",
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getBonusTime: function (): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getBonusTime", getRamCost("bladeburner", "getBonusTime"));
|
2021-09-05 01:09:30 +02:00
|
|
|
checkBladeburnerAccess("getBonusTime");
|
2021-09-25 03:49:49 +02:00
|
|
|
const bladeburner = Player.bladeburner;
|
|
|
|
if (bladeburner === null) throw new Error("Should not be called without Bladeburner");
|
|
|
|
return Math.round(bladeburner.storedCycles / 5);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
}, // End Bladeburner
|
|
|
|
|
2021-09-10 08:17:55 +02:00
|
|
|
// Hi, if you're reading this you're a bit nosy.
|
|
|
|
// There's a corporation API but it's very imbalanced right now.
|
|
|
|
// It's here so players can test with if they want.
|
|
|
|
corporation: {
|
2021-09-25 03:49:49 +02:00
|
|
|
expandIndustry: function (industryName: any, divisionName: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
NewIndustry(corporation, industryName, divisionName);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
expandCity: function (divisionName: any, cityName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const division = getDivision(divisionName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
NewCity(corporation, division, cityName);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
unlockUpgrade: function (upgradeName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const upgrade = Object.values(CorporationUnlockUpgrades).find((upgrade) => upgrade[2] === upgradeName);
|
2021-09-13 00:03:07 +02:00
|
|
|
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
|
2021-09-25 03:49:49 +02:00
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
UnlockUpgrade(corporation, upgrade);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
levelUpgrade: function (upgradeName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const upgrade = Object.values(CorporationUpgrades).find((upgrade) => upgrade[4] === upgradeName);
|
2021-09-13 00:03:07 +02:00
|
|
|
if (upgrade === undefined) throw new Error(`No upgrade named '${upgradeName}'`);
|
2021-09-25 03:49:49 +02:00
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
LevelUpgrade(corporation, upgrade);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
issueDividends: function (percent: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
IssueDividends(corporation, percent);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sellMaterial: function (divisionName: any, cityName: any, materialName: any, amt: any, price: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const material = getMaterial(divisionName, cityName, materialName);
|
|
|
|
SellMaterial(material, amt, price);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
sellProduct: function (divisionName: any, cityName: any, productName: any, amt: any, price: any, all: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const product = getProduct(divisionName, productName);
|
|
|
|
SellProduct(product, cityName, amt, price, all);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
discontinueProduct: function (divisionName: any, productName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
getDivision(divisionName).discontinueProduct(getProduct(divisionName, productName));
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setSmartSupply: function (divisionName: any, cityName: any, enabled: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const warehouse = getWarehouse(divisionName, cityName);
|
|
|
|
SetSmartSupply(warehouse, enabled);
|
|
|
|
},
|
2021-09-25 08:36:49 +02:00
|
|
|
// setSmartSupplyUseLeftovers: function (): any {},
|
2021-09-25 03:49:49 +02:00
|
|
|
buyMaterial: function (divisionName: any, cityName: any, materialName: any, amt: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const material = getMaterial(divisionName, cityName, materialName);
|
|
|
|
BuyMaterial(material, amt);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
employees: function (divisionName: any, cityName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const office = getOffice(divisionName, cityName);
|
|
|
|
return office.employees.map((e) => Object.assign({}, e));
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
assignJob: function (divisionName: any, cityName: any, employeeName: any, job: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const employee = getEmployee(divisionName, cityName, employeeName);
|
|
|
|
AssignJob(employee, job);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hireEmployee: function (divisionName: any, cityName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const office = getOffice(divisionName, cityName);
|
|
|
|
office.hireRandomEmployee();
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
upgradeOfficeSize: function (divisionName: any, cityName: any, size: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const office = getOffice(divisionName, cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
UpgradeOfficeSize(corporation, office, size);
|
2021-09-10 08:17:55 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
throwParty: function (divisionName: any, cityName: any, costPerEmployee: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
const office = getOffice(divisionName, cityName);
|
2021-09-25 03:49:49 +02:00
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
ThrowParty(corporation, office, costPerEmployee);
|
|
|
|
},
|
|
|
|
purchaseWarehouse: function (divisionName: any, cityName: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
PurchaseWarehouse(corporation, getDivision(divisionName), cityName);
|
|
|
|
},
|
|
|
|
upgradeWarehouse: function (divisionName: any, cityName: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
UpgradeWarehouse(corporation, getDivision(divisionName), getWarehouse(divisionName, cityName));
|
|
|
|
},
|
|
|
|
buyCoffee: function (divisionName: any, cityName: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
BuyCoffee(corporation, getDivision(divisionName), getOffice(divisionName, cityName));
|
|
|
|
},
|
|
|
|
hireAdVert: function (divisionName: any): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
HireAdVert(corporation, getDivision(divisionName), getOffice(divisionName, "Sector-12"));
|
|
|
|
},
|
|
|
|
makeProduct: function (
|
|
|
|
divisionName: any,
|
|
|
|
cityName: any,
|
|
|
|
productName: any,
|
|
|
|
designInvest: any,
|
|
|
|
marketingInvest: any,
|
|
|
|
): any {
|
|
|
|
const corporation = Player.corporation;
|
|
|
|
if (corporation === null) throw new Error("Should not be called without a corporation");
|
|
|
|
MakeProduct(corporation, getDivision(divisionName), cityName, productName, designInvest, marketingInvest);
|
|
|
|
},
|
|
|
|
research: function (divisionName: any, researchName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
Research(getDivision(divisionName), researchName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
exportMaterial: function (
|
|
|
|
sourceDivision: any,
|
|
|
|
sourceCity: any,
|
|
|
|
targetDivision: any,
|
|
|
|
targetCity: any,
|
|
|
|
materialName: any,
|
|
|
|
amt: any,
|
|
|
|
): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
ExportMaterial(targetDivision, targetCity, getMaterial(sourceDivision, sourceCity, materialName), amt + "");
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
cancelExportMaterial: function (
|
|
|
|
sourceDivision: any,
|
|
|
|
sourceCity: any,
|
|
|
|
targetDivision: any,
|
|
|
|
targetCity: any,
|
|
|
|
materialName: any,
|
|
|
|
amt: any,
|
|
|
|
): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
CancelExportMaterial(
|
|
|
|
targetDivision,
|
|
|
|
targetCity,
|
|
|
|
getMaterial(sourceDivision, sourceCity, materialName),
|
|
|
|
amt + "",
|
|
|
|
);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setMaterialMarketTA1: function (divisionName: any, cityName: any, materialName: any, on: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
SetMaterialMarketTA1(getMaterial(divisionName, cityName, materialName), on);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setMaterialMarketTA2: function (divisionName: any, cityName: any, materialName: any, on: any) {
|
2021-09-10 08:17:55 +02:00
|
|
|
SetMaterialMarketTA2(getMaterial(divisionName, cityName, materialName), on);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setProductMarketTA1: function (divisionName: any, productName: any, on: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
SetProductMarketTA1(getProduct(divisionName, productName), on);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
setProductMarketTA2: function (divisionName: any, productName: any, on: any) {
|
2021-09-10 08:17:55 +02:00
|
|
|
SetProductMarketTA2(getProduct(divisionName, productName), on);
|
|
|
|
},
|
|
|
|
// If you modify these objects you will affect them for real, it's not
|
|
|
|
// copies.
|
2021-09-25 03:49:49 +02:00
|
|
|
getDivision: function (divisionName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getDivision(divisionName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getOffice: function (divisionName: any, cityName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getOffice(divisionName, cityName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getWarehouse: function (divisionName: any, cityName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getWarehouse(divisionName, cityName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getMaterial: function (divisionName: any, cityName: any, materialName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getMaterial(divisionName, cityName, materialName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getProduct: function (divisionName: any, productName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getProduct(divisionName, productName);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
getEmployee: function (divisionName: any, cityName: any, employeeName: any): any {
|
2021-09-10 08:17:55 +02:00
|
|
|
return getEmployee(divisionName, cityName, employeeName);
|
|
|
|
},
|
|
|
|
}, // End Corporation API
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Coding Contract API
|
|
|
|
codingcontract: {
|
2021-10-07 23:55:49 +02:00
|
|
|
attempt: function (answer: any, fn: any, ip: any = workerScript.hostname, { returnReward }: any = {}): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("attempt", getRamCost("codingcontract", "attempt"));
|
|
|
|
const contract = getCodingContract("attempt", ip, fn);
|
|
|
|
|
|
|
|
// Convert answer to string. If the answer is a 2D array, then we have to
|
|
|
|
// manually add brackets for the inner arrays
|
|
|
|
if (is2DArray(answer)) {
|
2021-09-25 07:26:03 +02:00
|
|
|
const answerComponents = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
for (let i = 0; i < answer.length; ++i) {
|
|
|
|
answerComponents.push(["[", answer[i].toString(), "]"].join(""));
|
|
|
|
}
|
2019-05-02 00:20:14 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
answer = answerComponents.join(",");
|
|
|
|
} else {
|
|
|
|
answer = String(answer);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-25 03:49:49 +02:00
|
|
|
const creward = contract.reward;
|
|
|
|
if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward");
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
const serv = safeGetServer(ip, "codingcontract.attempt");
|
|
|
|
if (contract.isSolution(answer)) {
|
2021-09-25 03:49:49 +02:00
|
|
|
const reward = Player.gainCodingContractReward(creward, contract.getDifficulty());
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("attempt", `Successfully completed Coding Contract '${fn}'. Reward: ${reward}`);
|
2021-09-05 01:09:30 +02:00
|
|
|
serv.removeContract(fn);
|
|
|
|
return returnReward ? reward : true;
|
|
|
|
} else {
|
|
|
|
++contract.tries;
|
|
|
|
if (contract.tries >= contract.getMaxNumTries()) {
|
2021-09-09 05:47:34 +02:00
|
|
|
workerScript.log("attempt", `Coding Contract attempt '${fn}' failed. Contract is now self-destructing`);
|
2021-09-05 01:09:30 +02:00
|
|
|
serv.removeContract(fn);
|
|
|
|
} else {
|
|
|
|
workerScript.log(
|
|
|
|
"attempt",
|
|
|
|
`Coding Contract attempt '${fn}' failed. ${
|
|
|
|
contract.getMaxNumTries() - contract.tries
|
|
|
|
} attempts remaining.`,
|
|
|
|
);
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return returnReward ? "" : false;
|
|
|
|
}
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
getContractType: function (fn: any, ip: any = workerScript.hostname): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getContractType", getRamCost("codingcontract", "getContractType"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const contract = getCodingContract("getContractType", ip, fn);
|
|
|
|
return contract.getType();
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
getData: function (fn: any, ip: any = workerScript.hostname): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
updateDynamicRam("getData", getRamCost("codingcontract", "getData"));
|
|
|
|
const contract = getCodingContract("getData", ip, fn);
|
|
|
|
const data = contract.getData();
|
|
|
|
if (data.constructor === Array) {
|
|
|
|
// For two dimensional arrays, we have to copy the internal arrays using
|
|
|
|
// slice() as well. As of right now, no contract has arrays that have
|
|
|
|
// more than two dimensions
|
|
|
|
const copy = data.slice();
|
|
|
|
for (let i = 0; i < copy.length; ++i) {
|
|
|
|
if (data[i].constructor === Array) {
|
|
|
|
copy[i] = data[i].slice();
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return copy;
|
|
|
|
} else {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
getDescription: function (fn: any, ip: any = workerScript.hostname): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getDescription", getRamCost("codingcontract", "getDescription"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const contract = getCodingContract("getDescription", ip, fn);
|
|
|
|
return contract.getDescription();
|
|
|
|
},
|
2021-10-07 23:55:49 +02:00
|
|
|
getNumTriesRemaining: function (fn: any, ip: any = workerScript.hostname): any {
|
2021-09-09 05:47:34 +02:00
|
|
|
updateDynamicRam("getNumTriesRemaining", getRamCost("codingcontract", "getNumTriesRemaining"));
|
2021-09-05 01:09:30 +02:00
|
|
|
const contract = getCodingContract("getNumTriesRemaining", ip, fn);
|
|
|
|
return contract.getMaxNumTries() - contract.tries;
|
|
|
|
},
|
|
|
|
}, // End coding contracts
|
|
|
|
|
2021-10-05 04:25:21 +02:00
|
|
|
sleeve: sleeve,
|
2021-09-05 01:09:30 +02:00
|
|
|
formulas: {
|
|
|
|
basic: {
|
2021-09-25 03:49:49 +02:00
|
|
|
calculateSkill: function (exp: any, mult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.calculateSkill", 5);
|
|
|
|
return calculateSkill(exp, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
calculateExp: function (skill: any, mult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.calculateExp", 5);
|
|
|
|
return calculateExp(skill, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackChance: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.hackChance", 5);
|
|
|
|
return calculateHackingChance(server, player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackExp: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.hackExp", 5);
|
|
|
|
return calculateHackingExpGain(server, player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackPercent: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.hackPercent", 5);
|
|
|
|
return calculatePercentMoneyHacked(server, player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
growPercent: function (server: any, threads: any, player: any, cores: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.growPercent", 5);
|
|
|
|
return calculateServerGrowth(server, threads, player, cores);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hackTime: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.hackTime", 5);
|
|
|
|
return calculateHackingTime(server, player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
growTime: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.growTime", 5);
|
|
|
|
return calculateGrowTime(server, player);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
weakenTime: function (server: any, player: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("basic.weakenTime", 5);
|
|
|
|
return calculateWeakenTime(server, player);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hacknetNodes: {
|
2021-09-25 03:49:49 +02:00
|
|
|
moneyGainRate: function (level: any, ram: any, cores: any, mult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.moneyGainRate", 5);
|
|
|
|
return calculateMoneyGainRate(level, ram, cores, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
levelUpgradeCost: function (startingLevel: any, extraLevels: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.levelUpgradeCost", 5);
|
2021-09-09 05:47:34 +02:00
|
|
|
return calculateLevelUpgradeCost(startingLevel, extraLevels, costMult);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
ramUpgradeCost: function (startingRam: any, extraLevels: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.ramUpgradeCost", 5);
|
|
|
|
return calculateRamUpgradeCost(startingRam, extraLevels, costMult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
coreUpgradeCost: function (startingCore: any, extraCores: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.coreUpgradeCost", 5);
|
|
|
|
return calculateCoreUpgradeCost(startingCore, extraCores, costMult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hacknetNodeCost: function (n: any, mult: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.hacknetNodeCost", 5);
|
|
|
|
return calculateNodeCost(n, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
constants: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetNodes.constants", 5);
|
|
|
|
return Object.assign({}, HacknetNodeConstants);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hacknetServers: {
|
2021-09-25 03:49:49 +02:00
|
|
|
hashGainRate: function (level: any, ramUsed: any, maxRam: any, cores: any, mult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.hashGainRate", 9);
|
|
|
|
return HScalculateHashGainRate(level, ramUsed, maxRam, cores, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
levelUpgradeCost: function (startingLevel: any, extraLevels: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.levelUpgradeCost", 9);
|
2021-09-09 05:47:34 +02:00
|
|
|
return HScalculateLevelUpgradeCost(startingLevel, extraLevels, costMult);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
ramUpgradeCost: function (startingRam: any, extraLevels: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.ramUpgradeCost", 9);
|
|
|
|
return HScalculateRamUpgradeCost(startingRam, extraLevels, costMult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
coreUpgradeCost: function (startingCore: any, extraCores: any = 1, costMult: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.coreUpgradeCost", 9);
|
|
|
|
return HScalculateCoreUpgradeCost(startingCore, extraCores, costMult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
cacheUpgradeCost: function (startingCache: any, extraCache: any = 1): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.cacheUpgradeCost", 9);
|
2021-09-25 03:49:49 +02:00
|
|
|
return HScalculateCacheUpgradeCost(startingCache, extraCache);
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hashUpgradeCost: function (upgName: any, level: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.hashUpgradeCost", 9);
|
|
|
|
const upg = Player.hashManager.getUpgrade(upgName);
|
|
|
|
if (!upg) {
|
|
|
|
throw makeRuntimeErrorMsg(
|
|
|
|
"formulas.hacknetServers.calculateHashUpgradeCost",
|
|
|
|
`Invalid Hash Upgrade: ${upgName}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return upg.getCost(level);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
hacknetServerCost: function (n: any, mult: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.hacknetServerCost", 9);
|
|
|
|
return HScalculateServerCost(n, mult);
|
|
|
|
},
|
2021-09-25 03:49:49 +02:00
|
|
|
constants: function (): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
checkFormulasAccess("hacknetServers.constants", 9);
|
|
|
|
return Object.assign({}, HacknetServerConstants);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, // end formulas
|
2021-09-25 03:49:49 +02:00
|
|
|
flags: function (data: any): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
data = toNative(data);
|
|
|
|
// We always want the help flag.
|
2021-09-25 03:49:49 +02:00
|
|
|
const args: {
|
|
|
|
[key: string]: any;
|
|
|
|
} = {};
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
for (const d of data) {
|
2021-09-25 03:49:49 +02:00
|
|
|
let t: any = String;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (typeof d[1] === "number") {
|
|
|
|
t = Number;
|
|
|
|
} else if (typeof d[1] === "boolean") {
|
|
|
|
t = Boolean;
|
|
|
|
} else if (Array.isArray(d[1])) {
|
|
|
|
t = [String];
|
2021-05-12 21:10:36 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
const numDashes = d[0].length > 1 ? 2 : 1;
|
|
|
|
args["-".repeat(numDashes) + d[0]] = t;
|
|
|
|
}
|
|
|
|
const ret = libarg(args, { argv: workerScript.args });
|
|
|
|
for (const d of data) {
|
2021-09-09 05:47:34 +02:00
|
|
|
if (!ret.hasOwnProperty("--" + d[0]) || !ret.hasOwnProperty("-" + d[0])) ret[d[0]] = d[1];
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
for (const key of Object.keys(ret)) {
|
|
|
|
if (!key.startsWith("-")) continue;
|
|
|
|
const value = ret[key];
|
|
|
|
delete ret[key];
|
|
|
|
const numDashes = key.length === 2 ? 1 : 2;
|
|
|
|
ret[key.slice(numDashes)] = value;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
2021-10-05 04:25:21 +02:00
|
|
|
...extra,
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
|
|
|
|
2021-10-11 19:31:12 +02:00
|
|
|
function getFunctionNames(obj: NS, prefix: string): string[] {
|
2021-09-25 03:49:49 +02:00
|
|
|
const functionNames: string[] = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
if (typeof value == "function") {
|
2021-10-11 19:31:12 +02:00
|
|
|
functionNames.push(prefix + key);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (typeof value == "object") {
|
2021-10-11 19:31:12 +02:00
|
|
|
functionNames.push(...getFunctionNames(value, key + "."));
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-05-12 21:10:36 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return functionNames;
|
|
|
|
}
|
2021-05-12 21:10:36 +02:00
|
|
|
|
2021-10-11 19:31:12 +02:00
|
|
|
const possibleLogs = Object.fromEntries([...getFunctionNames(functions, "")].map((a) => [a, true]));
|
2021-05-12 21:10:36 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return functions;
|
2019-04-13 03:22:46 +02:00
|
|
|
} // End NetscriptFunction()
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2021-08-25 02:08:29 +02:00
|
|
|
export { NetscriptFunctions };
|