mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-24 18:22:58 +02:00
prettify, sorry for the big ass commit
This commit is contained in:
+115
-88
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
AllServers,
|
||||
createUniqueRandomIp,
|
||||
ipExists,
|
||||
} from "./AllServers";
|
||||
import { AllServers, createUniqueRandomIp, ipExists } from "./AllServers";
|
||||
import { Server, IConstructorParams } from "./Server";
|
||||
import { calculateServerGrowth } from "./formulas/grow";
|
||||
|
||||
@@ -21,21 +17,23 @@ import { isValidIPAddress } from "../../utils/helpers/isValidIPAddress";
|
||||
* does not have a duplicate hostname/ip.
|
||||
*/
|
||||
export function safetlyCreateUniqueServer(params: IConstructorParams): Server {
|
||||
if (params.ip != null && ipExists(params.ip)) {
|
||||
params.ip = createUniqueRandomIp();
|
||||
}
|
||||
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;
|
||||
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);
|
||||
return new Server(params);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,111 +44,140 @@ export function safetlyCreateUniqueServer(params: IConstructorParams): Server {
|
||||
* @param p - Reference to Player object
|
||||
* @returns Number of "growth cycles" needed
|
||||
*/
|
||||
export function numCycleForGrowth(server: Server, growth: number, p: IPlayer): number {
|
||||
let ajdGrowthRate = 1 + (CONSTANTS.ServerBaseGrowthRate - 1) / server.hackDifficulty;
|
||||
if (ajdGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
|
||||
ajdGrowthRate = CONSTANTS.ServerMaxGrowthRate;
|
||||
}
|
||||
export function numCycleForGrowth(
|
||||
server: Server,
|
||||
growth: number,
|
||||
p: IPlayer,
|
||||
): number {
|
||||
let ajdGrowthRate =
|
||||
1 + (CONSTANTS.ServerBaseGrowthRate - 1) / server.hackDifficulty;
|
||||
if (ajdGrowthRate > CONSTANTS.ServerMaxGrowthRate) {
|
||||
ajdGrowthRate = CONSTANTS.ServerMaxGrowthRate;
|
||||
}
|
||||
|
||||
const serverGrowthPercentage = server.serverGrowth / 100;
|
||||
const serverGrowthPercentage = server.serverGrowth / 100;
|
||||
|
||||
const cycles = Math.log(growth)/(Math.log(ajdGrowthRate) * p.hacking_grow_mult * serverGrowthPercentage * BitNodeMultipliers.ServerGrowthRate);
|
||||
const cycles =
|
||||
Math.log(growth) /
|
||||
(Math.log(ajdGrowthRate) *
|
||||
p.hacking_grow_mult *
|
||||
serverGrowthPercentage *
|
||||
BitNodeMultipliers.ServerGrowthRate);
|
||||
|
||||
return cycles;
|
||||
return cycles;
|
||||
}
|
||||
|
||||
//Applied server growth for a single server. Returns the percentage growth
|
||||
export function processSingleServerGrowth(server: Server, threads: number, p: IPlayer, cores = 1): number {
|
||||
let serverGrowth = calculateServerGrowth(server, threads, p, cores);
|
||||
if (serverGrowth < 1) {
|
||||
console.warn("serverGrowth calculated to be less than 1");
|
||||
serverGrowth = 1;
|
||||
}
|
||||
export function processSingleServerGrowth(
|
||||
server: Server,
|
||||
threads: number,
|
||||
p: IPlayer,
|
||||
cores = 1,
|
||||
): number {
|
||||
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;
|
||||
server.moneyAvailable *= serverGrowth;
|
||||
const oldMoneyAvailable = server.moneyAvailable;
|
||||
server.moneyAvailable *= serverGrowth;
|
||||
|
||||
// in case of data corruption
|
||||
if (isValidNumber(server.moneyMax) && isNaN(server.moneyAvailable)) {
|
||||
server.moneyAvailable = server.moneyMax;
|
||||
}
|
||||
// in case of data corruption
|
||||
if (isValidNumber(server.moneyMax) && isNaN(server.moneyAvailable)) {
|
||||
server.moneyAvailable = server.moneyMax;
|
||||
}
|
||||
|
||||
// cap at max
|
||||
if (isValidNumber(server.moneyMax) && server.moneyAvailable > server.moneyMax) {
|
||||
server.moneyAvailable = server.moneyMax;
|
||||
}
|
||||
// cap at max
|
||||
if (
|
||||
isValidNumber(server.moneyMax) &&
|
||||
server.moneyAvailable > server.moneyMax
|
||||
) {
|
||||
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
|
||||
let usedCycles = numCycleForGrowth(server, server.moneyAvailable / oldMoneyAvailable, p);
|
||||
usedCycles = Math.max(0, usedCycles);
|
||||
server.fortify(2 * CONSTANTS.ServerFortifyAmount * Math.ceil(usedCycles));
|
||||
}
|
||||
return server.moneyAvailable / oldMoneyAvailable;
|
||||
// if there was any growth at all, increase security
|
||||
if (oldMoneyAvailable !== server.moneyAvailable) {
|
||||
//Growing increases server security twice as much as hacking
|
||||
let usedCycles = numCycleForGrowth(
|
||||
server,
|
||||
server.moneyAvailable / oldMoneyAvailable,
|
||||
p,
|
||||
);
|
||||
usedCycles = Math.max(0, usedCycles);
|
||||
server.fortify(2 * CONSTANTS.ServerFortifyAmount * Math.ceil(usedCycles));
|
||||
}
|
||||
return server.moneyAvailable / oldMoneyAvailable;
|
||||
}
|
||||
|
||||
export function prestigeHomeComputer(homeComp: Server): void {
|
||||
const hasBitflume = homeComp.programs.includes(Programs.BitFlume.name);
|
||||
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); }
|
||||
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);
|
||||
});
|
||||
//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);
|
||||
homeComp.messages.length = 0; //Remove .lit and .msg files
|
||||
homeComp.messages.push(LiteratureNames.HackersStartingHandbook);
|
||||
}
|
||||
|
||||
//Returns server object with corresponding hostname
|
||||
// Relatively slow, would rather not use this a lot
|
||||
export function GetServerByHostname(hostname: string): Server | HacknetServer | null {
|
||||
for (const ip in AllServers) {
|
||||
if (AllServers.hasOwnProperty(ip)) {
|
||||
if (AllServers[ip].hostname == hostname) {
|
||||
return AllServers[ip];
|
||||
}
|
||||
}
|
||||
export function GetServerByHostname(
|
||||
hostname: string,
|
||||
): Server | HacknetServer | null {
|
||||
for (const ip in AllServers) {
|
||||
if (AllServers.hasOwnProperty(ip)) {
|
||||
if (AllServers[ip].hostname == hostname) {
|
||||
return AllServers[ip];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
//Get server by IP or hostname. Returns null if invalid
|
||||
export function getServer(s: string): Server | HacknetServer | null {
|
||||
if (!isValidIPAddress(s)) {
|
||||
return GetServerByHostname(s);
|
||||
}
|
||||
if (AllServers[s] !== undefined) {
|
||||
return AllServers[s];
|
||||
}
|
||||
if (!isValidIPAddress(s)) {
|
||||
return GetServerByHostname(s);
|
||||
}
|
||||
if (AllServers[s] !== undefined) {
|
||||
return AllServers[s];
|
||||
}
|
||||
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// 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): Server | HacknetServer | null {
|
||||
if (i > server.serversOnNetwork.length) {
|
||||
console.error("Tried to get server on network that was out of range");
|
||||
return null;
|
||||
}
|
||||
export function getServerOnNetwork(
|
||||
server: Server,
|
||||
i: number,
|
||||
): Server | HacknetServer | null {
|
||||
if (i > server.serversOnNetwork.length) {
|
||||
console.error("Tried to get server on network that was out of range");
|
||||
return null;
|
||||
}
|
||||
|
||||
return AllServers[server.serversOnNetwork[i]];
|
||||
return AllServers[server.serversOnNetwork[i]];
|
||||
}
|
||||
|
||||
export function isBackdoorInstalled(server: Server | HacknetServer): boolean {
|
||||
if ("backdoorInstalled" in server) {
|
||||
return server.backdoorInstalled;
|
||||
}
|
||||
return false;
|
||||
if ("backdoorInstalled" in server) {
|
||||
return server.backdoorInstalled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user