2021-10-07 22:56:01 +02:00
|
|
|
import { GetServer, createUniqueRandomIp, ipExists } from "./AllServers";
|
2019-05-05 06:03:40 +02:00
|
|
|
import { Server, IConstructorParams } from "./Server";
|
2021-09-16 01:50:44 +02:00
|
|
|
import { BaseServer } from "./BaseServer";
|
2021-03-31 06:45:21 +02:00
|
|
|
import { calculateServerGrowth } from "./formulas/grow";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers";
|
|
|
|
import { CONSTANTS } from "../Constants";
|
|
|
|
import { IPlayer } from "../PersonObjects/IPlayer";
|
|
|
|
import { Programs } from "../Programs/Programs";
|
2021-03-14 07:08:24 +01:00
|
|
|
import { LiteratureNames } from "../Literature/data/LiteratureNames";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-07-14 05:55:58 +02:00
|
|
|
import { isValidNumber } from "../utils/helpers/isValidNumber";
|
2019-05-05 06:03:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new server, while also ensuring that the new server
|
|
|
|
* does not have a duplicate hostname/ip.
|
|
|
|
*/
|
|
|
|
export function safetlyCreateUniqueServer(params: IConstructorParams): Server {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (params.ip != null && ipExists(params.ip)) {
|
|
|
|
params.ip = createUniqueRandomIp();
|
|
|
|
}
|
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
if (GetServer(params.hostname) != null) {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Use a for loop to ensure that we don't get suck in an infinite loop somehow
|
|
|
|
let hostname: string = params.hostname;
|
|
|
|
for (let i = 0; i < 200; ++i) {
|
|
|
|
hostname = `${params.hostname}-${i}`;
|
2021-10-07 22:56:01 +02:00
|
|
|
if (GetServer(hostname) == null) {
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
params.hostname = hostname;
|
|
|
|
}
|
2019-05-05 06:03:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Server(params);
|
2019-05-05 06:03:40 +02:00
|
|
|
}
|
2019-03-03 04:15:10 +01:00
|
|
|
|
2019-07-14 05:55:58 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of "growth cycles" needed to grow the specified server by the
|
|
|
|
* specified amount.
|
|
|
|
* @param server - Server being grown
|
|
|
|
* @param growth - How much the server is being grown by, in DECIMAL form (e.g. 1.5 rather than 50)
|
|
|
|
* @param p - Reference to Player object
|
|
|
|
* @returns Number of "growth cycles" needed
|
|
|
|
*/
|
2021-10-02 05:02:09 +02:00
|
|
|
export function numCycleForGrowth(server: Server, growth: number, p: IPlayer, cores = 1): number {
|
2021-09-09 05:47:34 +02:00
|
|
|
let ajdGrowthRate = 1 + (CONSTANTS.ServerBaseGrowthRate - 1) / server.hackDifficulty;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ajdGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
|
|
|
|
ajdGrowthRate = CONSTANTS.ServerMaxGrowthRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverGrowthPercentage = server.serverGrowth / 100;
|
|
|
|
|
2021-10-02 05:02:09 +02:00
|
|
|
const coreBonus = 1 + (cores - 1) / 16;
|
2021-09-05 01:09:30 +02:00
|
|
|
const cycles =
|
|
|
|
Math.log(growth) /
|
2021-10-02 05:02:09 +02:00
|
|
|
(Math.log(ajdGrowthRate) *
|
|
|
|
p.hacking_grow_mult *
|
|
|
|
serverGrowthPercentage *
|
|
|
|
BitNodeMultipliers.ServerGrowthRate *
|
|
|
|
coreBonus);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
return cycles;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Applied server growth for a single server. Returns the percentage growth
|
2021-09-09 05:47:34 +02:00
|
|
|
export function processSingleServerGrowth(server: Server, threads: number, p: IPlayer, cores = 1): number {
|
2021-09-05 01:09:30 +02:00
|
|
|
let serverGrowth = calculateServerGrowth(server, threads, p, cores);
|
|
|
|
if (serverGrowth < 1) {
|
|
|
|
console.warn("serverGrowth calculated to be less than 1");
|
|
|
|
serverGrowth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldMoneyAvailable = server.moneyAvailable;
|
2021-10-05 01:58:34 +02:00
|
|
|
server.moneyAvailable += 1 * threads; // It can be grown even if it has no money
|
2021-09-05 01:09:30 +02:00
|
|
|
server.moneyAvailable *= serverGrowth;
|
|
|
|
|
|
|
|
// in case of data corruption
|
|
|
|
if (isValidNumber(server.moneyMax) && isNaN(server.moneyAvailable)) {
|
|
|
|
server.moneyAvailable = server.moneyMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cap at max
|
2021-09-09 05:47:34 +02:00
|
|
|
if (isValidNumber(server.moneyMax) && server.moneyAvailable > server.moneyMax) {
|
2021-09-05 01:09:30 +02:00
|
|
|
server.moneyAvailable = server.moneyMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there was any growth at all, increase security
|
|
|
|
if (oldMoneyAvailable !== server.moneyAvailable) {
|
|
|
|
//Growing increases server security twice as much as hacking
|
2021-10-02 05:02:09 +02:00
|
|
|
let usedCycles = numCycleForGrowth(server, server.moneyAvailable / oldMoneyAvailable, p, cores);
|
2021-09-05 01:09:30 +02:00
|
|
|
usedCycles = Math.max(0, usedCycles);
|
|
|
|
server.fortify(2 * CONSTANTS.ServerFortifyAmount * Math.ceil(usedCycles));
|
|
|
|
}
|
|
|
|
return server.moneyAvailable / oldMoneyAvailable;
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
export function prestigeHomeComputer(homeComp: Server): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const hasBitflume = homeComp.programs.includes(Programs.BitFlume.name);
|
|
|
|
|
|
|
|
homeComp.programs.length = 0; //Remove programs
|
|
|
|
homeComp.runningScripts = [];
|
|
|
|
homeComp.serversOnNetwork = [];
|
|
|
|
homeComp.isConnectedTo = true;
|
|
|
|
homeComp.ramUsed = 0;
|
|
|
|
homeComp.programs.push(Programs.NukeProgram.name);
|
|
|
|
if (hasBitflume) {
|
|
|
|
homeComp.programs.push(Programs.BitFlume.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update RAM usage on all scripts
|
|
|
|
homeComp.scripts.forEach(function (script) {
|
|
|
|
script.updateRamUsage(homeComp.scripts);
|
|
|
|
});
|
|
|
|
|
|
|
|
homeComp.messages.length = 0; //Remove .lit and .msg files
|
|
|
|
homeComp.messages.push(LiteratureNames.HackersStartingHandbook);
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
// Returns the i-th server on the specified server's network
|
|
|
|
// A Server's serverOnNetwork property holds only the IPs. This function returns
|
|
|
|
// the actual Server object
|
2021-10-07 22:04:04 +02:00
|
|
|
export function getServerOnNetwork(server: BaseServer, i: number): BaseServer | null {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (i > server.serversOnNetwork.length) {
|
|
|
|
console.error("Tried to get server on network that was out of range");
|
|
|
|
return null;
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-10-07 22:56:01 +02:00
|
|
|
return GetServer(server.serversOnNetwork[i]);
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
|
2021-10-07 22:04:04 +02:00
|
|
|
export function isBackdoorInstalled(server: BaseServer): boolean {
|
|
|
|
if (server instanceof Server) {
|
2021-09-05 01:09:30 +02:00
|
|
|
return server.backdoorInstalled;
|
|
|
|
}
|
|
|
|
return false;
|
2021-04-29 02:07:26 +02:00
|
|
|
}
|