finish convert to hostname

This commit is contained in:
Olivier Gagnon
2021-10-07 17:55:49 -04:00
parent 2958034ad4
commit 7d0536a4d2
37 changed files with 211 additions and 314 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
+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();
}
+10 -10
View File
@@ -27,17 +27,17 @@ 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 = GetServer(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 = GetServer(runningScript.server);
@@ -60,12 +60,12 @@ 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 = GetServer(ip);
const serv = GetServer(hostname);
if (serv == null) {
continue;
}
@@ -74,7 +74,7 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
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;