added server utils scripts
This commit is contained in:
parent
217f78b239
commit
3a3ebb321f
68
src/utils/server/server-buy-max.ts
Normal file
68
src/utils/server/server-buy-max.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import { NS } from '@ns';
|
||||
|
||||
export const serverBuyMax = (ns: NS, serverNamePrefix: string) => {
|
||||
// get the maximum number of servers that can be purchased
|
||||
const maxServers = ns.getPurchasedServerLimit();
|
||||
|
||||
// get the number of servers currently owned
|
||||
const currentServers = ns.getPurchasedServers().length;
|
||||
|
||||
// calculate the number of servers that can still be purchased
|
||||
const serversToPurchase = maxServers - currentServers;
|
||||
|
||||
// if we can't buy any more servers, return
|
||||
if (serversToPurchase <= 0) {
|
||||
ns.tprint('Cannot purchase any more servers. Maximum limit reached.');
|
||||
return;
|
||||
}
|
||||
|
||||
// get current money available
|
||||
const moneyAvailable = ns.getServerMoneyAvailable('home');
|
||||
|
||||
// smallest ram we can buy is 2gb, check if can even afford that
|
||||
if (moneyAvailable < ns.getPurchasedServerCost(2)) {
|
||||
ns.tprint('Nigga you broke, not enough money to buy the smallest server');
|
||||
return;
|
||||
}
|
||||
|
||||
// find the maximum ram we can afford spread out over the number of servers we can buy
|
||||
// the ram amount that can be bought is in power of 2
|
||||
// we start with 2^1, then increase the power
|
||||
// once we hit the max, we return the -1 power that was working before
|
||||
let targetRam = 0;
|
||||
// do until 2^67
|
||||
for (let pow = 1; pow <= 67; pow++) {
|
||||
// calculate the ram based on power
|
||||
const ram = Math.pow(2, pow);
|
||||
// get the cost of the server with the ram
|
||||
const totalCost = ns.getPurchasedServerCost(ram) * serversToPurchase;
|
||||
|
||||
// check if can buy
|
||||
if (totalCost <= moneyAvailable) {
|
||||
// if can then update the target to the new one
|
||||
targetRam = ram;
|
||||
} else {
|
||||
// if not then break, it will return the previous ram amount
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// now buy the servers with the target ram
|
||||
let serversPurchased = 0;
|
||||
while (serversPurchased < serversToPurchase) {
|
||||
const serverName = `${serverNamePrefix}-${currentServers + serversPurchased + 1}`;
|
||||
|
||||
if (!ns.serverExists(serverName)) {
|
||||
ns.purchaseServer(serverName, targetRam);
|
||||
serversPurchased++;
|
||||
}
|
||||
}
|
||||
|
||||
ns.tprint(`Servers purchased: ${serversPurchased}`);
|
||||
ns.tprint(`RAM: ${targetRam} GB RAM each`);
|
||||
};
|
||||
|
||||
export const main = (ns: NS) => {
|
||||
const serverNamePrefix = 'worker';
|
||||
serverBuyMax(ns, serverNamePrefix);
|
||||
};
|
||||
35
src/utils/server/server-delete-all.ts
Normal file
35
src/utils/server/server-delete-all.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { NS } from '@ns';
|
||||
import { kill } from '../kill';
|
||||
|
||||
export const serverDeleteAll = (ns: NS) => {
|
||||
// get a list of all the purchased servers
|
||||
const serverNames: string[] = ns.getPurchasedServers();
|
||||
|
||||
// initialize a counter for total cost of all the servers
|
||||
let totalCost = 0;
|
||||
|
||||
// loop through the list of servers and remove each one
|
||||
serverNames.forEach((serverName) => {
|
||||
// get the ram of the server
|
||||
const ram = ns.getServerMaxRam(serverName);
|
||||
|
||||
// calculate the cost of the server and add it to the total cost
|
||||
const cost = ns.getPurchasedServerCost(ram);
|
||||
totalCost += cost;
|
||||
|
||||
// kill all the processes running on the server before removing it
|
||||
kill(ns, serverName);
|
||||
|
||||
// remove the server
|
||||
ns.deleteServer(serverName);
|
||||
});
|
||||
|
||||
ns.tprint(`Purchased servers deleted: ${serverNames.length}`);
|
||||
ns.tprint(`Total cost of deleted servers: $${totalCost}`);
|
||||
|
||||
return serverNames;
|
||||
};
|
||||
|
||||
export const main = (ns: NS) => {
|
||||
serverDeleteAll(ns);
|
||||
};
|
||||
Loading…
Reference in a new issue