bitburner-src/src/Script/RunningScript.ts

135 lines
4.0 KiB
TypeScript
Raw Normal View History

/**
* Class representing a Script instance that is actively running.
* A Script can have multiple active instances
*/
import { Script } from "./Script";
import { FconfSettings } from "../Fconf/FconfSettings";
import { Settings } from "../Settings/Settings";
import { IMap } from "../types";
import { post } from "../ui/postToTerminal";
import {
Generic_fromJSON,
Generic_toJSON,
2021-04-30 05:52:56 +02:00
Reviver,
} from "../../utils/JSONReviver";
import { getTimestamp } from "../../utils/helpers/getTimestamp";
2019-03-03 04:15:10 +01:00
export class RunningScript {
// Initializes a RunningScript Object from a JSON save state
static fromJSON(value: any): RunningScript {
return Generic_fromJSON(RunningScript, value.data);
}
// Script arguments
args: any[] = [];
// Map of [key: server ip] -> Hacking data. Used for offline progress calculations.
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
2019-03-03 04:15:10 +01:00
dataMap: IMap<number[]> = {};
// Script filename
2021-04-30 05:52:56 +02:00
filename = "";
2019-03-03 04:15:10 +01:00
// This script's logs. An array of log entries
logs: string[] = [];
// Flag indicating whether the logs have been updated since
// the last time the UI was updated
2021-04-30 05:52:56 +02:00
logUpd = false;
2019-03-03 04:15:10 +01:00
// Total amount of hacking experience earned from this script when offline
2021-04-30 05:52:56 +02:00
offlineExpGained = 0;
2019-03-03 04:15:10 +01:00
// Total amount of money made by this script when offline
2021-04-30 05:52:56 +02:00
offlineMoneyMade = 0;
2019-03-03 04:15:10 +01:00
// Number of seconds that the script has been running offline
2021-04-30 05:52:56 +02:00
offlineRunningTime = 0.01;
2019-03-03 04:15:10 +01:00
// Total amount of hacking experience earned from this script when online
2021-04-30 05:52:56 +02:00
onlineExpGained = 0;
2019-03-03 04:15:10 +01:00
// Total amount of money made by this script when online
2021-04-30 05:52:56 +02:00
onlineMoneyMade = 0;
2019-03-03 04:15:10 +01:00
// Number of seconds that this script has been running online
2021-04-30 05:52:56 +02:00
onlineRunningTime = 0.01;
2019-03-03 04:15:10 +01:00
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
2021-04-30 05:52:56 +02:00
pid = -1;
2019-03-03 04:15:10 +01:00
// How much RAM this script uses for ONE thread
2021-04-30 05:52:56 +02:00
ramUsage = 0;
2019-03-03 04:15:10 +01:00
// IP of the server on which this script is running
2021-04-30 05:52:56 +02:00
server = "";
2019-03-03 04:15:10 +01:00
// Number of threads that this script is running with
2021-04-30 05:52:56 +02:00
threads = 1;
2019-03-03 04:15:10 +01:00
constructor(script: Script | null = null, args: any[] = []) {
if (script == null) { return; }
this.filename = script.filename;
this.args = args;
2019-05-18 00:51:28 +02:00
this.server = script.server;
2019-03-03 04:15:10 +01:00
this.ramUsage = script.ramUsage;
}
log(txt: string): void {
2019-03-03 04:15:10 +01:00
if (this.logs.length > Settings.MaxLogCapacity) {
this.logs.shift();
}
2019-05-18 00:51:28 +02:00
2019-03-03 04:15:10 +01:00
let logEntry = txt;
if (FconfSettings.ENABLE_TIMESTAMPS) {
logEntry = "[" + getTimestamp() + "] " + logEntry;
}
2019-05-18 00:51:28 +02:00
2019-03-03 04:15:10 +01:00
this.logs.push(logEntry);
this.logUpd = true;
}
displayLog(): void {
2021-04-30 05:52:56 +02:00
for (let i = 0; i < this.logs.length; ++i) {
2019-03-03 04:15:10 +01:00
post(this.logs[i]);
}
}
clearLog(): void {
2019-03-03 04:15:10 +01:00
this.logs.length = 0;
}
// Update the moneyStolen and numTimesHack maps when hacking
2021-04-30 05:52:56 +02:00
recordHack(serverIp: string, moneyGained: number, n=1) {
2019-03-03 04:15:10 +01:00
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][0] += moneyGained;
this.dataMap[serverIp][1] += n;
}
// Update the grow map when calling grow()
2021-04-30 05:52:56 +02:00
recordGrow(serverIp: string, n=1) {
2019-03-03 04:15:10 +01:00
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][2] += n;
}
// Update the weaken map when calling weaken() {
2021-04-30 05:52:56 +02:00
recordWeaken(serverIp: string, n=1) {
2019-03-03 04:15:10 +01:00
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
}
this.dataMap[serverIp][3] += n;
}
// Serialize the current object to a JSON save state
toJSON(): any {
return Generic_toJSON("RunningScript", this);
}
}
Reviver.constructors.RunningScript = RunningScript;