u/AdPrior5658

My attempt at batching

This my attempt at writing a batching script as non-programmer. How did I do? Also I'm not entirely sure if I understood what batching is, but this is my interpretation.

controller.js:

/** u/param {NS} ns */
export async function main(ns) {


  const args = ns.flags([["help", false]]);
  if (args.help) {
    ns.tprint("This script is a HWGW batch controller.")
    ns.tprint("The script will also nuke the targeted server.")
    ns.tprint("The first argument is the host server, where the HWG scripts will run.")
    ns.tprint("The second argument is the server that the HWG scripts are targeting.")
    ns.tprint(`EXAMPLE: run ${ns.getScriptName()} home n00dles`)
    return;
  }


  // Server that is running the scripts
  let host = ns.args[0]
  // Server the controller is targeting
  let targetServer = ns.args[1]


  //Opens ports on the target server then nukes it.
  let hasAdmin = ns.hasRootAccess(targetServer)
  if (hasAdmin == false) {
    if (ns.fileExists("brutessh.exe", "home")) {
      ns.brutessh(targetServer)
    }
    if (ns.fileExists("ftpcrack.exe", "home")) {
      ns.ftpcrack(targetServer)
    }
    if (ns.fileExists("relaysmtp.exe", "home")) {
      ns.relaysmtp(targetServer)
    }
    if (ns.fileExists("httpworm.exe", "home")) {
      ns.httpworm(targetServer)
    }
    if (ns.fileExists("sqlinject.exe", "home")) {
      ns.sqlinject(targetServer)
    }
    if (ns.getServer(targetServer).openPortCount == 5) {
      ns.nuke(targetServer)
    }
    else {
      ns.tprint(`Failed to NUKE ${targetServer}.`)
      return
    }
  }


  //Copies worker scripts on to the host server
  const scripts = ["hack-worker.js", "grow-worker.js", "weaken-worker.js"]
  for (let script of scripts) {
    if (ns.fileExists(script, host)) {
      continue
    }
    else {
      ns.scp(script, host, "home")
    }
  }


  //Runs scripts in a HWGW pattern
  while (true) {
    const hackRam = ns.getScriptRam("hack-worker.js", host)
    const growRam = ns.getScriptRam("grow-worker.js", host)
    const weakenRam = ns.getScriptRam("weaken-worker.js", host)


    if (ns.fileExists("hack-worker.js", host)) {
      let freeRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host)
      let hackTime = ns.getHackTime(targetServer)
      let hackThreads = Math.floor(freeRam / hackRam)
      ns.exec("hack-worker.js", host, hackThreads, targetServer)
      await ns.sleep(hackTime + 500)
    }
    if (ns.fileExists("weaken-worker.js", host)) {
      let freeRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host)
      let weakenTime = ns.getWeakenTime(targetServer)
      let weakenThreads = Math.floor(freeRam / weakenRam)
      ns.exec("weaken-worker.js", host, weakenThreads, targetServer)
      await ns.sleep(weakenTime + 500)
    }
    if (ns.fileExists("grow-worker.js", host)) {
      let freeRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host)
      let growTime = ns.getGrowTime(targetServer)
      let growThreads = Math.floor(freeRam / growRam)
      ns.exec("grow-worker.js", host, growThreads, targetServer)
      await ns.sleep(growTime + 500)
    }
    if (ns.fileExists("weaken-worker.js", host)) {
      let freeRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host)
      let weakenTime = ns.getWeakenTime(targetServer)
      let weakenThreads = Math.floor(freeRam / weakenRam)
      ns.exec("weaken-worker.js", host, weakenThreads, targetServer)
      await ns.sleep(weakenTime + 500)
    }
    else {
      ns.tprint(`Failed! Check ${host} for scripts`)
      return
    }
  }
}
reddit.com
u/AdPrior5658 — 9 days ago

ns.cloud

I haven't played the game in over a year and decided to start playing again. I was writing a purchase server script but it took me an hour and pulling my hair out to try and figure out why the script just wouldn't work. I took a look at the documentation and saw you have to use ns.cloud. Is this new? And if so have any other ns functions changed?

reddit.com
u/AdPrior5658 — 13 days ago