2019-03-25 04:03:24 +01:00
|
|
|
// Class representing a single hackable Server
|
|
|
|
import { BaseServer } from "./BaseServer";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
|
|
|
|
|
2019-04-05 11:08:41 +02:00
|
|
|
import { createRandomString } from "../utils/helpers/createRandomString";
|
2021-09-25 20:42:57 +02:00
|
|
|
import { createRandomIp } from "../utils/IPAddress";
|
2022-07-15 01:00:10 +02:00
|
|
|
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
export interface IConstructorParams {
|
2021-09-05 01:09:30 +02:00
|
|
|
adminRights?: boolean;
|
|
|
|
hackDifficulty?: number;
|
|
|
|
hostname: string;
|
|
|
|
ip?: string;
|
|
|
|
isConnectedTo?: boolean;
|
|
|
|
maxRam?: number;
|
|
|
|
moneyAvailable?: number;
|
|
|
|
numOpenPortsRequired?: number;
|
|
|
|
organizationName?: string;
|
|
|
|
purchasedByPlayer?: boolean;
|
|
|
|
requiredHackingSkill?: number;
|
|
|
|
serverGrowth?: number;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 04:03:24 +01:00
|
|
|
export class Server extends BaseServer {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Flag indicating whether this server has a backdoor installed by a player
|
|
|
|
backdoorInstalled = false;
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Initial server security level
|
|
|
|
// (i.e. security level when the server was created)
|
|
|
|
baseDifficulty = 1;
|
2021-04-29 02:07:26 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Server Security Level
|
|
|
|
hackDifficulty = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Minimum server security level that this server can be weakened to
|
|
|
|
minDifficulty = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// How much money currently resides on the server and can be hacked
|
|
|
|
moneyAvailable = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Maximum amount of money that this server can hold
|
|
|
|
moneyMax = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Number of open ports required in order to gain admin/root access
|
|
|
|
numOpenPortsRequired = 5;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// How many ports are currently opened on the server
|
|
|
|
openPortCount = 0;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Hacking level required to hack this server
|
|
|
|
requiredHackingSkill = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Parameter that affects how effectively this server's money can
|
|
|
|
// be increased using the grow() Netscript function
|
|
|
|
serverGrowth = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
constructor(params: IConstructorParams = { hostname: "", ip: createRandomIp() }) {
|
2021-09-05 01:09:30 +02:00
|
|
|
super(params);
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// "hacknet-node-X" hostnames are reserved for Hacknet Servers
|
|
|
|
if (this.hostname.startsWith("hacknet-node-")) {
|
|
|
|
this.hostname = createRandomString(10);
|
2021-04-30 05:52:56 +02:00
|
|
|
}
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2021-09-09 05:47:34 +02:00
|
|
|
this.purchasedByPlayer = params.purchasedByPlayer != null ? params.purchasedByPlayer : false;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
//RAM, CPU speed and Scripts
|
|
|
|
this.maxRam = params.maxRam != null ? params.maxRam : 0; //GB
|
|
|
|
|
|
|
|
/* Hacking information (only valid for "foreign" aka non-purchased servers) */
|
2021-09-09 05:47:34 +02:00
|
|
|
this.requiredHackingSkill = params.requiredHackingSkill != null ? params.requiredHackingSkill : 1;
|
2021-09-05 01:09:30 +02:00
|
|
|
this.moneyAvailable =
|
2021-09-09 05:47:34 +02:00
|
|
|
params.moneyAvailable != null ? params.moneyAvailable * BitNodeMultipliers.ServerStartingMoney : 0;
|
|
|
|
this.moneyMax = 25 * this.moneyAvailable * BitNodeMultipliers.ServerMaxMoney;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
//Hack Difficulty is synonymous with server security. Base Difficulty = Starting difficulty
|
2022-09-23 08:39:58 +02:00
|
|
|
const realDifficulty =
|
|
|
|
params.hackDifficulty != null ? params.hackDifficulty * BitNodeMultipliers.ServerStartingSecurity : 1;
|
|
|
|
this.hackDifficulty = Math.min(realDifficulty, 100);
|
2021-09-05 01:09:30 +02:00
|
|
|
this.baseDifficulty = this.hackDifficulty;
|
2022-09-23 08:39:58 +02:00
|
|
|
this.minDifficulty = Math.min(Math.max(1, Math.round(realDifficulty / 3)), 100);
|
2021-09-05 01:09:30 +02:00
|
|
|
this.serverGrowth = params.serverGrowth != null ? params.serverGrowth : 1; //Integer from 0 to 100. Affects money increase from grow()
|
|
|
|
|
|
|
|
//Port information, required for porthacking servers to get admin rights
|
2021-09-09 05:47:34 +02:00
|
|
|
this.numOpenPortsRequired = params.numOpenPortsRequired != null ? params.numOpenPortsRequired : 5;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Ensures that the server's difficulty (server security) doesn't get too high */
|
2021-09-05 01:09:30 +02:00
|
|
|
capDifficulty(): void {
|
|
|
|
if (this.hackDifficulty < this.minDifficulty) {
|
|
|
|
this.hackDifficulty = this.minDifficulty;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (this.hackDifficulty < 1) {
|
|
|
|
this.hackDifficulty = 1;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Place some arbitrarily limit that realistically should never happen unless someone is
|
|
|
|
// screwing around with the game
|
|
|
|
if (this.hackDifficulty > 100) {
|
|
|
|
this.hackDifficulty = 100;
|
2019-04-10 08:07:12 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change this server's minimum security
|
|
|
|
* @param n - Value by which to increase/decrease the server's minimum security
|
|
|
|
* @param perc - Whether it should be changed by a percentage, or a flat value
|
|
|
|
*/
|
|
|
|
changeMinimumSecurity(n: number, perc = false): void {
|
|
|
|
if (perc) {
|
|
|
|
this.minDifficulty *= n;
|
|
|
|
} else {
|
|
|
|
this.minDifficulty += n;
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Server security cannot go below 1
|
|
|
|
this.minDifficulty = Math.max(1, this.minDifficulty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change this server's maximum money
|
|
|
|
* @param n - Value by which to change the server's maximum money
|
|
|
|
*/
|
2021-10-11 23:43:48 +02:00
|
|
|
changeMaximumMoney(n: number): void {
|
|
|
|
const softCap = 10e12;
|
|
|
|
if (this.moneyMax > softCap) {
|
|
|
|
const aboveCap = this.moneyMax - softCap;
|
|
|
|
n = 1 + (n - 1) / Math.log(aboveCap) / Math.log(8);
|
2021-05-01 09:17:31 +02:00
|
|
|
}
|
2021-10-11 23:43:48 +02:00
|
|
|
|
|
|
|
this.moneyMax *= n;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Strengthens a server's security level (difficulty) by the specified amount */
|
2021-09-05 01:09:30 +02:00
|
|
|
fortify(amt: number): void {
|
|
|
|
this.hackDifficulty += amt;
|
|
|
|
this.capDifficulty();
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Lowers the server's security level (difficulty) by the specified amount) */
|
2021-09-05 01:09:30 +02:00
|
|
|
weaken(amt: number): void {
|
|
|
|
this.hackDifficulty -= amt * BitNodeMultipliers.ServerWeakenRate;
|
|
|
|
this.capDifficulty();
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:40:10 +02:00
|
|
|
/** Serialize the current object to a JSON save state */
|
2022-07-15 01:00:10 +02:00
|
|
|
toJSON(): IReviverValue {
|
2021-09-05 01:09:30 +02:00
|
|
|
return Generic_toJSON("Server", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes a Server Object from a JSON save state
|
2022-07-15 01:00:10 +02:00
|
|
|
static fromJSON(value: IReviverValue): Server {
|
2021-09-05 01:09:30 +02:00
|
|
|
return Generic_fromJSON(Server, value.data);
|
|
|
|
}
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Server = Server;
|