bitburner-src/src/Script/ScriptHelpers.ts

110 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-09-05 01:09:30 +02:00
import { CONSTANTS } from "../Constants";
import { Player } from "@player";
2021-09-24 22:34:21 +02:00
import { BaseServer } from "../Server/BaseServer";
import { Server } from "../Server/Server";
2022-03-06 05:05:55 +01:00
import { RunningScript } from "./RunningScript";
2021-10-07 22:56:01 +02:00
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { GetServer } from "../Server/AllServers";
import { formatPercent } from "../ui/formatNumber";
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
import { workerScripts } from "../Netscript/WorkerScripts";
import { scriptKey } from "../utils/helpers/scriptKey";
import type { ScriptFilePath } from "../Paths/ScriptFilePath";
2021-09-25 07:26:03 +02:00
export function scriptCalculateOfflineProduction(runningScript: RunningScript): void {
2021-09-05 01:09:30 +02:00
//The Player object stores the last update time from when we were online
const thisUpdate = new Date().getTime();
const lastUpdate = Player.lastUpdate;
const timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds
2021-09-05 01:09:30 +02:00
//Calculate the "confidence" rating of the script's true production. This is based
//entirely off of time. We will arbitrarily say that if a script has been running for
//4 hours (14400 sec) then we are completely confident in its ability
2021-09-24 22:34:21 +02:00
let confidence = runningScript.onlineRunningTime / 14400;
2021-09-05 01:09:30 +02:00
if (confidence >= 1) {
confidence = 1;
}
2021-09-05 01:09:30 +02:00
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
2021-09-05 01:09:30 +02:00
// Grow
2022-01-16 01:45:03 +01:00
for (const hostname of Object.keys(runningScript.dataMap)) {
if (Object.hasOwn(runningScript.dataMap, hostname)) {
2021-10-07 23:55:49 +02:00
if (runningScript.dataMap[hostname][2] == 0 || runningScript.dataMap[hostname][2] == null) {
2021-09-05 01:09:30 +02:00
continue;
}
2021-10-07 23:55:49 +02:00
const serv = GetServer(hostname);
2021-09-05 01:09:30 +02:00
if (serv == null) {
continue;
}
const timesGrown = Math.round(
2021-10-07 23:55:49 +02:00
((0.5 * runningScript.dataMap[hostname][2]) / runningScript.onlineRunningTime) * timePassed,
2021-09-05 01:09:30 +02:00
);
2021-09-24 22:34:21 +02:00
runningScript.log(`Called on ${serv.hostname} ${timesGrown} times while offline`);
2021-10-07 22:56:01 +02:00
const host = GetServer(runningScript.server);
2021-10-07 22:04:04 +02:00
if (host === null) throw new Error("getServer of null key?");
2021-09-24 22:34:21 +02:00
if (!(serv instanceof Server)) throw new Error("trying to grow a non-normal server");
2022-09-18 03:09:15 +02:00
const growth = processSingleServerGrowth(serv, timesGrown, host.cpuCores);
runningScript.log(`'${serv.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`);
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
// Offline EXP gain
// A script's offline production will always be at most half of its online production.
2021-09-24 22:34:21 +02:00
const expGain = confidence * (runningScript.onlineExpGained / runningScript.onlineRunningTime) * timePassed;
2021-09-05 01:09:30 +02:00
Player.gainHackingExp(expGain);
const moneyGain =
(runningScript.onlineMoneyMade / runningScript.onlineRunningTime) * timePassed * CONSTANTS.OfflineHackingIncome;
// money is given to player during engine load
Player.scriptProdSinceLastAug += moneyGain;
2021-09-05 01:09:30 +02:00
// Update script stats
2021-09-24 22:34:21 +02:00
runningScript.offlineRunningTime += timePassed;
runningScript.offlineExpGained += expGain;
runningScript.offlineMoneyMade += moneyGain;
2021-09-05 01:09:30 +02:00
// Weaken
2022-01-16 01:45:03 +01:00
for (const hostname of Object.keys(runningScript.dataMap)) {
if (Object.hasOwn(runningScript.dataMap, hostname)) {
2021-10-07 23:55:49 +02:00
if (runningScript.dataMap[hostname][3] == 0 || runningScript.dataMap[hostname][3] == null) {
2021-09-05 01:09:30 +02:00
continue;
}
2021-10-07 23:55:49 +02:00
const serv = GetServer(hostname);
2021-09-05 01:09:30 +02:00
if (serv == null) {
continue;
}
2021-09-24 22:34:21 +02:00
if (!(serv instanceof Server)) throw new Error("trying to weaken a non-normal server");
2021-10-07 22:56:01 +02:00
const host = GetServer(runningScript.server);
2021-10-07 22:04:04 +02:00
if (host === null) throw new Error("getServer of null key?");
2021-09-05 01:09:30 +02:00
const timesWeakened = Math.round(
2021-10-07 23:55:49 +02:00
((0.5 * runningScript.dataMap[hostname][3]) / runningScript.onlineRunningTime) * timePassed,
2021-09-05 01:09:30 +02:00
);
2021-09-24 22:34:21 +02:00
runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
2021-09-05 01:09:30 +02:00
const coreBonus = 1 + (host.cpuCores - 1) / 16;
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus);
}
2021-09-05 01:09:30 +02:00
}
}
//Returns a RunningScript map containing scripts matching the filename and
//arguments on the designated server, or null if none were found
export function findRunningScripts(
path: ScriptFilePath,
2021-12-03 20:44:32 +01:00
args: (string | number | boolean)[],
2021-09-25 07:26:03 +02:00
server: BaseServer,
): Map<number, RunningScript> | null {
return server.runningScriptMap.get(scriptKey(path, args)) ?? null;
}
2021-04-21 14:20:26 +02:00
//Returns a RunningScript object matching the pid on the
//designated server, and false otherwise
2021-09-25 07:26:03 +02:00
export function findRunningScriptByPid(pid: number, server: BaseServer): RunningScript | null {
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
const ws = workerScripts.get(pid);
// Return null if no ws found or if it's on a different server.
if (!ws) return null;
if (ws.scriptRef.server !== server.hostname) return null;
return ws.scriptRef;
v0.51.6 (#905) * Make command `cd` without arguments an alias for `cd /` (#853) In most shells `cd` without arguments takes you to the home directory of the current user. I keep trying to do this due to muscle memory from working in terminals, so I figured I'd make it do something useful. There is no home directory in the game, but going to / is the closest thing we have, since that is the starting point for the user in the game. * Add new `backdoor` terminal command (#852) * Add the backdoor command to the terminal This command will perform a manual hack without rewarding money. It will be used for the story, mainly for faction hacking tests * Add tab completion for backdoor command * Add help text for backdoor command * Change condition syntax to be more consistent with others * Extract reused code block so it is always called after actions * Update documentation for new backdoor command Modified references to manual hack as it isn't for factions anymore * Remove extra parenthesis * Rename manuallyHacked to backdoorInstalled * Fix typo * Change faction test messages to use backdoor instad of hack * Rename more instances of manuallyHacked * fixed typo in helptext of darkweb buy (#858) * Fix typos and unify descriptions of augmentations (#859) Made an attempt to... - give all "+rep% company/faction" the same text - make all augmentations with a single effect use a single line to describe the effect - make all effects end with a period * Made Cashroot starter kit display its tooltip with the money formatted properly and in gold * fix typo in docs (#860) * Initial code for Casino Card Deck implementation * Casino Blackjack Implementation * Update some tools (eslint, typescript) * Blackjack code cleanup * Update README_contribution * Update ScriptHelpers.js (#861) expand error message * More augmentation typo fixes (#862) * Add Netscript function getCurrentScript (#856) Add netscript function that returns the current script. * Added milestones menu to guide new players. (#865) Milestone menu * fix typos in milestones (#866) Co-authored-by: sschmidTU <s.schmid@phonicscore.com> * Corrupt location title when backdoor is installed (#864) * Add corruptableText component * Corrupt location title if backdoor is installed * Formatting * Add helper to check value of backdoorInstalled Helper could be oneline but it would make it less readable * Fix some formatting * Add settings option to disable text effects * Import useState * getRunningScript (#867) * Replaced getCurrentScript with getRunningScript * Bunch of smaller fixes (#904) Fix #884 Fix #879 Fix #878 Fix #876 Fix #874 Fix #873 Fix #887 Fix #891 Fix #895 * rework the early servers to be more noob friendly (#903) * v0.51.6 Co-authored-by: Andreas Eriksson <2691182+AndreasTPC@users.noreply.github.com> Co-authored-by: Jack <jackdewinter1@gmail.com> Co-authored-by: Teun Pronk <5228255+Crownie88@users.noreply.github.com> Co-authored-by: Pimvgd <Pimvgd@gmail.com> Co-authored-by: Daniel Xie <daniel.xie@flockfreight.com> Co-authored-by: Simon <33069673+sschmidTU@users.noreply.github.com> Co-authored-by: sschmidTU <s.schmid@phonicscore.com>
2021-04-29 02:07:26 +02:00
}