bitburner-src/src/PersonObjects/Player/PlayerObjectServerMethods.ts

66 lines
2.3 KiB
TypeScript
Raw Normal View History

/**
* Server and HacknetServer-related methods for the Player class (PlayerObject)
*/
import { IPlayer } from "../IPlayer";
2019-04-04 02:08:11 +02:00
import { CONSTANTS } from "../../Constants";
import { BitNodeMultipliers } from "../../BitNode/BitNodeMultipliers";
2021-05-01 09:17:31 +02:00
import { Server } from "../../Server/Server";
2021-10-07 22:04:04 +02:00
import { BaseServer } from "../../Server/BaseServer";
import { HacknetServer } from "../../Hacknet/HacknetServer";
2021-10-07 22:56:01 +02:00
import { GetServer, AddToAllServers, createUniqueRandomIp } from "../../Server/AllServers";
2021-10-07 23:55:49 +02:00
import { SpecialServers } from "../../Server/data/SpecialServers";
2019-04-04 02:08:11 +02:00
2021-05-01 09:17:31 +02:00
export function hasTorRouter(this: IPlayer): boolean {
2021-10-07 23:55:49 +02:00
return !!GetServer(SpecialServers.DarkWeb);
2019-04-04 02:08:11 +02:00
}
2021-10-07 22:04:04 +02:00
export function getCurrentServer(this: IPlayer): BaseServer {
2021-10-07 22:56:01 +02:00
const server = GetServer(this.currentServer);
2021-09-24 00:47:43 +02:00
if (server === null) throw new Error("somehow connected to a server that does not exist.");
return server;
2019-04-04 02:08:11 +02:00
}
2021-09-24 00:47:43 +02:00
export function getHomeComputer(this: IPlayer): Server {
2021-10-07 22:56:01 +02:00
const home = GetServer("home");
2021-09-24 00:47:43 +02:00
if (home instanceof Server) return home;
throw new Error("home computer was not a normal server");
2019-04-04 02:08:11 +02:00
}
2021-05-01 09:17:31 +02:00
export function getUpgradeHomeRamCost(this: IPlayer): number {
2021-09-05 01:09:30 +02:00
//Calculate how many times ram has been upgraded (doubled)
const currentRam = this.getHomeComputer().maxRam;
const numUpgrades = Math.log2(currentRam);
//Calculate cost
//Have cost increase by some percentage each time RAM has been upgraded
const mult = Math.pow(1.58, numUpgrades);
2021-09-09 05:47:34 +02:00
const cost = currentRam * CONSTANTS.BaseCostFor1GBOfRamHome * mult * BitNodeMultipliers.HomeComputerRamCost;
2021-09-05 01:09:30 +02:00
return cost;
2019-04-04 02:08:11 +02:00
}
export function getUpgradeHomeCoresCost(this: IPlayer): number {
return 1e9 * Math.pow(7.5, this.getHomeComputer().cpuCores);
}
export function createHacknetServer(this: IPlayer): HacknetServer {
2021-09-05 01:09:30 +02:00
const numOwned = this.hacknetNodes.length;
const name = `hacknet-node-${numOwned}`;
const server = new HacknetServer({
adminRights: true,
hostname: name,
ip: createUniqueRandomIp(),
// player: this,
});
2021-10-07 23:55:49 +02:00
this.hacknetNodes.push(server.hostname);
2021-09-05 01:09:30 +02:00
// Configure the HacknetServer to actually act as a Server
AddToAllServers(server);
const homeComputer = this.getHomeComputer();
2021-10-07 23:55:49 +02:00
homeComputer.serversOnNetwork.push(server.hostname);
server.serversOnNetwork.push(SpecialServers.Home);
2021-09-05 01:09:30 +02:00
return server;
}