Compare commits

...

3 commits

Author SHA1 Message Date
Vomitblood 3a3ebb321f added server utils scripts 2026-04-30 09:51:28 +08:00
Vomitblood 217f78b239 asdf 2026-04-30 09:51:18 +08:00
Vomitblood 6a80f8eefa added kill script 2026-04-30 09:50:49 +08:00
5 changed files with 146 additions and 4 deletions

View file

@ -1,8 +1,18 @@
{
"arrowParens": "always",
"bracketSameLine": false,
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"jsxBracketSameLine": false,
"jsxSingleQuote": true,
"printWidth": 120,
"proseWrap": "preserve",
"quoteProps": "consistent",
"semi": true,
"singleAttributePerLine": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"proseWrap": "never",
"endOfLine": "auto"
}
"useTabs": false
}

View file

@ -1,7 +1,8 @@
import { NS } from '@ns';
import { scan } from './utils/scan';
import { startall } from './utils/startall';
import { kill } from './utils/kill';
export const main = (ns: NS) => {
ns.tprint(ns.purchaseServer('chungus', 128));
ns.tprint(ns.getPurchasedServerCost(256));
};

28
src/utils/kill.ts Normal file
View file

@ -0,0 +1,28 @@
import { NS } from '@ns';
export const kill = (ns: NS, hostname: string) => {
// get a list of all the running processes on the specified host
const processes = ns.ps(hostname);
let killedProcesses = 0;
let killedThreads = 0;
let memFreed = 0;
processes.forEach((process) => {
// calculate the amount of memory that will be freed by killing this process
memFreed = process.threads * ns.getScriptRam(process.filename);
// kill each process by its pid
ns.kill(process.pid);
// update the counters
killedProcesses++;
killedThreads += process.threads;
memFreed += memFreed;
});
ns.tprint(`Killed processes on ${hostname}:`);
ns.tprint(`Processes killed: ${killedProcesses}`);
ns.tprint(`Threads killed: ${killedThreads}`);
ns.tprint(`Memory freed: ${memFreed} GB`);
};

View 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);
};

View 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);
};