2019-05-05 06:03:40 +02:00
|
|
|
import { Script } from "./Script";
|
|
|
|
|
2019-05-12 04:20:20 +02:00
|
|
|
import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { calculateRamUsage } from "./RamCalculations";
|
|
|
|
import { isScriptFilename } from "./ScriptHelpersTS";
|
|
|
|
|
|
|
|
import {CONSTANTS} from "../Constants";
|
|
|
|
import {Engine} from "../engine";
|
|
|
|
import { parseFconfSettings } from "../Fconf/Fconf";
|
|
|
|
import {
|
|
|
|
iTutorialSteps,
|
|
|
|
iTutorialNextStep,
|
2021-04-30 05:52:56 +02:00
|
|
|
ITutorial,
|
2019-05-05 06:03:40 +02:00
|
|
|
} from "../InteractiveTutorial";
|
|
|
|
import { Player } from "../Player";
|
2021-04-20 03:26:51 +02:00
|
|
|
import { CursorPositions } from "../ScriptEditor/CursorPositions";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { AllServers } from "../Server/AllServers";
|
|
|
|
import { processSingleServerGrowth } from "../Server/ServerHelpers";
|
|
|
|
import { Settings } from "../Settings/Settings";
|
|
|
|
import { EditorSetting } from "../Settings/SettingEnums";
|
|
|
|
import { isValidFilePath } from "../Terminal/DirectoryHelpers";
|
|
|
|
import { TextFile } from "../TextFile";
|
|
|
|
|
|
|
|
import { Page, routing } from "../ui/navigationTracking";
|
|
|
|
import { numeralWrapper } from "../ui/numeralFormat";
|
|
|
|
|
|
|
|
import { dialogBoxCreate } from "../../utils/DialogBox";
|
|
|
|
import { compareArrays } from "../../utils/helpers/compareArrays";
|
|
|
|
import { createElement } from "../../utils/uiHelpers/createElement";
|
2018-05-06 00:13:35 +02:00
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
export function scriptCalculateOfflineProduction(runningScriptObj) {
|
2016-12-19 21:59:13 +01:00
|
|
|
//The Player object stores the last update time from when we were online
|
2021-05-11 02:11:03 +02:00
|
|
|
const thisUpdate = new Date().getTime();
|
|
|
|
const lastUpdate = Player.lastUpdate;
|
|
|
|
const timePassed = (thisUpdate - lastUpdate) / 1000; //Seconds
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2016-12-19 21:59:13 +01: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
|
2017-05-05 03:08:44 +02:00
|
|
|
//4 hours (14400 sec) then we are completely confident in its ability
|
2021-05-11 02:11:03 +02:00
|
|
|
let confidence = (runningScriptObj.onlineRunningTime) / 14400;
|
2016-12-19 21:59:13 +01:00
|
|
|
if (confidence >= 1) {confidence = 1;}
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2017-06-19 01:23:50 +02:00
|
|
|
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
// Grow
|
2021-05-11 02:11:03 +02:00
|
|
|
for (const ip in runningScriptObj.dataMap) {
|
2017-06-19 01:23:50 +02:00
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) {continue;}
|
2021-05-11 02:11:03 +02:00
|
|
|
const serv = AllServers[ip];
|
2017-06-13 17:58:31 +02:00
|
|
|
if (serv == null) {continue;}
|
2021-05-11 02:11:03 +02:00
|
|
|
const timesGrown = Math.round(0.5 * runningScriptObj.dataMap[ip][2] / runningScriptObj.onlineRunningTime * timePassed);
|
|
|
|
runningScriptObj.log(`Called on ${serv.hostname} ${timesGrown} times while offline`);
|
2021-08-19 22:37:59 +02:00
|
|
|
const host = AllServers[runningScriptObj.server];
|
|
|
|
const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores);
|
2021-05-11 02:11:03 +02:00
|
|
|
runningScriptObj.log(`'${serv.hostname}' grown by ${numeralWrapper.format(growth * 100 - 100, '0.000000%')} while offline`);
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
// Offline EXP gain
|
|
|
|
// A script's offline production will always be at most half of its online production.
|
2021-05-11 02:11:03 +02:00
|
|
|
const expGain = confidence * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed;
|
2017-05-05 03:08:44 +02:00
|
|
|
Player.gainHackingExp(expGain);
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
// Update script stats
|
2017-06-17 04:53:57 +02:00
|
|
|
runningScriptObj.offlineRunningTime += timePassed;
|
|
|
|
runningScriptObj.offlineExpGained += expGain;
|
2017-07-25 16:39:56 +02:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
// Weaken
|
2021-05-11 02:11:03 +02:00
|
|
|
for (const ip in runningScriptObj.dataMap) {
|
2017-06-19 01:23:50 +02:00
|
|
|
if (runningScriptObj.dataMap.hasOwnProperty(ip)) {
|
|
|
|
if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) {continue;}
|
2021-05-11 02:11:03 +02:00
|
|
|
const serv = AllServers[ip];
|
2017-06-01 03:57:49 +02:00
|
|
|
if (serv == null) {continue;}
|
2021-08-19 22:37:59 +02:00
|
|
|
const host = AllServers[runningScriptObj.server];
|
2021-05-11 02:11:03 +02:00
|
|
|
const timesWeakened = Math.round(0.5 * runningScriptObj.dataMap[ip][3] / runningScriptObj.onlineRunningTime * timePassed);
|
|
|
|
runningScriptObj.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
|
2021-08-19 22:37:59 +02:00
|
|
|
const coreBonus = 1+(host.cpuCores-1)/16;
|
|
|
|
serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus);
|
2017-06-01 03:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-10 19:42:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//Returns a RunningScript object matching the filename and arguments on the
|
|
|
|
//designated server, and false otherwise
|
2019-03-05 02:40:28 +01:00
|
|
|
export function findRunningScript(filename, args, server) {
|
2017-06-17 04:53:57 +02:00
|
|
|
for (var i = 0; i < server.runningScripts.length; ++i) {
|
2019-05-02 00:20:14 +02:00
|
|
|
if (server.runningScripts[i].filename === filename &&
|
2017-06-17 04:53:57 +02:00
|
|
|
compareArrays(server.runningScripts[i].args, args)) {
|
|
|
|
return server.runningScripts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
for (var i = 0; i < server.runningScripts.length; ++i) {
|
|
|
|
if (server.runningScripts[i].pid === pid) {
|
|
|
|
return server.runningScripts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2021-04-29 02:07:26 +02:00
|
|
|
}
|