2019-05-17 22:41:16 +02:00
|
|
|
/**
|
|
|
|
* Class representing a Script instance that is actively running.
|
|
|
|
* A Script can have multiple active instances
|
|
|
|
*/
|
2019-05-05 06:03:40 +02:00
|
|
|
import { Script } from "./Script";
|
2021-12-13 01:39:53 +01:00
|
|
|
import { ScriptUrl } from "./ScriptUrl";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { Settings } from "../Settings/Settings";
|
|
|
|
import { IMap } from "../types";
|
2021-09-16 08:52:45 +02:00
|
|
|
import { Terminal } from "../Terminal";
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../utils/JSONReviver";
|
2021-11-11 16:07:16 +01:00
|
|
|
import { formatTime } from "../utils/helpers/formatTime";
|
2019-03-03 04:15:10 +01:00
|
|
|
|
|
|
|
export class RunningScript {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Script arguments
|
|
|
|
args: any[] = [];
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
// Map of [key: hostname] -> Hacking data. Used for offline progress calculations.
|
2021-09-05 01:09:30 +02:00
|
|
|
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
|
|
|
|
dataMap: IMap<number[]> = {};
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Script filename
|
|
|
|
filename = "";
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// This script's logs. An array of log entries
|
|
|
|
logs: string[] = [];
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Flag indicating whether the logs have been updated since
|
|
|
|
// the last time the UI was updated
|
|
|
|
logUpd = false;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Total amount of hacking experience earned from this script when offline
|
|
|
|
offlineExpGained = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Total amount of money made by this script when offline
|
|
|
|
offlineMoneyMade = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Number of seconds that the script has been running offline
|
|
|
|
offlineRunningTime = 0.01;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Total amount of hacking experience earned from this script when online
|
|
|
|
onlineExpGained = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Total amount of money made by this script when online
|
|
|
|
onlineMoneyMade = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Number of seconds that this script has been running online
|
|
|
|
onlineRunningTime = 0.01;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Process ID. Must be an integer and equals the PID of corresponding WorkerScript
|
|
|
|
pid = -1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// How much RAM this script uses for ONE thread
|
|
|
|
ramUsage = 0;
|
2019-06-19 10:03:08 +02:00
|
|
|
|
2021-10-07 23:55:49 +02:00
|
|
|
// hostname of the server on which this script is running
|
2021-09-05 01:09:30 +02:00
|
|
|
server = "";
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Number of threads that this script is running with
|
|
|
|
threads = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-12-13 01:39:53 +01:00
|
|
|
// Script urls for the current running script for translating urls back to file names in errors
|
|
|
|
dependencies: ScriptUrl[] = [];
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
constructor(script: Script | null = null, args: any[] = []) {
|
|
|
|
if (script == null) {
|
|
|
|
return;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
this.filename = script.filename;
|
|
|
|
this.args = args;
|
|
|
|
this.server = script.server;
|
|
|
|
this.ramUsage = script.ramUsage;
|
2021-12-13 01:39:53 +01:00
|
|
|
this.dependencies = script.dependencies;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log(txt: string): void {
|
|
|
|
if (this.logs.length > Settings.MaxLogCapacity) {
|
|
|
|
this.logs.shift();
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
let logEntry = txt;
|
2021-11-11 16:07:16 +01:00
|
|
|
if (Settings.TimestampsFormat) {
|
|
|
|
logEntry = "[" + formatTime(Settings.TimestampsFormat) + "] " + logEntry;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
this.logs.push(logEntry);
|
|
|
|
this.logUpd = true;
|
|
|
|
}
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
displayLog(): void {
|
|
|
|
for (let i = 0; i < this.logs.length; ++i) {
|
2021-09-16 08:52:45 +02:00
|
|
|
Terminal.print(this.logs[i]);
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clearLog(): void {
|
|
|
|
this.logs.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the moneyStolen and numTimesHack maps when hacking
|
2021-10-07 23:55:49 +02:00
|
|
|
recordHack(hostname: string, moneyGained: number, n = 1): void {
|
|
|
|
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
|
|
|
|
this.dataMap[hostname] = [0, 0, 0, 0];
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
this.dataMap[hostname][0] += moneyGained;
|
|
|
|
this.dataMap[hostname][1] += n;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the grow map when calling grow()
|
2021-10-07 23:55:49 +02:00
|
|
|
recordGrow(hostname: string, n = 1): void {
|
|
|
|
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
|
|
|
|
this.dataMap[hostname] = [0, 0, 0, 0];
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
this.dataMap[hostname][2] += n;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the weaken map when calling weaken() {
|
2021-10-07 23:55:49 +02:00
|
|
|
recordWeaken(hostname: string, n = 1): void {
|
|
|
|
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
|
|
|
|
this.dataMap[hostname] = [0, 0, 0, 0];
|
2021-05-01 09:17:31 +02:00
|
|
|
}
|
2021-10-07 23:55:49 +02:00
|
|
|
this.dataMap[hostname][3] += n;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the current object to a JSON save state
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("RunningScript", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes a RunningScript Object from a JSON save state
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
static fromJSON(value: any): RunningScript {
|
|
|
|
return Generic_fromJSON(RunningScript, value.data);
|
|
|
|
}
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.RunningScript = RunningScript;
|