2017-08-30 19:44:29 +02:00
|
|
|
import {CONSTANTS} from "./Constants.js";
|
|
|
|
import {Player} from "./Player.js";
|
2017-09-08 05:05:44 +02:00
|
|
|
import {Server, AllServers, AddToAllServers} from "./Server.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
2017-09-08 05:05:44 +02:00
|
|
|
import {createRandomIp} from "../utils/IPAddress.js";
|
2017-08-30 19:44:29 +02:00
|
|
|
import {yesNoTxtInpBoxGetInput} from "../utils/YesNoBox.js";
|
|
|
|
|
2017-09-08 05:05:44 +02:00
|
|
|
|
2017-04-25 22:18:00 +02:00
|
|
|
/* Functions to handle any server-related purchasing:
|
|
|
|
* Purchasing new servers
|
|
|
|
* Purchasing more RAM for home computer
|
|
|
|
*/
|
2017-08-30 19:44:29 +02:00
|
|
|
function purchaseServer(ram, cost) {
|
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
|
2017-07-27 04:56:14 +02:00
|
|
|
if (Player.purchasedServers.length >= CONSTANTS.PurchasedServerLimit) {
|
|
|
|
dialogBoxCreate("You have reached the maximum limit of " + CONSTANTS.PurchasedServerLimit + " servers. " +
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
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
|
|
|
|
2017-04-25 23:35:17 +02:00
|
|
|
var homeComputer = Player.getHomeComputer();
|
|
|
|
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
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
|
|
|
export {purchaseServer, purchaseRamForHomeComputer};
|