Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns */
- export async function main(ns) {
- /**
- * SERVER MANAGER
- * ---------------------------------------------------------
- * THIS SCRIPT HANDLES BUYING AND UPGRADING SERVERS
- * ITS MAIN FUNCTION IS FINDING SPACE FOR A GIVEN SCRIPT AND STARTING IT THERE
- * ---------------------------------------------------------
- * IT LISTENS ON PORT 1, TAKING:
- *
- * 1. The Script you want to run ex. string "hack.js"
- * 2. The amount of wanted threads ex. int 2 default: 1
- * 3. The Structure Name ex. string "sv_TestServer_" default: "sv_StandardServer_"
- * 4. The location of the script ex. string "home" default: "home"
- *
- * EVERYTHING AFTER 4 IS PASSED AS AN ARGUMENT TO THE SCRIPT
- * ---------------------------------------------------------
- */
- //Only change things here.
- const portNum = 1; //Port Number. Ranges from 1-20
- const maxServer = 8; //Maxmimum Servers that are purchased for a single network.
- //Script starts here. Looping endlessly.
- while (true) {
- /**
- * VARIABLE MANAGEMENT
- * This means reading Port 1 and getting variables that change more regularly.
- * -----------------------------------------
- * 1. Get next argument in Port Queue 1.
- * 2. If port is empty, pause and wait for a write operation.
- * 3. Parse variables
- * 4. Check for errors
- * 5. Set additional variables
- * 5. Toast the host of current request.
- * -----------------------------------------
- */
- //Part 1 / 2 - Read Port
- //-----------------------------------------
- var args = ns.readPort(portNum); //Read from port
- if (args == "NULL PORT DATA") {
- await ns.nextPortWrite(portNum); //Wait for PortWrite. See step 2.
- var args = ns.readPort(portNum); //Read the new content.
- }
- var splitArgs = args.split(","); //Split Arguments
- ns.print (args)
- //Part 3 - Parse Variables
- //-----------------------------------------
- if (splitArgs[0]== null) {
- ns.alert("serverMaster wasn't passed any arguments. How did you even manage this. Breaking...");
- continue;
- } // Set Name
- else { var name = splitArgs[0] }
- if (splitArgs[1] == null || splitArgs[1] == "0") { var threads = 1 } // Set Threads
- else { var threads = Math.ceil(splitArgs[1]) }
- if (splitArgs[2] == null) { var host = "sv_StandardServer_" } // Set Server Name
- else { var host = splitArgs[2] }
- if (splitArgs[3] == null) { var scriptLoc = "home" } // Set Script Location
- else { var scriptLoc = splitArgs[3] }
- var restArgs = []; // Set script Arguments
- for (let i = 4; i < splitArgs.length; i++) { restArgs.push(splitArgs[i]) }
- ns.print(restArgs.toString())
- //Part 4 - Check for problems
- //-----------------------------------------
- if (!ns.fileExists(name, scriptLoc)) {
- ns.alert("The file " + name + " does not exist at " + scriptLoc + ". Breaking...")
- continue;
- }
- //Part 5 - Set additional variables
- //-----------------------------------------
- var actualWeight = ns.getScriptRam(name, scriptLoc) * threads; // Set total Ram Cost
- var roundedWeight = Math.ceil(actualWeight/2) * 2; // Sets Ram cost to the next higher, even int
- let player = ns.getPlayer(); // Get Player
- var availableServers= []; // Prepare for wanted servers
- var currentServers = ns.getPurchasedServers(); // Get already purchased servers
- ns.print ("Needed Ram = " + roundedWeight + " for " + threads + " threads.")
- //Get already established Servers and their Ram
- for (const server of currentServers) {
- if (server.includes(host)) {
- availableServers.push (server)
- ns.print ("Added " + server + " to available")
- }
- }
- ns.print(availableServers.toString())
- /**
- * MAIN FUNCTION
- * ---------------------------------------------------------
- * Automatically assigns script to a server.
- * If no server has enough space, automatically upgrades or buys new ones.
- * ---------------------------------------------------------
- * 1. Checks through already established servers and returns name if it finds enough space.
- * 2. Checks if any available server can be upgraded enough and chooses the cheapest.
- * 3. Checks if it can buy a new server with enough capacity. This has a maximum of 8 by default, more can be set above.
- * 4. If player doesn't have enough money to purchase anything, puts request back into queue.
- * ---------------------------------------------------------
- */
- var success = false;
- //Part 1 - Check for space
- //-----------------------------------------
- for (const server of availableServers) {
- //Check if server has enough space.
- //If yes, start script and end cycle.
- if ((ns.getServerMaxRam(server) - ns.getServerUsedRam(server)) > actualWeight) {
- await startScript(server, name, threads, restArgs, scriptLoc);
- success = true;
- break;
- }
- }
- if (success) { continue } // Move onto next cycle
- // Part 2 - Check for Upgrades
- // Check if an upgrade can be afforded and puts out the cheapest option.
- // --------------------------------------------------------------------------
- var cheapestPrice = -1;
- for (const server of availableServers) {
- //Calculates how much Ram needs to be bought
- var freeRam = Math.floor((ns.getServerMaxRam(server) - ns.getServerUsedRam(server)));
- var neededRam = Math.pow(2, Math.ceil(Math.log2(roundedWeight + ns.getServerMaxRam(server) - freeRam))) //Set to next power of 2
- //Get the price.
- var price = ns.getPurchasedServerUpgradeCost(server, neededRam);
- //Check if Cheapest / First Server
- if (price < cheapestPrice || cheapestPrice == -1) {
- var cheapestPrice = price;
- var cheapestServer = server;
- var cheapestNeeded = ns.getServerMaxRam(server) + neededRam;
- }
- }
- //Check if Cheapest Price is affordable
- if (cheapestPrice < player.money && !cheapestPrice == -1) {
- ns.print("Cheapest Server is " + cheapestServer + " at " + cheapestPrice)
- //Is affordable. Purchase, run and continue
- ns.upgradePurchasedServer(cheapestServer, cheapestNeeded);
- await startScript(cheapestServer, name, threads, restArgs, scriptLoc);
- continue;
- }
- // Part 3 - Buy a new Server
- // Check if you can buy a new Server and does so. Last line of defense.
- // --------------------------------------------------------------------------
- if (player.money > ns.getPurchasedServerCost(Math.pow(2, Math.ceil(Math.log2(roundedWeight)))) && availableServers.length < maxServer){
- //Can purchase server.
- var newName = host + availableServers.length.toString()
- await ns.purchaseServer(newName, Math.pow(2, Math.ceil(Math.log2(roundedWeight))));
- await startScript(newName, name, threads, restArgs, scriptLoc);
- continue;
- }
- else {
- //Can't purchase new server. Returning request to end of queue.
- ns.writePort(portNum, args);
- await ns.sleep(10000)
- continue;
- }
- }
- async function startScript(host, name, threads, args, fileLoc) {
- ns.scp (name, host, fileLoc);
- ns.exec (name, host, threads, args.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement