The3vilM0nk3y

tools/buyserver

Apr 23rd, 2022
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns */
  2. export async function main(ns) {
  3.     let validNums = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
  4.     1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576];
  5.     const args = ns.flags([["help", false]]);
  6.     let mem = args._[0];
  7.     if (!mem || args.help) {
  8.         ns.tprint("This script buys a server at the specified RAM.");
  9.         ns.tprint("If the second argument is optional and if left empty, the server will be named:");
  10.         ns.tprint("  MyServer-MEMSIZE");
  11.         ns.tprint("%m in the server name argument will be replaced with the memory size.");
  12.         ns.tprint(`Usage: run ${ns.getScriptName()} MEMSIZE [SERVERNAME]`);
  13.         ns.tprint("Examples:");
  14.         ns.tprint(`> run ${ns.getScriptName()} 32`);
  15.         ns.tprint(`> run ${ns.getScriptName()} 1024 MyAwesomeServer`);
  16.         ns.tprint(`> run ${ns.getScriptName()} 1048576 Server-%m`);
  17.         return;
  18.     }
  19.  
  20.     if (ns.args[0]){
  21.         let cost = ns.getPurchasedServerCost(ns.args[0]);
  22.         if (cost < ns.getServerMoneyAvailable("home")){
  23.             if (validNums.includes(ns.args[0] )){
  24.                 let name = "MyServer-" + ns.args[0];
  25.                 if (ns.args[1]) {
  26.                     name = ns.args[1];
  27.                     name = name.replace(/%m/g, ns.args[0]);
  28.                 }
  29.                 //ns.tprint(name);
  30.                 let hostname = ns.purchaseServer(name, ns.args[0]);
  31.                 if (hostname != "") ns.tprint(hostname, " purchased for ", cost);
  32.                 else ns.tprint("Something went wrong when purchasing the server.");
  33.             } else ns.tprint(ns.args[0], " is an invalid size. Valid sizes are - ", validNums);
  34.         } else ns.tprint("You do not have enough money! Cost: ", cost);
  35.     } else ns.tprint("You need to include the size of the server. Valid sizes are - ", validNums);
  36. }
  37. export function autocomplete(data, args) {
  38.     return ["1", "2", "4", "8", "16", "32", "64", "128", "256", "512",
  39.     "1024", "2048", "4096", "8192", "16384", "32768", "65536", "131072", "262144", "524288", "1048576"];
  40. }
Advertisement
Add Comment
Please, Sign In to add comment