Advertisement
tazyload

Untitled

Dec 4th, 2023
1,380
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 target = ns.args[0];
  4.   let moneyThresh = ns.getServerMaxMoney(target) * 0.75;
  5.   let securityThresh = ns.getServerMinSecurityLevel(target) + 5;
  6.   let servers = [];
  7.   let ramPerThread = ns.getScriptRam("/shared/weaken.js");
  8.  
  9.   // This is required for later on where I want the targets money printed into the log and actually look nice.
  10.   const formatter = new Intl.NumberFormat('en-US', {
  11.     style: 'currency',
  12.     currency: 'USD',
  13.  
  14.     // These options are needed to round to whole numbers if that's what you want.
  15.     //minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
  16.     maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
  17.   });
  18.  
  19.   // This goes through every server, NUKE's it (if we own the needed exploits) and dumps our little
  20.   // worker scripts so we can use them later on.
  21.   let serversToScan = ns.scan("home");
  22.   while (serversToScan.length > 0) {
  23.     let server = serversToScan.shift();
  24.     if (!servers.includes(server) && server !== "home") {
  25.       servers.push(server);
  26.       serversToScan = serversToScan.concat(ns.scan(server));
  27.       await ns.scp([
  28.         "/shared/weaken.js",
  29.         "/shared/grow.js",
  30.         "/shared/hack.js"
  31.       ], server);
  32.  
  33.       let openPorts = 0;
  34.       if (ns.fileExists("BruteSSH.exe")) {
  35.         ns.brutessh(server);
  36.         openPorts++;
  37.       }
  38.       if (ns.fileExists("FTPCrack.exe")) {
  39.         ns.ftpcrack(server);
  40.         openPorts++;
  41.       }
  42.       if (ns.fileExists("RelaySMTP.exe")) {
  43.         ns.relaysmtp(server);
  44.         openPorts++;
  45.       }
  46.       if (ns.fileExists("HTTPWorm.exe")) {
  47.         ns.httpworm(server);
  48.         openPorts++;
  49.       }
  50.       if (ns.fileExists("SQLInject.exe")) {
  51.         ns.sqlinject(server);
  52.         openPorts++;
  53.       }
  54.       if (ns.getServerNumPortsRequired(server) <= openPorts) {
  55.         ns.nuke(server);
  56.       }
  57.     }
  58.   }
  59.   // Everything below is the real "Meat and potatoes" of the script
  60.   if (ns.hasRootAccess(target)) {
  61.  
  62.     while (true) {
  63.       let sleepTime = 3000;
  64.       let activeScript = "";
  65.       let targetCurrentMoney = ns.getServerMoneyAvailable(target);
  66.       let targetMaxMoney = ns.getServerMaxMoney(target);
  67.       let targetCurrentSecurity = ns.getServerSecurityLevel(target);
  68.       let targetMaxSecurity = ns.getServerBaseSecurityLevel(target);
  69.       var threadTotal = 0;
  70.       var weakenActive = 0;
  71.       var growActive = 0;
  72.       var hackActive = 0;
  73.       let hackingLevel = ns.getServerRequiredHackingLevel(target);
  74.  
  75.       // I really wanted to have the time the current attack was activated and would end so this means
  76.       // we can use "time" to use later.
  77.       const today = new Date();
  78.       const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  79.  
  80.       for (let server of servers) {
  81.         if (ns.hasRootAccess(server)) {
  82.           let ramAvailable = ns.getServerMaxRam(server) - ns.getServerUsedRam(server);
  83.           let threads = Math.floor(ramAvailable / ramPerThread);
  84.           let weakenThreadTotal = Math.floor(ns.getServerSecurityLevel(target) / 0.05);
  85.           let growThreadTotal = Math.floor(ns.getServerBaseSecurityLevel(target) / 0.004);
  86.           let hackThreadTotal = Math.floor(ns.getServerBaseSecurityLevel(target) / 0.002);
  87.           let weakenThread = weakenThreadTotal - threadTotal;
  88.           let growThread = growThreadTotal - threadTotal;
  89.           let hackThread = hackThreadTotal - threadTotal;
  90.  
  91.           // Here we figure out which attack to execute but also make sure it only happens using the maximum number
  92.           // of threads it needs so we can then attack other servers.
  93.           if (threads > 0) {
  94.             if (ns.getServerSecurityLevel(target) > securityThresh && threadTotal < weakenThread && growActive == 0 && hackActive == 0) {
  95.               sleepTime = ns.getWeakenTime(target);
  96.               if (weakenThreadTotal < threads) {
  97.                 ns.exec("/shared/weaken.js", server, weakenThreadTotal, target);
  98.                 threadTotal += weakenThreadTotal;
  99.               } else if (weakenThread > threadTotal) {
  100.                 ns.exec("/shared/weaken.js", server, threads, target);
  101.                 threadTotal += threads;
  102.               } else {
  103.                 ns.exec("/shared/weaken.js", server, weakenThread, target);
  104.                 threadTotal += weakenThread;
  105.               }
  106.               activeScript = "Weaken";
  107.               weakenActive = 1;
  108.             } else if (ns.getServerMoneyAvailable(target) < moneyThresh && threadTotal < growThread && weakenActive == 0 && hackActive == 0) {
  109.               sleepTime = ns.getGrowTime(target);
  110.               if (growThreadTotal < threads) {
  111.                 ns.exec("/shared/grow.js", server, growThreadTotal, target);
  112.                 threadTotal += growThreadTotal;
  113.               } else if (growThread > threadTotal) {
  114.                 ns.exec("/shared/grow.js", server, threads, target);
  115.                 threadTotal += threads;
  116.               } else {
  117.                 ns.exec("/shared/grow.js", server, growThread, target);
  118.                 threadTotal += growThread;
  119.               }
  120.               activeScript = "Grow";
  121.               growActive = 1;
  122.             } else if (ns.getServerSecurityLevel(target) < securityThresh && threadTotal < hackThread && weakenActive == 0 && growActive == 0) {
  123.               sleepTime = ns.getHackTime(target);
  124.               if (hackThreadTotal < threads) {
  125.                 ns.exec("/shared/hack.js", server, hackThreadTotal, target);
  126.                 threadTotal += hackThreadTotal;
  127.               } else if (hackThread > threadTotal) {
  128.                 ns.exec("/shared/hack.js", server, threads, target);
  129.                 threadTotal += threads;
  130.               } else {
  131.                 ns.exec("/shared/hack.js", server, hackThread, target);
  132.                 threadTotal += hackThread;
  133.               }
  134.               activeScript = "Hack";
  135.               hackActive = 1;
  136.             }
  137.           }
  138.         }
  139.       }
  140.       // This took me a while to figure out but it allows us to modify the time information from earlier so we
  141.       // can then print out the time the script will finish. Handy for people that just like watching the
  142.       // game run.
  143.       function addSeconds(today, seconds) {
  144.         today.setSeconds(today.getSeconds() + seconds);
  145.         return today;
  146.       }
  147.  
  148.       const finish = addSeconds(today, (sleepTime / 1000));
  149.       const endTime = finish.getHours() + ":" + finish.getMinutes() + ":" + finish.getSeconds();
  150.  
  151.       // And this just wipes the log window and gives us all the relevant information that we actually care about.
  152.       ns.clearLog();
  153.       ns.print("Target Server:   " + target + " (" + hackingLevel + ")");
  154.       ns.print("Server Money:    " + formatter.format(targetCurrentMoney) + "/" + formatter.format(targetMaxMoney));
  155.       ns.print("Server Security: " + Math.floor(targetCurrentSecurity) + "/" + targetMaxSecurity + " (" + securityThresh + ")");
  156.       ns.print("Active Script:   " + activeScript + " (" + threadTotal + ")");
  157.       ns.print("Start/Finish:    " + time + "/" + endTime);
  158.       await ns.sleep(sleepTime + 100);
  159.     }
  160.   }
  161. }
Tags: Bitburner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement