merge dev

This commit is contained in:
Olivier Gagnon
2021-10-07 17:58:32 -04:00
59 changed files with 617 additions and 671 deletions
+15 -15
View File
@@ -14,7 +14,7 @@ export class RunningScript {
// Script arguments
args: any[] = [];
// Map of [key: server ip] -> Hacking data. Used for offline progress calculations.
// Map of [key: hostname] -> Hacking data. Used for offline progress calculations.
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
dataMap: IMap<number[]> = {};
@@ -52,7 +52,7 @@ export class RunningScript {
// How much RAM this script uses for ONE thread
ramUsage = 0;
// IP of the server on which this script is running
// hostname of the server on which this script is running
server = "";
// Number of threads that this script is running with
@@ -93,28 +93,28 @@ export class RunningScript {
}
// Update the moneyStolen and numTimesHack maps when hacking
recordHack(serverIp: string, moneyGained: number, n = 1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
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];
}
this.dataMap[serverIp][0] += moneyGained;
this.dataMap[serverIp][1] += n;
this.dataMap[hostname][0] += moneyGained;
this.dataMap[hostname][1] += n;
}
// Update the grow map when calling grow()
recordGrow(serverIp: string, n = 1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
recordGrow(hostname: string, n = 1): void {
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
this.dataMap[hostname] = [0, 0, 0, 0];
}
this.dataMap[serverIp][2] += n;
this.dataMap[hostname][2] += n;
}
// Update the weaken map when calling weaken() {
recordWeaken(serverIp: string, n = 1): void {
if (this.dataMap[serverIp] == null || this.dataMap[serverIp].constructor !== Array) {
this.dataMap[serverIp] = [0, 0, 0, 0];
recordWeaken(hostname: string, n = 1): void {
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
this.dataMap[hostname] = [0, 0, 0, 0];
}
this.dataMap[serverIp][3] += n;
this.dataMap[hostname][3] += n;
}
// Serialize the current object to a JSON save state
+2 -2
View File
@@ -1,4 +1,4 @@
import { AllServers } from "../Server/AllServers";
import { GetServer } from "../Server/AllServers";
import { RunningScript } from "./RunningScript";
export function getRamUsageFromRunningScript(script: RunningScript): number {
@@ -6,7 +6,7 @@ export function getRamUsageFromRunningScript(script: RunningScript): number {
return script.ramUsage; // Use cached value
}
const server = AllServers[script.server];
const server = GetServer(script.server);
if (server == null) {
return 0;
}
+4 -4
View File
@@ -37,14 +37,14 @@ export class Script {
// Amount of RAM this Script requres to run
ramUsage = 0;
// IP of server that this script is on.
// hostname of server that this script is on.
server = "";
constructor(fn = "", code = "", server = "", otherScripts: Script[] = []) {
this.filename = fn;
this.code = code;
this.ramUsage = 0;
this.server = server; // IP of server this script is on
this.server = server; // hostname of server this script is on
this.module = "";
this.moduleSequenceNumber = ++globalModuleSequenceNumber;
if (this.code !== "") {
@@ -90,12 +90,12 @@ export class Script {
* @param {string} code - The new contents of the script
* @param {Script[]} otherScripts - Other scripts on the server. Used to process imports
*/
saveScript(filename: string, code: string, serverIp: string, otherScripts: Script[]): void {
saveScript(filename: string, code: string, hostname: string, otherScripts: Script[]): void {
// Update code and filename
this.code = code.replace(/^\s+|\s+$/g, "");
this.filename = filename;
this.server = serverIp;
this.server = hostname;
this.updateRamUsage(otherScripts);
this.markUpdated();
}
+15 -13
View File
@@ -1,10 +1,10 @@
import { CONSTANTS } from "../Constants";
import { Player } from "../Player";
import { AllServers } from "../Server/AllServers";
import { BaseServer } from "../Server/BaseServer";
import { Server } from "../Server/Server";
import { RunningScript } from "../Script/RunningScript";
import { processSingleServerGrowth } from "../Server/ServerHelpers";
import { GetServer } from "../Server/AllServers";
import { numeralWrapper } from "../ui/numeralFormat";
@@ -27,20 +27,21 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
// Grow
for (const ip in runningScript.dataMap) {
if (runningScript.dataMap.hasOwnProperty(ip)) {
if (runningScript.dataMap[ip][2] == 0 || runningScript.dataMap[ip][2] == null) {
for (const hostname in runningScript.dataMap) {
if (runningScript.dataMap.hasOwnProperty(hostname)) {
if (runningScript.dataMap[hostname][2] == 0 || runningScript.dataMap[hostname][2] == null) {
continue;
}
const serv = AllServers[ip];
const serv = GetServer(hostname);
if (serv == null) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScript.dataMap[ip][2]) / runningScript.onlineRunningTime) * timePassed,
((0.5 * runningScript.dataMap[hostname][2]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called on ${serv.hostname} ${timesGrown} times while offline`);
const host = AllServers[runningScript.server];
const host = GetServer(runningScript.server);
if (host === null) throw new Error("getServer of null key?");
if (!(serv instanceof Server)) throw new Error("trying to grow a non-normal server");
const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores);
runningScript.log(
@@ -59,20 +60,21 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
runningScript.offlineExpGained += expGain;
// Weaken
for (const ip in runningScript.dataMap) {
if (runningScript.dataMap.hasOwnProperty(ip)) {
if (runningScript.dataMap[ip][3] == 0 || runningScript.dataMap[ip][3] == null) {
for (const hostname in runningScript.dataMap) {
if (runningScript.dataMap.hasOwnProperty(hostname)) {
if (runningScript.dataMap[hostname][3] == 0 || runningScript.dataMap[hostname][3] == null) {
continue;
}
const serv = AllServers[ip];
const serv = GetServer(hostname);
if (serv == null) {
continue;
}
if (!(serv instanceof Server)) throw new Error("trying to weaken a non-normal server");
const host = AllServers[runningScript.server];
const host = GetServer(runningScript.server);
if (host === null) throw new Error("getServer of null key?");
const timesWeakened = Math.round(
((0.5 * runningScript.dataMap[ip][3]) / runningScript.onlineRunningTime) * timePassed,
((0.5 * runningScript.dataMap[hostname][3]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
const coreBonus = 1 + (host.cpuCores - 1) / 16;