Advertisement
Guest User

burst hack

a guest
Feb 9th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns **/
  2. /** weaken.js
  3. export async function main(ns) {
  4.     if (ns.args[1]) {
  5.         await ns.sleep(ns.args[1])
  6.     }
  7.     await ns.weaken(ns.args[0])
  8. }
  9. */
  10. export async function main(ns) {
  11.     const disablelogfns = ["disableLog", "purchaseServer", "deleteServer", "travelToCity", "universityCourse", "scp", "purchaseAugmentation", "workForFaction", "joinFaction", "purchaseProgram", "connect", "scan", "getHackingLevel", "getServerUsedRam", "getServerMaxRam", "getServerSecurityLevel", "getServerMinSecurityLevel", "getServerMaxMoney", "getServerMaxMoney", "sleep", "exec", "brutessh", "ftpcrack", "relaysmtp", "httpworm", "sqlinject", "getServerNumPortsRequired", "nuke"]
  12.     for (const fn of disablelogfns) {
  13.         ns.disableLog(fn)
  14.     }
  15.     let TARGET = ns.getServer(ns.args[0] ? ns.args[0] : "n00dles")
  16.     const DELAY = 25 // time between consecutive h/g completions
  17.     const SAFETYDELAY = 15 // accounts for code execution time during burst()
  18.     const RESERVERAM = 8 // reserve ram on home
  19.  
  20.     const SCPFILES = ["weaken.js", "grow.js", "hack.js"]
  21.     const THREADRAM = 1.75
  22.     let HOSTSERVERS = ns.getPurchasedServers()
  23.     HOSTSERVERS.push("home")
  24.     let MAXTHREADS = 0 // max threads of w|g|h across all host servers
  25.     for (const server of HOSTSERVERS) {
  26.         MAXTHREADS += Math.floor(ns.getServerMaxRam(server) / THREADRAM)
  27.         if (server == "home") {
  28.             MAXTHREADS -= Math.ceil((RESERVERAM + ns.getScriptRam(ns.getScriptName())) / THREADRAM)
  29.         }
  30.         await ns.scp(SCPFILES, server)
  31.     }
  32.     let COUNT = 0
  33.  
  34.     function execany(script, threads, ...args) {
  35.         // exec script on any free host server
  36.         // does not raise error on 0 threads
  37.         const scriptram = ns.getScriptRam(script)
  38.         for (const server of HOSTSERVERS) {
  39.             if (threads <= 0) {
  40.                 return
  41.             }
  42.             let thisthreads = Math.floor((ns.getServerMaxRam(server) - ns.getServerUsedRam(server)) / scriptram) // threads allocated to this server
  43.             if (server == "home") {
  44.                 thisthreads -= Math.ceil(RESERVERAM / THREADRAM)
  45.             }
  46.             if (thisthreads <= 0) {
  47.                 continue
  48.             }
  49.             if (thisthreads > threads) {
  50.                 thisthreads = threads
  51.             }
  52.             threads -= thisthreads
  53.             ns.exec(script, server, thisthreads, ...args)
  54.         }
  55.         if (threads > 0) {
  56.             ns.print("execany excess threads ", threads)
  57.         }
  58.     }
  59.  
  60.     function anyscriptsrunning() {
  61.         for (const server of HOSTSERVERS) {
  62.             if (ns.getServerUsedRam(server) > 0) {
  63.                 for (const script of SCPFILES) {
  64.                     if (ns.scriptRunning(script, server)) {
  65.                         return true
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         return false
  71.     }
  72.  
  73.     function killall() {
  74.         let killed = 0
  75.         for (const server of HOSTSERVERS) {
  76.             for (const script of SCPFILES) {
  77.                 killed += ns.scriptKill(script, server)
  78.             }
  79.         }
  80.         ns.print("killed ", killed, " scripts")
  81.     }
  82.  
  83.     function getthreadsweakengrow() {
  84.         let weakenthreads = (TARGET.hackDifficulty - TARGET.minDifficulty) / 0.05
  85.         if (weakenthreads < MAXTHREADS) {
  86.             weakenthreads += (MAXTHREADS - weakenthreads) * 0.004 / (0.054) // to counter grow threads
  87.         } else {
  88.             weakenthreads = MAXTHREADS
  89.         }
  90.         weakenthreads = Math.ceil(weakenthreads)
  91.         let growthreads = MAXTHREADS - weakenthreads
  92.         return [weakenthreads, growthreads]
  93.     }
  94.  
  95.     async function weakengrow() {
  96.         // weakens and grows the TARGET
  97.         let weakenthreads, growthreads
  98.         [weakenthreads, growthreads] = getthreadsweakengrow()
  99.         ns.print(`${TARGET.hostname} sec+ ${TARGET.hackDifficulty - TARGET.minDifficulty}`
  100.             + ` money% ${Math.floor(100 * TARGET.moneyAvailable / TARGET.moneyMax)}`
  101.             + ` w ${weakenthreads} g ${growthreads} | ${Math.floor(ns.getWeakenTime(TARGET.hostname) / 1000)}s`)
  102.         execany("weaken.js", weakenthreads, TARGET.hostname)
  103.         execany("grow.js", growthreads, TARGET.hostname)
  104.         await ns.sleep(ns.getWeakenTime(TARGET.hostname) + DELAY)
  105.         while (anyscriptsrunning()) {
  106.             await ns.sleep(100)
  107.         }
  108.     }
  109.  
  110.     function getthreadsburst() {
  111.         let hackamount = 0.4 // portion of server's money per hack()
  112.         let hackthreads, growthreads, weakenthreads
  113.         while (true) {
  114.             hackthreads = Math.floor(ns.hackAnalyzeThreads(TARGET.hostname, hackamount * TARGET.moneyMax))
  115.             hackthreads = Math.max(1, hackthreads)
  116.             growthreads = Math.ceil(ns.growthAnalyze(TARGET.hostname, 2.2 / (1 - hackamount)))
  117.             weakenthreads = Math.ceil((hackthreads * 0.002 + growthreads * 0.004) / 0.05)
  118.             if (hackthreads + growthreads + weakenthreads > MAXTHREADS) {
  119.                 hackamount *= 0.9 * MAXTHREADS / (hackthreads + growthreads + weakenthreads)
  120.             } else {
  121.                 break
  122.             }
  123.         }
  124.         return [hackthreads, growthreads, weakenthreads]
  125.     }
  126.  
  127.     async function burst() {
  128.         // precisely timed burst avoids levelup interference
  129.         // hacks the TARGET using alternating hacks and grows in rapid succession followed by a weaken
  130.         let hackthreads, growthreads, weakenthreads
  131.         [hackthreads, growthreads, weakenthreads] = getthreadsburst()
  132.         let rounds = MAXTHREADS / (hackthreads + growthreads + weakenthreads)
  133.         let wtime = ns.getWeakenTime(TARGET.hostname)
  134.         rounds = Math.floor(Math.min(rounds, 0.25 * wtime / (2 * DELAY + SAFETYDELAY) - 1))
  135.         execany("weaken.js", MAXTHREADS - rounds * (hackthreads + growthreads), TARGET.hostname, 0.05 * wtime)
  136.         ns.print(TARGET.hostname, " r|g|h ", rounds, "|", hackthreads, "|", growthreads,
  137.             " | ", COUNT, " | ", Math.floor(1.05 * wtime / 1000), "s")
  138.         for (let i = 0; i < rounds; i++) {
  139.             execany("grow.js", growthreads, TARGET.hostname, 0, i)
  140.             execany("hack.js", hackthreads, TARGET.hostname, 0.55 * wtime - DELAY, i)
  141.             await ns.sleep(2 * DELAY)
  142.             TARGET = ns.getServer(TARGET.hostname)
  143.         }
  144.  
  145.         await ns.sleep(0.8 * wtime)
  146.         let now = ns.getTimeSinceLastAug()
  147.         while (anyscriptsrunning()) {
  148.             await ns.sleep(100)
  149.         }
  150.         let diff = ns.getTimeSinceLastAug() - now
  151.         // ns.print("slack time ", diff / 1000, "s ", Math.round(100 * diff / (0.25 * wtime)), "%")
  152.     }
  153.  
  154.     // START
  155.     ns.tail()
  156.     killall()
  157.     while (true) {
  158.         TARGET = ns.getServer(ns.args[0])
  159.         if (TARGET.hackDifficulty > TARGET.minDifficulty || TARGET.moneyAvailable < TARGET.moneyMax) {
  160.             COUNT = 0
  161.             await weakengrow()
  162.         } else {
  163.             COUNT++
  164.             await burst()
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement