2019-01-30 07:02:27 +01:00
|
|
|
/**
|
|
|
|
* Implements functions for purchasing servers or purchasing more RAM for
|
|
|
|
* the home computer
|
|
|
|
*/
|
|
|
|
import { BitNodeMultipliers } from "./BitNode/BitNodeMultipliers";
|
|
|
|
import { CONSTANTS } from "./Constants";
|
|
|
|
import { Player } from "./Player";
|
|
|
|
import { Server,
|
|
|
|
AllServers,
|
|
|
|
AddToAllServers} from "./Server";
|
|
|
|
import { dialogBoxCreate } from "../utils/DialogBox";
|
|
|
|
import { createRandomIp } from "../utils/IPAddress";
|
|
|
|
import { yesNoTxtInpBoxGetInput } from "../utils/YesNoBox";
|
|
|
|
import { isPowerOfTwo } from "../utils/helpers/isPowerOfTwo";
|
|
|
|
|
|
|
|
// Returns the cost of purchasing a server with the given RAM
|
|
|
|
// Returns Infinity for invalid 'ram' arguments
|
|
|
|
export function getPurchaseServerCost(ram) {
|
|
|
|
const sanitizedRam = Math.round(ram);
|
|
|
|
if (isNaN(sanitizedRam) || !isPowerOfTwo(sanitizedRam)) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-01-30 07:02:27 +01:00
|
|
|
if (sanitizedRam > getPurchaseServerMaxRam()) {
|
|
|
|
return Infinity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sanitizedRam * CONSTANTS.BaseCostFor1GBOfRamServer * BitNodeMultipliers.PurchasedServerCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPurchaseServerLimit() {
|
|
|
|
return Math.round(CONSTANTS.PurchasedServerLimit * BitNodeMultipliers.PurchasedServerLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPurchaseServerMaxRam() {
|
|
|
|
// TODO ensure this is a power of 2?
|
|
|
|
return Math.round(CONSTANTS.PurchasedServerMaxRam * BitNodeMultipliers.PurchasedServerMaxRam);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually purchase a server (NOT through Netscript)
|
|
|
|
export function purchaseServer(ram) {
|
|
|
|
const cost = getPurchaseServerCost(ram);
|
2017-09-08 05:05:44 +02:00
|
|
|
|
2017-02-06 02:29:17 +01:00
|
|
|
//Check if player has enough money
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(cost)) {
|
2017-02-06 02:29:17 +01:00
|
|
|
dialogBoxCreate("You don't have enough money to purchase this server!");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-08-15 22:22:46 +02:00
|
|
|
//Maximum server limit
|
2019-01-30 07:02:27 +01:00
|
|
|
if (Player.purchasedServers.length >= getPurchaseServerLimit()) {
|
|
|
|
dialogBoxCreate("You have reached the maximum limit of " + getPurchaseServerLimit() + " servers. " +
|
2017-07-27 04:56:14 +02:00
|
|
|
"You cannot purchase any more. You can " +
|
|
|
|
"delete some of your purchased servers using the deleteServer() Netscript function in a script");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:22:46 +02:00
|
|
|
var hostname = yesNoTxtInpBoxGetInput();
|
2017-02-06 02:29:17 +01:00
|
|
|
if (hostname == "") {
|
|
|
|
dialogBoxCreate("You must enter a hostname for your new server!");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-02-16 19:52:11 +01:00
|
|
|
//Create server
|
2018-05-06 23:20:00 +02:00
|
|
|
var newServ = new Server({
|
|
|
|
ip:createRandomIp(), hostname:hostname, organizationName:"",
|
|
|
|
isConnectedTo:false, adminRights:true, purchasedByPlayer:true, maxRam:ram
|
|
|
|
});
|
2017-02-03 23:05:59 +01:00
|
|
|
AddToAllServers(newServ);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-02-16 19:52:11 +01:00
|
|
|
//Add to Player's purchasedServers array
|
2017-04-19 23:39:25 +02:00
|
|
|
Player.purchasedServers.push(newServ.ip);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2017-02-16 19:52:11 +01:00
|
|
|
//Connect new server to home computer
|
|
|
|
var homeComputer = Player.getHomeComputer();
|
2017-04-19 23:39:25 +02:00
|
|
|
homeComputer.serversOnNetwork.push(newServ.ip);
|
2017-04-20 10:29:07 +02:00
|
|
|
newServ.serversOnNetwork.push(homeComputer.ip);
|
2017-07-27 04:56:14 +02:00
|
|
|
|
|
|
|
Player.loseMoney(cost);
|
|
|
|
|
2017-02-06 02:29:17 +01:00
|
|
|
dialogBoxCreate("Server successfully purchased with hostname " + hostname);
|
2017-04-25 22:18:00 +02:00
|
|
|
}
|
|
|
|
|
2019-01-30 07:02:27 +01:00
|
|
|
// Manually upgrade RAM on home computer (NOT through Netscript)
|
|
|
|
export function purchaseRamForHomeComputer(cost) {
|
2017-07-29 18:25:40 +02:00
|
|
|
if (Player.money.lt(cost)) {
|
2017-04-25 23:35:17 +02:00
|
|
|
dialogBoxCreate("You do not have enough money to purchase additional RAM for your home computer");
|
|
|
|
return;
|
|
|
|
}
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2019-01-09 11:43:59 +01:00
|
|
|
const homeComputer = Player.getHomeComputer();
|
|
|
|
if (homeComputer.maxRam >= CONSTANTS.HomeComputerMaxRam) {
|
|
|
|
dialogBoxCreate(`You cannot upgrade your home computer RAM because it is at its maximum possible value`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-27 04:56:14 +02:00
|
|
|
|
2019-01-09 11:43:59 +01:00
|
|
|
homeComputer.maxRam *= 2;
|
2017-07-27 04:56:14 +02:00
|
|
|
Player.loseMoney(cost);
|
|
|
|
|
2017-04-25 23:35:17 +02:00
|
|
|
dialogBoxCreate("Purchased additional RAM for home computer! It now has " + homeComputer.maxRam + "GB of RAM.");
|
2017-07-27 04:56:14 +02:00
|
|
|
}
|