2019-03-25 04:03:24 +01:00
|
|
|
import { CONSTANTS } from "../Constants";
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
import { IHacknetNode } from "./IHacknetNode";
|
|
|
|
|
2019-03-25 04:03:24 +01:00
|
|
|
import { BaseServer } from "../Server/BaseServer";
|
2021-03-31 06:45:21 +02:00
|
|
|
import { HacknetServerConstants } from "./data/Constants";
|
2021-09-05 01:09:30 +02:00
|
|
|
import {
|
|
|
|
calculateHashGainRate,
|
|
|
|
calculateLevelUpgradeCost,
|
|
|
|
calculateRamUpgradeCost,
|
|
|
|
calculateCoreUpgradeCost,
|
|
|
|
calculateCacheUpgradeCost,
|
2021-03-31 06:45:21 +02:00
|
|
|
} from "./formulas/HacknetServers";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
2021-09-25 20:42:57 +02:00
|
|
|
import { createRandomIp } from "../utils/IPAddress";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
2023-04-28 00:21:06 +02:00
|
|
|
import { IReviverValue, constructorsForReviver } from "../utils/JSONReviver";
|
2022-10-10 00:42:14 +02:00
|
|
|
import { Player } from "@player";
|
2019-03-25 04:03:24 +01:00
|
|
|
|
|
|
|
interface IConstructorParams {
|
2021-09-05 01:09:30 +02:00
|
|
|
adminRights?: boolean;
|
|
|
|
hostname: string;
|
|
|
|
ip?: string;
|
|
|
|
isConnectedTo?: boolean;
|
|
|
|
maxRam?: number;
|
|
|
|
organizationName?: string;
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Hacknet Servers - Reworked Hacknet Node mechanic for BitNode-9 */
|
2019-03-25 04:03:24 +01:00
|
|
|
export class HacknetServer extends BaseServer implements IHacknetNode {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Cache level. Affects hash Capacity
|
|
|
|
cache = 1;
|
|
|
|
|
|
|
|
// Number of cores. Improves hash production
|
|
|
|
cores = 1;
|
2021-11-11 23:00:36 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Number of hashes that can be stored by this Hacknet Server
|
|
|
|
hashCapacity = 0;
|
|
|
|
|
|
|
|
// Hashes produced per second
|
|
|
|
hashRate = 0;
|
|
|
|
|
|
|
|
// Similar to Node level. Improves hash production
|
|
|
|
level = 1;
|
|
|
|
|
|
|
|
// How long this HacknetServer has existed, in seconds
|
|
|
|
onlineTimeSeconds = 0;
|
|
|
|
|
|
|
|
// Total number of hashes earned by this server
|
|
|
|
totalHashesGenerated = 0;
|
|
|
|
|
2022-10-09 07:25:31 +02:00
|
|
|
// Flag indicating whether this is a purchased server
|
2021-11-11 23:00:36 +01:00
|
|
|
purchasedByPlayer = true;
|
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
constructor(params: IConstructorParams = { hostname: "", ip: createRandomIp() }) {
|
2021-09-05 01:09:30 +02:00
|
|
|
super(params);
|
|
|
|
|
|
|
|
this.maxRam = 1;
|
|
|
|
this.updateHashCapacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateCacheUpgradeCost(levels: number): number {
|
|
|
|
return calculateCacheUpgradeCost(this.cache, levels);
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateCoreUpgradeCost(levels: number, costMult: number): number {
|
|
|
|
return calculateCoreUpgradeCost(this.cores, levels, costMult);
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateLevelUpgradeCost(levels: number, costMult: number): number {
|
|
|
|
return calculateLevelUpgradeCost(this.level, levels, costMult);
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateRamUpgradeCost(levels: number, costMult: number): number {
|
|
|
|
return calculateRamUpgradeCost(this.maxRam, levels, costMult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process this Hacknet Server in the game loop. Returns the number of hashes generated
|
|
|
|
process(numCycles = 1): number {
|
|
|
|
const seconds = (numCycles * CONSTANTS.MilliPerCycle) / 1000;
|
2021-10-04 03:33:48 +02:00
|
|
|
this.onlineTimeSeconds += seconds;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-10-04 03:33:48 +02:00
|
|
|
const hashes = this.hashRate * seconds;
|
|
|
|
this.totalHashesGenerated += hashes;
|
|
|
|
|
|
|
|
return hashes;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
upgradeCache(levels: number): void {
|
2021-09-09 05:47:34 +02:00
|
|
|
this.cache = Math.min(HacknetServerConstants.MaxCache, Math.round(this.cache + levels));
|
2021-09-05 01:09:30 +02:00
|
|
|
this.updateHashCapacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
upgradeCore(levels: number, prodMult: number): void {
|
2021-09-09 05:47:34 +02:00
|
|
|
this.cores = Math.min(HacknetServerConstants.MaxCores, Math.round(this.cores + levels));
|
2021-09-05 01:09:30 +02:00
|
|
|
this.updateHashRate(prodMult);
|
2021-11-11 23:00:36 +01:00
|
|
|
this.cpuCores = this.cores;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
upgradeLevel(levels: number, prodMult: number): void {
|
2021-09-09 05:47:34 +02:00
|
|
|
this.level = Math.min(HacknetServerConstants.MaxLevel, Math.round(this.level + levels));
|
2021-09-05 01:09:30 +02:00
|
|
|
this.updateHashRate(prodMult);
|
|
|
|
}
|
|
|
|
|
|
|
|
upgradeRam(levels: number, prodMult: number): boolean {
|
|
|
|
for (let i = 0; i < levels; ++i) {
|
|
|
|
this.maxRam *= 2;
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
this.maxRam = Math.min(HacknetServerConstants.MaxRam, Math.round(this.maxRam));
|
2021-09-05 01:09:30 +02:00
|
|
|
this.updateHashRate(prodMult);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-29 08:41:17 +02:00
|
|
|
updateRamUsed(ram: number): void {
|
|
|
|
super.updateRamUsed(ram);
|
|
|
|
this.updateHashRate(Player.mults.hacknet_node_money);
|
2021-11-16 05:49:33 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
updateHashCapacity(): void {
|
|
|
|
this.hashCapacity = 32 * Math.pow(2, this.cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHashRate(prodMult: number): void {
|
2021-09-09 05:47:34 +02:00
|
|
|
this.hashRate = calculateHashGainRate(this.level, this.ramUsed, this.maxRam, this.cores, prodMult);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
if (isNaN(this.hashRate)) {
|
|
|
|
this.hashRate = 0;
|
|
|
|
console.error(
|
|
|
|
`Error calculating Hacknet Server hash production. This is a bug. Please report to game dev`,
|
|
|
|
false,
|
|
|
|
);
|
2021-05-01 09:17:31 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the current object to a JSON save state
|
2022-07-15 01:00:10 +02:00
|
|
|
toJSON(): IReviverValue {
|
2023-04-28 00:21:06 +02:00
|
|
|
return this.toJSONBase("HacknetServer", includedKeys);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes a HacknetServer Object from a JSON save state
|
2022-07-15 01:00:10 +02:00
|
|
|
static fromJSON(value: IReviverValue): HacknetServer {
|
2023-04-28 00:21:06 +02:00
|
|
|
return BaseServer.fromJSONBase(value, HacknetServer, includedKeys);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-25 04:03:24 +01:00
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
const includedKeys = BaseServer.getIncludedKeys(HacknetServer);
|
2019-03-25 04:03:24 +01:00
|
|
|
|
2023-04-07 06:33:51 +02:00
|
|
|
constructorsForReviver.HacknetServer = HacknetServer;
|