Advertisement
GBH007

Bitburner v2

Dec 26th, 2021
1,260
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 toFarm = []
  4.  
  5.     const farmScriptName = "farm.js"
  6.     const forceUpdate = ns.args.length > 0 && ns.args[0] == "force"
  7.     const myServers = [...ns.getPurchasedServers(), "home"]
  8.  
  9.     let usedServers = {
  10.         "home": extendServerInfo(ns.getServer("home")),
  11.         "darkweb": extendServerInfo(ns.getServer("darkweb"))
  12.     }
  13.  
  14.     const player = ns.getPlayer()
  15.  
  16.     function extendServerInfo(sInfo) {
  17.         sInfo.hostname ||= ""
  18.         sInfo.moneyMax ||= 0
  19.         sInfo.name = sInfo.hostname
  20.         sInfo.hasAdminRights ||= false
  21.         sInfo.requiredHackingSkill ||= 0
  22.         sInfo.moneyMax ||= 0
  23.         try {
  24.             sInfo.ht = ns.getHackTime(sInfo.hostname) / 1000
  25.             sInfo.gt = ns.getGrowTime(sInfo.hostname) / 1000
  26.             sInfo.wt = ns.getWeakenTime(sInfo.hostname) / 1000
  27.             sInfo.cf = sInfo.moneyMax / (sInfo.ht + sInfo.gt + sInfo.wt)
  28.         } catch {
  29.             sInfo.ht = 0
  30.             sInfo.gt = 0
  31.             sInfo.wt = 0
  32.             sInfo.cf = 0
  33.         }
  34.         return sInfo
  35.     }
  36.  
  37.     async function prepareToHackServer(sInfo) {
  38.         if (!sInfo.sshPortOpen && ns.fileExists("BruteSSH.exe", "home")) {
  39.             await ns.brutessh(sInfo.hostname)
  40.             ns.print(sInfo.hostname, " sshPortOpen")
  41.         }
  42.         if (!sInfo.ftpPortOpen && ns.fileExists("FTPCrack.exe", "home")) {
  43.             await ns.ftpcrack(sInfo.hostname)
  44.             ns.print(sInfo.hostname, " ftpPortOpen")
  45.         }
  46.         if (!sInfo.httpPortOpen && ns.fileExists("HTTPWorm.exe", "home")) {
  47.             await ns.httpworm(sInfo.hostname)
  48.             ns.print(sInfo.hostname, " httpPortOpen")
  49.         }
  50.         if (!sInfo.smtpPortOpen && ns.fileExists("relaySMTP.exe", "home")) {
  51.             await ns.relaysmtp(sInfo.hostname)
  52.             ns.print(sInfo.hostname, " smtpPortOpen")
  53.         }
  54.         if (!sInfo.sqlPortOpen && ns.fileExists("SQLInject.exe", "home")) {
  55.             await ns.sqlinject(sInfo.hostname)
  56.             ns.print(sInfo.hostname, " sqlPortOpen")
  57.         }
  58.         if (!sInfo.hasAdminRights && sInfo.openPortCount >= sInfo.numOpenPortsRequired) {
  59.             try {
  60.                 await ns.nuke(sInfo.hostname)
  61.                 ns.print(sInfo.hostname, " hasAdminRights")
  62.             } catch {
  63.                 ns.print(sInfo.hostname, " hack fail")
  64.             }
  65.         }
  66.     }
  67.  
  68.  
  69.     async function createFarm() {
  70.         const content = `/** @param {NS} ns **/
  71. export async function main(ns) {
  72.     let target = ns.args[0];
  73.     let moneyThresh = ns.getServerMaxMoney(target) * 0.99;
  74.     let securityThresh = ns.getServerMinSecurityLevel(target) + 5;
  75.     while (true) {
  76.         if (ns.getServerSecurityLevel(target) > securityThresh) {
  77.             await ns.weaken(target);
  78.         } else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
  79.             await ns.grow(target);
  80.         } else {
  81.             await ns.hack(target);
  82.         }
  83.     }
  84. }`
  85.         await ns.write(farmScriptName, content, "w")
  86.     }
  87.  
  88.  
  89.  
  90.     if (forceUpdate || !ns.fileExists(farmScriptName, "home")) {
  91.         await createFarm()
  92.     }
  93.     const farmScriptCost = ns.getScriptRam(farmScriptName, "home")
  94.  
  95.     let toScan = ["home"]
  96.     while (toScan.length > 0) {
  97.         const servers = ns.scan(toScan.pop())
  98.         for (let serverName of servers) {
  99.             if (serverName in usedServers) {
  100.                 continue
  101.             }
  102.             toScan.push(serverName)
  103.             let sInfo = extendServerInfo(ns.getServer(serverName))
  104.             usedServers[serverName] = sInfo
  105.             if (player.hacking + 5 < sInfo.requiredHackingSkill) { continue }
  106.  
  107.             await prepareToHackServer(sInfo)
  108.             sInfo = extendServerInfo(ns.getServer(serverName))
  109.             usedServers[serverName] = sInfo
  110.  
  111.             if (sInfo.hasAdminRights) {
  112.                 if (myServers.indexOf(serverName) != -1) {
  113.                     continue
  114.                 }
  115.                 if (sInfo.moneyMax > 0) {
  116.                     toFarm.push(sInfo)
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122.     toFarm.sort((a, b) => a.moneyMax - b.moneyMax)
  123.     let servers = Object.values(usedServers).sort((a, b) => b.maxRam - a.maxRam)
  124.     for (const srv of servers) {
  125.         ns.scriptKill(farmScriptName, srv.hostname)
  126.  
  127.         if (toFarm.length < 1) {
  128.             continue
  129.         }
  130.         if (!srv.hasAdminRights) {
  131.             continue
  132.         }
  133.         const maxRam = srv.maxRam + (srv.hostname == "home" ? -20 : 0)
  134.         if (maxRam < farmScriptCost) {
  135.             continue
  136.         }
  137.         ns.tprintf("%s RAM %f", srv.hostname, maxRam)
  138.  
  139.         const farmData = toFarm.pop()
  140.         ns.tprintf("FARM %s MONEY MAX %f", farmData.hostname, farmData.moneyMax)
  141.  
  142.         if (forceUpdate || !ns.fileExists(farmScriptName, srv.hostname)) {
  143.             await ns.scp(farmScriptName, "home", srv.hostname)
  144.         }
  145.         const threads = Math.trunc(maxRam / farmScriptCost)
  146.         ns.exec(farmScriptName, srv.hostname, threads, farmData.hostname)
  147.  
  148.     }
  149.  
  150.     ns.tprintf("FARM SCRIPT COST %f", farmScriptCost)
  151.     ns.tprint("FINISH")
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement