bitburner-src/src/Script/ScriptHelpers.js

127 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-09-05 01:09:30 +02:00
import { CONSTANTS } from "../Constants";
import { Player } from "../Player";
import { AllServers } from "../Server/AllServers";
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { numeralWrapper } from "../ui/numeralFormat";
import { compareArrays } from "../../utils/helpers/compareArrays";
export function scriptCalculateOfflineProduction(runningScriptObj) {
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
let confidence = runningScriptObj.onlineRunningTime / 14400;
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
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][2] == 0 ||
runningScriptObj.dataMap[ip][2] == null
) {
continue;
}
const serv = AllServers[ip];
if (serv == null) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScriptObj.dataMap[ip][2]) /
runningScriptObj.onlineRunningTime) *
timePassed,
);
runningScriptObj.log(
`Called on ${serv.hostname} ${timesGrown} times while offline`,
);
const host = AllServers[runningScriptObj.server];
const growth = processSingleServerGrowth(
serv,
timesGrown,
Player,
host.cpuCores,
);
runningScriptObj.log(
`'${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
// Offline EXP gain
// A script's offline production will always be at most half of its online production.
const expGain =
confidence *
(runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) *
timePassed;
Player.gainHackingExp(expGain);
2021-09-05 01:09:30 +02:00
// Update script stats
runningScriptObj.offlineRunningTime += timePassed;
runningScriptObj.offlineExpGained += expGain;
2021-09-05 01:09:30 +02:00
// Weaken
for (const ip in runningScriptObj.dataMap) {
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
if (
runningScriptObj.dataMap[ip][3] == 0 ||
runningScriptObj.dataMap[ip][3] == null
) {
continue;
}
const serv = AllServers[ip];
if (serv == null) {
continue;
}
const host = AllServers[runningScriptObj.server];
const timesWeakened = Math.round(
((0.5 * runningScriptObj.dataMap[ip][3]) /
runningScriptObj.onlineRunningTime) *
timePassed,
);
runningScriptObj.log(
`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`,
);
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
export function findRunningScript(filename, args, server) {
2021-09-05 01:09:30 +02:00
for (var i = 0; i < server.runningScripts.length; ++i) {
if (
server.runningScripts[i].filename === filename &&
compareArrays(server.runningScripts[i].args, args)
) {
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
export function findRunningScriptByPid(pid, server) {
2021-09-05 01:09:30 +02:00
for (var i = 0; i < server.runningScripts.length; ++i) {
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
}