2019-04-11 10:37:40 +02:00
const sprintf = require ( "sprintf-js" ) . sprintf ;
const vsprintf = require ( "sprintf-js" ) . vsprintf ;
2021-04-21 14:20:26 +02:00
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" ;
import {
augmentationExists ,
2021-04-30 05:52:56 +02:00
installAugmentations ,
2019-04-11 10:37:40 +02:00
} 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" ;
import { Bladeburner } from "./Bladeburner" ;
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" ;
import {
calculateHackingChance ,
calculateHackingExpGain ,
calculatePercentMoneyHacked ,
calculateHackingTime ,
calculateGrowTime ,
2021-04-30 05:52:56 +02:00
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-03-08 04:12:02 +01:00
import {
AllGangs ,
GangMemberUpgrades ,
2021-04-30 05:52:56 +02:00
GangMemberTasks ,
2021-05-01 09:17:31 +02:00
Gang ,
2021-03-08 04:12:02 +01:00
} from "./Gang" ;
2019-04-11 10:37:40 +02:00
import { Factions , factionExists } from "./Faction/Factions" ;
import { joinFaction , purchaseAugmentation } from "./Faction/FactionHelpers" ;
import { FactionWorkType } from "./Faction/FactionWorkTypeEnum" ;
import {
netscriptCanGrow ,
netscriptCanHack ,
2021-04-30 05:52:56 +02:00
netscriptCanWeaken ,
2019-04-11 10:37:40 +02:00
} from "./Hacking/netscriptCanHack" ;
2019-03-30 04:01:34 +01:00
2019-04-11 10:37:40 +02:00
import {
getCostOfNextHacknetNode ,
getCostOfNextHacknetServer ,
hasHacknetServers ,
2019-05-05 06:03:40 +02:00
purchaseHacknet ,
purchaseLevelUpgrade ,
purchaseRamUpgrade ,
purchaseCoreUpgrade ,
purchaseCacheUpgrade ,
purchaseHashUpgrade ,
updateHashManagerCapacity ,
2019-04-11 10:37:40 +02:00
} from "./Hacknet/HacknetHelpers" ;
2021-03-31 06:45:21 +02:00
import {
calculateMoneyGainRate ,
calculateLevelUpgradeCost ,
calculateRamUpgradeCost ,
calculateCoreUpgradeCost ,
calculateNodeCost ,
} from "./Hacknet/formulas/HacknetNodes" ;
import {
calculateHashGainRate as HScalculateHashGainRate ,
calculateLevelUpgradeCost as HScalculateLevelUpgradeCost ,
calculateRamUpgradeCost as HScalculateRamUpgradeCost ,
calculateCoreUpgradeCost as HScalculateCoreUpgradeCost ,
calculateCacheUpgradeCost as HScalculateCacheUpgradeCost ,
calculateServerCost as HScalculateServerCost ,
} from "./Hacknet/formulas/HacknetServers" ;
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-03-31 06:45:21 +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 { inMission } from "./Missions" ;
import { Player } from "./Player" ;
import { Programs } from "./Programs/Programs" ;
import { Script } from "./Script/Script" ;
2021-04-21 14:20:26 +02:00
import {
findRunningScript ,
findRunningScriptByPid ,
} from "./Script/ScriptHelpers" ;
2019-04-11 10:37:40 +02:00
import { isScriptFilename } from "./Script/ScriptHelpersTS" ;
2019-05-05 06:03:40 +02:00
import {
AllServers ,
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 {
GetServerByHostname ,
getServer ,
getServerOnNetwork ,
numCycleForGrowth ,
2019-05-05 06:03:40 +02:00
processSingleServerGrowth ,
safetlyCreateUniqueServer ,
2019-04-11 10:37:40 +02:00
} from "./Server/ServerHelpers" ;
import {
getPurchaseServerCost ,
getPurchaseServerLimit ,
2021-04-30 05:52:56 +02:00
getPurchaseServerMaxRam ,
2019-04-11 10:37:40 +02:00
} from "./Server/ServerPurchases" ;
import { SpecialServerIps } from "./Server/SpecialServerIps" ;
2019-05-05 06:03:40 +02:00
import { SourceFileFlags } from "./SourceFile/SourceFileFlags" ;
2019-04-11 10:37:40 +02:00
import {
buyStock ,
sellStock ,
shortStock ,
sellShort ,
2019-05-05 06:03:40 +02:00
} from "./StockMarket/BuyingAndSelling" ;
2019-06-04 07:21:36 +02:00
import {
influenceStockThroughServerHack ,
influenceStockThroughServerGrow ,
} from "./StockMarket/PlayerInfluencing" ;
2019-05-05 06:03:40 +02:00
import {
StockMarket ,
SymbolToStockMap ,
2019-04-11 10:37:40 +02:00
placeOrder ,
2019-05-05 06:03:40 +02:00
cancelOrder ,
displayStockMarketContent ,
2019-04-11 10:37:40 +02:00
} from "./StockMarket/StockMarket" ;
2019-06-03 05:28:02 +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" ;
2019-04-11 10:37:40 +02:00
import {
2019-05-10 04:03:13 +02:00
getStockMarket4SDataCost ,
2021-04-30 05:52:56 +02:00
getStockMarket4STixApiCost ,
2019-04-11 10:37:40 +02:00
} from "./StockMarket/StockMarketCosts" ;
2019-05-12 04:20:20 +02:00
import { isValidFilePath } 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
2019-04-11 10:37:40 +02:00
import {
2019-05-05 06:03:40 +02:00
NetscriptPorts ,
runScriptFromScript ,
2021-05-01 09:17:31 +02:00
startWorkerScript ,
2019-04-11 10:37:40 +02:00
} from "./NetscriptWorker" ;
2019-05-16 08:05:36 +02:00
import { killWorkerScript } from "./Netscript/killWorkerScript" ;
import { workerScripts } from "./Netscript/WorkerScripts" ;
2019-04-11 10:37:40 +02:00
import {
makeRuntimeRejectMsg ,
netscriptDelay ,
2019-05-03 10:01:43 +02:00
resolveNetscriptRequestedThreads ,
2019-04-11 10:37:40 +02:00
} from "./NetscriptEvaluator" ;
2021-04-21 14:20:26 +02:00
import { Interpreter } from "./JSInterpreter" ;
2019-04-11 10:37:40 +02:00
import { NetscriptPort } from "./NetscriptPort" ;
import { SleeveTaskType } from "./PersonObjects/Sleeve/SleeveTaskTypesEnum" ;
2019-04-30 05:54:20 +02:00
import { findSleevePurchasableAugs } from "./PersonObjects/Sleeve/SleeveHelpers" ;
2021-03-09 02:31:34 +01:00
import { Exploit } from './Exploits/Exploit.ts' ;
2017-08-30 19:44:29 +02:00
2019-04-11 10:37:40 +02:00
import { numeralWrapper } from "./ui/numeralFormat" ;
import { post } from "./ui/postToTerminal" ;
import { setTimeoutRef } from "./utils/SetTimeoutRef" ;
import { is2DArray } from "./utils/helpers/is2DArray" ;
2021-05-01 09:17:31 +02:00
import {
formatNumber ,
convertTimeMsToTimeElapsedString ,
} from "../utils/StringHelperFunctions" ;
2019-02-27 03:26:29 +01:00
2019-07-05 03:01:09 +02:00
import { logBoxCreate } from "../utils/LogBox" ;
import { arrayToString } from "../utils/helpers/arrayToString" ;
2019-04-11 10:37:40 +02:00
import { isString } from "../utils/helpers/isString" ;
2017-08-21 18:59:06 +02:00
2019-04-11 10:37:40 +02:00
import { createElement } from "../utils/uiHelpers/createElement" ;
import { createPopup } from "../utils/uiHelpers/createPopup" ;
import { removeElementById } from "../utils/uiHelpers/removeElementById" ;
2019-02-09 03:46:30 +01:00
2019-05-05 06:03:40 +02:00
const possibleLogs = {
2018-05-06 05:59:55 +02:00
ALL : true ,
scan : true ,
hack : true ,
sleep : true ,
disableLog : true ,
enableLog : true ,
grow : true ,
weaken : true ,
nuke : true ,
brutessh : true ,
ftpcrack : true ,
relaysmtp : true ,
httpworm : true ,
sqlinject : true ,
2018-06-14 21:51:06 +02:00
run : true ,
exec : true ,
2018-05-06 05:59:55 +02:00
spawn : true ,
kill : true ,
killall : true ,
scp : true ,
getHackingLevel : true ,
getServerMoneyAvailable : true ,
getServerSecurityLevel : true ,
getServerBaseSecurityLevel : true ,
getServerMinSecurityLevel : true ,
getServerRequiredHackingLevel : true ,
getServerMaxMoney : true ,
getServerGrowth : true ,
getServerNumPortsRequired : true ,
getServerRam : true ,
2018-11-01 19:18:32 +01:00
2018-10-29 17:08:41 +01:00
// TIX API
2018-05-06 05:59:55 +02:00
buyStock : true ,
sellStock : true ,
2018-10-29 17:08:41 +01:00
shortStock : true ,
sellShort : true ,
2018-11-01 19:18:32 +01:00
purchase4SMarketData : true ,
purchase4SMarketDataTixApi : true ,
2018-10-29 17:08:41 +01:00
// Singularity Functions
2018-05-06 05:59:55 +02:00
purchaseServer : true ,
deleteServer : true ,
universityCourse : true ,
gymWorkout : true ,
travelToCity : true ,
purchaseTor : true ,
purchaseProgram : true ,
stopAction : true ,
upgradeHomeRam : true ,
workForCompany : true ,
applyToCompany : true ,
joinFaction : true ,
workForFaction : true ,
2018-09-11 20:56:23 +02:00
donateToFaction : true ,
2018-05-06 05:59:55 +02:00
createProgram : true ,
commitCrime : true ,
2018-10-29 17:08:41 +01:00
// Bladeburner API
2018-06-08 17:51:48 +02:00
startAction : true ,
upgradeSkill : true ,
setTeamSize : true ,
joinBladeburnerFaction : true ,
2018-10-29 17:08:41 +01:00
// Gang API
recruitMember : true ,
setMemberTask : true ,
purchaseEquipment : true ,
setTerritoryWarfare : true ,
2018-05-06 05:59:55 +02:00
}
2018-01-20 05:47:57 +01:00
2021-05-01 09:17:31 +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.
function toNative ( pseudoObj ) {
2021-04-29 02:07:26 +02:00
if ( pseudoObj == null ) return null ;
2021-04-21 14:20:26 +02:00
if ( ! pseudoObj . hasOwnProperty ( 'properties' ) ||
! pseudoObj . hasOwnProperty ( 'getter' ) ||
! pseudoObj . hasOwnProperty ( 'setter' ) ||
! pseudoObj . hasOwnProperty ( 'proto' ) ) {
return pseudoObj ; // it wasn't a pseudo object anyway.
}
let nativeObj ;
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 ) ) ;
}
}
} else { // Object.
nativeObj = { } ;
for ( var key in pseudoObj . properties ) {
const val = pseudoObj . properties [ key ] ;
nativeObj [ key ] = toNative ( val ) ;
}
}
return nativeObj ;
}
2017-06-28 11:47:42 +02:00
function NetscriptFunctions ( workerScript ) {
2019-03-30 00:14:32 +01:00
const updateDynamicRam = function ( fnName , ramCost ) {
2019-05-05 06:03:40 +02:00
if ( workerScript . dynamicLoadedFns [ fnName ] ) { return ; }
2018-06-06 18:02:21 +02:00
workerScript . dynamicLoadedFns [ fnName ] = true ;
2018-10-26 22:51:45 +02:00
2019-03-30 00:14:32 +01:00
let threads = workerScript . scriptRef . threads ;
2018-10-26 22:51:45 +02:00
if ( typeof threads !== 'number' ) {
console . warn ( ` WorkerScript detected NaN for threadcount for ${ workerScript . name } on ${ workerScript . serverIp } ` ) ;
threads = 1 ;
}
workerScript . dynamicRamUsage += ( ramCost * threads ) ;
2018-06-06 18:02:21 +02:00
if ( workerScript . dynamicRamUsage > 1.01 * workerScript . ramUsage ) {
throw makeRuntimeRejectMsg ( workerScript ,
2018-06-25 02:13:50 +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 " +
2018-07-10 07:11:18 +02:00
"calculation.<br><br>Please don't do that :(<br><br>" +
"Dynamic RAM Usage: " + workerScript . dynamicRamUsage + "<br>" +
"Static RAM Usage: " + workerScript . ramUsage ) ;
2018-06-06 18:02:21 +02:00
}
} ;
2018-09-23 02:25:48 +02:00
/ * *
* Gets the Server for a specific hostname / ip , throwing an error
* if the server doesn ' t exist .
2019-07-05 03:01:09 +02:00
* @ param { string } ip - Hostname or IP of the server
2019-03-31 04:53:57 +02:00
* @ param { string } callingFnName - Name of calling function . For logging purposes
2018-09-23 02:25:48 +02:00
* @ returns { Server } The specified Server
* /
2019-03-30 00:14:32 +01:00
const safeGetServer = function ( ip , callingFnName = "" ) {
2018-09-23 02:25:48 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( callingFnName , ` Invalid IP/hostname: ${ ip } ` ) ;
2018-09-23 02:25:48 +02:00
}
return server ;
}
2019-07-05 03:01:09 +02:00
/ * *
* 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 .
* /
const getRunningScript = function ( fn , ip , callingFnName , scriptArgs ) {
if ( typeof callingFnName !== "string" || callingFnName === "" ) {
callingFnName = "getRunningScript" ;
}
if ( ! Array . isArray ( scriptArgs ) ) {
throw makeRuntimeRejectMsg (
workerScript ,
` Invalid scriptArgs argument passed into getRunningScript() from ${ callingFnName } (). ` +
2021-04-30 05:52:56 +02:00
` This is probably a bug. Please report to game developer ` ,
2019-07-05 03:01:09 +02:00
) ;
}
if ( fn != null && typeof fn === "string" ) {
// Get Logs of another script
if ( ip == null ) { ip = workerScript . serverIp ; }
2019-07-12 04:37:17 +02:00
const server = safeGetServer ( ip , callingFnName ) ;
2019-07-05 03:01:09 +02:00
return findRunningScript ( fn , scriptArgs , server ) ;
}
// If no arguments are specified, return the current RunningScript
return workerScript . scriptRef ;
}
2021-04-29 02:07:26 +02:00
const getRunningScriptByPid = function ( pid , callingFnName ) {
2021-04-21 14:20:26 +02:00
if ( typeof callingFnName !== "string" || callingFnName === "" ) {
callingFnName = "getRunningScriptgetRunningScriptByPid" ;
}
2021-04-29 02:07:26 +02:00
for ( const name of Object . keys ( AllServers ) ) {
const server = AllServers [ name ] ;
const runningScript = findRunningScriptByPid ( pid , server ) ;
if ( runningScript ) return runningScript ;
}
return null ;
2021-04-21 14:20:26 +02:00
}
2019-07-05 03:01:09 +02:00
/ * *
* 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
* /
2019-07-05 03:11:05 +02:00
const getCannotFindRunningScriptErrorMessage = function ( fn , ip , scriptArgs ) {
2019-07-05 03:01:09 +02:00
if ( ! Array . isArray ( scriptArgs ) ) {
scriptArgs = [ ] ;
}
return ` Cannot find running script ${ fn } on server ${ ip } with args: ${ arrayToString ( scriptArgs ) } ` ;
}
2019-04-29 08:20:27 +02:00
/ * *
* Checks if the player has TIX API access . Throws an error if the player does not
* /
const checkTixApiAccess = function ( callingFn = "" ) {
2019-04-30 05:54:20 +02:00
if ( ! Player . hasWseAccount ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , ` You don't have WSE Access! Cannot use ${ callingFn } () ` ) ;
2019-04-30 05:54:20 +02:00
}
2019-04-29 08:20:27 +02:00
if ( ! Player . hasTixApiAccess ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , ` You don't have TIX API Access! Cannot use ${ callingFn } () ` ) ;
2019-04-29 08:20:27 +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
* /
const getStockFromSymbol = function ( symbol , callingFn = "" ) {
const stock = SymbolToStockMap [ symbol ] ;
if ( stock == null ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , ` Invalid stock symbol: ' ${ symbol } ' ` ) ;
2019-04-29 08:20:27 +02:00
}
return stock ;
}
2019-03-31 04:53:57 +02:00
/ * *
* 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
* /
const failOnHacknetServer = function ( server , callingFn = "" ) {
if ( server instanceof HacknetServer ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( callingFn , ` Does not work on Hacknet Servers ` ) ;
2019-03-31 04:53:57 +02:00
return true ;
} else {
return false ;
}
}
2018-09-23 02:25:48 +02:00
// Utility function to get Hacknet Node object
2021-03-16 11:01:15 +01:00
const getHacknetNode = function ( i , callingFn = "" ) {
2018-07-15 02:25:50 +02:00
if ( isNaN ( i ) ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , "Invalid index specified for Hacknet Node: " + i ) ;
2018-07-15 02:25:50 +02:00
}
if ( i < 0 || i >= Player . hacknetNodes . length ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , "Index specified for Hacknet Node is out-of-bounds: " + i ) ;
2018-07-15 02:25:50 +02:00
}
2019-03-30 00:14:32 +01:00
if ( hasHacknetServers ( ) ) {
const hserver = AllServers [ Player . hacknetNodes [ i ] ] ;
if ( hserver == null ) {
2021-03-16 11:01:15 +01:00
throw makeRuntimeErrorMsg ( callingFn , ` Could not get Hacknet Server for index ${ i } . This is probably a bug, please report to game dev ` ) ;
2019-03-30 00:14:32 +01:00
}
return hserver ;
} else {
return Player . hacknetNodes [ i ] ;
}
2018-06-20 18:43:05 +02:00
} ;
2021-03-11 09:02:05 +01:00
const makeRuntimeErrorMsg = function ( caller , msg ) {
const stack = ( new Error ( ) ) . stack . split ( '\n' ) . slice ( 1 ) ;
2021-03-10 07:27:14 +01:00
const scripts = workerScript . getServer ( ) . scripts ;
2021-03-11 09:02:05 +01:00
let userstack = [ ] ;
for ( const stackline of stack ) {
let filename ;
for ( const script of scripts ) {
if ( script . url && stackline . includes ( script . url ) ) {
filename = script . filename ;
}
for ( const dependency of script . dependencies ) {
if ( stackline . includes ( dependency . url ) ) {
filename = dependency . filename ;
}
}
}
if ( ! filename ) continue
2021-03-25 07:48:15 +01:00
function parseChromeStackline ( line ) {
const lineRe = /.*:(\d+):\d+.*/ ;
const funcRe = /.*at (.+) \(.*/ ;
const lineMatch = line . match ( lineRe ) ;
const funcMatch = line . match ( funcRe ) ;
if ( lineMatch && funcMatch ) {
let func = funcMatch [ 1 ] ;
return { line : lineMatch [ 1 ] , func : func } ;
}
return null ;
}
2021-04-30 05:52:56 +02:00
let call = { line : "-1" , func : "unknown" } ;
2021-03-25 07:48:15 +01:00
let chromeCall = parseChromeStackline ( stackline ) ;
if ( chromeCall ) {
call = chromeCall ;
}
function parseFirefoxStackline ( line ) {
const lineRe = /.*:(\d+):\d+$/ ;
const lineMatch = line . match ( lineRe ) ;
2021-03-10 07:27:14 +01:00
2021-03-25 07:48:15 +01:00
const lio = line . lastIndexOf ( "@" ) ;
2021-03-11 09:02:05 +01:00
2021-03-25 07:48:15 +01:00
if ( lineMatch && lio !== - 1 ) {
return { line : lineMatch [ 1 ] , func : line . slice ( 0 , lio ) } ;
}
return null ;
}
let firefoxCall = parseFirefoxStackline ( stackline ) ;
if ( firefoxCall ) {
call = firefoxCall ;
}
2021-03-11 09:02:05 +01:00
2021-03-25 07:48:15 +01:00
userstack . push ( ` ${ filename } :L ${ call . line } @ ${ call . func } ` ) ;
2021-03-11 09:02:05 +01:00
}
workerScript . log ( caller , msg ) ;
const rejectMsg = ` ${ caller } : ${ msg } <br><br>Stack:<br> ${ userstack . join ( '<br>' ) } `
return makeRuntimeRejectMsg ( workerScript , rejectMsg ) ;
}
2021-03-31 06:45:21 +02:00
const checkFormulasAccess = function ( func , n ) {
2021-04-29 19:39:03 +02:00
if ( ( SourceFileFlags [ 5 ] < 1 && Player . bitNodeN !== 5 ) || ( SourceFileFlags [ n ] < 1 && Player . bitNodeN !== n ) ) {
2021-03-31 06:45:21 +02:00
let extra = '' ;
if ( n !== 5 ) {
extra = ` and Source-File ${ n } -1 ` ;
}
throw makeRuntimeErrorMsg ( ` formulas. ${ func } ` , ` Requires Source-File 5-1 ${ extra } to run. ` ) ;
}
}
2021-03-11 09:02:05 +01:00
const checkSingularityAccess = function ( func , n ) {
if ( Player . bitNodeN !== 4 ) {
if ( SourceFileFlags [ 4 ] < n ) {
throw makeRuntimeErrorMsg ( func , ` This singularity function requires Source-File 4- ${ n } to run. ` ) ;
2021-03-10 07:27:14 +01:00
}
}
2021-03-11 09:02:05 +01:00
}
2021-03-13 21:10:55 +01:00
const checkBladeburnerAccess = function ( func , skipjoined = false ) {
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 ) {
const bladeburnerAccess = Player . bladeburner instanceof Bladeburner ;
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
}
}
const checkBladeburnerCity = function ( func , city ) {
if ( ! Player . bladeburner . cities . hasOwnProperty ( city ) ) {
throw makeRuntimeErrorMsg ( ` bladeburner. ${ func } ` , ` Invalid city: ${ city } ` ) ;
}
}
const checkSleeveAPIAccess = function ( func ) {
if ( Player . bitNodeN !== 10 && ! SourceFileFlags [ 10 ] ) {
throw makeRuntimeErrorMsg ( ` sleeve. ${ func } ` , "You do not currently have access to the Sleeve API. This is either because you are not in BitNode-10 or because you do not have Source-File 10" ) ;
}
}
const checkSleeveNumber = function ( func , sleeveNumber ) {
if ( sleeveNumber >= Player . sleeves . length || sleeveNumber < 0 ) {
const msg = ` Invalid sleeve number: ${ sleeveNumber } ` ;
workerScript . log ( func , msg ) ;
throw makeRuntimeErrorMsg ( ` sleeve. ${ func } ` , msg ) ;
}
}
const getCodingContract = function ( func , ip , fn ) {
const server = safeGetServer ( ip , func ) ;
const contract = server . getContract ( fn ) ;
if ( contract == null ) {
throw makeRuntimeErrorMsg ( ` codingcontract. ${ func } ` , ` Cannot find contract ' ${ fn } ' on server ' ${ ip } ' ` )
}
return contract ;
}
const checkGangApiAccess = function ( func ) {
const hasAccess = Player . gang instanceof Gang ;
if ( ! hasAccess ) {
throw makeRuntimeErrorMsg ( ` gang. ${ func } ` , ` You do not currently have a Gang ` ) ;
}
}
const getGangMember = function ( func , name ) {
for ( const member of Player . gang . members )
if ( member . name === name )
return member ;
throw makeRuntimeErrorMsg ( ` gang. ${ func } ` , ` Invalid gang member: ' ${ name } ' ` )
}
const getGangTask = function ( func , name ) {
2021-03-22 23:42:39 +01:00
const task = GangMemberTasks [ name ] ;
2021-03-11 09:02:05 +01:00
if ( ! task ) {
2021-03-22 23:42:39 +01:00
throw makeRuntimeErrorMsg ( ` gang. ${ func } ` , ` Invalid task: ' ${ name } ' ` ) ;
2021-03-11 09:02:05 +01:00
}
return task ;
}
const getBladeburnerActionObject = function ( func , type , name ) {
const actionId = Player . bladeburner . getActionIdFromTypeAndName ( type , name )
if ( ! actionId ) {
throw makeRuntimeErrorMsg ( ` bladeburner. ${ func } ` , ` Invalid action type=' ${ type } ', name=' ${ name } ' ` ) ;
}
const actionObj = Player . bladeburner . getActionObject ( actionId ) ;
if ( ! actionObj ) {
throw makeRuntimeErrorMsg ( ` bladeburner. ${ func } ` , ` Invalid action type=' ${ type } ', name=' ${ name } ' ` ) ;
}
return actionObj ;
}
const getCompany = function ( func , name ) {
const company = Companies [ name ] ;
if ( company == null || ! ( company instanceof Company ) ) {
throw makeRuntimeErrorMsg ( func , ` Invalid company name: ' ${ name } ' ` )
}
return company ;
}
const getFaction = function ( func , name ) {
if ( ! factionExists ( name ) ) {
throw makeRuntimeErrorMsg ( func , ` Invalid faction name: ' ${ name } ` )
}
return Factions [ name ] ;
}
const getAugmentation = function ( func , name ) {
if ( ! augmentationExists ( name ) ) {
throw makeRuntimeErrorMsg ( func , ` Invalid augmentation: ' ${ name } ' ` ) ;
}
2021-03-10 07:27:14 +01:00
2021-03-11 09:02:05 +01:00
return Augmentations [ name ] ;
2021-03-10 07:27:14 +01:00
}
2021-03-12 02:37:58 +01:00
const runAfterReset = function ( cbScript = null ) {
//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-24 02:30:15 +01:00
const hack = function ( ip , manual , { threads : requestedThreads , stock } = { } ) {
if ( ip === undefined ) {
throw makeRuntimeErrorMsg ( "hack" , "Takes 1 argument." ) ;
}
const threads = resolveNetscriptRequestedThreads ( workerScript , "hack" , requestedThreads ) ;
const server = getServer ( ip ) ;
if ( server == null ) {
throw makeRuntimeErrorMsg ( "hack" , ` Invalid IP/hostname: ${ ip } . ` ) ;
}
// Calculate the hacking time
2021-03-31 06:45:21 +02:00
var hackingTime = calculateHackingTime ( server , Player ) ; // This is in seconds
2021-03-24 02:30:15 +01:00
// No root access or skill level too low
const canHack = netscriptCanHack ( server , Player ) ;
if ( ! canHack . res ) {
throw makeRuntimeErrorMsg ( 'hack' , canHack . msg ) ;
}
2021-05-03 06:45:21 +02:00
workerScript . log ( "hack" , ` Executing ${ ip } in ${ convertTimeMsToTimeElapsedString ( hackingTime * 1000 , true ) } (t= ${ numeralWrapper . formatThreads ( threads ) } ) ` ) ;
2021-03-24 02:30:15 +01:00
return netscriptDelay ( hackingTime * 1000 , workerScript ) . then ( function ( ) {
if ( workerScript . env . stopFlag ) { return Promise . reject ( workerScript ) ; }
2021-03-31 06:45:21 +02:00
var hackChance = calculateHackingChance ( server , Player ) ;
2021-03-24 02:30:15 +01:00
var rand = Math . random ( ) ;
2021-03-31 06:45:21 +02:00
var expGainedOnSuccess = calculateHackingExpGain ( server , Player ) * threads ;
2021-03-24 02:30:15 +01:00
var expGainedOnFailure = ( expGainedOnSuccess / 4 ) ;
if ( rand < hackChance ) { // Success!
2021-03-31 06:45:21 +02:00
const percentHacked = calculatePercentMoneyHacked ( server , Player ) ;
2021-03-24 02:30:15 +01:00
let maxThreadNeeded = Math . ceil ( 1 / percentHacked * ( server . moneyAvailable / server . moneyMax ) ) ;
if ( isNaN ( maxThreadNeeded ) ) {
// Server has a 'max money' of 0 (probably). We'll set this to an arbitrarily large value
maxThreadNeeded = 1e6 ;
}
let moneyDrained = Math . floor ( server . moneyAvailable * percentHacked ) * threads ;
// Over-the-top safety checks
if ( moneyDrained <= 0 ) {
moneyDrained = 0 ;
expGainedOnSuccess = expGainedOnFailure ;
}
if ( moneyDrained > server . moneyAvailable ) { moneyDrained = server . moneyAvailable ; }
server . moneyAvailable -= moneyDrained ;
if ( server . moneyAvailable < 0 ) { server . moneyAvailable = 0 ; }
const moneyGained = moneyDrained * BitNodeMultipliers . ScriptHackMoneyGain ;
Player . gainMoney ( moneyGained ) ;
workerScript . scriptRef . onlineMoneyMade += moneyGained ;
Player . scriptProdSinceLastAug += moneyGained ;
Player . recordMoneySource ( moneyGained , "hacking" ) ;
workerScript . scriptRef . recordHack ( server . ip , moneyGained , threads ) ;
Player . gainHackingExp ( expGainedOnSuccess ) ;
workerScript . scriptRef . onlineExpGained += expGainedOnSuccess ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "hack" , ` Successfully hacked ' ${ server . hostname } ' for ${ numeralWrapper . formatMoney ( moneyGained ) } and ${ numeralWrapper . formatExp ( expGainedOnSuccess ) } exp (t= ${ numeralWrapper . formatThreads ( threads ) } ) ` ) ;
2021-03-24 02:30:15 +01:00
server . fortify ( CONSTANTS . ServerFortifyAmount * Math . min ( threads , maxThreadNeeded ) ) ;
if ( stock ) {
influenceStockThroughServerHack ( server , moneyGained ) ;
}
if ( manual ) {
2021-04-29 02:07:26 +02:00
server . backdoorInstalled = true ;
2021-03-24 02:30:15 +01:00
}
return Promise . resolve ( moneyGained ) ;
} else {
// Player only gains 25% exp for failure?
Player . gainHackingExp ( expGainedOnFailure ) ;
workerScript . scriptRef . onlineExpGained += expGainedOnFailure ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "hack" , ` Failed to hack ' ${ server . hostname } '. Gained ${ numeralWrapper . formatExp ( expGainedOnFailure ) } exp (t= ${ numeralWrapper . formatThreads ( threads ) } ) ` ) ;
2021-03-24 02:30:15 +01:00
return Promise . resolve ( 0 ) ;
}
} ) ;
}
2021-04-18 17:18:56 +02:00
const argsToString = function ( args ) {
let out = '' ;
for ( let arg of args ) {
2021-04-21 14:20:26 +02:00
arg = toNative ( arg ) ;
2021-04-18 17:18:56 +02:00
if ( typeof arg === 'object' ) {
out += JSON . stringify ( arg ) ;
continue
}
out += ` ${ arg } ` ;
}
return out ;
}
2017-06-28 11:47:42 +02:00
return {
2018-07-15 02:25:50 +02:00
hacknet : {
numNodes : function ( ) {
return Player . hacknetNodes . length ;
} ,
2021-03-07 22:03:21 +01:00
maxNumNodes : function ( ) {
2021-04-18 17:18:56 +02:00
if ( hasHacknetServers ( ) ) {
return HacknetServerConstants . MaxServers ;
}
return Infinity ;
2021-03-07 22:03:21 +01:00
} ,
2018-07-15 02:25:50 +02:00
purchaseNode : function ( ) {
return purchaseHacknet ( ) ;
} ,
getPurchaseNodeCost : function ( ) {
2019-03-30 00:14:32 +01:00
if ( hasHacknetServers ( ) ) {
return getCostOfNextHacknetServer ( ) ;
} else {
return getCostOfNextHacknetNode ( ) ;
}
2018-07-15 02:25:50 +02:00
} ,
getNodeStats : function ( i ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "getNodeStats" ) ;
2019-03-30 00:14:32 +01:00
const hasUpgraded = hasHacknetServers ( ) ;
const res = {
2019-07-05 09:26:22 +02:00
name : hasUpgraded ? node . hostname : node . name ,
2018-07-15 02:25:50 +02:00
level : node . level ,
2019-03-30 00:14:32 +01:00
ram : hasUpgraded ? node . maxRam : node . ram ,
2018-07-15 02:25:50 +02:00
cores : node . cores ,
2019-03-30 00:14:32 +01:00
production : hasUpgraded ? node . hashRate : node . moneyGainRatePerSecond ,
2018-07-15 02:25:50 +02:00
timeOnline : node . onlineTimeSeconds ,
2019-03-30 00:14:32 +01:00
totalProduction : hasUpgraded ? node . totalHashesGenerated : node . totalMoneyGenerated ,
2018-07-15 02:25:50 +02:00
} ;
2019-03-30 00:14:32 +01:00
if ( hasUpgraded ) {
res . cache = node . cache ;
2019-07-05 09:26:22 +02:00
res . hashCapacity = node . hashCapacity ;
2019-03-30 00:14:32 +01:00
}
return res ;
2018-07-15 02:25:50 +02:00
} ,
upgradeLevel : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeLevel" ) ;
2019-05-05 06:03:40 +02:00
return purchaseLevelUpgrade ( node , n ) ;
2018-07-15 02:25:50 +02:00
} ,
upgradeRam : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeRam" ) ;
2019-05-05 06:03:40 +02:00
return purchaseRamUpgrade ( node , n ) ;
2018-07-15 02:25:50 +02:00
} ,
upgradeCore : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeCore" ) ;
2019-05-05 06:03:40 +02:00
return purchaseCoreUpgrade ( node , n ) ;
2018-07-15 02:25:50 +02:00
} ,
2019-03-30 00:14:32 +01:00
upgradeCache : function ( i , n ) {
if ( ! hasHacknetServers ( ) ) { return false ; }
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeCache" ) ;
2019-05-05 06:03:40 +02:00
const res = purchaseCacheUpgrade ( node , n ) ;
2019-03-30 04:01:34 +01:00
if ( res ) {
2019-05-05 06:03:40 +02:00
updateHashManagerCapacity ( ) ;
2019-03-30 04:01:34 +01:00
}
return res ;
2019-03-30 00:14:32 +01:00
} ,
2018-07-15 02:25:50 +02:00
getLevelUpgradeCost : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeLevel" ) ;
2019-05-05 06:03:40 +02:00
return node . calculateLevelUpgradeCost ( n , Player . hacknet _node _level _cost _mult ) ;
2018-07-15 02:25:50 +02:00
} ,
getRamUpgradeCost : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeRam" ) ;
2019-05-05 06:03:40 +02:00
return node . calculateRamUpgradeCost ( n , Player . hacknet _node _ram _cost _mult ) ;
2018-07-15 02:25:50 +02:00
} ,
getCoreUpgradeCost : function ( i , n ) {
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeCore" ) ;
2019-05-05 06:03:40 +02:00
return node . calculateCoreUpgradeCost ( n , Player . hacknet _node _core _cost _mult ) ;
2019-03-30 00:14:32 +01:00
} ,
getCacheUpgradeCost : function ( i , n ) {
if ( ! hasHacknetServers ( ) ) { return Infinity ; }
2021-03-16 11:01:15 +01:00
const node = getHacknetNode ( i , "upgradeCache" ) ;
2019-03-30 00:14:32 +01:00
return node . calculateCacheUpgradeCost ( n ) ;
2019-04-05 11:08:41 +02:00
} ,
numHashes : function ( ) {
if ( ! hasHacknetServers ( ) ) { return 0 ; }
return Player . hashManager . hashes ;
} ,
2021-04-06 09:50:09 +02:00
hashCapacity : function ( ) {
if ( ! hasHacknetServers ( ) ) { return 0 ; }
return Player . hashManager . capacity ;
} ,
2019-04-05 11:08:41 +02:00
hashCost : function ( upgName ) {
if ( ! hasHacknetServers ( ) ) { return Infinity ; }
return Player . hashManager . getUpgradeCost ( upgName ) ;
} ,
spendHashes : function ( upgName , upgTarget ) {
if ( ! hasHacknetServers ( ) ) { return false ; }
return purchaseHashUpgrade ( upgName , upgTarget ) ;
2021-03-31 06:45:21 +02:00
} ,
getHashUpgradeLevel : function ( upgName ) {
const level = Player . hashManager . upgrades [ upgName ] ;
if ( level === undefined ) {
throw makeRuntimeErrorMsg ( "hacknet.hashUpgradeLevel" , ` Invalid Hash Upgrade: ${ upgName } ` ) ;
}
return level ;
} ,
getStudyMult : function ( ) {
if ( ! hasHacknetServers ( ) ) { return false ; }
return Player . hashManager . getStudyMult ( ) ;
} ,
getTrainingMult : function ( ) {
if ( ! hasHacknetServers ( ) ) { return false ; }
return Player . hashManager . getTrainingMult ( ) ;
} ,
2018-07-09 01:53:24 +02:00
} ,
2017-10-12 22:10:34 +02:00
sprintf : sprintf ,
vsprintf : vsprintf ,
2019-05-05 06:03:40 +02:00
scan : function ( ip = workerScript . serverIp , hostnames = true ) {
updateDynamicRam ( "scan" , getRamCost ( "scan" ) ) ;
2017-06-28 11:47:42 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( 'scan' , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
var out = [ ] ;
for ( var i = 0 ; i < server . serversOnNetwork . length ; i ++ ) {
2017-09-19 20:38:03 +02:00
var entry ;
if ( hostnames ) {
2019-03-05 02:40:28 +01:00
entry = getServerOnNetwork ( server , i ) . hostname ;
2017-09-19 20:38:03 +02:00
} else {
2019-03-05 02:40:28 +01:00
entry = getServerOnNetwork ( server , i ) . ip ;
2017-09-19 20:38:03 +02:00
}
2017-06-28 11:47:42 +02:00
if ( entry == null ) {
continue ;
2017-06-11 08:52:52 +02:00
}
2017-06-28 11:47:42 +02:00
out . push ( entry ) ;
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "scan" , ` returned ${ server . serversOnNetwork . length } connections for ${ server . hostname } ` ) ;
2017-06-28 11:47:42 +02:00
return out ;
} ,
2019-06-04 07:21:36 +02:00
hack : function ( ip , { threads : requestedThreads , stock } = { } ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "hack" , getRamCost ( "hack" ) ) ;
2021-03-24 02:30:15 +01:00
return hack ( ip , false , { threads : requestedThreads , stock : stock } ) ;
2017-06-28 11:47:42 +02:00
} ,
2019-01-03 09:39:32 +01:00
hackAnalyzeThreads : function ( ip , hackAmount ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "hackAnalyzeThreads" , getRamCost ( "hackAnalyzeThreads" ) ) ;
2019-01-03 09:39:32 +01:00
// Check argument validity
const server = safeGetServer ( ip , 'hackAnalyzeThreads' ) ;
if ( isNaN ( hackAmount ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( workerScript , ` Invalid growth argument passed into hackAnalyzeThreads: ${ hackAmount } . Must be numeric. ` ) ;
2019-01-03 09:39:32 +01:00
}
if ( hackAmount < 0 || hackAmount > server . moneyAvailable ) {
return - 1 ;
}
2021-03-31 06:45:21 +02:00
const percentHacked = calculatePercentMoneyHacked ( server , Player ) ;
2019-01-03 09:39:32 +01:00
return hackAmount / Math . floor ( server . moneyAvailable * percentHacked ) ;
} ,
hackAnalyzePercent : function ( ip ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "hackAnalyzePercent" , getRamCost ( "hackAnalyzePercent" ) ) ;
2019-01-03 09:39:32 +01:00
const server = safeGetServer ( ip , 'hackAnalyzePercent' ) ;
2021-03-31 06:45:21 +02:00
return calculatePercentMoneyHacked ( server , Player ) * 100 ;
2019-01-03 09:39:32 +01:00
} ,
hackChance : function ( ip ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "hackChance" , getRamCost ( "hackChance" ) ) ;
2019-01-03 09:39:32 +01:00
const server = safeGetServer ( ip , 'hackChance' ) ;
2021-03-31 06:45:21 +02:00
return calculateHackingChance ( server , Player ) ;
2019-01-03 09:39:32 +01:00
} ,
2018-02-24 23:55:06 +01:00
sleep : function ( time ) {
2017-06-28 11:47:42 +02:00
if ( time === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sleep" , "Takes 1 argument." ) ;
2017-07-22 00:54:55 +02:00
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "sleep" , ` Sleeping for ${ time } milliseconds ` ) ;
2017-09-15 16:06:59 +02:00
return netscriptDelay ( time , workerScript ) . then ( function ( ) {
2017-06-28 11:47:42 +02:00
return Promise . resolve ( true ) ;
} ) ;
} ,
2019-06-04 07:21:36 +02:00
grow : function ( ip , { threads : requestedThreads , stock } = { } ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "grow" , getRamCost ( "grow" ) ) ;
2019-05-11 11:20:09 +02:00
const threads = resolveNetscriptRequestedThreads ( workerScript , "grow" , requestedThreads ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "grow" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "grow" , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2019-04-13 03:22:46 +02:00
// No root access or skill level too low
2019-03-30 00:14:32 +01:00
const canHack = netscriptCanGrow ( server ) ;
if ( ! canHack . res ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "grow" , canHack . msg ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2021-03-31 06:45:21 +02:00
var growTime = calculateGrowTime ( server , Player ) ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "grow" , ` Executing on ' ${ server . hostname } ' in ${ convertTimeMsToTimeElapsedString ( growTime * 1000 , true ) } (t= ${ numeralWrapper . formatThreads ( threads ) } ). ` ) ;
2018-08-30 19:00:38 +02:00
return netscriptDelay ( growTime * 1000 , workerScript ) . then ( function ( ) {
2017-06-28 11:47:42 +02:00
if ( workerScript . env . stopFlag ) { return Promise . reject ( workerScript ) ; }
2018-09-28 04:09:02 +02:00
const moneyBefore = server . moneyAvailable <= 0 ? 1 : server . moneyAvailable ;
2019-04-13 03:22:46 +02:00
server . moneyAvailable += ( 1 * threads ) ; // It can be grown even if it has no money
2021-03-31 06:45:21 +02:00
var growthPercentage = processSingleServerGrowth ( server , threads , Player ) ;
2018-06-09 08:58:33 +02:00
const moneyAfter = server . moneyAvailable ;
2017-06-28 11:47:42 +02:00
workerScript . scriptRef . recordGrow ( server . ip , threads ) ;
2021-03-31 06:45:21 +02:00
var expGain = calculateHackingExpGain ( server , Player ) * threads ;
2017-06-28 11:47:42 +02:00
if ( growthPercentage == 1 ) {
expGain = 0 ;
2017-06-11 22:28:20 +02:00
}
2021-03-10 07:27:14 +01:00
const logGrowPercent = ( moneyAfter / moneyBefore ) * 100 - 100 ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "grow" , ` Available money on ' ${ server . hostname } ' grown by ${ formatNumber ( logGrowPercent , 6 ) } %. Gained ${ numeralWrapper . formatExp ( expGain ) } hacking exp (t= ${ numeralWrapper . formatThreads ( threads ) } ). ` ) ;
2017-06-28 11:47:42 +02:00
workerScript . scriptRef . onlineExpGained += expGain ;
2017-07-25 03:06:40 +02:00
Player . gainHackingExp ( expGain ) ;
2019-06-04 07:21:36 +02:00
if ( stock ) {
influenceStockThroughServerGrow ( server , moneyAfter - moneyBefore ) ;
}
2018-06-14 21:51:06 +02:00
return Promise . resolve ( moneyAfter / moneyBefore ) ;
2017-07-25 03:06:40 +02:00
} ) ;
2017-06-28 11:47:42 +02:00
} ,
2018-11-20 06:54:03 +01:00
growthAnalyze : function ( ip , growth ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "growthAnalyze" , getRamCost ( "growthAnalyze" ) ) ;
2018-11-20 06:54:03 +01:00
// Check argument validity
const server = safeGetServer ( ip , 'growthAnalyze' ) ;
2021-04-20 03:26:51 +02:00
if ( typeof growth !== "number" || isNaN ( growth ) || growth < 1 || ! isFinite ( growth ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "growthAnalyze" , ` Invalid argument: growth must be numeric and >= 1, is ${ growth } . ` ) ;
2018-11-20 06:54:03 +01:00
}
2019-03-05 02:40:28 +01:00
return numCycleForGrowth ( server , Number ( growth ) , Player ) ;
2018-11-20 06:54:03 +01:00
} ,
2019-05-03 10:01:43 +02:00
weaken : function ( ip , { threads : requestedThreads } = { } ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "weaken" , getRamCost ( "weaken" ) ) ;
2019-05-03 10:01:43 +02:00
var threads = resolveNetscriptRequestedThreads ( workerScript , "weaken" , requestedThreads )
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "weaken" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "weaken" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2019-04-13 03:22:46 +02:00
// No root access or skill level too low
2019-03-30 00:14:32 +01:00
const canHack = netscriptCanWeaken ( server ) ;
if ( ! canHack . res ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "weaken" , canHack . msg ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2021-03-31 06:45:21 +02:00
var weakenTime = calculateWeakenTime ( server , Player ) ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "weaken" , ` Executing on ' ${ server . hostname } ' in ${ convertTimeMsToTimeElapsedString ( weakenTime * 1000 , true ) } (t= ${ numeralWrapper . formatThreads ( threads ) } ) ` ) ;
2018-08-30 19:00:38 +02:00
return netscriptDelay ( weakenTime * 1000 , workerScript ) . then ( function ( ) {
2017-06-28 11:47:42 +02:00
if ( workerScript . env . stopFlag ) { return Promise . reject ( workerScript ) ; }
server . weaken ( CONSTANTS . ServerWeakenAmount * threads ) ;
workerScript . scriptRef . recordWeaken ( server . ip , threads ) ;
2021-03-31 06:45:21 +02:00
var expGain = calculateHackingExpGain ( server , Player ) * threads ;
2021-05-03 06:45:21 +02:00
workerScript . log ( "weaken" , ` ' ${ server . hostname } ' security level weakened to ${ server . hackDifficulty } . Gained ${ numeralWrapper . formatExp ( expGain ) } hacking exp (t= ${ numeralWrapper . formatThreads ( threads ) } ) ` ) ;
2017-06-28 11:47:42 +02:00
workerScript . scriptRef . onlineExpGained += expGain ;
2017-07-25 03:06:40 +02:00
Player . gainHackingExp ( expGain ) ;
2017-06-28 11:47:42 +02:00
return Promise . resolve ( CONSTANTS . ServerWeakenAmount * threads ) ;
2017-07-25 03:06:40 +02:00
} ) ;
2017-06-28 11:47:42 +02:00
} ,
2021-04-18 17:18:56 +02:00
print : function ( ) {
if ( arguments . length === 0 ) {
throw makeRuntimeErrorMsg ( "print" , "Takes at least 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
2021-04-18 17:18:56 +02:00
workerScript . print ( argsToString ( arguments ) ) ;
2017-06-28 11:47:42 +02:00
} ,
2021-04-18 17:18:56 +02:00
tprint : function ( ) {
if ( arguments . length === 0 ) {
throw makeRuntimeErrorMsg ( "tprint" , "Takes at least 1 argument." ) ;
2017-09-01 16:12:40 +02:00
}
2021-04-18 17:18:56 +02:00
post ( ` ${ workerScript . scriptRef . filename } : ${ argsToString ( arguments ) } ` ) ;
2017-09-01 16:12:40 +02:00
} ,
2019-05-05 06:03:40 +02:00
clearLog : function ( ) {
2017-07-22 00:54:55 +02:00
workerScript . scriptRef . clearLog ( ) ;
} ,
2019-05-05 06:03:40 +02:00
disableLog : function ( fn ) {
if ( possibleLogs [ fn ] === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "disableLog" , ` Invalid argument: ${ fn } . ` ) ;
2018-05-06 05:59:55 +02:00
}
2018-02-24 23:55:06 +01:00
workerScript . disableLogs [ fn ] = true ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "disableLog" , ` Disabled logging for ${ fn } ` ) ;
2018-02-24 23:55:06 +01:00
} ,
2019-05-05 06:03:40 +02:00
enableLog : function ( fn ) {
if ( possibleLogs [ fn ] === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "enableLog" , ` Invalid argument: ${ fn } . ` ) ;
2018-05-06 05:59:55 +02:00
}
2018-02-24 23:55:06 +01:00
delete workerScript . disableLogs [ fn ] ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "enableLog" , ` Enabled logging for ${ fn } ` ) ;
2018-02-24 23:55:06 +01:00
} ,
2018-09-11 20:56:23 +02:00
isLogEnabled : function ( fn ) {
if ( possibleLogs [ fn ] === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "isLogEnabled" , ` Invalid argument: ${ fn } . ` ) ;
2018-09-11 20:56:23 +02:00
}
return workerScript . disableLogs [ fn ] ? false : true ;
} ,
2019-07-05 03:01:09 +02:00
getScriptLogs : function ( fn , ip , ... scriptArgs ) {
const runningScriptObj = getRunningScript ( fn , ip , "getScriptLogs" , scriptArgs ) ;
if ( runningScriptObj == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "getScriptLogs" , getCannotFindRunningScriptErrorMessage ( fn , ip , scriptArgs ) ) ;
2019-07-05 03:01:09 +02:00
return "" ;
}
2018-11-05 00:57:31 +01:00
2019-07-05 03:01:09 +02:00
return runningScriptObj . logs . slice ( ) ;
} ,
2021-04-21 14:20:26 +02:00
tail : function ( fn , ip = workerScript . serverIp , ... scriptArgs ) {
let runningScriptObj ;
2021-04-29 02:07:26 +02:00
if ( arguments . length === 0 ) {
runningScriptObj = workerScript . scriptRef ;
} else if ( typeof fn === 'number' ) {
runningScriptObj = getRunningScriptByPid ( fn , 'tail' ) ;
2021-04-21 14:20:26 +02:00
} else {
runningScriptObj = getRunningScript ( fn , ip , "tail" , scriptArgs ) ;
}
2019-07-05 03:01:09 +02:00
if ( runningScriptObj == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "tail" , getCannotFindRunningScriptErrorMessage ( fn , ip , scriptArgs ) ) ;
2019-07-05 03:01:09 +02:00
return ;
2018-11-01 19:18:32 +01:00
}
2019-07-05 03:01:09 +02:00
logBoxCreate ( runningScriptObj ) ;
2018-09-11 20:56:23 +02:00
} ,
2019-05-05 06:03:40 +02:00
nuke : function ( ip ) {
updateDynamicRam ( "nuke" , getRamCost ( "nuke" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "nuke" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "nuke" , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . NukeProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "nuke" , "You do not have the NUKE.exe virus!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( server . openPortCount < server . numOpenPortsRequired ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "nuke" , "Not enough ports opened to use NUKE.exe virus." ) ;
2017-06-28 11:47:42 +02:00
}
if ( server . hasAdminRights ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "nuke" , ` Already have root access to ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
} else {
server . hasAdminRights = true ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "nuke" , ` Executed NUKE.exe virus on ' ${ server . hostname } ' to gain root access. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2019-05-05 06:03:40 +02:00
brutessh : function ( ip ) {
updateDynamicRam ( "brutessh" , getRamCost ( "brutessh" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "brutessh" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "brutessh" , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . BruteSSHProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "brutessh" , "You do not have the BruteSSH.exe program!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( ! server . sshPortOpen ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "brutessh" , ` Executed BruteSSH.exe on ' ${ server . hostname } ' to open SSH port (22). ` ) ;
2017-07-25 03:06:40 +02:00
server . sshPortOpen = true ;
2017-06-28 11:47:42 +02:00
++ server . openPortCount ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "brutessh" , ` SSH Port (22) already opened on ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2019-05-05 06:03:40 +02:00
ftpcrack : function ( ip ) {
updateDynamicRam ( "ftpcrack" , getRamCost ( "ftpcrack" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ftpcrack" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ftpcrack" , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . FTPCrackProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ftpcrack" , "You do not have the FTPCrack.exe program!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( ! server . ftpPortOpen ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "ftpcrack" , ` Executed FTPCrack.exe on ' ${ server . hostname } ' to open FTP port (21). ` ) ;
2017-07-25 03:06:40 +02:00
server . ftpPortOpen = true ;
2017-06-28 11:47:42 +02:00
++ server . openPortCount ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "ftpcrack" , ` FTP Port (21) already opened on ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2019-05-05 06:03:40 +02:00
relaysmtp : function ( ip ) {
updateDynamicRam ( "relaysmtp" , getRamCost ( "relaysmtp" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "relaysmtp" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "relaysmtp" , ` Invalid IP/hostname: ${ ip } . ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . RelaySMTPProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "relaysmtp" , "You do not have the relaySMTP.exe program!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( ! server . smtpPortOpen ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "relaysmtp" , ` Executed relaySMTP.exe on ' ${ server . hostname } ' to open SMTP port (25). ` ) ;
2017-07-25 03:06:40 +02:00
server . smtpPortOpen = true ;
2017-06-28 11:47:42 +02:00
++ server . openPortCount ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "relaysmtp" , ` SMTP Port (25) already opened on ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2019-05-05 06:03:40 +02:00
httpworm : function ( ip ) {
updateDynamicRam ( "httpworm" , getRamCost ( "httpworm" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "httpworm" , "Takes 1 argument" ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "httpworm" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . HTTPWormProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "httpworm" , "You do not have the HTTPWorm.exe program!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( ! server . httpPortOpen ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "httpworm" , ` Executed HTTPWorm.exe on ' ${ server . hostname } ' to open HTTP port (80). ` ) ;
2017-07-25 03:06:40 +02:00
server . httpPortOpen = true ;
2017-06-28 11:47:42 +02:00
++ server . openPortCount ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "httpworm" , ` HTTP Port (80) already opened on ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2019-05-05 06:03:40 +02:00
sqlinject : function ( ip ) {
updateDynamicRam ( "sqlinject" , getRamCost ( "sqlinject" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sqlinject" , "Takes 1 argument." ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sqlinject" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2018-06-02 01:31:34 +02:00
if ( ! Player . hasProgram ( Programs . SQLInjectProgram . name ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sqlinject" , "You do not have the SQLInject.exe program!" ) ;
2017-07-22 00:54:55 +02:00
}
2017-06-28 11:47:42 +02:00
if ( ! server . sqlPortOpen ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "sqlinject" , ` Executed SQLInject.exe on ' ${ server . hostname } ' to open SQL port (1433). ` ) ;
2017-07-25 03:06:40 +02:00
server . sqlPortOpen = true ;
2017-06-28 11:47:42 +02:00
++ server . openPortCount ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "sqlinject" , ` SQL Port (1433) already opened on ' ${ server . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
}
return true ;
} ,
2021-03-11 09:02:05 +01:00
run : function ( scriptname , threads = 1 ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "run" , getRamCost ( "run" ) ) ;
2017-06-28 11:47:42 +02:00
if ( scriptname === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "run" , "Usage: run(scriptname, [numThreads], [arg1], [arg2]...)" ) ;
2017-06-28 11:47:42 +02:00
}
2019-04-27 00:18:19 +02:00
if ( isNaN ( threads ) || threads <= 0 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "run" , ` Invalid thread count. Must be numeric and > 0, is ${ thread } ` ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-13 18:54:29 +02:00
var argsForNewScript = [ ] ;
for ( var i = 2 ; i < arguments . length ; ++ i ) {
argsForNewScript . push ( arguments [ i ] ) ;
}
2017-06-28 11:47:42 +02:00
var scriptServer = getServer ( workerScript . serverIp ) ;
if ( scriptServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "run" , "Could not find server. This is a bug. Report to dev." ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2021-03-11 09:02:05 +01:00
return runScriptFromScript ( "run" , scriptServer , scriptname , argsForNewScript , workerScript , threads ) ;
2017-06-28 11:47:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
exec : function ( scriptname , ip , threads = 1 ) {
updateDynamicRam ( "exec" , getRamCost ( "exec" ) ) ;
2017-06-28 11:47:42 +02:00
if ( scriptname === undefined || ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "exec" , "Usage: exec(scriptname, server, [numThreads], [arg1], [arg2]...)" ) ;
2017-06-28 11:47:42 +02:00
}
2019-04-30 05:54:20 +02:00
if ( isNaN ( threads ) || threads <= 0 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "exec" , ` Invalid thread count. Must be numeric and > 0, is ${ threads } ` ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-13 18:54:29 +02:00
var argsForNewScript = [ ] ;
for ( var i = 3 ; i < arguments . length ; ++ i ) {
argsForNewScript . push ( arguments [ i ] ) ;
}
2017-06-28 11:47:42 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "exec" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2021-03-11 09:02:05 +01:00
return runScriptFromScript ( "exec" , server , scriptname , argsForNewScript , workerScript , threads ) ;
2017-06-28 11:47:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
spawn : function ( scriptname , threads ) {
updateDynamicRam ( "spawn" , getRamCost ( "spawn" ) ) ;
2021-03-11 09:02:05 +01:00
if ( ! scriptname || ! threads ) {
throw makeRuntimeErrorMsg ( "spawn" , "Usage: spawn(scriptname, threads)" ) ;
2018-02-15 05:26:43 +01:00
}
2019-07-12 04:37:17 +02:00
const spawnDelay = 10 ;
2019-02-20 09:42:27 +01:00
setTimeoutRef ( ( ) => {
2019-04-30 05:54:20 +02:00
if ( isNaN ( threads ) || threads <= 0 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "spawn" , ` Invalid thread count. Must be numeric and > 0, is ${ threads } ` ) ;
2018-06-17 21:19:43 +02:00
}
var argsForNewScript = [ ] ;
for ( var i = 2 ; i < arguments . length ; ++ i ) {
argsForNewScript . push ( arguments [ i ] ) ;
}
var scriptServer = getServer ( workerScript . serverIp ) ;
if ( scriptServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "spawn" , "Could not find server. This is a bug. Report to dev" ) ;
2018-06-17 21:19:43 +02:00
}
2021-03-11 09:02:05 +01:00
return runScriptFromScript ( "spawn" , scriptServer , scriptname , argsForNewScript , workerScript , threads ) ;
2019-07-12 04:37:17 +02:00
} , spawnDelay * 1e3 ) ;
2019-07-14 05:55:58 +02:00
2021-03-11 09:02:05 +01:00
workerScript . log ( "spawn" , ` Will execute ' ${ scriptname } ' in ${ spawnDelay } seconds ` ) ;
2019-07-14 05:55:58 +02:00
workerScript . running = false ; // Prevent workerScript from "finishing execution naturally"
if ( killWorkerScript ( workerScript ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "spawn" , "Exiting..." ) ;
2018-02-24 23:55:06 +01:00
}
2018-02-15 05:26:43 +01:00
} ,
2019-07-12 04:37:17 +02:00
kill : function ( filename , ip , ... scriptArgs ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "kill" , getRamCost ( "kill" ) ) ;
2019-07-12 04:37:17 +02:00
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-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "kill" , "Usage: kill(scriptname, server, [arg1], [arg2]...)" ) ;
2019-07-12 04:37:17 +02:00
}
const server = safeGetServer ( ip ) ;
const runningScriptObj = getRunningScript ( filename , ip , "kill" , scriptArgs ) ;
if ( runningScriptObj == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "kill" , getCannotFindRunningScriptErrorMessage ( filename , ip , scriptArgs ) ) ;
2019-07-12 04:37:17 +02:00
return false ;
}
res = killWorkerScript ( runningScriptObj , server . ip ) ;
2017-06-28 11:47:42 +02:00
}
2019-07-12 04:37:17 +02:00
2017-06-28 11:47:42 +02:00
if ( res ) {
2021-03-11 23:01:28 +01:00
if ( killByPid ) {
workerScript . log ( "kill" , ` Killing script with PID ${ filename } ` ) ;
} else {
workerScript . log ( "kill" , ` Killing ' ${ filename } ' on ' ${ ip } ' with args: ${ arrayToString ( scriptArgs ) } . ` ) ;
2018-02-24 23:55:06 +01:00
}
2017-06-28 11:47:42 +02:00
return true ;
} else {
2021-03-11 09:02:05 +01:00
if ( killByPid ) {
workerScript . log ( "kill" , ` No script with PID ${ filename } ` ) ;
} else {
workerScript . log ( "kill" , ` No such script ' ${ filename } ' on ' ${ ip } ' with args: ${ arrayToString ( scriptArgs ) } ` ) ;
2018-02-24 23:55:06 +01:00
}
2017-06-28 11:47:42 +02:00
return false ;
}
} ,
2019-05-05 06:03:40 +02:00
killall : function ( ip = workerScript . serverIp ) {
updateDynamicRam ( "killall" , getRamCost ( "killall" ) ) ;
2017-06-28 11:47:42 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "killall" , "Takes 1 argument" ) ;
2017-06-28 11:47:42 +02:00
}
2019-06-22 10:12:17 +02:00
const server = getServer ( ip ) ;
2017-06-28 11:47:42 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "killall" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2019-06-22 10:12:17 +02:00
const scriptsRunning = ( server . runningScripts . length > 0 ) ;
for ( let i = server . runningScripts . length - 1 ; i >= 0 ; -- i ) {
killWorkerScript ( server . runningScripts [ i ] , server . ip , false ) ;
2017-06-28 11:47:42 +02:00
}
2019-06-22 10:12:17 +02:00
WorkerScriptStartStopEventEmitter . emitEvent ( ) ;
2021-03-11 23:01:28 +01:00
workerScript . log ( "killall" , ` Killing all scripts on ' ${ server . hostname } '. May take a few minutes for the scripts to die. ` ) ;
2019-06-22 10:12:17 +02:00
2018-02-24 23:55:06 +01:00
return scriptsRunning ;
2017-06-28 11:47:42 +02:00
} ,
2017-12-07 04:45:47 +01:00
exit : function ( ) {
2019-07-14 05:55:58 +02:00
workerScript . running = false ; // Prevent workerScript from "finishing execution naturally"
if ( killWorkerScript ( workerScript ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "exit" , "Exiting..." ) ;
2017-12-07 04:45:47 +01:00
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "exit" , "Failed. This is a bug. Report to dev." ) ;
2017-12-07 04:45:47 +01:00
}
} ,
2019-05-05 06:03:40 +02:00
scp : function ( scriptname , ip1 , ip2 ) {
updateDynamicRam ( "scp" , getRamCost ( "scp" ) ) ;
2017-09-19 20:38:03 +02:00
if ( arguments . length !== 2 && arguments . length !== 3 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
2017-09-07 07:45:14 +02:00
}
2017-09-27 17:13:42 +02:00
if ( scriptname && scriptname . constructor === Array ) {
2019-04-13 03:22:46 +02:00
// Recursively call scp on all elements of array
2017-09-27 17:13:42 +02:00
var res = false ;
scriptname . forEach ( function ( script ) {
if ( NetscriptFunctions ( workerScript ) . scp ( script , ip1 , ip2 ) ) {
res = true ;
2021-04-30 05:52:56 +02:00
}
2017-09-27 17:13:42 +02:00
} ) ;
return res ;
}
2019-05-12 04:20:20 +02:00
// Invalid file type
if ( ! isValidFilePath ( scriptname ) ) {
2021-03-11 23:01:28 +01:00
throw makeRuntimeErrorMsg ( "scp" , ` Invalid filename: ' ${ scriptname } ' ` ) ;
2019-05-12 04:20:20 +02:00
}
// Invalid file name
if ( ! scriptname . endsWith ( ".lit" ) && ! isScriptFilename ( scriptname ) && ! scriptname . endsWith ( "txt" ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Only works for .script, .lit, and .txt files" ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2017-09-19 20:38:03 +02:00
var destServer , currServ ;
2018-11-18 01:23:48 +01:00
if ( ip2 != null ) { // 3 Argument version: scriptname, source, destination
2017-09-19 20:38:03 +02:00
if ( scriptname === undefined || ip1 === undefined || ip2 === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
2017-09-19 20:38:03 +02:00
}
destServer = getServer ( ip2 ) ;
if ( destServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , ` Invalid IP/hostname: ${ ip2 } ` ) ;
2017-09-19 20:38:03 +02:00
}
currServ = getServer ( ip1 ) ;
if ( currServ == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , ` Invalid IP/hostname: ${ ip1 } ` ) ;
2017-09-19 20:38:03 +02:00
}
2018-11-18 01:23:48 +01:00
} else if ( ip1 != null ) { // 2 Argument version: scriptname, destination
2017-09-19 20:38:03 +02:00
if ( scriptname === undefined || ip1 === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
2017-09-19 20:38:03 +02:00
}
destServer = getServer ( ip1 ) ;
if ( destServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , ` Invalid IP/hostname: ${ ip1 } ` ) ;
2017-09-19 20:38:03 +02:00
}
currServ = getServer ( workerScript . serverIp ) ;
if ( currServ == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Could not find server ip for this script. This is a bug. Report to dev." ) ;
2017-09-19 20:38:03 +02:00
}
2018-11-18 01:23:48 +01:00
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scp" , "Takes 2 or 3 arguments" ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2019-04-13 03:22:46 +02:00
// Scp for lit files
2017-09-07 07:45:14 +02:00
if ( scriptname . endsWith ( ".lit" ) ) {
var found = false ;
for ( var i = 0 ; i < currServ . messages . length ; ++ i ) {
if ( ! ( currServ . messages [ i ] instanceof Message ) && currServ . messages [ i ] == scriptname ) {
found = true ;
2018-02-24 23:55:06 +01:00
break ;
2017-09-07 07:45:14 +02:00
}
}
if ( ! found ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' does not exist. ` ) ;
2017-09-07 07:45:14 +02:00
return false ;
}
for ( var i = 0 ; i < destServer . messages . length ; ++ i ) {
if ( destServer . messages [ i ] === scriptname ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' copied over to ' ${ destServer . hostname } '. ` ) ;
2019-04-13 03:22:46 +02:00
return true ; // Already exists
2017-09-07 07:45:14 +02:00
}
}
destServer . messages . push ( scriptname ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' copied over to ' ${ destServer . hostname } '. ` ) ;
2018-02-24 23:55:06 +01:00
return true ;
}
2019-04-13 03:22:46 +02:00
// Scp for text files
2018-02-24 23:55:06 +01:00
if ( scriptname . endsWith ( ".txt" ) ) {
var found = false , txtFile ;
for ( var i = 0 ; i < currServ . textFiles . length ; ++ i ) {
if ( currServ . textFiles [ i ] . fn === scriptname ) {
found = true ;
txtFile = currServ . textFiles [ i ] ;
break ;
}
}
if ( ! found ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' does not exist. ` ) ;
2018-02-24 23:55:06 +01:00
return false ;
}
for ( var i = 0 ; i < destServer . textFiles . length ; ++ i ) {
if ( destServer . textFiles [ i ] . fn === scriptname ) {
2019-04-13 03:22:46 +02:00
// Overwrite
2018-02-24 23:55:06 +01:00
destServer . textFiles [ i ] . text = txtFile . text ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' copied over to ' ${ destServer . hostname } '. ` ) ;
2018-02-24 23:55:06 +01:00
return true ;
}
}
var newFile = new TextFile ( txtFile . fn , txtFile . text ) ;
destServer . textFiles . push ( newFile ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' copied over to ' ${ destServer . hostname } '. ` ) ;
2017-09-07 07:45:14 +02:00
return true ;
}
2019-04-13 03:22:46 +02:00
// Scp for script files
2021-03-11 09:02:05 +01:00
let sourceScript = null ;
for ( let i = 0 ; i < currServ . scripts . length ; ++ i ) {
2017-06-28 11:47:42 +02:00
if ( scriptname == currServ . scripts [ i ] . filename ) {
sourceScript = currServ . scripts [ i ] ;
break ;
2017-06-11 22:28:20 +02:00
}
2017-06-28 11:47:42 +02:00
}
if ( sourceScript == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' does not exist. ` ) ;
2017-06-28 11:47:42 +02:00
return false ;
}
2017-07-25 03:06:40 +02:00
2019-04-13 03:22:46 +02:00
// Overwrite script if it already exists
2021-03-11 09:02:05 +01:00
for ( let i = 0 ; i < destServer . scripts . length ; ++ i ) {
2017-06-28 11:47:42 +02:00
if ( scriptname == destServer . scripts [ i ] . filename ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` WARNING: File ' ${ scriptname } ' overwritten on ' ${ destServer . hostname } ' ` ) ;
const oldScript = destServer . scripts [ i ] ;
2021-05-04 00:53:10 +02:00
// 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 ;
2017-06-28 11:47:42 +02:00
oldScript . code = sourceScript . code ;
oldScript . ramUsage = sourceScript . ramUsage ;
2019-06-02 21:40:13 +02:00
oldScript . markUpdated ( ) ;
2017-06-28 11:47:42 +02:00
return true ;
2017-06-18 06:36:16 +02:00
}
2017-06-28 11:47:42 +02:00
}
2017-07-25 03:06:40 +02:00
2019-04-13 03:22:46 +02:00
// Create new script if it does not already exist
2021-03-11 09:02:05 +01:00
const newScript = new Script ( scriptname ) ;
2017-06-28 11:47:42 +02:00
newScript . code = sourceScript . code ;
newScript . ramUsage = sourceScript . ramUsage ;
newScript . server = destServer . ip ;
destServer . scripts . push ( newScript ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "scp" , ` File ' ${ scriptname } ' copied over to ' ${ destServer . hostname } '. ` ) ;
2017-06-28 11:47:42 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
ls : function ( ip , grep ) {
updateDynamicRam ( "ls" , getRamCost ( "ls" ) ) ;
2017-09-07 07:45:14 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ls" , "Usage: ls(ip/hostname, [grep filter])" ) ;
2017-09-07 07:45:14 +02:00
}
2021-03-11 09:02:05 +01:00
const server = getServer ( ip ) ;
2017-10-26 00:05:12 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ls" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-09-07 07:45:14 +02:00
}
2019-04-13 03:22:46 +02:00
// Get the grep filter, if one exists
2021-03-11 09:02:05 +01:00
let filter = false ;
2017-09-07 07:45:14 +02:00
if ( arguments . length >= 2 ) {
filter = grep . toString ( ) ;
}
2021-03-11 09:02:05 +01:00
const allFiles = [ ] ;
for ( let i = 0 ; i < server . programs . length ; i ++ ) {
2017-09-07 07:45:14 +02:00
if ( filter ) {
if ( server . programs [ i ] . includes ( filter ) ) {
allFiles . push ( server . programs [ i ] ) ;
}
} else {
allFiles . push ( server . programs [ i ] ) ;
}
}
2021-03-11 09:02:05 +01:00
for ( let i = 0 ; i < server . scripts . length ; i ++ ) {
2017-09-07 07:45:14 +02:00
if ( filter ) {
if ( server . scripts [ i ] . filename . includes ( filter ) ) {
allFiles . push ( server . scripts [ i ] . filename ) ;
}
} else {
allFiles . push ( server . scripts [ i ] . filename ) ;
}
}
2021-03-11 09:02:05 +01:00
for ( let i = 0 ; i < server . messages . length ; i ++ ) {
2017-09-07 07:45:14 +02:00
if ( filter ) {
if ( server . messages [ i ] instanceof Message ) {
if ( server . messages [ i ] . filename . includes ( filter ) ) {
allFiles . push ( server . messages [ i ] . filename ) ;
}
} else if ( server . messages [ i ] . includes ( filter ) ) {
allFiles . push ( server . messages [ i ] ) ;
}
} else {
if ( server . messages [ i ] instanceof Message ) {
allFiles . push ( server . messages [ i ] . filename ) ;
} else {
allFiles . push ( server . messages [ i ] ) ;
}
}
}
2018-02-18 04:08:54 +01:00
for ( var i = 0 ; i < server . textFiles . length ; i ++ ) {
if ( filter ) {
if ( server . textFiles [ i ] . fn . includes ( filter ) ) {
allFiles . push ( server . textFiles [ i ] . fn ) ;
}
} else {
allFiles . push ( server . textFiles [ i ] . fn ) ;
}
}
2018-09-23 02:25:48 +02:00
for ( var i = 0 ; i < server . contracts . length ; ++ i ) {
if ( filter ) {
if ( server . contracts [ i ] . fn . includes ( filter ) ) {
allFiles . push ( server . contracts [ i ] . fn ) ;
}
} else {
allFiles . push ( server . contracts [ i ] . fn ) ;
}
}
2019-04-13 03:22:46 +02:00
// Sort the files alphabetically then print each
2017-09-07 07:45:14 +02:00
allFiles . sort ( ) ;
return allFiles ;
} ,
2019-05-05 06:03:40 +02:00
ps : function ( ip = workerScript . serverIp ) {
updateDynamicRam ( "ps" , getRamCost ( "ps" ) ) ;
2018-06-13 06:03:02 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "ps" , ` Invalid IP/hostname: ${ ip } ` ) ;
2018-06-13 06:03:02 +02:00
}
const processes = [ ] ;
2018-10-23 20:55:42 +02:00
for ( const i in server . runningScripts ) {
2018-06-13 06:03:02 +02:00
const script = server . runningScripts [ i ] ;
2021-04-21 14:20:26 +02:00
processes . push ( {
filename : script . filename ,
threads : script . threads ,
args : script . args . slice ( ) ,
pid : script . pid ,
} )
2018-06-13 06:03:02 +02:00
}
return processes ;
} ,
2019-05-05 06:03:40 +02:00
hasRootAccess : function ( ip ) {
updateDynamicRam ( "hasRootAccess" , getRamCost ( "hasRootAccess" ) ) ;
2017-07-13 18:54:29 +02:00
if ( ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "hasRootAccess" , "Takes 1 argument" ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "hasRootAccess" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
2017-07-13 18:54:29 +02:00
return server . hasAdminRights ;
2017-06-28 11:47:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getIp : function ( ) {
updateDynamicRam ( "getIp" , getRamCost ( "getIp" ) ) ;
2017-09-19 20:38:03 +02:00
var scriptServer = getServer ( workerScript . serverIp ) ;
if ( scriptServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getIp" , "Could not find server. This is a bug. Report to dev." ) ;
2017-09-19 20:38:03 +02:00
}
return scriptServer . ip ;
} ,
2019-05-05 06:03:40 +02:00
getHostname : function ( ) {
updateDynamicRam ( "getHostname" , getRamCost ( "getHostname" ) ) ;
2017-06-28 11:47:42 +02:00
var scriptServer = getServer ( workerScript . serverIp ) ;
if ( scriptServer == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( workerScript , "Could not find server. This is a bug. Report to dev." ) ;
2017-06-28 11:47:42 +02:00
}
return scriptServer . hostname ;
} ,
2019-05-05 06:03:40 +02:00
getHackingLevel : function ( ) {
updateDynamicRam ( "getHackingLevel" , getRamCost ( "getHackingLevel" ) ) ;
2017-06-28 11:47:42 +02:00
Player . updateSkillLevels ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "getHackingLevel" , ` returned ${ Player . hacking _skill } ` ) ;
2017-06-28 11:47:42 +02:00
return Player . hacking _skill ;
} ,
2019-05-05 06:03:40 +02:00
getHackingMultipliers : function ( ) {
updateDynamicRam ( "getHackingMultipliers" , getRamCost ( "getHackingMultipliers" ) ) ;
2017-09-19 20:38:03 +02:00
return {
chance : Player . hacking _chance _mult ,
speed : Player . hacking _speed _mult ,
money : Player . hacking _money _mult ,
growth : Player . hacking _grow _mult ,
} ;
} ,
2019-05-05 06:03:40 +02:00
getHacknetMultipliers : function ( ) {
updateDynamicRam ( "getHacknetMultipliers" , getRamCost ( "getHacknetMultipliers" ) ) ;
2018-05-06 20:22:29 +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 ,
} ;
} ,
2017-09-19 20:38:03 +02:00
getBitNodeMultipliers : function ( ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getBitNodeMultipliers" , getRamCost ( "getBitNodeMultipliers" ) ) ;
2021-05-03 17:58:11 +02:00
if ( SourceFileFlags [ 5 ] <= 0 && Player . bitNodeN !== 5 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getBitNodeMultipliers" , "Requires Source-File 5 to run." ) ;
2017-09-19 20:38:03 +02:00
}
2018-06-03 03:37:56 +02:00
let copy = Object . assign ( { } , BitNodeMultipliers ) ;
return copy ;
2017-09-19 20:38:03 +02:00
} ,
2021-03-31 06:45:21 +02:00
getServer : function ( ip ) {
updateDynamicRam ( "getServer" , getRamCost ( "getServer" ) ) ;
2021-04-29 19:39:03 +02:00
if ( SourceFileFlags [ 5 ] <= 0 && Player . bitNodeN !== 5 ) {
2021-03-31 06:45:21 +02:00
throw makeRuntimeErrorMsg ( "getServer" , "Requires Source-File 5 to run." ) ;
}
const server = safeGetServer ( ip , "getServer" ) ;
const copy = Object . assign ( { } , server ) ;
// These fields should be hidden.
copy . contracts = undefined ;
copy . messages = undefined ;
copy . runningScripts = undefined ;
copy . scripts = undefined ;
copy . textFiles = undefined ;
copy . programs = undefined ;
copy . serversOnNetwork = undefined ;
return copy ;
} ,
2019-05-05 06:03:40 +02:00
getServerMoneyAvailable : function ( ip ) {
updateDynamicRam ( "getServerMoneyAvailable" , getRamCost ( "getServerMoneyAvailable" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerMoneyAvailable" ) ;
if ( failOnHacknetServer ( server , "getServerMoneyAvailable" ) ) { return 0 ; }
2017-07-13 18:54:29 +02:00
if ( server . hostname == "home" ) {
2019-03-31 04:53:57 +02:00
// Return player's money
2021-03-31 06:45:21 +02:00
workerScript . log ( "getServerMoneyAvailable" , ` returned player's money: ${ numeralWrapper . formatMoney ( Player . money . toNumber ( ) ) } ` ) ;
2017-07-29 18:25:40 +02:00
return Player . money . toNumber ( ) ;
2017-06-28 11:47:42 +02:00
}
2021-03-31 06:45:21 +02:00
workerScript . log ( "getServerMoneyAvailable" , ` returned ${ numeralWrapper . formatMoney ( server . moneyAvailable ) } for ' ${ server . hostname } ` ) ;
2017-06-28 11:47:42 +02:00
return server . moneyAvailable ;
} ,
2019-05-05 06:03:40 +02:00
getServerSecurityLevel : function ( ip ) {
updateDynamicRam ( "getServerSecurityLevel" , getRamCost ( "getServerSecurityLevel" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerSecurityLevel" ) ;
if ( failOnHacknetServer ( server , "getServerSecurityLevel" ) ) { return 1 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerSecurityLevel" , ` returned ${ formatNumber ( server . hackDifficulty , 3 ) } for ' ${ server . hostname } ' ` ) ;
2017-06-28 11:47:42 +02:00
return server . hackDifficulty ;
} ,
2019-05-05 06:03:40 +02:00
getServerBaseSecurityLevel : function ( ip ) {
updateDynamicRam ( "getServerBaseSecurityLevel" , getRamCost ( "getServerBaseSecurityLevel" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerBaseSecurityLevel" ) ;
if ( failOnHacknetServer ( server , "getServerBaseSecurityLevel" ) ) { return 1 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerBaseSecurityLevel" , ` returned ${ formatNumber ( server . baseDifficulty , 3 ) } for ' ${ server . hostname } ' ` ) ;
2017-06-28 11:47:42 +02:00
return server . baseDifficulty ;
} ,
2019-05-05 06:03:40 +02:00
getServerMinSecurityLevel : function ( ip ) {
updateDynamicRam ( "getServerMinSecurityLevel" , getRamCost ( "getServerMinSecurityLevel" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerMinSecurityLevel" ) ;
if ( failOnHacknetServer ( server , "getServerMinSecurityLevel" ) ) { return 1 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerMinSecurityLevel" , ` returned ${ formatNumber ( server . minDifficulty , 3 ) } for ${ server . hostname } ` ) ;
2017-09-26 04:44:33 +02:00
return server . minDifficulty ;
} ,
2019-05-05 06:03:40 +02:00
getServerRequiredHackingLevel : function ( ip ) {
updateDynamicRam ( "getServerRequiredHackingLevel" , getRamCost ( "getServerRequiredHackingLevel" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerRequiredHackingLevel" ) ;
if ( failOnHacknetServer ( server , "getServerRequiredHackingLevel" ) ) { return 1 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerRequiredHackingLevel" , ` returned ${ formatNumber ( server . requiredHackingSkill , 0 ) } for ' ${ server . hostname } ' ` ) ;
2017-06-28 11:47:42 +02:00
return server . requiredHackingSkill ;
} ,
2019-05-05 06:03:40 +02:00
getServerMaxMoney : function ( ip ) {
updateDynamicRam ( "getServerMaxMoney" , getRamCost ( "getServerMaxMoney" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerMaxMoney" ) ;
if ( failOnHacknetServer ( server , "getServerMaxMoney" ) ) { return 0 ; }
2021-03-31 06:45:21 +02:00
workerScript . log ( "getServerMaxMoney" , ` returned ${ numeralWrapper . formatMoney ( server . moneyMax ) } for ' ${ server . hostname } ' ` ) ;
2017-06-28 11:47:42 +02:00
return server . moneyMax ;
} ,
2019-05-05 06:03:40 +02:00
getServerGrowth : function ( ip ) {
updateDynamicRam ( "getServerGrowth" , getRamCost ( "getServerGrowth" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerGrowth" ) ;
if ( failOnHacknetServer ( server , "getServerGrowth" ) ) { return 1 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerGrowth" , ` returned ${ formatNumber ( server . serverGrowth , 0 ) } for ' ${ server . hostname } ' ` ) ;
2017-08-18 19:20:51 +02:00
return server . serverGrowth ;
} ,
2019-05-05 06:03:40 +02:00
getServerNumPortsRequired : function ( ip ) {
updateDynamicRam ( "getServerNumPortsRequired" , getRamCost ( "getServerNumPortsRequired" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerNumPortsRequired" ) ;
if ( failOnHacknetServer ( server , "getServerNumPortsRequired" ) ) { return 5 ; }
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerNumPortsRequired" , ` returned ${ formatNumber ( server . numOpenPortsRequired , 0 ) } for ' ${ server . hostname } ' ` ) ;
2017-07-13 18:54:29 +02:00
return server . numOpenPortsRequired ;
} ,
2019-05-05 06:03:40 +02:00
getServerRam : function ( ip ) {
updateDynamicRam ( "getServerRam" , getRamCost ( "getServerRam" ) ) ;
2019-03-31 04:53:57 +02:00
const server = safeGetServer ( ip , "getServerRam" ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "getServerRam" , ` returned [ ${ formatNumber ( server . maxRam , 2 ) } GB, ${ formatNumber ( server . ramUsed , 2 ) } GB] ` ) ;
2017-07-25 03:06:40 +02:00
return [ server . maxRam , server . ramUsed ] ;
} ,
2021-05-03 07:07:46 +02:00
getServerMaxRam : function ( ip ) {
updateDynamicRam ( "getServerMaxRam" , getRamCost ( "getServerMaxRam" ) ) ;
const server = safeGetServer ( ip , "getServerMaxRam" ) ;
workerScript . log ( "getServerMaxRam" , ` returned ${ formatNumber ( server . maxRam , 2 ) } GB ` ) ;
return server . maxRam ;
} ,
getServerUsedRam : function ( ip ) {
updateDynamicRam ( "getServerUsedRam" , getRamCost ( "getServerUsedRam" ) ) ;
const server = safeGetServer ( ip , "getServerUsedRam" ) ;
workerScript . log ( "getServerUsedRam" , ` returned ${ formatNumber ( server . ramUsed , 2 ) } GB ` ) ;
return server . ramUsed ;
} ,
2019-05-05 06:03:40 +02:00
serverExists : function ( ip ) {
updateDynamicRam ( "serverExists" , getRamCost ( "serverExists" ) ) ;
2017-09-15 16:06:59 +02:00
return ( getServer ( ip ) !== null ) ;
} ,
2019-05-05 06:03:40 +02:00
fileExists : function ( filename , ip = workerScript . serverIp ) {
updateDynamicRam ( "fileExists" , getRamCost ( "fileExists" ) ) ;
2017-06-28 11:47:42 +02:00
if ( filename === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "fileExists" , "Usage: fileExists(scriptname, [server])" ) ;
2017-06-28 11:47:42 +02:00
}
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "fileExists" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-06-28 11:47:42 +02:00
}
for ( var i = 0 ; i < server . scripts . length ; ++ i ) {
if ( filename == server . scripts [ i ] . filename ) {
return true ;
}
}
2017-07-13 18:54:29 +02:00
for ( var i = 0 ; i < server . programs . length ; ++ i ) {
if ( filename . toLowerCase ( ) == server . programs [ i ] . toLowerCase ( ) ) {
return true ;
}
2017-06-28 11:47:42 +02:00
}
2017-09-21 23:27:31 +02:00
for ( var i = 0 ; i < server . messages . length ; ++ i ) {
if ( ! ( server . messages [ i ] instanceof Message ) &&
filename . toLowerCase ( ) === server . messages [ i ] ) {
return true ;
}
}
2018-01-02 08:12:54 +01:00
var txtFile = getTextFile ( filename , server ) ;
2018-01-21 21:53:16 +01:00
if ( txtFile != null ) {
2018-01-02 08:12:54 +01:00
return true ;
}
2017-06-28 11:47:42 +02:00
return false ;
} ,
2021-04-21 14:20:26 +02:00
isRunning : function ( fn , ip = workerScript . serverIp , ... scriptArgs ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "isRunning" , getRamCost ( "isRunning" ) ) ;
2021-04-21 14:20:26 +02:00
if ( fn === undefined || ip === undefined ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "isRunning" , "Usage: isRunning(scriptname, server, [arg1], [arg2]...)" ) ;
2017-06-28 11:47:42 +02:00
}
2021-04-21 14:20:26 +02:00
if ( typeof fn === 'number' ) {
2021-04-29 02:07:26 +02:00
return getRunningScriptByPid ( fn , 'isRunning' ) != null ;
2021-04-21 14:20:26 +02:00
} else {
return getRunningScript ( fn , ip , "isRunning" , scriptArgs ) != null ;
2017-07-13 18:54:29 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getStockSymbols : function ( ) {
updateDynamicRam ( "getStockSymbols" , getRamCost ( "getStockSymbols" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockSymbols" ) ;
2018-09-30 03:52:38 +02:00
return Object . values ( StockSymbols ) ;
} ,
2019-05-05 06:03:40 +02:00
getStockPrice : function ( symbol ) {
updateDynamicRam ( "getStockPrice" , getRamCost ( "getStockPrice" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockPrice" ) ;
const stock = getStockFromSymbol ( symbol , "getStockPrice" ) ;
return stock . price ;
} ,
2019-05-05 06:03:40 +02:00
getStockAskPrice : function ( symbol ) {
updateDynamicRam ( "getStockAskPrice" , getRamCost ( "getStockAskPrice" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockAskPrice" ) ;
const stock = getStockFromSymbol ( symbol , "getStockAskPrice" ) ;
return stock . getAskPrice ( ) ;
} ,
2019-05-05 06:03:40 +02:00
getStockBidPrice : function ( symbol ) {
updateDynamicRam ( "getStockBidPrice" , getRamCost ( "getStockBidPrice" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockBidPrice" ) ;
const stock = getStockFromSymbol ( symbol , "getStockBidPrice" ) ;
return stock . getBidPrice ( ) ;
2017-07-13 18:54:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
getStockPosition : function ( symbol ) {
updateDynamicRam ( "getStockPosition" , getRamCost ( "getStockPosition" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockPosition" ) ;
2017-07-13 18:54:29 +02:00
var stock = SymbolToStockMap [ symbol ] ;
if ( stock == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getStockPosition" , ` Invalid stock symbol: ${ symbol } ` ) ;
2017-07-13 18:54:29 +02:00
}
2017-10-25 21:47:47 +02:00
return [ stock . playerShares , stock . playerAvgPx , stock . playerShortShares , stock . playerAvgShortPx ] ;
2017-07-13 18:54:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
getStockMaxShares : function ( symbol ) {
updateDynamicRam ( "getStockMaxShares" , getRamCost ( "getStockMaxShares" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "getStockMaxShares" ) ;
const stock = getStockFromSymbol ( symbol , "getStockMaxShares" ) ;
2019-01-09 11:06:49 +01:00
2019-04-29 08:20:27 +02:00
return stock . maxShares ;
} ,
2019-06-03 05:28:02 +02:00
getStockPurchaseCost : function ( symbol , shares , posType ) {
updateDynamicRam ( "getStockPurchaseCost" , getRamCost ( "getStockPurchaseCost" ) ) ;
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 ;
}
const res = getBuyTransactionCost ( stock , shares , pos ) ;
if ( res == null ) { return Infinity ; }
return res ;
} ,
getStockSaleGain : function ( symbol , shares , posType ) {
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 ;
}
const res = getSellTransactionGain ( stock , shares , pos ) ;
if ( res == null ) { return 0 ; }
return res ;
} ,
2019-05-05 06:03:40 +02:00
buyStock : function ( symbol , shares ) {
updateDynamicRam ( "buyStock" , getRamCost ( "buyStock" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "buyStock" ) ;
const stock = getStockFromSymbol ( symbol , "buyStock" ) ;
2019-05-05 06:03:40 +02:00
const res = buyStock ( stock , shares , workerScript , { rerenderFn : displayStockMarketContent } ) ;
2019-04-21 07:31:19 +02:00
return res ? stock . price : 0 ;
2017-07-13 18:54:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
sellStock : function ( symbol , shares ) {
updateDynamicRam ( "sellStock" , getRamCost ( "sellStock" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "sellStock" ) ;
const stock = getStockFromSymbol ( symbol , "sellStock" ) ;
2019-05-05 06:03:40 +02:00
const res = sellStock ( stock , shares , workerScript , { rerenderFn : displayStockMarketContent } ) ;
2017-07-25 03:06:40 +02:00
2019-04-21 07:31:19 +02:00
return res ? stock . price : 0 ;
2017-07-13 18:54:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
shortStock : function ( symbol , shares ) {
updateDynamicRam ( "shortStock" , getRamCost ( "shortStock" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "shortStock" ) ;
2017-10-25 21:47:47 +02:00
if ( Player . bitNodeN !== 8 ) {
2019-05-05 06:03:40 +02:00
if ( SourceFileFlags [ 8 ] <= 1 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( shortStock , "You must either be in BitNode-8 or you must have Source-File 8 Level 2." ) ;
2017-10-25 21:47:47 +02:00
}
}
2019-04-29 08:20:27 +02:00
const stock = getStockFromSymbol ( symbol , "shortStock" ) ;
2019-05-05 06:03:40 +02:00
const res = shortStock ( stock , shares , workerScript , { rerenderFn : displayStockMarketContent } ) ;
2019-04-21 07:31:19 +02:00
2017-12-03 05:19:49 +01:00
return res ? stock . price : 0 ;
2017-10-25 21:47:47 +02:00
} ,
2019-05-05 06:03:40 +02:00
sellShort : function ( symbol , shares ) {
updateDynamicRam ( "sellShort" , getRamCost ( "sellShort" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "sellShort" ) ;
2017-10-25 21:47:47 +02:00
if ( Player . bitNodeN !== 8 ) {
2019-05-05 06:03:40 +02:00
if ( SourceFileFlags [ 8 ] <= 1 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sellShort" , "You must either be in BitNode-8 or you must have Source-File 8 Level 2." ) ;
2017-10-25 21:47:47 +02:00
}
}
2019-04-29 08:20:27 +02:00
const stock = getStockFromSymbol ( symbol , "sellShort" ) ;
2019-05-05 06:03:40 +02:00
const res = sellShort ( stock , shares , workerScript , { rerenderFn : displayStockMarketContent } ) ;
2019-04-23 10:23:08 +02:00
2017-12-03 05:19:49 +01:00
return res ? stock . price : 0 ;
2017-10-25 21:47:47 +02:00
} ,
2019-05-05 06:03:40 +02:00
placeOrder : function ( symbol , shares , price , type , pos ) {
updateDynamicRam ( "placeOrder" , getRamCost ( "placeOrder" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "placeOrder" ) ;
2017-10-25 21:47:47 +02:00
if ( Player . bitNodeN !== 8 ) {
2019-05-05 06:03:40 +02:00
if ( SourceFileFlags [ 8 ] <= 2 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "placeOrder" , "You must either be in BitNode-8 or you must have Source-File 8 Level 3." ) ;
2017-10-25 21:47:47 +02:00
}
}
2019-04-29 08:20:27 +02:00
const stock = getStockFromSymbol ( symbol , "placeOrder" ) ;
2021-03-11 09:02:05 +01:00
let orderType , orderPos ;
ltype = type . toLowerCase ( ) ;
if ( ltype . includes ( "limit" ) && ltype . includes ( "buy" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . LimitBuy ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "limit" ) && ltype . includes ( "sell" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . LimitSell ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "stop" ) && ltype . includes ( "buy" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . StopBuy ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "stop" ) && ltype . includes ( "sell" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . StopSell ;
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "placeOrder" , ` Invalid order type: ${ type } ` ) ;
2017-10-25 21:47:47 +02:00
}
2021-03-11 09:02:05 +01:00
lpos = pos . toLowerCase ( ) ;
if ( lpos . includes ( "l" ) ) {
2017-10-25 21:47:47 +02:00
orderPos = PositionTypes . Long ;
2021-03-11 09:02:05 +01:00
} else if ( lpos . includes ( 's' ) ) {
2017-10-25 21:47:47 +02:00
orderPos = PositionTypes . Short ;
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "placeOrder" , ` Invalid position type: ${ pos } ` ) ;
2017-10-25 21:47:47 +02:00
}
return placeOrder ( stock , shares , price , orderType , orderPos , workerScript ) ;
} ,
2019-05-05 06:03:40 +02:00
cancelOrder : function ( symbol , shares , price , type , pos ) {
updateDynamicRam ( "cancelOrder" , getRamCost ( "cancelOrder" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "cancelOrder" ) ;
2017-10-25 21:47:47 +02:00
if ( Player . bitNodeN !== 8 ) {
2019-05-05 06:03:40 +02:00
if ( SourceFileFlags [ 8 ] <= 2 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "cancelOrder" , "You must either be in BitNode-8 or you must have Source-File 8 Level 3." ) ;
2017-10-25 21:47:47 +02:00
}
}
2019-04-29 08:20:27 +02:00
const stock = getStockFrom ( symbol , "cancelOrder" ) ;
2017-10-25 21:47:47 +02:00
if ( isNaN ( shares ) || isNaN ( price ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "cancelOrder" , ` Invalid shares or price. Must be numeric. shares= ${ shares } , price= ${ price } ` ) ;
2017-10-25 21:47:47 +02:00
}
var orderType , orderPos ;
2021-03-11 09:02:05 +01:00
ltype = type . toLowerCase ( ) ;
if ( ltype . includes ( "limit" ) && ltype . includes ( "buy" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . LimitBuy ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "limit" ) && ltype . includes ( "sell" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . LimitSell ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "stop" ) && ltype . includes ( "buy" ) ) {
2017-10-25 21:47:47 +02:00
orderType = OrderTypes . StopBuy ;
2021-03-11 09:02:05 +01:00
} else if ( ltype . includes ( "stop" ) && ltype . includes ( "sell" ) ) {
2017-12-11 06:04:00 +01:00
orderType = OrderTypes . StopSell ;
2017-10-25 21:47:47 +02:00
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "cancelOrder" , ` Invalid order type: ${ type } ` ) ;
2017-10-25 21:47:47 +02:00
}
2021-03-11 09:02:05 +01:00
lpos = pos . toLowerCase ( ) ;
if ( lpos . includes ( "l" ) ) {
2017-10-25 21:47:47 +02:00
orderPos = PositionTypes . Long ;
2021-03-11 09:02:05 +01:00
} else if ( lpos . includes ( 's' ) ) {
2017-10-25 21:47:47 +02:00
orderPos = PositionTypes . Short ;
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "cancelOrder" , ` Invalid position type: ${ pos } ` ) ;
2017-10-25 21:47:47 +02:00
}
2017-12-11 06:04:00 +01:00
var params = {
2017-10-25 21:47:47 +02:00
stock : stock ,
shares : shares ,
price : price ,
type : orderType ,
2021-04-30 05:52:56 +02:00
pos : orderPos ,
2017-10-25 21:47:47 +02:00
} ;
return cancelOrder ( params , workerScript ) ;
} ,
2019-05-05 06:03:40 +02:00
getOrders : function ( ) {
updateDynamicRam ( "getOrders" , getRamCost ( "getOrders" ) ) ;
2019-04-30 05:54:20 +02:00
checkTixApiAccess ( "getOrders" ) ;
2018-12-31 01:11:48 +01:00
if ( Player . bitNodeN !== 8 ) {
2019-05-05 06:03:40 +02:00
if ( SourceFileFlags [ 8 ] <= 2 ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( workerScript , "You must either be in BitNode-8 or have Source-File 8 Level 3." ) ;
2018-12-31 01:11:48 +01:00
}
}
const orders = { } ;
const stockMarketOrders = StockMarket [ "Orders" ] ;
for ( let symbol in stockMarketOrders ) {
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 ,
} ) ;
}
}
}
return orders ;
} ,
2019-05-05 06:03:40 +02:00
getStockVolatility : function ( symbol ) {
updateDynamicRam ( "getStockVolatility" , getRamCost ( "getStockVolatility" ) ) ;
2018-08-24 22:44:48 +02:00
if ( ! Player . has4SDataTixApi ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getStockVolatility" , "You don't have 4S Market Data TIX API Access!" ) ;
2018-08-24 22:44:48 +02:00
}
2019-04-29 08:20:27 +02:00
const stock = getStockFromSymbol ( symbol , "getStockVolatility" ) ;
2019-04-13 03:22:46 +02:00
return stock . mv / 100 ; // Convert from percentage to decimal
2018-08-24 22:44:48 +02:00
} ,
2019-05-05 06:03:40 +02:00
getStockForecast : function ( symbol ) {
updateDynamicRam ( "getStockForecast" , getRamCost ( "getStockForecast" ) ) ;
2018-08-24 22:44:48 +02:00
if ( ! Player . has4SDataTixApi ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getStockForecast" , "You don't have 4S Market Data TIX API Access!" ) ;
2018-08-24 22:44:48 +02:00
}
2019-04-29 08:20:27 +02:00
const stock = getStockFromSymbol ( symbol , "getStockForecast" ) ;
2018-08-24 22:44:48 +02:00
var forecast = 50 ;
stock . b ? forecast += stock . otlkMag : forecast -= stock . otlkMag ;
2019-04-13 03:22:46 +02:00
return forecast / 100 ; // Convert from percentage to decimal
2018-08-24 22:44:48 +02:00
} ,
2019-05-05 06:03:40 +02:00
purchase4SMarketData : function ( ) {
updateDynamicRam ( "purchase4SMarketData" , getRamCost ( "purchase4SMarketData" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "purchase4SMarketData" ) ;
2018-11-01 19:18:32 +01:00
2018-11-04 23:39:30 +01:00
if ( Player . has4SData ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketData" , "Already purchased 4S Market Data." ) ;
2018-11-01 19:18:32 +01:00
return true ;
}
2019-02-14 03:08:18 +01:00
if ( Player . money . lt ( getStockMarket4SDataCost ( ) ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketData" , "Not enough money to purchase 4S Market Data." ) ;
2018-11-01 19:18:32 +01:00
return false ;
}
Player . has4SData = true ;
2019-02-14 03:08:18 +01:00
Player . loseMoney ( getStockMarket4SDataCost ( ) ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketData" , "Purchased 4S Market Data" ) ;
2019-05-12 04:20:20 +02:00
displayStockMarketContent ( ) ;
2018-11-01 19:18:32 +01:00
return true ;
} ,
purchase4SMarketDataTixApi : function ( ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "purchase4SMarketDataTixApi" , getRamCost ( "purchase4SMarketDataTixApi" ) ) ;
2019-04-29 08:20:27 +02:00
checkTixApiAccess ( "purchase4SMarketDataTixApi" ) ;
2018-11-01 19:18:32 +01:00
if ( Player . has4SDataTixApi ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketDataTixApi" , "Already purchased 4S Market Data TIX API" ) ;
2018-11-01 19:18:32 +01:00
return true ;
}
2019-02-14 03:08:18 +01:00
if ( Player . money . lt ( getStockMarket4STixApiCost ( ) ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketDataTixApi" , "Not enough money to purchase 4S Market Data TIX API" ) ;
2018-11-01 19:18:32 +01:00
return false ;
}
Player . has4SDataTixApi = true ;
2019-02-14 03:08:18 +01:00
Player . loseMoney ( getStockMarket4STixApiCost ( ) ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchase4SMarketDataTixApi" , "Purchased 4S Market Data TIX API" ) ;
2019-05-12 04:20:20 +02:00
displayStockMarketContent ( ) ;
2018-11-01 19:18:32 +01:00
return true ;
} ,
2018-07-20 04:21:11 +02:00
getPurchasedServerLimit : function ( ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getPurchasedServerLimit" , getRamCost ( "getPurchasedServerLimit" ) ) ;
2018-07-20 04:21:11 +02:00
2019-01-30 07:02:27 +01:00
return getPurchaseServerLimit ( ) ;
2018-07-20 04:21:11 +02:00
} ,
getPurchasedServerMaxRam : function ( ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getPurchasedServerMaxRam" , getRamCost ( "getPurchasedServerMaxRam" ) ) ;
2018-07-20 04:21:11 +02:00
2019-01-30 07:02:27 +01:00
return getPurchaseServerMaxRam ( ) ;
2018-07-20 04:21:11 +02:00
} ,
2018-06-20 18:43:05 +02:00
getPurchasedServerCost : function ( ram ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getPurchasedServerCost" , getRamCost ( "getPurchasedServerCost" ) ) ;
2018-06-20 18:43:05 +02:00
2019-01-30 07:02:27 +01:00
const cost = getPurchaseServerCost ( ram ) ;
if ( cost === Infinity ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "getPurchasedServerCost" , ` Invalid argument: ram=' ${ ram } ' ` ) ;
2018-11-04 23:39:30 +01:00
return Infinity ;
2018-06-20 18:43:05 +02:00
}
return cost ;
} ,
2019-05-05 06:03:40 +02:00
purchaseServer : function ( hostname , ram ) {
updateDynamicRam ( "purchaseServer" , getRamCost ( "purchaseServer" ) ) ;
2017-07-13 18:54:29 +02:00
var hostnameStr = String ( hostname ) ;
2018-03-03 22:05:33 +01:00
hostnameStr = hostnameStr . replace ( /\s+/g , '' ) ;
2017-07-13 18:54:29 +02:00
if ( hostnameStr == "" ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseServer" , ` Invalid argument: hostname=' ${ hostnameStr } ' ` ) ;
2017-07-13 18:54:29 +02:00
return "" ;
}
2017-07-25 03:06:40 +02:00
2019-01-30 07:02:27 +01:00
if ( Player . purchasedServers . length >= getPurchaseServerLimit ( ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseServer" , ` You have reached the maximum limit of ${ getPurchaseServerLimit ( ) } servers. You cannot purchase any more. ` ) ;
2017-07-27 04:56:14 +02:00
return "" ;
}
2019-01-30 07:02:27 +01:00
const cost = getPurchaseServerCost ( ram ) ;
if ( cost === Infinity ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseServer" , ` Invalid argument: ram=' ${ ram } ' ` ) ;
2021-04-20 03:26:51 +02:00
return "" ;
2017-07-13 18:54:29 +02:00
}
2017-07-25 03:06:40 +02:00
2017-07-29 18:25:40 +02:00
if ( Player . money . lt ( cost ) ) {
2021-03-31 06:45:21 +02:00
workerScript . log ( "purchaseServer" , ` Not enough money to purchase server. Need ${ numeralWrapper . formatMoney ( cost ) } ` ) ;
2017-07-13 18:54:29 +02:00
return "" ;
}
2019-05-05 06:03:40 +02:00
var newServ = safetlyCreateUniqueServer ( {
ip : createUniqueRandomIp ( ) ,
2018-05-06 15:23:07 +02:00
hostname : hostnameStr ,
organizationName : "" ,
isConnectedTo : false ,
adminRights : true ,
purchasedByPlayer : true ,
maxRam : ram ,
} ) ;
2017-07-13 18:54:29 +02:00
AddToAllServers ( newServ ) ;
2017-07-25 03:06:40 +02:00
2017-07-13 18:54:29 +02:00
Player . purchasedServers . push ( newServ . ip ) ;
var homeComputer = Player . getHomeComputer ( ) ;
homeComputer . serversOnNetwork . push ( newServ . ip ) ;
newServ . serversOnNetwork . push ( homeComputer . ip ) ;
Player . loseMoney ( cost ) ;
2021-03-31 06:45:21 +02:00
workerScript . log ( "purchaseServer" , ` Purchased new server with hostname ' ${ newServ . hostname } ' for ${ numeralWrapper . formatMoney ( cost ) } ` ) ;
2017-07-13 18:54:29 +02:00
return newServ . hostname ;
2017-06-28 11:47:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
deleteServer : function ( hostname ) {
updateDynamicRam ( "deleteServer" , getRamCost ( "deleteServer" ) ) ;
2017-07-27 04:56:14 +02:00
var hostnameStr = String ( hostname ) ;
hostnameStr = hostnameStr . replace ( /\s\s+/g , '' ) ;
var server = GetServerByHostname ( hostnameStr ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , ` Invalid argument: hostname=' ${ hostnameStr } ' ` ) ;
2017-07-27 04:56:14 +02:00
return false ;
}
2018-05-23 02:09:04 +02:00
if ( ! server . purchasedByPlayer || server . hostname === "home" ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , "Cannot delete non-purchased server." ) ;
2017-07-27 04:56:14 +02:00
return false ;
}
2017-08-03 06:21:15 +02:00
2017-07-27 04:56:14 +02:00
var ip = server . ip ;
2019-04-13 03:22:46 +02:00
// Can't delete server you're currently connected to
2018-05-23 02:09:04 +02:00
if ( server . isConnectedTo ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , "You are currently connected to the server you are trying to delete." ) ;
2018-05-23 02:09:04 +02:00
return false ;
}
2019-04-13 03:22:46 +02:00
// A server cannot delete itself
2018-05-23 02:09:04 +02:00
if ( ip === workerScript . serverIp ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , "Cannot delete the server this script is running on." ) ;
2017-08-13 07:01:33 +02:00
return false ;
2017-08-03 06:21:15 +02:00
}
2019-04-13 03:22:46 +02:00
// Delete all scripts running on server
2017-08-13 07:01:33 +02:00
if ( server . runningScripts . length > 0 ) {
2021-03-11 23:01:28 +01:00
workerScript . log ( "deleteServer" , ` Cannot delete server ' ${ server . hostname } ' because it still has scripts running. ` ) ;
2017-08-13 07:01:33 +02:00
return false ;
}
2017-07-27 04:56:14 +02:00
2019-04-13 03:22:46 +02:00
// Delete from player's purchasedServers array
2017-07-27 04:56:14 +02:00
var found = false ;
for ( var i = 0 ; i < Player . purchasedServers . length ; ++ i ) {
if ( ip == Player . purchasedServers [ i ] ) {
found = true ;
Player . purchasedServers . splice ( i , 1 ) ;
break ;
}
}
if ( ! found ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , ` Could not identify server ${ server . hostname } as a purchased server. This is a bug. Report to dev. ` ) ;
2017-07-27 04:56:14 +02:00
return false ;
}
2019-04-13 03:22:46 +02:00
// Delete from all servers
2017-08-13 07:01:33 +02:00
delete AllServers [ ip ] ;
2019-04-13 03:22:46 +02:00
// Delete from home computer
2017-07-27 04:56:14 +02:00
found = false ;
var homeComputer = Player . getHomeComputer ( ) ;
for ( var i = 0 ; i < homeComputer . serversOnNetwork . length ; ++ i ) {
if ( ip == homeComputer . serversOnNetwork [ i ] ) {
homeComputer . serversOnNetwork . splice ( i , 1 ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , ` Deleted server ' ${ hostnameStr } ` ) ;
2017-07-27 04:56:14 +02:00
return true ;
}
}
2019-04-13 03:22:46 +02:00
// Wasn't found on home computer
2021-03-11 09:02:05 +01:00
workerScript . log ( "deleteServer" , ` Could not find server ${ server . hostname } as a purchased server. This is a bug. Report to dev. ` ) ;
2017-07-27 04:56:14 +02:00
return false ;
} ,
2019-05-05 06:03:40 +02:00
getPurchasedServers : function ( hostname = true ) {
updateDynamicRam ( "getPurchasedServers" , getRamCost ( "getPurchasedServers" ) ) ;
2017-09-26 04:44:33 +02:00
var res = [ ] ;
Player . purchasedServers . forEach ( function ( ip ) {
if ( hostname ) {
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getPurchasedServers" , "Could not find server. This is a bug. Report to dev." ) ;
2017-09-26 04:44:33 +02:00
}
res . push ( server . hostname ) ;
} else {
res . push ( ip ) ;
}
} ) ;
return res ;
} ,
2019-05-05 06:03:40 +02:00
write : function ( port , data = "" , mode = "a" ) {
updateDynamicRam ( "write" , getRamCost ( "write" ) ) ;
2019-04-13 03:22:46 +02:00
if ( ! isNaN ( port ) ) { // Write to port
// Port 1-10
2018-03-03 22:05:33 +01:00
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , ` Trying to write to invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ) ;
2017-07-22 00:54:55 +02:00
}
2018-03-03 22:05:33 +01:00
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2017-07-22 00:54:55 +02:00
}
2018-03-03 22:05:33 +01:00
return port . write ( data ) ;
2019-04-13 03:22:46 +02:00
} else if ( isString ( port ) ) { // Write to script or text file
2019-05-12 04:20:20 +02:00
const fn = port ;
if ( ! isValidFilePath ( fn ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , ` Invalid filepath: ${ fn } ` ) ;
2019-05-12 04:20:20 +02:00
}
2019-07-05 09:26:22 +02:00
// Coerce 'data' to be a string
try {
data = String ( data ) ;
} catch ( e ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , ` Invalid data ( ${ e } ). Data being written must be convertible to a string ` ) ;
2019-07-05 09:26:22 +02:00
}
2019-05-12 04:20:20 +02:00
const server = workerScript . getServer ( ) ;
2017-10-26 00:05:12 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , "Error getting Server. This is a bug. Report to dev." ) ;
2017-10-12 04:00:22 +02:00
}
2018-09-14 23:03:31 +02:00
if ( isScriptFilename ( fn ) ) {
2019-04-13 03:22:46 +02:00
// Write to script
2018-09-14 23:03:31 +02:00
let script = workerScript . getScriptOnServer ( fn ) ;
if ( script == null ) {
2019-04-13 03:22:46 +02:00
// Create a new script
2019-05-07 03:01:06 +02:00
script = new Script ( fn , data , server . ip , server . scripts ) ;
2018-09-14 23:03:31 +02:00
server . scripts . push ( script ) ;
return true ;
}
mode === "w" ? script . code = data : script . code += data ;
2019-05-07 03:01:06 +02:00
script . updateRamUsage ( server . scripts ) ;
2019-06-02 21:32:35 +02:00
script . markUpdated ( ) ;
2017-10-12 04:00:22 +02:00
} else {
2019-04-13 03:22:46 +02:00
// Write to text file
2018-09-14 23:03:31 +02:00
let txtFile = getTextFile ( fn , server ) ;
if ( txtFile == null ) {
txtFile = createTextFile ( fn , data , server ) ;
return true ;
}
if ( mode === "w" ) {
txtFile . write ( data ) ;
} else {
txtFile . append ( data ) ;
}
2017-10-12 04:00:22 +02:00
}
return true ;
2017-07-22 00:54:55 +02:00
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "write" , ` Invalid argument: ${ port } ` ) ;
2017-07-22 00:54:55 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
tryWrite : function ( port , data = "" ) {
updateDynamicRam ( "tryWrite" , getRamCost ( "tryWrite" ) ) ;
2018-08-30 22:27:47 +02:00
if ( ! isNaN ( port ) ) {
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "tryWrite" , ` Invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ) ;
2018-08-30 22:27:47 +02:00
}
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "tryWrite" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2018-08-30 22:27:47 +02:00
}
return port . tryWrite ( data ) ;
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "tryWrite" , ` Invalid argument: ${ port } ` ) ;
2018-08-30 22:27:47 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
read : function ( port ) {
updateDynamicRam ( "read" , getRamCost ( "read" ) ) ;
2019-04-13 03:22:46 +02:00
if ( ! isNaN ( port ) ) { // Read from port
// Port 1-10
2018-03-03 22:05:33 +01:00
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "read" , ` Invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ) ;
2017-07-22 00:54:55 +02:00
}
2018-03-03 22:05:33 +01:00
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "read" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2017-07-22 00:54:55 +02:00
}
2018-03-03 22:05:33 +01:00
return port . read ( ) ;
2019-04-13 03:22:46 +02:00
} else if ( isString ( port ) ) { // Read from script or text file
2018-09-14 23:03:31 +02:00
let fn = port ;
let server = getServer ( workerScript . serverIp ) ;
2017-10-26 00:05:12 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "read" , "Error getting Server. This is a bug. Report to dev." ) ;
2017-10-12 04:00:22 +02:00
}
2018-09-14 23:03:31 +02:00
if ( isScriptFilename ( fn ) ) {
2019-04-13 03:22:46 +02:00
// Read from script
2018-09-14 23:03:31 +02:00
let script = workerScript . getScriptOnServer ( fn ) ;
if ( script == null ) {
return "" ;
}
return script . code ;
2017-10-12 04:00:22 +02:00
} else {
2019-04-13 03:22:46 +02:00
// Read from text file
2018-09-14 23:03:31 +02:00
let txtFile = getTextFile ( fn , server ) ;
if ( txtFile !== null ) {
return txtFile . text ;
} else {
return "" ;
}
2017-10-12 04:00:22 +02:00
}
2017-07-22 00:54:55 +02:00
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "read" , ` Invalid argument: ${ port } ` ) ;
2017-12-07 04:45:47 +01:00
}
} ,
2019-05-05 06:03:40 +02:00
peek : function ( port ) {
updateDynamicRam ( "peek" , getRamCost ( "peek" ) ) ;
2018-03-03 22:05:33 +01:00
if ( isNaN ( port ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "peek" , ` Invalid argument. Must be a port number between 1 and ${ CONSTANTS . NumNetscriptPorts } , is ${ port } ` ) ;
2018-02-24 23:55:06 +01:00
}
2018-03-03 22:05:33 +01:00
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "peek" , ` Invalid argument. Must be a port number between 1 and ${ CONSTANTS . NumNetscriptPorts } , is ${ port } ` ) ;
2018-02-24 23:55:06 +01:00
}
2018-03-03 22:05:33 +01:00
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "peek" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2018-02-24 23:55:06 +01:00
}
2018-03-03 22:05:33 +01:00
return port . peek ( ) ;
2018-02-24 23:55:06 +01:00
} ,
2019-05-05 06:03:40 +02:00
clear : function ( port ) {
updateDynamicRam ( "clear" , getRamCost ( "clear" ) ) ;
2019-04-13 03:22:46 +02:00
if ( ! isNaN ( port ) ) { // Clear port
2018-03-03 22:05:33 +01:00
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "clear" , ` Trying to clear invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid ` ) ;
2017-12-07 04:45:47 +01:00
}
2018-03-03 22:05:33 +01:00
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "clear" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2017-12-07 04:45:47 +01:00
}
2018-03-03 22:05:33 +01:00
return port . clear ( ) ;
2019-04-13 03:22:46 +02:00
} else if ( isString ( port ) ) { // Clear text file
2017-12-07 04:45:47 +01:00
var fn = port ;
var server = getServer ( workerScript . serverIp ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "clear" , "Error getting Server. This is a bug. Report to dev." ) ;
2017-12-07 04:45:47 +01:00
}
var txtFile = getTextFile ( fn , server ) ;
if ( txtFile != null ) {
txtFile . write ( "" ) ;
}
} else {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "clear" , ` Invalid argument: ${ port } ` ) ;
2017-07-22 00:54:55 +02:00
}
2017-12-07 04:45:47 +01:00
return 0 ;
2017-08-13 07:01:33 +02:00
} ,
2019-05-05 06:03:40 +02:00
getPortHandle : function ( port ) {
updateDynamicRam ( "getPortHandle" , getRamCost ( "getPortHandle" ) ) ;
2018-03-03 22:05:33 +01:00
if ( isNaN ( port ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getPortHandle" , ` Invalid port: ${ port } Must be an integer between 1 and ${ CONSTANTS . NumNetscriptPorts } . ` ) ;
2018-03-03 22:05:33 +01:00
}
port = Math . round ( port ) ;
if ( port < 1 || port > CONSTANTS . NumNetscriptPorts ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getPortHandle" , ` Invalid port: ${ port } . Only ports 1- ${ CONSTANTS . NumNetscriptPorts } are valid. ` ) ;
2018-03-03 22:05:33 +01:00
}
var port = NetscriptPorts [ port - 1 ] ;
if ( port == null || ! ( port instanceof NetscriptPort ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getPortHandle" , ` Could not find port: ${ port } . This is a bug. Report to dev. ` ) ;
2018-03-03 22:05:33 +01:00
}
return port ;
} ,
2019-05-05 06:03:40 +02:00
rm : function ( fn , ip ) {
updateDynamicRam ( "rm" , getRamCost ( "rm" ) ) ;
2018-11-14 07:05:51 +01:00
if ( ip == null || ip === "" ) {
ip = workerScript . serverIp ;
}
2019-04-10 08:07:12 +02:00
const s = safeGetServer ( ip , "rm" ) ;
2018-01-27 07:52:39 +01:00
2019-04-10 08:07:12 +02:00
const status = s . removeFile ( fn ) ;
if ( ! status . res ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "rm" , status . msg ) ;
2018-01-27 07:52:39 +01:00
}
2019-04-10 08:07:12 +02:00
return status . res ;
2018-01-27 07:52:39 +01:00
} ,
2019-05-05 06:03:40 +02:00
scriptRunning : function ( scriptname , ip ) {
updateDynamicRam ( "scriptRunning" , getRamCost ( "scriptRunning" ) ) ;
2017-08-13 07:01:33 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scriptRunning" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-08-13 07:01:33 +02:00
}
for ( var i = 0 ; i < server . runningScripts . length ; ++ i ) {
if ( server . runningScripts [ i ] . filename == scriptname ) {
return true ;
}
}
return false ;
} ,
2019-05-05 06:03:40 +02:00
scriptKill : function ( scriptname , ip ) {
updateDynamicRam ( "scriptKill" , getRamCost ( "scriptKill" ) ) ;
2017-08-13 07:01:33 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "scriptKill" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-08-13 07:01:33 +02:00
}
var suc = false ;
for ( var i = 0 ; i < server . runningScripts . length ; ++ i ) {
if ( server . runningScripts [ i ] . filename == scriptname ) {
killWorkerScript ( server . runningScripts [ i ] , server . ip ) ;
suc = true ;
}
}
return suc ;
} ,
2019-05-05 06:03:40 +02:00
getScriptName : function ( ) {
2018-05-06 09:27:16 +02:00
return workerScript . name ;
} ,
2019-05-05 06:03:40 +02:00
getScriptRam : function ( scriptname , ip = workerScript . serverIp ) {
updateDynamicRam ( "getScriptRam" , getRamCost ( "getScriptRam" ) ) ;
2017-08-13 07:01:33 +02:00
var server = getServer ( ip ) ;
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getScriptRam" , ` Invalid IP/hostname: ${ ip } ` ) ;
2017-08-13 07:01:33 +02:00
}
2017-08-18 19:20:51 +02:00
for ( var i = 0 ; i < server . scripts . length ; ++ i ) {
if ( server . scripts [ i ] . filename == scriptname ) {
return server . scripts [ i ] . ramUsage ;
2017-08-13 07:01:33 +02:00
}
}
return 0 ;
} ,
2021-04-29 02:07:26 +02:00
getRunningScript : function ( fn , ip ) {
updateDynamicRam ( "getRunningScript" , getRamCost ( "getRunningScript" ) ) ;
let runningScript ;
if ( arguments . length === 0 ) {
runningScript = workerScript . scriptRef ;
} else if ( typeof fn === 'number' ) {
runningScript = getRunningScriptByPid ( fn , 'getRunningScript' ) ;
} else {
const scriptArgs = [ ] ;
for ( var i = 2 ; i < arguments . length ; ++ i ) {
scriptArgs . push ( arguments [ i ] ) ;
}
runningScript = getRunningScript ( fn , ip , 'getRunningScript' , scriptArgs ) ;
}
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-05-01 09:17:31 +02:00
getHackTime : function ( ip ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getHackTime" , getRamCost ( "getHackTime" ) ) ;
2019-05-14 10:35:37 +02:00
const server = safeGetServer ( ip , "getHackTime" ) ;
if ( failOnHacknetServer ( server , "getHackTime" ) ) { return Infinity ; }
2021-03-31 06:45:21 +02:00
return calculateHackingTime ( server , Player ) ; // Returns seconds
2017-08-20 03:36:19 +02:00
} ,
2021-05-01 09:17:31 +02:00
getGrowTime : function ( ip ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getGrowTime" , getRamCost ( "getGrowTime" ) ) ;
2019-05-14 10:35:37 +02:00
const server = safeGetServer ( ip , "getGrowTime" ) ;
if ( failOnHacknetServer ( server , "getGrowTime" ) ) { return Infinity ; }
2021-03-31 06:45:21 +02:00
return calculateGrowTime ( server , Player ) ; // Returns seconds
2017-08-20 03:36:19 +02:00
} ,
2021-05-01 09:17:31 +02:00
getWeakenTime : function ( ip ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getWeakenTime" , getRamCost ( "getWeakenTime" ) ) ;
2019-05-14 10:35:37 +02:00
const server = safeGetServer ( ip , "getWeakenTime" ) ;
if ( failOnHacknetServer ( server , "getWeakenTime" ) ) { return Infinity ; }
2021-03-31 06:45:21 +02:00
return calculateWeakenTime ( server , Player ) ; // Returns seconds
2017-08-21 18:59:06 +02:00
} ,
2019-05-05 06:03:40 +02:00
getScriptIncome : function ( scriptname , ip ) {
updateDynamicRam ( "getScriptIncome" , getRamCost ( "getScriptIncome" ) ) ;
2017-09-12 01:14:51 +02:00
if ( arguments . length === 0 ) {
2017-09-27 17:13:42 +02:00
var res = [ ] ;
2019-05-14 13:23:55 +02:00
// First element is total income of all currently running scripts
let total = 0 ;
2019-06-19 10:51:25 +02:00
for ( const script of workerScripts . values ( ) ) {
2019-05-14 13:23:55 +02:00
total += ( script . scriptRef . onlineMoneyMade / script . scriptRef . onlineRunningTime ) ;
}
res . push ( total ) ;
// Second element is total income you've earned from scripts since you installed Augs
res . push ( Player . scriptProdSinceLastAug / ( Player . playtimeSinceLastAug / 1000 ) ) ;
2017-09-27 17:13:42 +02:00
return res ;
2017-09-12 01:14:51 +02:00
} else {
2019-04-13 03:22:46 +02:00
// Get income for a particular script
2017-09-12 01:14:51 +02:00
var server = getServer ( ip ) ;
2017-10-26 00:05:12 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getScriptIncome" , ` Invalid IP/hostnamed: ${ ip } ` ) ;
2017-09-12 01:14:51 +02:00
}
var argsForScript = [ ] ;
for ( var i = 2 ; i < arguments . length ; ++ i ) {
argsForScript . push ( arguments [ i ] ) ;
}
var runningScriptObj = findRunningScript ( scriptname , argsForScript , server ) ;
if ( runningScriptObj == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "getScriptIncome" , ` No such script ' ${ scriptname } ' on ' ${ server . hostname } ' with args: ${ arrayToString ( argsForScript ) } ` ) ;
2017-09-12 01:14:51 +02:00
return - 1 ;
}
return runningScriptObj . onlineMoneyMade / runningScriptObj . onlineRunningTime ;
}
} ,
2019-05-05 06:03:40 +02:00
getScriptExpGain : function ( scriptname , ip ) {
updateDynamicRam ( "getScriptExpGain" , getRamCost ( "getScriptExpGain" ) ) ;
2017-09-15 16:06:59 +02:00
if ( arguments . length === 0 ) {
var total = 0 ;
2019-06-19 10:51:25 +02:00
for ( const ws of workerScripts . values ( ) ) {
total += ( ws . scriptRef . onlineExpGained / ws . scriptRef . onlineRunningTime ) ;
2017-09-15 16:06:59 +02:00
}
return total ;
} else {
2019-04-13 03:22:46 +02:00
// Get income for a particular script
2017-09-15 16:06:59 +02:00
var server = getServer ( ip ) ;
2017-10-26 00:05:12 +02:00
if ( server == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getScriptExpGain" , ` Invalid IP/hostnamed: ${ ip } ` ) ;
2017-09-15 16:06:59 +02:00
}
var argsForScript = [ ] ;
for ( var i = 2 ; i < arguments . length ; ++ i ) {
argsForScript . push ( arguments [ i ] ) ;
}
var runningScriptObj = findRunningScript ( scriptname , argsForScript , server ) ;
if ( runningScriptObj == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "getScriptExpGain" , ` No such script ' ${ scriptname } ' on ' ${ server . hostname } ' with args: ${ arrayToString ( argsForScript ) } ` ) ;
2017-09-15 16:06:59 +02:00
return - 1 ;
}
return runningScriptObj . onlineExpGained / runningScriptObj . onlineRunningTime ;
}
} ,
2019-05-05 06:03:40 +02:00
nFormat : function ( n , format ) {
2019-02-09 03:46:30 +01:00
if ( isNaN ( n ) || isNaN ( parseFloat ( n ) ) || typeof format !== "string" ) {
return "" ;
}
return numeralWrapper . format ( parseFloat ( n ) , format ) ;
} ,
2021-03-12 20:32:07 +01:00
tFormat : function ( milliseconds ) {
return convertTimeMsToTimeElapsedString ( milliseconds ) ;
} ,
2019-05-05 06:03:40 +02:00
getTimeSinceLastAug : function ( ) {
updateDynamicRam ( "getTimeSinceLastAug" , getRamCost ( "getTimeSinceLastAug" ) ) ;
2017-09-27 17:13:42 +02:00
return Player . playtimeSinceLastAug ;
} ,
2017-11-03 04:11:14 +01:00
prompt : function ( txt ) {
2021-04-29 02:07:26 +02:00
if ( ! isString ( txt ) ) { txt = JSON . stringify ( txt ) ; }
2019-02-09 03:46:30 +01:00
// The id for this popup will consist of the first 20 characters of the prompt string..
// Thats hopefully good enough to be unique
const popupId = ` prompt-popup- ${ txt . slice ( 0 , 20 ) } ` ;
const textElement = createElement ( "p" , { innerHTML : txt } ) ;
2021-05-01 09:17:31 +02:00
return new Promise ( function ( resolve ) {
2019-02-09 03:46:30 +01:00
const yesBtn = createElement ( "button" , {
class : "popup-box-button" ,
innerText : "Yes" ,
clickListener : ( ) => {
removeElementById ( popupId ) ;
resolve ( true ) ;
} ,
2017-11-02 22:47:09 +01:00
} ) ;
2019-02-09 03:46:30 +01:00
const noBtn = createElement ( "button" , {
class : "popup-box-button" ,
innerText : "No" ,
clickListener : ( ) => {
removeElementById ( popupId ) ;
resolve ( false ) ;
} ,
2017-11-02 22:47:09 +01:00
} ) ;
2019-02-09 03:46:30 +01:00
createPopup ( popupId , [ textElement , yesBtn , noBtn ] ) ;
2017-11-02 22:47:09 +01:00
} ) ;
2017-11-01 23:56:30 +01:00
} ,
2019-05-05 06:03:40 +02:00
wget : async function ( url , target , ip = workerScript . serverIp ) {
2018-09-27 23:49:23 +02:00
if ( ! isScriptFilename ( target ) && ! target . endsWith ( ".txt" ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "wget" , ` Invalid target file: ' ${ target } '. Must be a script or text file. ` ) ;
2018-09-28 06:45:13 +02:00
return Promise . resolve ( false ) ;
2018-09-27 23:49:23 +02:00
}
var s = safeGetServer ( ip , "wget" ) ;
2021-05-01 09:17:31 +02:00
return new Promise ( function ( resolve ) {
2018-09-28 06:19:52 +02:00
$ . get ( url , function ( data ) {
let res ;
if ( isScriptFilename ( target ) ) {
res = s . writeToScriptFile ( target , data ) ;
} else {
res = s . writeToTextFile ( target , data ) ;
}
if ( ! res . success ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "wget" , "Failed." ) ;
2018-09-28 06:19:52 +02:00
return resolve ( false ) ;
}
if ( res . overwritten ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "wget" , ` Successfully retrieved content and overwrote ' ${ target } ' on ' ${ ip } ' ` ) ;
2018-09-28 06:19:52 +02:00
return resolve ( true ) ;
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "wget" , ` Successfully retrieved content to new file ' ${ target } ' on ' ${ ip } ' ` ) ;
2018-09-28 06:19:52 +02:00
return resolve ( true ) ;
} , 'text' ) . fail ( function ( e ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "wget" , JSON . stringify ( e ) ) ;
2018-09-28 06:19:52 +02:00
return resolve ( false )
} ) ;
2018-09-27 23:49:23 +02:00
} ) ;
} ,
2018-07-28 03:46:37 +02:00
getFavorToDonate : function ( ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getFavorToDonate" , getRamCost ( "getFavorToDonate" ) ) ;
2018-07-28 03:46:37 +02:00
return Math . floor ( CONSTANTS . BaseFavorToDonate * BitNodeMultipliers . RepToDonateToFaction ) ;
} ,
2017-08-21 18:59:06 +02:00
/* Singularity Functions */
2019-05-05 06:03:40 +02:00
universityCourse : function ( universityName , className ) {
updateDynamicRam ( "universityCourse" , getRamCost ( "universityCourse" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "universityCourse" , 1 ) ;
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , "You are in the middle of a mission." ) ;
2017-11-01 23:56:30 +01:00
return ;
}
2017-08-21 18:59:06 +02:00
if ( Player . isWorking ) {
var txt = Player . singularityStopWork ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , txt ) ;
2017-08-21 18:59:06 +02:00
}
2017-08-30 19:44:29 +02:00
2017-08-22 20:48:04 +02:00
var costMult , expMult ;
switch ( universityName . toLowerCase ( ) ) {
2019-04-01 11:23:25 +02:00
case LocationName . AevumSummitUniversity . toLowerCase ( ) :
if ( Player . city != CityName . Aevum ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , "You cannot study at 'Summit University' because you are not in 'Aevum'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . gotoLocation ( LocationName . AevumSummitUniversity ) ;
2017-08-22 20:48:04 +02:00
costMult = 4 ;
expMult = 3 ;
break ;
2019-04-01 11:23:25 +02:00
case LocationName . Sector12RothmanUniversity . toLowerCase ( ) :
if ( Player . city != CityName . Sector12 ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , "You cannot study at 'Rothman University' because you are not in 'Sector-12'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . Sector12RothmanUniversity ;
2017-08-22 20:48:04 +02:00
costMult = 3 ;
expMult = 2 ;
break ;
2019-04-01 11:23:25 +02:00
case LocationName . VolhavenZBInstituteOfTechnology . toLowerCase ( ) :
if ( Player . city != CityName . Volhaven ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , "You cannot study at 'ZB Institute of Technology' because you are not in 'Volhaven'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . VolhavenZBInstituteOfTechnology ;
2017-08-22 20:48:04 +02:00
costMult = 5 ;
expMult = 4 ;
break ;
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , ` Invalid university name: ' ${ universityName } '. ` ) ;
2017-08-22 20:48:04 +02:00
return false ;
}
2017-08-30 19:44:29 +02:00
var task ;
2017-08-22 20:48:04 +02:00
switch ( className . toLowerCase ( ) ) {
case "Study Computer Science" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassStudyComputerScience ;
2017-08-22 20:48:04 +02:00
break ;
case "Data Structures" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassDataStructures ;
2017-08-22 20:48:04 +02:00
break ;
case "Networks" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassNetworks ;
2017-08-22 20:48:04 +02:00
break ;
case "Algorithms" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassAlgorithms ;
2017-08-22 20:48:04 +02:00
break ;
case "Management" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassManagement ;
2017-08-22 20:48:04 +02:00
break ;
case "Leadership" . toLowerCase ( ) :
2017-08-30 19:44:29 +02:00
task = CONSTANTS . ClassLeadership ;
2017-08-22 20:48:04 +02:00
break ;
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , ` Invalid class name: ${ className } . ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
Player . startClass ( costMult , expMult , task ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "universityCourse" , ` Started ${ task } at ${ universityName } ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
gymWorkout : function ( gymName , stat ) {
updateDynamicRam ( "gymWorkout" , getRamCost ( "gymWorkout" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "gymWorkout" , 1 ) ;
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You are in the middle of a mission." ) ;
2017-11-01 23:56:30 +01:00
return ;
}
2017-08-30 19:44:29 +02:00
if ( Player . isWorking ) {
var txt = Player . singularityStopWork ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , txt ) ;
2017-08-30 19:44:29 +02:00
}
var costMult , expMult ;
switch ( gymName . toLowerCase ( ) ) {
2019-04-01 11:23:25 +02:00
case LocationName . AevumCrushFitnessGym . toLowerCase ( ) :
if ( Player . city != CityName . Aevum ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You cannot workout at 'Crush Fitness' because you are not in 'Aevum'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . AevumCrushFitnessGym ;
2018-05-23 02:09:04 +02:00
costMult = 3 ;
expMult = 2 ;
2017-08-30 19:44:29 +02:00
break ;
2019-04-01 11:23:25 +02:00
case LocationName . AevumSnapFitnessGym . toLowerCase ( ) :
if ( Player . city != CityName . Aevum ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You cannot workout at 'Snap Fitness' because you are not in 'Aevum'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . AevumSnapFitnessGym ;
2018-05-23 02:09:04 +02:00
costMult = 10 ;
expMult = 5 ;
2017-08-30 19:44:29 +02:00
break ;
2019-04-01 11:23:25 +02:00
case LocationName . Sector12IronGym . toLowerCase ( ) :
if ( Player . city != CityName . Sector12 ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You cannot workout at 'Iron Gym' because you are not in 'Sector-12'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . Sector12IronGym ;
2017-08-30 19:44:29 +02:00
costMult = 1 ;
expMult = 1 ;
break ;
2019-04-01 11:23:25 +02:00
case LocationName . Sector12PowerhouseGym . toLowerCase ( ) :
if ( Player . city != CityName . Sector12 ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You cannot workout at 'Powerhouse Gym' because you are not in 'Sector-12'." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . Sector12PowerhouseGym ;
2018-05-23 02:09:04 +02:00
costMult = 20 ;
expMult = 10 ;
2017-08-30 19:44:29 +02:00
break ;
2019-04-01 11:23:25 +02:00
case LocationName . VolhavenMilleniumFitnessGym . toLowerCase ( ) :
if ( Player . city != CityName . Volhaven ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , "You cannot workout at 'Millenium Fitness Gym' because you are not in 'Volhaven'." ) ;
2017-08-30 20:36:59 +02:00
return false ;
}
2019-04-01 11:23:25 +02:00
Player . location = LocationName . VolhavenMilleniumFitnessGym ;
2018-05-23 02:09:04 +02:00
costMult = 7 ;
expMult = 4 ;
2017-08-30 20:36:59 +02:00
break ;
2017-08-30 19:44:29 +02:00
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , ` Invalid gym name: ${ gymName } . gymWorkout() failed ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
switch ( stat . toLowerCase ( ) ) {
case "strength" . toLowerCase ( ) :
case "str" . toLowerCase ( ) :
Player . startClass ( costMult , expMult , CONSTANTS . ClassGymStrength ) ;
break ;
case "defense" . toLowerCase ( ) :
case "def" . toLowerCase ( ) :
Player . startClass ( costMult , expMult , CONSTANTS . ClassGymDefense ) ;
break ;
case "dexterity" . toLowerCase ( ) :
case "dex" . toLowerCase ( ) :
Player . startClass ( costMult , expMult , CONSTANTS . ClassGymDexterity ) ;
break ;
case "agility" . toLowerCase ( ) :
case "agi" . toLowerCase ( ) :
Player . startClass ( costMult , expMult , CONSTANTS . ClassGymAgility ) ;
break ;
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , ` Invalid stat: ${ stat } . ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "gymWorkout" , ` Started training ${ stat } at ${ gymName } ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
travelToCity : function ( cityname ) {
updateDynamicRam ( "travelToCity" , getRamCost ( "travelToCity" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "travelToCity" , 1 ) ;
2017-08-30 19:44:29 +02:00
switch ( cityname ) {
2019-04-01 11:23:25 +02:00
case CityName . Aevum :
case CityName . Chongqing :
case CityName . Sector12 :
case CityName . NewTokyo :
case CityName . Ishima :
case CityName . Volhaven :
2018-06-04 03:09:12 +02:00
if ( Player . money . lt ( CONSTANTS . TravelCost ) ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "travelToCity" , "Not enough money to travel." ) ;
2018-06-04 03:09:12 +02:00
}
Player . loseMoney ( CONSTANTS . TravelCost ) ;
2017-08-30 19:44:29 +02:00
Player . city = cityname ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "travelToCity" , ` Traveled to ${ cityname } ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "travelToCity" , ` Invalid city name: ' ${ cityname } '. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
} ,
2019-05-05 06:03:40 +02:00
purchaseTor : function ( ) {
updateDynamicRam ( "purchaseTor" , getRamCost ( "purchaseTor" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "purchaseTor" , 1 ) ;
2017-08-30 19:44:29 +02:00
if ( SpecialServerIps [ "Darkweb Server" ] != null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseTor" , "You already have a TOR router!" ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
if ( Player . money . lt ( CONSTANTS . TorRouterCost ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseTor" , "You cannot afford to purchase a Tor router." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
Player . loseMoney ( CONSTANTS . TorRouterCost ) ;
2019-05-05 06:03:40 +02:00
var darkweb = safetlyCreateUniqueServer ( {
ip : createUniqueRandomIp ( ) , hostname : "darkweb" , organizationName : "" ,
2021-04-30 05:52:56 +02:00
isConnectedTo : false , adminRights : false , purchasedByPlayer : false , maxRam : 1 ,
2018-05-06 23:20:00 +02:00
} ) ;
2017-08-30 19:44:29 +02:00
AddToAllServers ( darkweb ) ;
SpecialServerIps . addIp ( "Darkweb Server" , darkweb . ip ) ;
Player . getHomeComputer ( ) . serversOnNetwork . push ( darkweb . ip ) ;
darkweb . serversOnNetwork . push ( Player . getHomeComputer ( ) . ip ) ;
2017-09-19 20:38:03 +02:00
Player . gainIntelligenceExp ( CONSTANTS . IntelligenceSingFnBaseExpGain ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseTor" , "You have purchased a Tor router!" ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
purchaseProgram : function ( programName ) {
updateDynamicRam ( "purchaseProgram" , getRamCost ( "purchaseProgram" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "purchaseProgram" , 1 ) ;
2017-08-30 19:44:29 +02:00
if ( SpecialServerIps [ "Darkweb Server" ] == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseProgram" , "You do not have the TOR router." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2018-06-01 23:05:05 +02:00
programName = programName . toLowerCase ( ) ;
let item = null ;
for ( const key in DarkWebItems ) {
const i = DarkWebItems [ key ] ;
if ( i . program . toLowerCase ( ) == programName ) {
item = i ;
}
}
if ( item == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseProgram" , ` Invalid program name: ' ${ programName } . ` ) ;
2018-06-01 23:05:05 +02:00
return false ;
}
if ( Player . money . lt ( item . price ) ) {
2021-03-31 06:45:21 +02:00
workerScript . log ( "purchaseProgram" , ` Not enough money to purchase ' ${ item . program } '. Need ${ numeralWrapper . formatMoney ( item . price ) } ` ) ;
2018-06-01 23:05:05 +02:00
return false ;
}
if ( Player . hasProgram ( item . program ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseProgram" , ` You already have the ' ${ item . program } ' program ` ) ;
2018-06-01 23:05:05 +02:00
return true ;
}
Player . loseMoney ( item . price ) ;
Player . getHomeComputer ( ) . programs . push ( item . program ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseProgram" , ` You have purchased the ' ${ item . program } ' program. The new program can be found on your home computer. ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2021-03-31 06:45:21 +02:00
getCurrentServer : function ( ) {
updateDynamicRam ( "getCurrentServer" , getRamCost ( "getCurrentServer" ) ) ;
checkSingularityAccess ( "getCurrentServer" , 1 ) ;
return Player . getCurrentServer ( ) . hostname ;
} ,
2021-03-24 02:30:15 +01:00
connect : function ( hostname ) {
2021-03-31 06:45:21 +02:00
updateDynamicRam ( "connect" , getRamCost ( "connect" ) ) ;
checkSingularityAccess ( "connect" , 1 ) ;
2021-03-24 02:30:15 +01:00
if ( ! hostname ) {
throw makeRuntimeErrorMsg ( "connect" , ` Invalid hostname: ' ${ hostname } ' ` ) ;
}
let target = getServer ( hostname ) ;
if ( target == null ) {
throw makeRuntimeErrorMsg ( "connect" , ` Invalid hostname: ' ${ hostname } ' ` ) ;
return ;
}
if ( hostname === 'home' ) {
Player . getCurrentServer ( ) . isConnectedTo = false ;
Player . currentServer = Player . getHomeComputer ( ) . ip ;
Player . getCurrentServer ( ) . isConnectedTo = true ;
Terminal . currDir = "/" ;
Terminal . resetTerminalInput ( true ) ;
return true ;
}
const server = Player . getCurrentServer ( ) ;
for ( let i = 0 ; i < server . serversOnNetwork . length ; i ++ ) {
const other = getServerOnNetwork ( server , i ) ;
if ( other . ip == hostname || other . hostname == hostname ) {
Player . getCurrentServer ( ) . isConnectedTo = false ;
Player . currentServer = target . ip ;
Player . getCurrentServer ( ) . isConnectedTo = true ;
Terminal . currDir = "/" ;
Terminal . resetTerminalInput ( true ) ;
return true ;
}
}
return false ;
} ,
manualHack : function ( ) {
updateDynamicRam ( "manualHack" , getRamCost ( "manualHack" ) ) ;
checkSingularityAccess ( "manualHack" , 1 ) ;
const server = Player . getCurrentServer ( ) ;
return hack ( server . hostname , true ) ;
2021-03-23 21:02:34 +01:00
} ,
2019-05-05 06:03:40 +02:00
getStats : function ( ) {
updateDynamicRam ( "getStats" , getRamCost ( "getStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getStats" , 1 ) ;
2021-03-31 06:45:21 +02:00
workerScript . log ( "getStats" , ` getStats is deprecated, please use getPlayer ` ) ;
2017-10-12 04:00:22 +02:00
return {
hacking : Player . hacking _skill ,
strength : Player . strength ,
defense : Player . defense ,
dexterity : Player . dexterity ,
agility : Player . agility ,
charisma : Player . charisma ,
2021-04-30 05:52:56 +02:00
intelligence : Player . intelligence ,
2017-10-12 04:00:22 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getCharacterInformation : function ( ) {
updateDynamicRam ( "getCharacterInformation" , getRamCost ( "getCharacterInformation" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCharacterInformation" , 1 ) ;
2021-03-31 06:45:21 +02:00
workerScript . log ( "getCharacterInformation" , ` getCharacterInformation is deprecated, please use getPlayer ` ) ;
2018-03-03 22:05:33 +01:00
return {
bitnode : Player . bitNodeN ,
city : Player . city ,
factions : Player . factions . slice ( ) ,
2019-02-20 09:42:27 +01:00
hp : Player . hp ,
2019-01-15 04:34:04 +01:00
jobs : Object . keys ( Player . jobs ) ,
jobTitles : Object . values ( Player . jobs ) ,
2019-02-20 09:42:27 +01:00
maxHp : Player . max _hp ,
2018-10-26 22:51:45 +02:00
mult : {
2018-10-26 19:37:28 +02:00
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 ,
} ,
2018-03-03 22:05:33 +01:00
timeWorked : Player . timeWorked ,
2018-10-26 19:37:28 +02:00
tor : SpecialServerIps . hasOwnProperty ( "Darkweb Server" ) ,
2018-03-03 22:05:33 +01: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 ,
2021-03-07 11:01:24 +01:00
hackingExp : Player . hacking _exp ,
strengthExp : Player . strength _exp ,
defenseExp : Player . defense _exp ,
dexterityExp : Player . dexterity _exp ,
agilityExp : Player . agility _exp ,
charismaExp : Player . charisma _exp ,
2018-03-03 22:05:33 +01:00
} ;
} ,
2021-03-31 06:45:21 +02:00
getPlayer : function ( ) {
updateDynamicRam ( "getPlayer" , getRamCost ( "getPlayer" ) ) ;
checkSingularityAccess ( "getPlayer" , 1 ) ;
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 ,
money : Player . money . toNumber ( ) ,
city : Player . city ,
location : Player . location ,
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 _success _chance _mult : Player . bladeburner _success _chance _mult ,
bitNodeN : Player . bitNodeN ,
totalPlaytime : Player . totalPlaytime ,
playtimeSinceLastAug : Player . playtimeSinceLastAug ,
playtimeSinceLastBitnode : Player . playtimeSinceLastBitnode ,
jobs : { } ,
2021-04-01 18:55:43 +02:00
factions : Player . factions . slice ( ) ,
tor : SpecialServerIps . hasOwnProperty ( "Darkweb Server" ) ,
2021-03-31 06:45:21 +02:00
} ;
Object . assign ( data . jobs , Player . jobs ) ;
return data ;
} ,
2021-04-06 09:50:09 +02:00
hospitalize : function ( ) {
updateDynamicRam ( "hospitalize" , getRamCost ( "hospitalize" ) ) ;
checkSingularityAccess ( "hospitalize" , 1 ) ;
return Player . hospitalize ( ) ;
} ,
2019-05-05 06:03:40 +02:00
isBusy : function ( ) {
updateDynamicRam ( "isBusy" , getRamCost ( "isBusy" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "isBusy" , 1 ) ;
2019-07-14 07:44:53 +02:00
return Player . isWorking || inMission ;
2017-10-16 04:09:49 +02:00
} ,
2019-05-05 06:03:40 +02:00
stopAction : function ( ) {
updateDynamicRam ( "stopAction" , getRamCost ( "stopAction" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "stopAction" , 1 ) ;
2018-02-15 05:26:43 +01:00
if ( Player . isWorking ) {
var txt = Player . singularityStopWork ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "stopAction" , txt ) ;
2018-02-15 05:26:43 +01:00
return true ;
}
return false ;
} ,
2019-05-05 06:03:40 +02:00
upgradeHomeRam : function ( ) {
updateDynamicRam ( "upgradeHomeRam" , getRamCost ( "upgradeHomeRam" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "upgradeHomeRam" , 2 ) ;
2017-08-30 19:44:29 +02:00
2019-01-09 11:43:59 +01:00
// Check if we're at max RAM
const homeComputer = Player . getHomeComputer ( ) ;
if ( homeComputer . maxRam >= CONSTANTS . HomeComputerMaxRam ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "upgradeHomeRam" , ` Your home computer is at max RAM. ` ) ;
2019-01-09 11:43:59 +01:00
return false ;
}
2017-08-30 19:44:29 +02:00
2019-01-09 11:43:59 +01:00
const cost = Player . getUpgradeHomeRamCost ( ) ;
2017-08-30 19:44:29 +02:00
if ( Player . money . lt ( cost ) ) {
2021-03-31 06:45:21 +02:00
workerScript . log ( "upgradeHomeRam" , ` You don't have enough money. Need ${ numeralWrapper . formatMoney ( cost ) } ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
homeComputer . maxRam *= 2 ;
Player . loseMoney ( cost ) ;
2017-09-19 20:38:03 +02:00
Player . gainIntelligenceExp ( CONSTANTS . IntelligenceSingFnBaseExpGain ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "upgradeHomeRam" , ` Purchased additional RAM for home computer! It now has ${ homeComputer . maxRam } GB of RAM. ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
getUpgradeHomeRamCost : function ( ) {
updateDynamicRam ( "getUpgradeHomeRamCost" , getRamCost ( "getUpgradeHomeRamCost" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getUpgradeHomeRamCost" , 2 ) ;
2017-08-30 19:44:29 +02:00
2018-06-07 20:54:34 +02:00
return Player . getUpgradeHomeRamCost ( ) ;
2017-08-30 19:44:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
workForCompany : function ( companyName ) {
updateDynamicRam ( "workForCompany" , getRamCost ( "workForCompany" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "workForCompany" , 2 ) ;
2017-08-30 19:44:29 +02:00
2019-01-15 04:34:04 +01:00
// Sanitize input
if ( companyName == null ) {
companyName = Player . companyName ;
}
// Make sure its a valid company
if ( companyName == null || companyName === "" || ! ( Companies [ companyName ] instanceof Company ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , ` Invalid company: ' ${ companyName } ' ` ) ;
2019-01-15 04:34:04 +01:00
return false ;
}
// Make sure player is actually employed at the comapny
if ( ! Object . keys ( Player . jobs ) . includes ( companyName ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , ` You do not have a job at ' ${ companyName } ' ` ) ;
2019-01-15 04:34:04 +01:00
return false ;
}
// Cant work while in a mission
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , "You are in the middle of a mission." ) ;
2019-01-15 04:34:04 +01:00
return false ;
2017-11-01 23:56:30 +01:00
}
2019-01-15 04:34:04 +01:00
// Check to make sure company position data is valid
const companyPositionName = Player . jobs [ companyName ] ;
const companyPosition = CompanyPositions [ companyPositionName ] ;
if ( companyPositionName === "" || ! ( companyPosition instanceof CompanyPosition ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , "You do not have a job" ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
if ( Player . isWorking ) {
var txt = Player . singularityStopWork ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , txt ) ;
2017-08-30 19:44:29 +02:00
}
2018-11-16 02:28:23 +01:00
if ( companyPosition . isPartTimeJob ( ) ) {
2019-01-18 18:57:21 +01:00
Player . startWorkPartTime ( companyName ) ;
2017-08-30 19:44:29 +02:00
} else {
2019-01-18 18:57:21 +01:00
Player . startWork ( companyName ) ;
2017-08-30 19:44:29 +02:00
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForCompany" , ` Began working at ' ${ Player . companyName } ' as a ' ${ companyPositionName } ' ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
applyToCompany : function ( companyName , field ) {
updateDynamicRam ( "applyToCompany" , getRamCost ( "applyToCompany" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "applyToCompany" , 2 ) ;
getCompany ( "applyToCompany" , companyName ) ;
2017-08-30 20:36:59 +02:00
2017-08-30 19:44:29 +02:00
Player . location = companyName ;
var res ;
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 ) ;
2017-08-22 20:48:04 +02:00
break ;
2017-08-30 19:44:29 +02:00
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 :
2021-03-11 09:02:05 +01:00
workerScript . log ( "applyToCompany" , ` Invalid job: ' ${ field } '. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
2017-08-22 20:48:04 +02:00
}
2019-04-13 03:22:46 +02:00
// The Player object's applyForJob function can return string with special error messages
2017-08-30 19:44:29 +02:00
if ( isString ( res ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "applyToCompany" , res ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
if ( res ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "applyToCompany" , ` You were offered a new job at ' ${ companyName } ' as a ' ${ Player . jobs [ companyName ] } ' ` ) ;
2017-08-30 19:44:29 +02:00
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "applyToCompany" , ` You failed to get a new job/promotion at ' ${ companyName } ' in the ' ${ field } ' field. ` ) ;
2017-08-30 19:44:29 +02:00
}
return res ;
2017-08-21 18:59:06 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCompanyRep : function ( companyName ) {
updateDynamicRam ( "getCompanyRep" , getRamCost ( "getCompanyRep" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCompanyRep" , 2 ) ;
const company = getCompany ( "getCompanyRep" , companyName ) ;
2017-08-30 19:44:29 +02:00
return company . playerReputation ;
} ,
2019-05-05 06:03:40 +02:00
getCompanyFavor : function ( companyName ) {
updateDynamicRam ( "getCompanyFavor" , getRamCost ( "getCompanyFavor" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCompanyFavor" , 2 ) ;
const company = getCompany ( "getCompanyFavor" , companyName ) ;
2018-03-03 22:05:33 +01:00
return company . favor ;
} ,
2019-05-05 06:03:40 +02:00
getCompanyFavorGain : function ( companyName ) {
updateDynamicRam ( "getCompanyFavorGain" , getRamCost ( "getCompanyFavorGain" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCompanyFavorGain" , 2 ) ;
const company = getCompany ( "getCompanyFavorGain" , companyName ) ;
2018-07-28 03:46:37 +02:00
return company . getFavorGain ( ) [ 0 ] ;
} ,
2019-05-05 06:03:40 +02:00
checkFactionInvitations : function ( ) {
updateDynamicRam ( "checkFactionInvitations" , getRamCost ( "checkFactionInvitations" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "checkFactionInvitations" , 2 ) ;
2019-04-13 03:22:46 +02:00
// Make a copy of Player.factionInvitations
2017-08-30 19:44:29 +02:00
return Player . factionInvitations . slice ( ) ;
} ,
2019-05-05 06:03:40 +02:00
joinFaction : function ( name ) {
updateDynamicRam ( "joinFaction" , getRamCost ( "joinFaction" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "joinFaction" , 2 ) ;
2021-03-31 06:45:21 +02:00
getFaction ( "joinFaction" , name ) ;
2017-08-21 18:59:06 +02:00
2017-08-30 19:44:29 +02:00
if ( ! Player . factionInvitations . includes ( name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "joinFaction" , ` You have not been invited by faction ' ${ name } ' ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
const fac = Factions [ name ] ;
2017-08-30 19:44:29 +02:00
joinFaction ( fac ) ;
2018-08-26 06:26:42 +02:00
2019-04-13 03:22:46 +02:00
// Update Faction Invitation list to account for joined + banned factions
2021-03-11 09:02:05 +01:00
for ( let i = 0 ; i < Player . factionInvitations . length ; ++ i ) {
2018-08-26 06:26:42 +02:00
if ( Player . factionInvitations [ i ] == name || Factions [ Player . factionInvitations [ i ] ] . isBanned ) {
Player . factionInvitations . splice ( i , 1 ) ;
i -- ;
}
}
2017-09-19 20:38:03 +02:00
Player . gainIntelligenceExp ( CONSTANTS . IntelligenceSingFnBaseExpGain ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "joinFaction" , ` Joined the ' ${ name } ' faction. ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
workForFaction : function ( name , type ) {
updateDynamicRam ( "workForFaction" , getRamCost ( "workForFaction" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "workForFaction" , 2 ) ;
getFaction ( "workForFaction" , name ) ;
2017-08-21 18:59:06 +02:00
2018-06-10 06:55:25 +02:00
// if the player is in a gang and the target faction is any of the gang faction, fail
2018-06-14 21:51:06 +02:00
if ( Player . inGang ( ) && AllGangs [ name ] !== undefined ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Faction ' ${ name } ' does not offer work at the moment. ` ) ;
2018-06-10 06:55:25 +02:00
return ;
}
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , "You are in the middle of a mission." ) ;
2017-11-01 23:56:30 +01:00
return ;
}
2017-08-30 20:36:59 +02:00
if ( ! Player . factions . includes ( name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` You are not a member of ' ${ name } ' ` ) ;
2017-08-30 20:36:59 +02:00
return false ;
}
2017-08-30 19:44:29 +02:00
if ( Player . isWorking ) {
2021-03-11 09:02:05 +01:00
const txt = Player . singularityStopWork ( ) ;
workerScript . log ( "workForFaction" , txt ) ;
2017-08-30 19:44:29 +02:00
}
2017-08-21 18:59:06 +02:00
2017-08-30 19:44:29 +02:00
var fac = Factions [ name ] ;
2019-04-13 03:22:46 +02:00
// Arrays listing factions that allow each time of work
2017-08-30 19:44:29 +02:00
var hackAvailable = [ "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" ] ;
var fdWkAvailable = [ "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" ] ;
var scWkAvailable = [ "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" ] ;
2017-08-21 18:59:06 +02:00
2017-08-30 19:44:29 +02:00
switch ( type . toLowerCase ( ) ) {
case "hacking" :
case "hacking contracts" :
case "hackingcontracts" :
if ( ! hackAvailable . includes ( fac . name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Faction ' ${ fac . name } ' do not need help with hacking contracts. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
Player . startFactionHackWork ( fac ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Started carrying out hacking contracts for ' ${ fac . name } ' ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
case "field" :
case "fieldwork" :
case "field work" :
if ( ! fdWkAvailable . includes ( fac . name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Faction ' ${ fac . name } ' do not need help with field missions. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
Player . startFactionFieldWork ( fac ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Started carrying out field missions for ' ${ fac . name } ' ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
case "security" :
case "securitywork" :
case "security work" :
if ( ! scWkAvailable . includes ( fac . name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Faction ' ${ fac . name } ' do not need help with security work. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
Player . startFactionSecurityWork ( fac ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Started carrying out security work for ' ${ fac . name } ' ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
default :
2021-03-11 09:02:05 +01:00
workerScript . log ( "workForFaction" , ` Invalid work type: ' ${ type } ` ) ;
2017-08-30 19:44:29 +02:00
}
2017-08-30 20:36:59 +02:00
return true ;
2017-08-30 19:44:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
getFactionRep : function ( name ) {
updateDynamicRam ( "getFactionRep" , getRamCost ( "getFactionRep" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getFactionRep" , 2 ) ;
const faction = getFaction ( "getFactionRep" , name ) ;
return faction . playerReputation ;
2017-08-30 19:44:29 +02:00
} ,
2019-05-05 06:03:40 +02:00
getFactionFavor : function ( name ) {
updateDynamicRam ( "getFactionFavor" , getRamCost ( "getFactionFavor" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getFactionFavor" , 2 ) ;
const faction = getFaction ( "getFactionFavor" , name ) ;
return faction . favor ;
2018-03-03 22:05:33 +01:00
} ,
2018-09-11 20:56:23 +02:00
getFactionFavorGain : function ( name ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getFactionFavorGain" , getRamCost ( "getFactionFavorGain" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getFactionFavorGain" , 2 ) ;
const faction = getFaction ( "getFactionFavorGain" , name ) ;
return faction . getFavorGain ( ) [ 0 ] ;
2018-07-28 03:46:37 +02:00
} ,
2019-05-05 06:03:40 +02:00
donateToFaction : function ( name , amt ) {
updateDynamicRam ( "donateToFaction" , getRamCost ( "donateToFaction" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "donateToFaction" , 3 ) ;
const faction = getFaction ( "donateToFaction" , name ) ;
2018-09-11 20:56:23 +02:00
if ( typeof amt !== 'number' || amt <= 0 ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "donateToFaction" , ` Invalid donation amount: ' ${ amt } '. ` ) ;
2018-09-11 20:56:23 +02:00
return false ;
}
if ( Player . money . lt ( amt ) ) {
2021-03-31 06:45:21 +02:00
workerScript . log ( "donateToFaction" , ` You do not have enough money to donate ${ numeralWrapper . formatMoney ( amt ) } to ' ${ name } ' ` ) ;
2018-09-11 20:56:23 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
const repNeededToDonate = Math . round ( CONSTANTS . BaseFavorToDonate * BitNodeMultipliers . RepToDonateToFaction ) ;
if ( faction . favor < repNeededToDonate ) {
workerScript . log ( "donateToFaction" , ` You do not have enough favor to donate to this faction. Have ${ faction . favor } , need ${ repNeededToDonate } ` ) ;
2018-09-11 20:56:23 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
const repGain = amt / CONSTANTS . DonateMoneyToRepDivisor * Player . faction _rep _mult ;
faction . playerReputation += repGain ;
2018-09-11 20:56:23 +02:00
Player . loseMoney ( amt ) ;
2021-03-31 06:45:21 +02:00
workerScript . log ( "donateToFaction" , ` ${ numeralWrapper . formatMoney ( amt ) } donated to ' ${ name } ' for ${ numeralWrapper . formatReputation ( repGain ) } reputation ` ) ;
2018-09-11 20:56:23 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
createProgram : function ( name ) {
updateDynamicRam ( "createProgram" , getRamCost ( "createProgram" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "createProgram" , 3 ) ;
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , "You are in the middle of a mission." ) ;
2017-11-01 23:56:30 +01:00
return ;
}
2017-08-30 19:44:29 +02:00
if ( Player . isWorking ) {
var txt = Player . singularityStopWork ( ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , txt ) ;
2017-08-30 19:44:29 +02:00
}
2018-06-02 01:31:34 +02:00
name = name . toLowerCase ( ) ;
let p = null ;
for ( const key in Programs ) {
if ( Programs [ key ] . name . toLowerCase ( ) == name ) {
p = Programs [ key ] ;
}
}
2018-06-26 16:49:28 +02:00
if ( p == null ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , ` The specified program does not exist: ' ${ name } ` ) ;
2018-06-02 01:31:34 +02:00
return false ;
}
2018-06-26 16:49:28 +02:00
if ( Player . hasProgram ( p . name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , ` You already have the ' ${ p . name } ' program ` ) ;
2018-06-02 01:31:34 +02:00
return false ;
}
2019-01-17 06:27:24 +01:00
if ( ! p . create . req ( Player ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , ` Hacking level is too low to create ' ${ p . name } ' (level ${ p . create . level } req) ` ) ;
2018-06-02 01:31:34 +02:00
return false
}
Player . startCreateProgramWork ( p . name , p . create . time , p . create . level ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "createProgram" , ` Began creating program: ' ${ name } ' ` ) ;
2017-08-30 19:44:29 +02:00
return true ;
} ,
2019-05-05 06:03:40 +02:00
commitCrime : function ( crimeRoughName ) {
updateDynamicRam ( "commitCrime" , getRamCost ( "commitCrime" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "commitCrime" , 3 ) ;
2017-11-01 23:56:30 +01:00
if ( inMission ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "commitCrime" , "You are in the middle of a mission." ) ;
2017-11-01 23:56:30 +01:00
return ;
}
2017-10-12 04:00:22 +02:00
if ( Player . isWorking ) {
2021-03-11 09:02:05 +01:00
const txt = Player . singularityStopWork ( ) ;
workerScript . log ( "commitCrime" , txt ) ;
2017-10-12 04:00:22 +02:00
}
2019-04-01 11:23:25 +02:00
// Set Location to slums
Player . gotoLocation ( LocationName . Slums ) ;
2017-10-20 23:59:54 +02:00
2018-05-29 22:36:52 +02:00
const crime = findCrime ( crimeRoughName . toLowerCase ( ) ) ;
if ( crime == null ) { // couldn't find crime
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "commitCrime" , ` Invalid crime: ' ${ crimeRoughName } ' ` ) ;
2018-05-29 22:36:52 +02:00
}
2021-03-11 09:02:05 +01:00
workerScript . log ( "commitCrime" , ` Attempting to commit ${ crime . name } ... ` ) ;
2019-01-15 04:34:04 +01:00
return crime . commit ( Player , 1 , { workerscript : workerScript } ) ;
2017-10-12 04:00:22 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCrimeChance : function ( crimeRoughName ) {
updateDynamicRam ( "getCrimeChance" , getRamCost ( "getCrimeChance" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCrimeChance" , 3 ) ;
2017-10-15 03:55:41 +02:00
2018-05-29 22:36:52 +02:00
const crime = findCrime ( crimeRoughName . toLowerCase ( ) ) ;
if ( crime == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getCrimeChance" , ` Invalid crime: ${ crimeRoughName } ` ) ;
2017-10-15 03:55:41 +02:00
}
2018-05-29 22:36:52 +02:00
2019-02-09 03:02:00 +01:00
return crime . successRate ( Player ) ;
2017-10-15 03:55:41 +02:00
} ,
2021-03-08 04:22:23 +01:00
getCrimeStats : function ( crimeRoughName ) {
updateDynamicRam ( "getCrimeStats" , getRamCost ( "getCrimeStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getCrimeStats" , 3 ) ;
2021-03-08 04:22:23 +01:00
const crime = findCrime ( crimeRoughName . toLowerCase ( ) ) ;
if ( crime == null ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getCrimeStats" , ` Invalid crime: ${ crimeRoughName } ` ) ;
2021-03-08 04:22:23 +01:00
}
return Object . assign ( { } , crime ) ;
} ,
2019-05-05 06:03:40 +02:00
getOwnedAugmentations : function ( purchased = false ) {
updateDynamicRam ( "getOwnedAugmentations" , getRamCost ( "getOwnedAugmentations" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getOwnedAugmentations" , 3 ) ;
2017-10-10 06:56:48 +02:00
var res = [ ] ;
for ( var i = 0 ; i < Player . augmentations . length ; ++ i ) {
res . push ( Player . augmentations [ i ] . name ) ;
}
if ( purchased ) {
for ( var i = 0 ; i < Player . queuedAugmentations . length ; ++ i ) {
res . push ( Player . queuedAugmentations [ i ] . name ) ;
}
}
return res ;
} ,
2019-05-05 06:03:40 +02:00
getOwnedSourceFiles : function ( ) {
updateDynamicRam ( "getOwnedSourceFiles" , getRamCost ( "getOwnedSourceFiles" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getOwnedSourceFiles" , 3 ) ;
2018-07-25 06:31:17 +02:00
let res = [ ] ;
for ( let i = 0 ; i < Player . sourceFiles . length ; ++ i ) {
res . push ( { n : Player . sourceFiles [ i ] . n , lvl : Player . sourceFiles [ i ] . lvl } ) ;
}
return res ;
} ,
2019-05-05 06:03:40 +02:00
getAugmentationsFromFaction : function ( facname ) {
updateDynamicRam ( "getAugmentationsFromFaction" , getRamCost ( "getAugmentationsFromFaction" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getAugmentationsFromFaction" , 3 ) ;
const faction = getFaction ( "getAugmentationsFromFaction" , facname ) ;
2017-10-10 06:56:48 +02:00
2021-03-11 09:02:05 +01:00
// If player has a gang with this faction, return all augmentations.
2019-05-02 00:20:14 +02:00
if ( Player . hasGangWith ( facname ) ) {
const res = [ ] ;
for ( const augName in Augmentations ) {
const aug = Augmentations [ augName ] ;
if ( ! aug . isSpecial ) {
res . push ( augName ) ;
}
}
return res ;
2017-10-10 06:56:48 +02:00
}
2019-05-02 00:20:14 +02:00
2021-03-11 09:02:05 +01:00
return faction . augmentations . slice ( ) ;
2017-10-10 06:56:48 +02:00
} ,
2019-05-05 06:03:40 +02:00
getAugmentationPrereq : function ( name ) {
updateDynamicRam ( "getAugmentationPrereq" , getRamCost ( "getAugmentationPrereq" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getAugmentationPrereq" , 3 ) ;
const aug = getAugmentation ( "getAugmentationPrereq" , name ) ;
2018-12-31 01:36:44 +01:00
return aug . prereqs . slice ( ) ;
2018-12-30 16:28:42 +01:00
} ,
2019-05-05 06:03:40 +02:00
getAugmentationCost : function ( name ) {
updateDynamicRam ( "getAugmentationCost" , getRamCost ( "getAugmentationCost" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getAugmentationCost" , 3 ) ;
const aug = getAugmentation ( "getAugmentationCost" , name ) ;
2017-08-30 19:44:29 +02:00
return [ aug . baseRepRequirement , aug . baseCost ] ;
} ,
2021-03-07 10:57:49 +01:00
getAugmentationStats : function ( name ) {
updateDynamicRam ( "getAugmentationStats" , getRamCost ( "getAugmentationStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "getAugmentationStats" , 3 ) ;
const aug = getAugmentation ( "getAugmentationStats" , name ) ;
2021-03-07 10:57:49 +01:00
return Object . assign ( { } , aug . mults ) ;
} ,
2019-05-05 06:03:40 +02:00
purchaseAugmentation : function ( faction , name ) {
updateDynamicRam ( "purchaseAugmentation" , getRamCost ( "purchaseAugmentation" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "purchaseAugmentation" , 3 ) ;
const fac = getFaction ( "purchaseAugmentation" , faction ) ;
const aug = getAugmentation ( "purchaseAugmentation" , name ) ;
2017-08-30 19:44:29 +02:00
2019-05-02 00:20:14 +02:00
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
}
2019-05-02 00:20:14 +02:00
if ( ! augs . includes ( name ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseAugmentation" , ` Faction ' ${ faction } ' does not have the ' ${ name } ' augmentation. ` ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
const isNeuroflux = ( aug . name === AugmentationNames . NeuroFluxGovernor ) ;
2017-09-08 21:16:48 +02:00
if ( ! isNeuroflux ) {
2019-05-02 00:20:14 +02:00
for ( let j = 0 ; j < Player . queuedAugmentations . length ; ++ j ) {
2017-09-08 21:16:48 +02:00
if ( Player . queuedAugmentations [ j ] . name === aug . name ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseAugmentation" , ` You already have the ' ${ name } ' augmentation. ` ) ;
2017-09-08 21:16:48 +02:00
return false ;
}
}
2019-05-02 00:20:14 +02:00
for ( let j = 0 ; j < Player . augmentations . length ; ++ j ) {
2017-09-08 21:16:48 +02:00
if ( Player . augmentations [ j ] . name === aug . name ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseAugmentation" , ` You already have the ' ${ name } ' augmentation. ` ) ;
2017-09-08 21:16:48 +02:00
return false ;
}
2017-08-30 19:44:29 +02:00
}
}
2017-08-30 20:36:59 +02:00
if ( fac . playerReputation < aug . baseRepRequirement ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "purchaseAugmentation" , ` You do not have enough reputation with ' ${ fac . name } '. ` ) ;
2017-08-30 20:36:59 +02:00
return false ;
}
2021-03-11 09:02:05 +01:00
const res = purchaseAugmentation ( aug , fac , true ) ;
workerScript . log ( "purchaseAugmentation" , res ) ;
2017-08-30 19:44:29 +02:00
if ( isString ( res ) && res . startsWith ( "You purchased" ) ) {
2017-09-19 20:38:03 +02:00
Player . gainIntelligenceExp ( CONSTANTS . IntelligenceSingFnBaseExpGain ) ;
2017-08-30 19:44:29 +02:00
return true ;
} else {
return false ;
}
} ,
2021-03-11 09:02:05 +01:00
softReset : function ( cbScript ) {
2021-03-07 10:49:57 +01:00
updateDynamicRam ( "softReset" , getRamCost ( "softReset" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "softReset" , 3 ) ;
2021-03-07 10:49:57 +01:00
2021-03-11 09:02:05 +01:00
workerScript . log ( "softReset" , "Soft resetting. This will cause this script to be killed" ) ;
2021-03-07 10:49:57 +01:00
setTimeoutRef ( ( ) => {
2021-03-12 02:37:58 +01:00
prestigeAugmentation ( ) ;
runAfterReset ( cbScript ) ;
2021-03-07 10:49:57 +01:00
} , 0 ) ;
// Prevent workerScript from "finishing execution naturally"
workerScript . running = false ;
killWorkerScript ( workerScript ) ;
} ,
2019-05-05 06:03:40 +02:00
installAugmentations : function ( cbScript ) {
updateDynamicRam ( "installAugmentations" , getRamCost ( "installAugmentations" ) ) ;
2021-03-11 09:02:05 +01:00
checkSingularityAccess ( "installAugmentations" , 3 ) ;
2017-08-30 19:44:29 +02:00
if ( Player . queuedAugmentations . length === 0 ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "installAugmentations" , "You do not have any Augmentations to be installed." ) ;
2017-08-30 19:44:29 +02:00
return false ;
}
2017-09-19 20:38:03 +02:00
Player . gainIntelligenceExp ( CONSTANTS . IntelligenceSingFnBaseExpGain ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "installAugmentations" , "Installing Augmentations. This will cause this script to be killed" ) ;
2019-07-14 05:55:58 +02:00
setTimeoutRef ( ( ) => {
2021-03-12 02:37:58 +01:00
installAugmentations ( ) ;
runAfterReset ( cbScript ) ;
2019-07-14 05:55:58 +02:00
} , 0 ) ;
workerScript . running = false ; // Prevent workerScript from "finishing execution naturally"
killWorkerScript ( workerScript ) ;
2018-05-23 02:09:04 +02:00
} ,
2018-10-23 20:55:42 +02:00
// Gang API
2019-05-05 06:03:40 +02:00
gang : {
getMemberNames : function ( ) {
updateDynamicRam ( "getMemberNames" , getRamCost ( "gang" , "getMemberNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getMemberNames" ) ;
return Player . gang . members . map ( member => member . name ) ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getGangInformation : function ( ) {
updateDynamicRam ( "getGangInformation" , getRamCost ( "gang" , "getGangInformation" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getGangInformation" ) ;
return {
2018-10-23 20:55:42 +02:00
faction : Player . gang . facName ,
isHacking : Player . gang . isHackingGang ,
moneyGainRate : Player . gang . moneyGainRate ,
power : Player . gang . getPower ( ) ,
respect : Player . gang . respect ,
respectGainRate : Player . gang . respectGainRate ,
territory : Player . gang . getTerritory ( ) ,
territoryClashChance : Player . gang . territoryClashChance ,
territoryWarfareEngaged : Player . gang . territoryWarfareEngaged ,
wantedLevel : Player . gang . wanted ,
wantedLevelGainRate : Player . gang . wantedGainRate ,
2021-03-11 09:02:05 +01:00
} ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getOtherGangInformation : function ( ) {
updateDynamicRam ( "getOtherGangInformation" , getRamCost ( "gang" , "getOtherGangInformation" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getOtherGangInformation" ) ;
const cpy = { } ;
for ( const gang in AllGangs ) {
cpy [ gang ] = Object . assign ( { } , AllGangs [ gang ] ) ;
2018-10-25 03:26:53 +02:00
}
2021-03-11 09:02:05 +01:00
return cpy ;
2018-10-25 03:26:53 +02:00
} ,
2019-05-05 06:03:40 +02:00
getMemberInformation : function ( name ) {
updateDynamicRam ( "getMemberInformation" , getRamCost ( "gang" , "getMemberInformation" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getMemberInformation" ) ;
const member = getGangMember ( "getMemberInformation" , name ) ;
return {
2021-04-06 09:50:09 +02:00
name : member . name ,
task : member . task ,
earnedRespect : member . earnedRespect ,
hack : member . hack ,
str : member . str ,
def : member . def ,
dex : member . dex ,
agi : member . agi ,
cha : member . cha ,
hack _exp : member . hack _exp ,
str _exp : member . str _exp ,
def _exp : member . def _exp ,
dex _exp : member . dex _exp ,
agi _exp : member . agi _exp ,
cha _exp : member . cha _exp ,
hack _mult : member . hack _mult ,
str _mult : member . str _mult ,
def _mult : member . def _mult ,
dex _mult : member . dex _mult ,
agi _mult : member . agi _mult ,
cha _mult : member . cha _mult ,
hack _asc _mult : member . hack _asc _mult ,
str _asc _mult : member . str _asc _mult ,
def _asc _mult : member . def _asc _mult ,
dex _asc _mult : member . dex _asc _mult ,
agi _asc _mult : member . agi _asc _mult ,
cha _asc _mult : member . cha _asc _mult ,
upgrades : member . upgrades . slice ( ) ,
augmentations : member . augmentations . slice ( ) ,
2018-10-23 20:55:42 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
canRecruitMember : function ( ) {
updateDynamicRam ( "canRecruitMember" , getRamCost ( "gang" , "canRecruitMember" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "canRecruitMember" ) ;
return Player . gang . canRecruitMember ( ) ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
recruitMember : function ( name ) {
updateDynamicRam ( "recruitMember" , getRamCost ( "gang" , "recruitMember" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "recruitMember" ) ;
const recruited = Player . gang . recruitMember ( name ) ;
2021-03-11 23:01:28 +01:00
if ( recruited ) {
workerScript . log ( "recruitMember" , ` Successfully recruited Gang Member ' ${ name } ' ` ) ;
} else {
workerScript . log ( "recruitMember" , ` Failed to recruit Gang Member ' ${ name } ' ` ) ;
2018-10-23 20:55:42 +02:00
}
2021-03-11 09:02:05 +01:00
return recruited ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getTaskNames : function ( ) {
updateDynamicRam ( "getTaskNames" , getRamCost ( "gang" , "getTaskNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getTaskNames" ) ;
const tasks = Player . gang . getAllTaskNames ( ) ;
tasks . unshift ( "Unassigned" ) ;
return tasks ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
setMemberTask : function ( memberName , taskName ) {
updateDynamicRam ( "setMemberTask" , getRamCost ( "gang" , "setMemberTask" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "setMemberTask" ) ;
const member = getGangMember ( "setMemberTask" , memberName ) ;
const success = member . assignToTask ( taskName ) ;
2021-03-11 23:01:28 +01:00
if ( success ) {
workerScript . log ( "setMemberTask" , ` Successfully assigned Gang Member ' ${ memberName } ' to ' ${ taskName } ' task ` ) ;
} else {
workerScript . log ( "setMemberTask" , ` Failed to assign Gang Member ' ${ memberName } ' to ' ${ taskName } ' task. ' ${ memberName } ' is now Unassigned ` ) ;
2018-10-23 20:55:42 +02:00
}
2021-03-11 09:02:05 +01:00
return success ;
2018-10-23 20:55:42 +02:00
} ,
2021-03-08 04:12:02 +01:00
getTaskStats : function ( taskName ) {
updateDynamicRam ( "getTaskStats" , getRamCost ( "gang" , "getTaskStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getTaskStats" ) ;
const task = getGangTask ( "getTaskStats" , taskName ) ;
2021-03-08 04:12:02 +01:00
const copy = Object . assign ( { } , task ) ;
copy . territory = Object . assign ( { } , task . territory )
return copy ;
} ,
2019-05-05 06:03:40 +02:00
getEquipmentNames : function ( ) {
updateDynamicRam ( "getEquipmentNames" , getRamCost ( "gang" , "getEquipmentNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getEquipmentNames" ) ;
return Player . gang . getAllUpgradeNames ( ) ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getEquipmentCost : function ( equipName ) {
updateDynamicRam ( "getEquipmentCost" , getRamCost ( "gang" , "getEquipmentCost" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getEquipmentCost" ) ;
return Player . gang . getUpgradeCost ( equipName ) ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
getEquipmentType : function ( equipName ) {
updateDynamicRam ( "getEquipmentType" , getRamCost ( "gang" , "getEquipmentType" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getEquipmentType" ) ;
return Player . gang . getUpgradeType ( equipName ) ;
2018-11-20 06:54:03 +01:00
} ,
2021-03-08 04:01:31 +01:00
getEquipmentStats : function ( equipName ) {
updateDynamicRam ( "getEquipmentStats" , getRamCost ( "gang" , "getEquipmentStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getEquipmentStats" ) ;
2021-03-08 04:01:31 +01:00
const equipment = GangMemberUpgrades [ equipName ] ;
if ( ! equipment ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "getEquipmentStats" , ` Invalid equipment: ${ equipName } ` ) ;
2021-03-08 04:01:31 +01:00
}
return Object . assign ( { } , equipment . mults ) ;
} ,
2019-05-05 06:03:40 +02:00
purchaseEquipment : function ( memberName , equipName ) {
updateDynamicRam ( "purchaseEquipment" , getRamCost ( "gang" , "purchaseEquipment" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "purchaseEquipment" ) ;
const member = getGangMember ( "purchaseEquipment" , memberName ) ;
const res = member . buyUpgrade ( equipName , Player , Player . gang ) ;
2021-03-11 23:01:28 +01:00
if ( res ) {
workerScript . log ( "purchaseEquipment" , ` Purchased ' ${ equipName } ' for Gang member ' ${ memberName } ' ` ) ;
} else {
workerScript . log ( "purchaseEquipment" , ` Failed to purchase ' ${ equipName } ' for Gang member ' ${ memberName } ' ` ) ;
2018-10-23 20:55:42 +02:00
}
2021-03-11 09:02:05 +01:00
return res ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
ascendMember : function ( name ) {
updateDynamicRam ( "ascendMember" , getRamCost ( "gang" , "ascendMember" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "ascendMember" ) ;
const member = getGangMember ( "ascendMember" , name ) ;
return Player . gang . ascendMember ( member , workerScript ) ;
2018-10-23 20:55:42 +02:00
} ,
2019-05-05 06:03:40 +02:00
setTerritoryWarfare : function ( engage ) {
updateDynamicRam ( "setTerritoryWarfare" , getRamCost ( "gang" , "setTerritoryWarfare" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "setTerritoryWarfare" ) ;
if ( engage ) {
Player . gang . territoryWarfareEngaged = true ;
workerScript . log ( "setTerritoryWarfare" , "Engaging in Gang Territory Warfare" ) ;
} else {
Player . gang . territoryWarfareEngaged = false ;
workerScript . log ( "setTerritoryWarfare" , "Disengaging in Gang Territory Warfare" ) ;
2018-10-23 20:55:42 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getChanceToWinClash : function ( otherGang ) {
updateDynamicRam ( "getChanceToWinClash" , getRamCost ( "gang" , "getChanceToWinClash" ) ) ;
2021-03-11 09:02:05 +01:00
checkGangApiAccess ( "getChanceToWinClash" ) ;
if ( AllGangs [ otherGang ] == null ) {
throw makeRuntimeErrorMsg ( ` gang. ${ getChanceToWinClash } ` , ` Invalid gang: ${ otherGang } ` ) ;
}
2018-11-14 07:05:51 +01:00
2021-03-11 09:02:05 +01:00
const playerPower = AllGangs [ Player . gang . facName ] . power ;
const otherPower = AllGangs [ otherGang ] . power ;
2018-11-14 07:05:51 +01:00
2021-03-11 09:02:05 +01:00
return playerPower / ( otherPower + playerPower ) ;
2018-11-14 07:05:51 +01:00
} ,
2019-05-05 06:03:40 +02:00
getBonusTime : function ( ) {
2021-03-11 09:02:05 +01:00
updateDynamicRam ( "getBonusTime" , getRamCost ( "gang" , "getBonusTime" ) ) ;
checkGangApiAccess ( "getBonusTime" ) ;
return Math . round ( Player . gang . storedCycles / 5 ) ;
2018-10-23 20:55:42 +02:00
} ,
} , // end gang namespace
// Bladeburner API
2019-05-05 06:03:40 +02:00
bladeburner : {
getContractNames : function ( ) {
updateDynamicRam ( "getContractNames" , getRamCost ( "bladeburner" , "getContractNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getContractNames" ) ;
return Player . bladeburner . getContractNamesNetscriptFn ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getOperationNames : function ( ) {
updateDynamicRam ( "getOperationNames" , getRamCost ( "bladeburner" , "getOperationNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getOperationNames" ) ;
return Player . bladeburner . getOperationNamesNetscriptFn ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getBlackOpNames : function ( ) {
updateDynamicRam ( "getBlackOpNames" , getRamCost ( "bladeburner" , "getBlackOpNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getBlackOpNames" ) ;
return Player . bladeburner . getBlackOpNamesNetscriptFn ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getBlackOpRank : function ( name = "" ) {
updateDynamicRam ( "getBlackOpRank" , getRamCost ( "bladeburner" , "getBlackOpRank" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getBlackOpRank" ) ;
const action = getBladeburnerActionObject ( "getBlackOpRank" , "blackops" , name ) ;
return action . reqdRank ;
2019-03-12 07:39:32 +01:00
} ,
2019-05-05 06:03:40 +02:00
getGeneralActionNames : function ( ) {
updateDynamicRam ( "getGeneralActionNames" , getRamCost ( "bladeburner" , "getGeneralActionNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getGeneralActionNames" ) ;
return Player . bladeburner . getGeneralActionNamesNetscriptFn ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getSkillNames : function ( ) {
updateDynamicRam ( "getSkillNames" , getRamCost ( "bladeburner" , "getSkillNames" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getSkillNames" ) ;
return Player . bladeburner . getSkillNamesNetscriptFn ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
startAction : function ( type = "" , name = "" ) {
updateDynamicRam ( "startAction" , getRamCost ( "bladeburner" , "startAction" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "startAction" ) ;
try {
return Player . bladeburner . startActionNetscriptFn ( type , name , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.startAction" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
stopBladeburnerAction : function ( ) {
updateDynamicRam ( "stopBladeburnerAction" , getRamCost ( "bladeburner" , "stopBladeburnerAction" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "stopBladeburnerAction" ) ;
return Player . bladeburner . resetAction ( ) ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCurrentAction : function ( ) {
updateDynamicRam ( "getCurrentAction" , getRamCost ( "bladeburner" , "getCurrentAction" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getCurrentAction" ) ;
return Player . bladeburner . getTypeAndNameFromActionId ( Player . bladeburner . action ) ;
2018-07-11 06:41:26 +02:00
} ,
2019-05-05 06:03:40 +02:00
getActionTime : function ( type = "" , name = "" ) {
updateDynamicRam ( "getActionTime" , getRamCost ( "bladeburner" , "getActionTime" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionTime" ) ;
try {
return Player . bladeburner . getActionTimeNetscriptFn ( type , name , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getActionTime" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getActionEstimatedSuccessChance : function ( type = "" , name = "" ) {
updateDynamicRam ( "getActionEstimatedSuccessChance" , getRamCost ( "bladeburner" , "getActionEstimatedSuccessChance" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionEstimatedSuccessChance" ) ;
try {
return Player . bladeburner . getActionEstimatedSuccessChanceNetscriptFn ( type , name , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getActionEstimatedSuccessChance" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2018-09-24 06:27:31 +02:00
getActionRepGain : function ( type = "" , name = "" , level ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getActionRepGain" , getRamCost ( "bladeburner" , "getActionRepGain" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionRepGain" ) ;
const action = getBladeburnerActionObject ( "getActionRepGain" , type , name ) ;
let rewardMultiplier ;
if ( level == null || isNaN ( level ) ) {
rewardMultiplier = Math . pow ( action . rewardFac , action . level - 1 ) ;
} else {
rewardMultiplier = Math . pow ( action . rewardFac , level - 1 ) ;
2018-09-24 06:27:31 +02:00
}
2021-03-11 09:02:05 +01:00
return action . rankGain * rewardMultiplier * BitNodeMultipliers . BladeburnerRank ;
2018-09-24 06:27:31 +02:00
} ,
2019-05-05 06:03:40 +02:00
getActionCountRemaining : function ( type = "" , name = "" ) {
updateDynamicRam ( "getActionCountRemaining" , getRamCost ( "bladeburner" , "getActionCountRemaining" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionCountRemaining" ) ;
try {
return Player . bladeburner . getActionCountRemainingNetscriptFn ( type , name , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getActionCountRemaining" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2018-07-20 06:06:59 +02:00
getActionMaxLevel : function ( type = "" , name = "" ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getActionMaxLevel" , getRamCost ( "bladeburner" , "getActionMaxLevel" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionMaxLevel" ) ;
const action = getBladeburnerActionObject ( "getActionMaxLevel" , type , name ) ;
return action . maxLevel ;
2018-07-20 06:06:59 +02:00
} ,
getActionCurrentLevel : function ( type = "" , name = "" ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getActionCurrentLevel" , getRamCost ( "bladeburner" , "getActionCurrentLevel" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionCurrentLevel" ) ;
const action = getBladeburnerActionObject ( "getActionCurrentLevel" , type , name ) ;
return action . level ;
2018-07-20 06:06:59 +02:00
} ,
getActionAutolevel : function ( type = "" , name = "" ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "getActionAutolevel" , getRamCost ( "bladeburner" , "getActionAutolevel" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getActionAutolevel" ) ;
const action = getBladeburnerActionObject ( "getActionCurrentLevel" , type , name ) ;
return action . autoLevel ;
2018-07-20 06:06:59 +02:00
} ,
setActionAutolevel : function ( type = "" , name = "" , autoLevel = true ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "setActionAutolevel" , getRamCost ( "bladeburner" , "setActionAutolevel" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "setActionAutolevel" ) ;
const action = getBladeburnerActionObject ( "getActionCurrentLevel" , type , name ) ;
action . autoLevel = autoLevel ;
2018-07-20 06:06:59 +02:00
} ,
setActionLevel : function ( type = "" , name = "" , level = 1 ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "setActionLevel" , getRamCost ( "bladeburner" , "setActionLevel" ) ) ;
2021-03-11 09:02:05 +01: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 } ` )
2018-07-20 06:06:59 +02:00
}
2021-03-11 09:02:05 +01:00
actionObj . level = level ;
2018-07-20 06:06:59 +02:00
} ,
2019-05-05 06:03:40 +02:00
getRank : function ( ) {
updateDynamicRam ( "getRank" , getRamCost ( "bladeburner" , "getRank" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getRank" ) ;
return Player . bladeburner . rank ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getSkillPoints : function ( ) {
updateDynamicRam ( "getSkillPoints" , getRamCost ( "bladeburner" , "getSkillPoints" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getSkillPoints" ) ;
return Player . bladeburner . skillPoints ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getSkillLevel : function ( skillName = "" ) {
updateDynamicRam ( "getSkillLevel" , getRamCost ( "bladeburner" , "getSkillLevel" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getSkillLevel" ) ;
try {
return Player . bladeburner . getSkillLevelNetscriptFn ( skillName , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getSkillLevel" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getSkillUpgradeCost : function ( skillName = "" ) {
updateDynamicRam ( "getSkillUpgradeCost" , getRamCost ( "bladeburner" , "getSkillUpgradeCost" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getSkillUpgradeCost" ) ;
try {
return Player . bladeburner . getSkillUpgradeCostNetscriptFn ( skillName , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getSkillUpgradeCost" , e ) ;
2018-08-12 03:05:32 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
upgradeSkill : function ( skillName ) {
updateDynamicRam ( "upgradeSkill" , getRamCost ( "bladeburner" , "upgradeSkill" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "upgradeSkill" ) ;
try {
return Player . bladeburner . upgradeSkillNetscriptFn ( skillName , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.upgradeSkill" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getTeamSize : function ( type = "" , name = "" ) {
updateDynamicRam ( "getTeamSize" , getRamCost ( "bladeburner" , "getTeamSize" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getTeamSize" ) ;
try {
return Player . bladeburner . getTeamSizeNetscriptFn ( type , name , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.getTeamSize" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
setTeamSize : function ( type = "" , name = "" , size ) {
updateDynamicRam ( "setTeamSize" , getRamCost ( "bladeburner" , "setTeamSize" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "setTeamSize" ) ;
try {
return Player . bladeburner . setTeamSizeNetscriptFn ( type , name , size , workerScript ) ;
} catch ( e ) {
throw makeRuntimeErrorMsg ( "bladeburner.setTeamSize" , e ) ;
2018-05-23 02:09:04 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getCityEstimatedPopulation : function ( cityName ) {
updateDynamicRam ( "getCityEstimatedPopulation" , getRamCost ( "bladeburner" , "getCityEstimatedPopulation" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getCityEstimatedPopulation" ) ;
checkBladeburnerCity ( "getCityEstimatedPopulation" , cityName ) ;
return Player . bladeburner . cities [ cityName ] . popEst ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCityEstimatedCommunities : function ( cityName ) {
updateDynamicRam ( "getCityEstimatedCommunities" , getRamCost ( "bladeburner" , "getCityEstimatedCommunities" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getCityEstimatedCommunities" ) ;
checkBladeburnerCity ( "getCityEstimatedCommunities" , cityName ) ;
return Player . bladeburner . cities [ cityName ] . commsEst ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCityChaos : function ( cityName ) {
updateDynamicRam ( "getCityChaos" , getRamCost ( "bladeburner" , "getCityChaos" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getCityChaos" ) ;
checkBladeburnerCity ( "getCityChaos" , cityName ) ;
return Player . bladeburner . cities [ cityName ] . chaos ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getCity : function ( ) {
updateDynamicRam ( "getCity" , getRamCost ( "bladeburner" , "getCity" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getCityChaos" ) ;
return Player . bladeburner . city ;
2018-08-24 22:44:48 +02:00
} ,
2019-05-05 06:03:40 +02:00
switchCity : function ( cityName ) {
updateDynamicRam ( "switchCity" , getRamCost ( "bladeburner" , "switchCity" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "switchCity" ) ;
checkBladeburnerCity ( "switchCity" , cityName ) ;
return Player . bladeburner . city = cityName ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
getStamina : function ( ) {
updateDynamicRam ( "getStamina" , getRamCost ( "bladeburner" , "getStamina" ) ) ;
2021-03-11 09:02:05 +01:00
checkBladeburnerAccess ( "getStamina" ) ;
return [ Player . bladeburner . stamina , Player . bladeburner . maxStamina ] ;
2018-05-23 02:09:04 +02:00
} ,
2019-05-05 06:03:40 +02:00
joinBladeburnerFaction : function ( ) {
updateDynamicRam ( "joinBladeburnerFaction" , getRamCost ( "bladeburner" , "joinBladeburnerFaction" ) ) ;
2021-03-13 21:10:55 +01:00
checkBladeburnerAccess ( "joinBladeburnerFaction" , true ) ;
2021-03-11 09:02:05 +01:00
return Player . bladeburner . joinBladeburnerFactionNetscriptFn ( workerScript ) ;
2018-06-25 02:13:50 +02:00
} ,
2019-05-05 06:03:40 +02:00
joinBladeburnerDivision : function ( ) {
updateDynamicRam ( "joinBladeburnerDivision" , getRamCost ( "bladeburner" , "joinBladeburnerDivision" ) ) ;
2021-03-13 21:10:55 +01:00
checkBladeburnerAccess ( "joinBladeburnerDivision" , true ) ;
2019-05-05 06:03:40 +02:00
if ( ( Player . bitNodeN === 7 || SourceFileFlags [ 7 ] > 0 ) ) {
2019-04-20 07:27:33 +02:00
if ( Player . bitNodeN === 8 ) { return false ; }
2018-07-03 05:35:12 +02:00
if ( Player . bladeburner instanceof Bladeburner ) {
2019-04-13 03:22:46 +02:00
return true ; // Already member
2018-07-03 05:35:12 +02:00
} else if ( Player . strength >= 100 && Player . defense >= 100 &&
Player . dexterity >= 100 && Player . agility >= 100 ) {
Player . bladeburner = new Bladeburner ( { new : true } ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "joinBladeburnerDivision" , "You have been accepted into the Bladeburner division" ) ;
2019-04-20 07:27:33 +02:00
const worldHeader = document . getElementById ( "world-menu-header" ) ;
if ( worldHeader instanceof HTMLElement ) {
worldHeader . click ( ) ; worldHeader . click ( ) ;
}
2018-07-03 05:35:12 +02:00
return true ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "joinBladeburnerDivision" , "You do not meet the requirements for joining the Bladeburner division" ) ;
2018-07-03 05:35:12 +02:00
return false ;
}
}
2018-08-12 03:05:32 +02:00
} ,
2019-05-05 06:03:40 +02:00
getBonusTime : function ( ) {
2021-03-11 09:02:05 +01:00
updateDynamicRam ( "getBonusTime" , getRamCost ( "bladeburner" , "getBonusTime" ) ) ;
checkBladeburnerAccess ( "getBonusTime" ) ;
return Math . round ( Player . bladeburner . storedCycles / 5 ) ;
2021-04-30 05:52:56 +02:00
} ,
2018-09-23 02:25:48 +02:00
} , // End Bladeburner
2019-05-05 06:03:40 +02:00
// Coding Contract API
codingcontract : {
2019-05-14 10:35:37 +02:00
attempt : function ( answer , fn , ip = workerScript . serverIp , { returnReward } = { } ) {
2019-05-05 06:03:40 +02:00
updateDynamicRam ( "attempt" , getRamCost ( "codingcontract" , "attempt" ) ) ;
2021-03-11 09:02:05 +01:00
const contract = getCodingContract ( "attempt" , ip , fn ) ;
2019-02-27 03:26:29 +01:00
// 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 ) ) {
let answerComponents = [ ] ;
for ( let i = 0 ; i < answer . length ; ++ i ) {
answerComponents . push ( [ "[" , answer [ i ] . toString ( ) , "]" ] . join ( "" ) ) ;
}
answer = answerComponents . join ( "," ) ;
} else {
answer = String ( answer ) ;
}
2019-03-31 04:53:57 +02:00
const serv = safeGetServer ( ip , "codingcontract.attempt" ) ;
2018-09-23 02:25:48 +02:00
if ( contract . isSolution ( answer ) ) {
const reward = Player . gainCodingContractReward ( contract . reward , contract . getDifficulty ( ) ) ;
2021-03-11 09:02:05 +01:00
workerScript . log ( "attempt" , ` Successfully completed Coding Contract ' ${ fn } '. Reward: ${ reward } ` ) ;
2018-09-23 02:25:48 +02:00
serv . removeContract ( fn ) ;
2019-05-14 10:35:37 +02:00
return returnReward ? reward : true ;
2018-09-23 02:25:48 +02:00
} else {
++ contract . tries ;
if ( contract . tries >= contract . getMaxNumTries ( ) ) {
2021-03-11 09:02:05 +01:00
workerScript . log ( "attempt" , ` Coding Contract attempt ' ${ fn } ' failed. Contract is now self-destructing ` ) ;
2018-09-23 02:25:48 +02:00
serv . removeContract ( fn ) ;
} else {
2021-03-11 09:02:05 +01:00
workerScript . log ( "attempt" , ` Coding Contract attempt ' ${ fn } ' failed. ${ contract . getMaxNumTries ( ) - contract . tries } attempts remaining. ` ) ;
2018-09-23 02:25:48 +02:00
}
2019-05-14 10:35:37 +02:00
return returnReward ? "" : false ;
2018-09-23 02:25:48 +02:00
}
} ,
2019-05-05 06:03:40 +02:00
getContractType : function ( fn , ip = workerScript . serverIp ) {
updateDynamicRam ( "getContractType" , getRamCost ( "codingcontract" , "getContractType" ) ) ;
2021-03-11 09:02:05 +01:00
const contract = getCodingContract ( "getContractType" , ip , fn ) ;
2018-10-09 02:26:24 +02:00
return contract . getType ( ) ;
} ,
2019-05-05 06:03:40 +02:00
getData : function ( fn , ip = workerScript . serverIp ) {
updateDynamicRam ( "getData" , getRamCost ( "codingcontract" , "getData" ) ) ;
2021-03-11 09:02:05 +01:00
const contract = getCodingContract ( "getData" , ip , fn ) ;
const data = contract . getData ( ) ;
2018-10-09 02:33:07 +02:00
if ( data . constructor === Array ) {
2019-02-01 09:27:24 +01:00
// 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 ( ) ;
}
}
return copy ;
2018-10-09 02:26:24 +02:00
} else {
return data ;
}
2018-09-23 02:25:48 +02:00
} ,
2019-05-05 06:03:40 +02:00
getDescription : function ( fn , ip = workerScript . serverIp ) {
updateDynamicRam ( "getDescription" , getRamCost ( "codingcontract" , "getDescription" ) ) ;
2021-03-11 09:02:05 +01:00
const contract = getCodingContract ( "getDescription" , ip , fn ) ;
2018-09-23 02:25:48 +02:00
return contract . getDescription ( ) ;
} ,
2019-05-05 06:03:40 +02:00
getNumTriesRemaining : function ( fn , ip = workerScript . serverIp ) {
updateDynamicRam ( "getNumTriesRemaining" , getRamCost ( "codingcontract" , "getNumTriesRemaining" ) ) ;
2021-03-11 09:02:05 +01:00
const contract = getCodingContract ( "getNumTriesRemaining" , ip , fn ) ;
2018-09-23 02:25:48 +02:00
return contract . getMaxNumTries ( ) - contract . tries ;
} ,
2019-03-12 08:40:48 +01:00
} , // End coding contracts
2019-05-05 06:03:40 +02:00
// Duplicate Sleeve API
sleeve : {
getNumSleeves : function ( ) {
updateDynamicRam ( "getNumSleeves" , getRamCost ( "sleeve" , "getNumSleeves" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getNumSleeves" ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves . length ;
} ,
2019-05-05 06:03:40 +02:00
setToShockRecovery : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "setToShockRecovery" , getRamCost ( "sleeve" , "setToShockRecovery" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToShockRecovery" ) ;
checkSleeveNumber ( "setToShockRecovery" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . shockRecovery ( Player ) ;
} ,
2019-05-05 06:03:40 +02:00
setToSynchronize : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "setToSynchronize" , getRamCost ( "sleeve" , "setToSynchronize" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToSynchronize" ) ;
checkSleeveNumber ( "setToSynchronize" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . synchronize ( Player ) ;
} ,
2019-05-05 06:03:40 +02:00
setToCommitCrime : function ( sleeveNumber = 0 , crimeName = "" ) {
updateDynamicRam ( "setToCommitCrime" , getRamCost ( "sleeve" , "setToCommitCrime" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToCommitCrime" ) ;
checkSleeveNumber ( "setToCommitCrime" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . commitCrime ( Player , crimeName ) ;
} ,
2019-05-05 06:03:40 +02:00
setToUniversityCourse : function ( sleeveNumber = 0 , universityName = "" , className = "" ) {
updateDynamicRam ( "setToUniversityCourse" , getRamCost ( "sleeve" , "setToUniversityCourse" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToUniversityCourse" ) ;
checkSleeveNumber ( "setToUniversityCourse" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . takeUniversityCourse ( Player , universityName , className ) ;
} ,
2019-05-05 06:03:40 +02:00
travel : function ( sleeveNumber = 0 , cityName = "" ) {
updateDynamicRam ( "travel" , getRamCost ( "sleeve" , "travel" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "travel" ) ;
checkSleeveNumber ( "travel" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . travel ( Player , cityName ) ;
} ,
2019-05-05 06:03:40 +02:00
setToCompanyWork : function ( sleeveNumber = 0 , companyName = "" ) {
updateDynamicRam ( "setToCompanyWork" , getRamCost ( "sleeve" , "setToCompanyWork" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToCompanyWork" ) ;
checkSleeveNumber ( "setToCompanyWork" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
2019-03-20 00:38:49 +01:00
// Cannot work at the same company that another sleeve is working at
for ( let i = 0 ; i < Player . sleeves . length ; ++ i ) {
if ( i === sleeveNumber ) { continue ; }
const other = Player . sleeves [ i ] ;
if ( other . currentTask === SleeveTaskType . Company && other . currentTaskLocation === companyName ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sleeve.setToFactionWork" , ` Sleeve ${ sleeveNumber } cannot work for company ${ companyName } because Sleeve ${ i } is already working for them. ` )
2019-03-20 00:38:49 +01:00
}
}
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . workForCompany ( Player , companyName ) ;
} ,
2019-05-05 06:03:40 +02:00
setToFactionWork : function ( sleeveNumber = 0 , factionName = "" , workType = "" ) {
updateDynamicRam ( "setToFactionWork" , getRamCost ( "sleeve" , "setToFactionWork" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToFactionWork" ) ;
checkSleeveNumber ( "setToFactionWork" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
2019-03-20 00:38:49 +01:00
// Cannot work at the same faction that another sleeve is working at
for ( let i = 0 ; i < Player . sleeves . length ; ++ i ) {
if ( i === sleeveNumber ) { continue ; }
const other = Player . sleeves [ i ] ;
if ( other . currentTask === SleeveTaskType . Faction && other . currentTaskLocation === factionName ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sleeve.setToFactionWork" , ` Sleeve ${ sleeveNumber } cannot work for faction ${ factionName } because Sleeve ${ i } is already working for them. ` )
2019-03-20 00:38:49 +01:00
}
}
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . workForFaction ( Player , factionName , workType ) ;
} ,
2019-05-05 06:03:40 +02:00
setToGymWorkout : function ( sleeveNumber = 0 , gymName = "" , stat = "" ) {
updateDynamicRam ( "setToGymWorkout" , getRamCost ( "sleeve" , "setToGymWorkout" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "setToGymWorkout" ) ;
checkSleeveNumber ( "setToGymWorkout" , sleeveNumber ) ;
2019-03-12 08:40:48 +01:00
return Player . sleeves [ sleeveNumber ] . workoutAtGym ( Player , gymName , stat ) ;
} ,
2019-05-05 06:03:40 +02:00
getSleeveStats : function ( sleeveNumber = 0 ) {
2019-05-10 11:24:50 +02:00
updateDynamicRam ( "getSleeveStats" , getRamCost ( "sleeve" , "getSleeveStats" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getSleeveStats" ) ;
checkSleeveNumber ( "getSleeveStats" , sleeveNumber ) ;
2019-03-12 23:10:12 +01:00
2019-03-24 03:40:35 +01:00
const sl = Player . sleeves [ sleeveNumber ] ;
2019-03-12 23:10:12 +01:00
return {
2019-03-24 03:40:35 +01:00
shock : 100 - sl . shock ,
2019-03-12 23:10:12 +01:00
sync : sl . sync ,
hacking _skill : sl . hacking _skill ,
strength : sl . strength ,
defense : sl . defense ,
dexterity : sl . dexterity ,
agility : sl . agility ,
charisma : sl . charisma ,
} ;
} ,
2019-05-05 06:03:40 +02:00
getTask : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "getTask" , getRamCost ( "sleeve" , "getTask" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getTask" ) ;
checkSleeveNumber ( "getTask" , sleeveNumber ) ;
2019-03-12 23:10:12 +01:00
const sl = Player . sleeves [ sleeveNumber ] ;
return {
task : SleeveTaskType [ sl . currentTask ] ,
crime : sl . crimeType ,
location : sl . currentTaskLocation ,
gymStatType : sl . gymStatType ,
factionWorkType : FactionWorkType [ sl . factionWorkType ] ,
} ;
} ,
2019-05-05 06:03:40 +02:00
getInformation : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "getInformation" , getRamCost ( "sleeve" , "getInformation" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getInformation" ) ;
checkSleeveNumber ( "getInformation" , sleeveNumber ) ;
2019-03-12 23:10:12 +01:00
const sl = Player . sleeves [ sleeveNumber ] ;
return {
city : sl . city ,
hp : sl . hp ,
jobs : Object . keys ( Player . jobs ) , // technically sleeves have the same jobs as the player.
2019-03-18 06:04:12 +01:00
jobTitle : Object . values ( Player . jobs ) ,
2019-03-12 23:10:12 +01:00
maxHp : sl . max _hp ,
tor : SpecialServerIps . hasOwnProperty ( "Darkweb Server" ) , // There's no reason not to give that infomation here as well. Worst case scenario it isn't used.
mult : {
agility : sl . agility _mult ,
agilityExp : sl . agility _exp _mult ,
companyRep : sl . company _rep _mult ,
crimeMoney : sl . crime _money _mult ,
crimeSuccess : sl . crime _success _mult ,
defense : sl . defense _mult ,
defenseExp : sl . defense _exp _mult ,
dexterity : sl . dexterity _mult ,
dexterityExp : sl . dexterity _exp _mult ,
factionRep : sl . faction _rep _mult ,
hacking : sl . hacking _mult ,
hackingExp : sl . hacking _exp _mult ,
strength : sl . strength _mult ,
strengthExp : sl . strength _exp _mult ,
workMoney : sl . work _money _mult ,
} ,
timeWorked : sl . currentTaskTime ,
earningsForSleeves : {
workHackExpGain : sl . earningsForSleeves . hack ,
workStrExpGain : sl . earningsForSleeves . str ,
workDefExpGain : sl . earningsForSleeves . def ,
workDexExpGain : sl . earningsForSleeves . dex ,
workAgiExpGain : sl . earningsForSleeves . agi ,
workChaExpGain : sl . earningsForSleeves . cha ,
workMoneyGain : sl . earningsForSleeves . money ,
} ,
earningsForPlayer : {
workHackExpGain : sl . earningsForPlayer . hack ,
workStrExpGain : sl . earningsForPlayer . str ,
workDefExpGain : sl . earningsForPlayer . def ,
workDexExpGain : sl . earningsForPlayer . dex ,
workAgiExpGain : sl . earningsForPlayer . agi ,
workChaExpGain : sl . earningsForPlayer . cha ,
workMoneyGain : sl . earningsForPlayer . money ,
} ,
earningsForTask : {
workHackExpGain : sl . earningsForTask . hack ,
workStrExpGain : sl . earningsForTask . str ,
workDefExpGain : sl . earningsForTask . def ,
workDexExpGain : sl . earningsForTask . dex ,
workAgiExpGain : sl . earningsForTask . agi ,
workChaExpGain : sl . earningsForTask . cha ,
workMoneyGain : sl . earningsForTask . money ,
} ,
2019-04-30 05:54:20 +02:00
workRepGain : sl . getRepGain ( Player ) ,
2019-03-12 23:10:12 +01:00
}
} ,
2019-05-05 06:03:40 +02:00
getSleeveAugmentations : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "getSleeveAugmentations" , getRamCost ( "sleeve" , "getSleeveAugmentations" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getSleeveAugmentations" ) ;
checkSleeveNumber ( "getSleeveAugmentations" , sleeveNumber ) ;
2019-03-23 05:22:40 +01:00
const augs = [ ] ;
for ( let i = 0 ; i < Player . sleeves [ sleeveNumber ] . augmentations . length ; i ++ ) {
augs . push ( Player . sleeves [ sleeveNumber ] . augmentations [ i ] . name ) ;
}
return augs ;
} ,
2019-05-05 06:03:40 +02:00
getSleevePurchasableAugs : function ( sleeveNumber = 0 ) {
updateDynamicRam ( "getSleevePurchasableAugs" , getRamCost ( "sleeve" , "getSleevePurchasableAugs" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "getSleevePurchasableAugs" ) ;
checkSleeveNumber ( "getSleevePurchasableAugs" , sleeveNumber ) ;
2019-03-23 05:22:40 +01:00
const purchasableAugs = findSleevePurchasableAugs ( Player . sleeves [ sleeveNumber ] , Player ) ;
const augs = [ ] ;
for ( let i = 0 ; i < purchasableAugs . length ; i ++ ) {
const aug = purchasableAugs [ i ] ;
augs . push ( {
name : aug . name ,
cost : aug . startingCost ,
} ) ;
}
return augs ;
} ,
2019-05-05 06:03:40 +02:00
purchaseSleeveAug : function ( sleeveNumber = 0 , augName = "" ) {
updateDynamicRam ( "purchaseSleeveAug" , getRamCost ( "sleeve" , "purchaseSleeveAug" ) ) ;
2021-03-11 09:02:05 +01:00
checkSleeveAPIAccess ( "purchaseSleeveAug" ) ;
checkSleeveNumber ( "purchaseSleeveAug" , sleeveNumber ) ;
2019-03-23 05:22:40 +01:00
const aug = Augmentations [ augName ] ;
if ( ! aug ) {
2021-03-11 09:02:05 +01:00
throw makeRuntimeErrorMsg ( "sleeve.purchaseSleeveAug" , ` Invalid aug: ${ augName } ` )
2019-03-23 05:22:40 +01:00
}
return Player . sleeves [ sleeveNumber ] . tryBuyAugmentation ( Player , aug ) ;
2021-04-30 05:52:56 +02:00
} ,
2019-04-13 09:33:30 +02:00
} , // End sleeve
2021-03-31 06:45:21 +02:00
formulas : {
basic : {
calculateSkill : function ( exp , mult = 1 ) {
checkFormulasAccess ( "basic.calculateSkill" , 5 ) ;
return calculateSkill ( exp , mult ) ;
} ,
calculateExp : function ( skill , mult = 1 ) {
checkFormulasAccess ( "basic.calculateExp" , 5 ) ;
return calculateExp ( skill , mult ) ;
} ,
hackChance : function ( server , player ) {
checkFormulasAccess ( "basic.hackChance" , 5 ) ;
return calculateHackingChance ( server , player ) ;
} ,
hackExp : function ( server , player ) {
checkFormulasAccess ( "basic.hackExp" , 5 ) ;
return calculateHackingExpGain ( server , player ) ;
} ,
hackPercent : function ( server , player ) {
checkFormulasAccess ( "basic.hackPercent" , 5 ) ;
return calculatePercentMoneyHacked ( server , player ) ;
} ,
growPercent : function ( server , threads , player ) {
checkFormulasAccess ( "basic.growPercent" , 5 ) ;
return calculateServerGrowth ( server , threads , player ) ;
} ,
hackTime : function ( server , player ) {
checkFormulasAccess ( "basic.hackTime" , 5 ) ;
return calculateHackingTime ( server , player ) ;
} ,
growTime : function ( server , player ) {
checkFormulasAccess ( "basic.growTime" , 5 ) ;
return calculateGrowTime ( server , player ) ;
} ,
weakenTime : function ( server , player ) {
checkFormulasAccess ( "basic.weakenTime" , 5 ) ;
return calculateWeakenTime ( server , player ) ;
} ,
} ,
hacknetNodes : {
moneyGainRate : function ( level , ram , cores , mult = 1 ) {
checkFormulasAccess ( "hacknetNodes.moneyGainRate" , 5 ) ;
return calculateMoneyGainRate ( level , ram , cores , mult ) ;
} ,
levelUpgradeCost : function ( startingLevel , extraLevels = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetNodes.levelUpgradeCost" , 5 ) ;
return calculateLevelUpgradeCost ( startingLevel , extraLevels , costMult ) ;
} ,
ramUpgradeCost : function ( startingRam , extraLevels = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetNodes.ramUpgradeCost" , 5 ) ;
return calculateRamUpgradeCost ( startingRam , extraLevels , costMult ) ;
} ,
coreUpgradeCost : function ( startingCore , extraCores = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetNodes.coreUpgradeCost" , 5 ) ;
return calculateCoreUpgradeCost ( startingCore , extraCores , costMult ) ;
} ,
hacknetNodeCost : function ( n , mult ) {
checkFormulasAccess ( "hacknetNodes.hacknetNodeCost" , 5 ) ;
return calculateNodeCost ( n , mult ) ;
} ,
constants : function ( ) {
checkFormulasAccess ( "hacknetNodes.constants" , 5 ) ;
return Object . assign ( { } , HacknetNodeConstants , HacknetServerConstants ) ;
2021-04-30 05:52:56 +02:00
} ,
2021-03-31 06:45:21 +02:00
} ,
hacknetServers : {
2021-05-06 01:18:47 +02:00
hashGainRate : function ( level , ramUsed , maxRam , cores , mult = 1 ) {
2021-03-31 06:45:21 +02:00
checkFormulasAccess ( "hacknetServers.hashGainRate" , 9 ) ;
2021-05-06 01:18:47 +02:00
return HScalculateHashGainRate ( level , ramUsed , maxRam , cores , mult = 1 ) ;
2021-03-31 06:45:21 +02:00
} ,
levelUpgradeCost : function ( startingLevel , extraLevels = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetServers.levelUpgradeCost" , 9 ) ;
return HScalculateLevelUpgradeCost ( startingLevel , extraLevels , costMult ) ;
} ,
ramUpgradeCost : function ( startingRam , extraLevels = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetServers.ramUpgradeCost" , 9 ) ;
return HScalculateRamUpgradeCost ( startingRam , extraLevels , costMult ) ;
} ,
coreUpgradeCost : function ( startingCore , extraCores = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetServers.coreUpgradeCost" , 9 ) ;
return HScalculateCoreUpgradeCost ( startingCore , extraCores , costMult ) ;
} ,
cacheUpgradeCost : function ( startingCache , extraCache = 1 , costMult = 1 ) {
checkFormulasAccess ( "hacknetServers.cacheUpgradeCost" , 9 ) ;
return HScalculateCacheUpgradeCost ( startingCache , extraCache , costMult ) ;
} ,
hashUpgradeCost : function ( upgName , level ) {
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 ) ;
} ,
hacknetServerCost : function ( n , mult ) {
checkFormulasAccess ( "hacknetServers.hacknetServerCost" , 9 ) ;
return HScalculateServerCost ( n , mult ) ;
} ,
constants : function ( ) {
checkFormulasAccess ( "hacknetServers.constants" , 9 ) ;
return Object . assign ( { } , HacknetServerConstants ) ;
2021-04-30 05:52:56 +02:00
} ,
2021-03-31 06:45:21 +02:00
} ,
} , // end formulas
2019-04-13 09:33:30 +02:00
heart : {
// Easter egg function
2019-05-05 06:03:40 +02:00
break : function ( ) {
2019-04-13 09:33:30 +02:00
return Player . karma ;
2021-04-30 05:52:56 +02:00
} ,
2021-03-09 02:31:34 +01:00
} ,
exploit : function ( ) {
Player . giveExploit ( Exploit . UndocumentedFunctionCall ) ;
2021-04-21 14:20:26 +02:00
} ,
flags : function ( data ) {
data = toNative ( data ) ;
// We always want the help flag.
const args = { } ;
for ( const d of data ) {
let t = String ;
if ( typeof d [ 1 ] === 'number' ) {
t = Number ;
} else if ( typeof d [ 1 ] === 'boolean' ) {
t = Boolean ;
} else if ( Array . isArray ( d [ 1 ] ) ) {
t = [ String ] ;
}
args [ '--' + d [ 0 ] ] = t
}
const ret = libarg ( args , { argv : workerScript . args } ) ;
for ( const d of data ) {
if ( ! ret . hasOwnProperty ( '--' + d [ 0 ] ) ) ret [ d [ 0 ] ] = d [ 1 ] ;
}
for ( const key of Object . keys ( ret ) ) {
if ( ! key . startsWith ( '--' ) ) continue ;
const value = ret [ key ] ;
delete ret [ key ] ;
ret [ key . slice ( 2 ) ] = value ;
}
return ret ;
2021-04-30 05:52:56 +02:00
} ,
2019-04-13 03:22:46 +02:00
} // End return
} // End NetscriptFunction()
2017-08-30 19:44:29 +02:00
2021-04-18 17:18:56 +02:00
export { NetscriptFunctions } ;