2019-05-05 06:03:40 +02:00
|
|
|
import {
|
|
|
|
AllServers,
|
|
|
|
createUniqueRandomIp,
|
|
|
|
ipExists,
|
|
|
|
} from "./AllServers";
|
|
|
|
import { Server, IConstructorParams } from "./Server";
|
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 { HacknetServer } from "../Hacknet/HacknetServer";
|
|
|
|
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
|
|
|
import { isValidIPAddress } from "../../utils/helpers/isValidIPAddress";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new server, while also ensuring that the new server
|
|
|
|
* does not have a duplicate hostname/ip.
|
|
|
|
*/
|
|
|
|
export function safetlyCreateUniqueServer(params: IConstructorParams): Server {
|
|
|
|
if (params.ip != null && ipExists(params.ip)) {
|
|
|
|
params.ip = createUniqueRandomIp();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetServerByHostname(params.hostname) != null) {
|
|
|
|
// 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}`;
|
|
|
|
if (GetServerByHostname(hostname) == null) { break; }
|
|
|
|
}
|
|
|
|
params.hostname = hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Server(params);
|
|
|
|
}
|
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
|
|
|
|
*/
|
2019-03-05 02:40:28 +01:00
|
|
|
export function numCycleForGrowth(server: Server, growth: number, p: IPlayer) {
|
2019-03-03 04:15:10 +01:00
|
|
|
let ajdGrowthRate = 1 + (CONSTANTS.ServerBaseGrowthRate - 1) / server.hackDifficulty;
|
2019-07-14 05:55:58 +02:00
|
|
|
if (ajdGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
|
2019-03-03 04:15:10 +01:00
|
|
|
ajdGrowthRate = CONSTANTS.ServerMaxGrowthRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverGrowthPercentage = server.serverGrowth / 100;
|
|
|
|
|
2019-03-20 01:53:46 +01:00
|
|
|
const cycles = Math.log(growth)/(Math.log(ajdGrowthRate) * p.hacking_grow_mult * serverGrowthPercentage * BitNodeMultipliers.ServerGrowthRate);
|
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
return cycles;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Applied server growth for a single server. Returns the percentage growth
|
2019-03-05 02:40:28 +01:00
|
|
|
export function processSingleServerGrowth(server: Server, numCycles: number, p: IPlayer) {
|
2019-03-03 04:15:10 +01:00
|
|
|
//Server growth processed once every 450 game cycles
|
|
|
|
const numServerGrowthCycles = Math.max(Math.floor(numCycles / 450), 0);
|
|
|
|
|
|
|
|
//Get adjusted growth rate, which accounts for server security
|
|
|
|
const growthRate = CONSTANTS.ServerBaseGrowthRate;
|
|
|
|
var adjGrowthRate = 1 + (growthRate - 1) / server.hackDifficulty;
|
|
|
|
if (adjGrowthRate > CONSTANTS.ServerMaxGrowthRate) {adjGrowthRate = CONSTANTS.ServerMaxGrowthRate;}
|
|
|
|
|
|
|
|
//Calculate adjusted server growth rate based on parameters
|
|
|
|
const serverGrowthPercentage = server.serverGrowth / 100;
|
|
|
|
const numServerGrowthCyclesAdjusted = numServerGrowthCycles * serverGrowthPercentage * BitNodeMultipliers.ServerGrowthRate;
|
|
|
|
|
|
|
|
//Apply serverGrowth for the calculated number of growth cycles
|
2019-03-05 02:40:28 +01:00
|
|
|
let serverGrowth = Math.pow(adjGrowthRate, numServerGrowthCyclesAdjusted * p.hacking_grow_mult);
|
2019-03-03 04:15:10 +01:00
|
|
|
if (serverGrowth < 1) {
|
2021-03-08 04:46:50 +01:00
|
|
|
console.warn("serverGrowth calculated to be less than 1");
|
2019-03-03 04:15:10 +01:00
|
|
|
serverGrowth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldMoneyAvailable = server.moneyAvailable;
|
|
|
|
server.moneyAvailable *= serverGrowth;
|
|
|
|
|
|
|
|
// in case of data corruption
|
2019-07-14 05:55:58 +02:00
|
|
|
if (isValidNumber(server.moneyMax) && isNaN(server.moneyAvailable)) {
|
2019-03-03 04:15:10 +01:00
|
|
|
server.moneyAvailable = server.moneyMax;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cap at max
|
2019-07-14 05:55:58 +02:00
|
|
|
if (isValidNumber(server.moneyMax) && server.moneyAvailable > server.moneyMax) {
|
2019-03-03 04:15:10 +01: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
|
2019-03-05 02:40:28 +01:00
|
|
|
let usedCycles = numCycleForGrowth(server, server.moneyAvailable / oldMoneyAvailable, p);
|
2019-03-03 04:15:10 +01:00
|
|
|
usedCycles = Math.max(0, usedCycles);
|
|
|
|
server.fortify(2 * CONSTANTS.ServerFortifyAmount * Math.ceil(usedCycles));
|
|
|
|
}
|
|
|
|
return server.moneyAvailable / oldMoneyAvailable;
|
|
|
|
}
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
export function prestigeHomeComputer(homeComp: Server) {
|
2019-03-03 04:15:10 +01: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) {
|
2019-05-07 03:01:06 +02:00
|
|
|
script.updateRamUsage(homeComp.scripts);
|
2019-03-03 04:15:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
homeComp.messages.length = 0; //Remove .lit and .msg files
|
2021-03-14 07:08:24 +01:00
|
|
|
homeComp.messages.push(LiteratureNames.HackersStartingHandbook);
|
2019-03-03 04:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns server object with corresponding hostname
|
|
|
|
// Relatively slow, would rather not use this a lot
|
2019-03-27 09:36:14 +01:00
|
|
|
export function GetServerByHostname(hostname: string): Server | HacknetServer | null {
|
2019-03-03 04:15:10 +01:00
|
|
|
for (var ip in AllServers) {
|
|
|
|
if (AllServers.hasOwnProperty(ip)) {
|
|
|
|
if (AllServers[ip].hostname == hostname) {
|
|
|
|
return AllServers[ip];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 01:53:46 +01:00
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get server by IP or hostname. Returns null if invalid
|
2019-03-27 09:36:14 +01:00
|
|
|
export function getServer(s: string): Server | HacknetServer | null {
|
2019-03-03 04:15:10 +01:00
|
|
|
if (!isValidIPAddress(s)) {
|
|
|
|
return GetServerByHostname(s);
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
if (AllServers[s] !== undefined) {
|
2019-03-03 04:15:10 +01:00
|
|
|
return AllServers[s];
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-03-03 04:15:10 +01:00
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
|
export function getServerOnNetwork(server: Server, i: number) {
|
|
|
|
if (i > server.serversOnNetwork.length) {
|
|
|
|
console.error("Tried to get server on network that was out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AllServers[server.serversOnNetwork[i]];
|
|
|
|
}
|