bitburner-src/src/Script/ScriptHelpers.ts

111 lines
4.3 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";
import { RunningScript } from "../Script/RunningScript";
2021-10-07 22:56:01 +02:00
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { GetServer } from "../Server/AllServers";
import { numeralWrapper } from "../ui/numeralFormat";
2021-09-25 20:42:57 +02:00
import { compareArrays } from "../utils/helpers/compareArrays";
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)) {
2021-10-07 23:55:49 +02:00
if (runningScript.dataMap.hasOwnProperty(hostname)) {
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");
2021-09-09 05:47:34 +02:00
const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores);
2021-09-24 22:34:21 +02:00
runningScript.log(
2021-09-09 05:47:34 +02:00
`'${serv.hostname}' grown by ${numeralWrapper.format(growth * 100 - 100, "0.000000%")} while offline`,
2021-09-05 01:09:30 +02:00
);
}
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);
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;
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)) {
2021-10-07 23:55:49 +02:00
if (runningScript.dataMap.hasOwnProperty(hostname)) {
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 object matching the filename and arguments on the
//designated server, and false otherwise
2021-09-25 07:26:03 +02:00
export function findRunningScript(
filename: string,
2021-12-03 20:44:32 +01:00
args: (string | number | boolean)[],
2021-09-25 07:26:03 +02:00
server: BaseServer,
): RunningScript | null {
for (let i = 0; i < server.runningScripts.length; ++i) {
2021-09-09 05:47:34 +02:00
if (server.runningScripts[i].filename === filename && compareArrays(server.runningScripts[i].args, args)) {
2021-09-05 01:09:30 +02:00
return server.runningScripts[i];
}
2021-09-05 01:09:30 +02:00
}
return 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 {
for (let i = 0; i < server.runningScripts.length; ++i) {
2021-09-05 01:09:30 +02:00
if (server.runningScripts[i].pid === pid) {
return server.runningScripts[i];
2021-04-21 14:20:26 +02:00
}
2021-09-05 01:09:30 +02:00
}
return null;
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
}