Advertisement
azielke

/HackBatch/hack_batch_executor.js

Jan 22nd, 2022
1,347
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** @param {NS} ns **/
  2.  
  3. import { HACKING_SCRIPT, GROW_SCRIPT, WEAKENING_SCRIPT } from "/HackBatch/hack_utils.js";
  4.  
  5.  
  6. class Action {
  7.     constructor(script, offset, threads) {
  8.         this.script = script;
  9.         this.offset = offset;
  10.         this.threads = threads;
  11.     }
  12. }
  13.  
  14. class Batch {
  15.     constructor(ns, executingServer, target, batchId = Date.now(), ramAvailabilityFactor = 1) {
  16.         this.ns = ns;
  17.         this.batchId = batchId;
  18.         this.actions = [];
  19.         this.executingServer = executingServer;
  20.         this.target = target;
  21.         this.ramAvailabilityFactor = ramAvailabilityFactor;
  22.     }
  23.  
  24.     addAction(action) {
  25.         if (this.actions.length < 4) {
  26.             this.actions.push(action);
  27.         }
  28.         if (this.actions.length == 4) {
  29.             this.actions.sort((a, b) => a.offset - b.offset);
  30.         }
  31.     }
  32.  
  33.     async execute() {
  34.         if (this.actions.length != 4) {
  35.             this.ns.print("Wrong number of actions " + this.actions.length);
  36.             return;
  37.         }
  38.  
  39.         const ramAvailable = this.ns.getServerMaxRam(this.executingServer) - this.ns.getServerUsedRam(this.executingServer);
  40.         const ramNeeded = Math.ceil(this.actions.reduce((accumulator, element) =>
  41.             accumulator + this.ns.getScriptRam(element.script, this.target) * element.threads,
  42.             0));
  43.         if (ramNeeded * this.ramAvailabilityFactor > ramAvailable) {
  44.             this.ns.print("Not enough RAM. Needed: " + (ramNeeded * this.ramAvailabilityFactor) + " available: " + ramAvailable)
  45.             return;
  46.         }
  47.  
  48.         this.ns.print("Starting batch " + this.batchId + " on server " + this.executingServer + " attacking " + this.target);
  49.         let accumBuffer = 0;
  50.         for (let idx = 0; idx < this.actions.length; ++idx) {
  51.             const action = this.actions[idx];
  52.             let sleepTime = (idx == 0) ? action.offset : action.offset - this.actions[idx - 1].offset;
  53.             if (idx > 0 && sleepTime < 50) {
  54.                 accumBuffer += 50;
  55.             }
  56.             sleepTime += accumBuffer;
  57.             if (sleepTime > 0) {
  58.                 await this.ns.sleep(sleepTime);
  59.             }
  60.             if (action.threads > 0) {
  61.                 //this.ns.print("Executing " + action.script + " with " + action.threads + " threads.");
  62.                 this.ns.exec(action.script, this.executingServer, action.threads, this.target, this.batchId + idx);
  63.             } else {
  64.                 this.ns.print("Warning: action " + action.script + " without threads!");
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. function calculateWeakenThreads(ns, securityDelta, serverInfo) {
  71.     const weakenThreadsCalc = securityDelta > 0 ? Math.ceil(securityDelta / 0.05) : 0;
  72.     let decrease = ns.weakenAnalyze(weakenThreadsCalc, serverInfo.cpuCores);
  73.     let weakenThreads = weakenThreadsCalc;
  74.     while (decrease > securityDelta) {
  75.         weakenThreads--;
  76.         decrease = ns.weakenAnalyze(weakenThreads, serverInfo.cpuCores);
  77.     }
  78.     return Math.max(1, weakenThreads);
  79. }
  80.  
  81.  
  82. export async function main(ns) {
  83.     const weakenOffset = 1000; // TODO reduce
  84.  
  85.     const executingServer = ns.args[0];
  86.     const target = ns.args[1];
  87.     const batchId = parseInt(ns.args[2]);
  88.     const initialSecurityDelta = ns.args.length >=3 ? parseInt(ns.args[3]) : -1;
  89.  
  90.     const batch = new Batch(ns, executingServer, target, batchId);
  91.  
  92.     const serverInfo = ns.getServer(executingServer);
  93.     const weakenTime = ns.getWeakenTime(target);
  94.     const securityDelta = initialSecurityDelta < 0 ? ns.getServerSecurityLevel(target) - ns.getServerMinSecurityLevel(target) : initialSecurityDelta;
  95.     let weakenThreads = calculateWeakenThreads(ns, securityDelta, serverInfo);
  96.     batch.addAction(new Action(WEAKENING_SCRIPT, 0, weakenThreads));
  97.  
  98.     let growOffset = weakenTime + weakenOffset / 2 - ns.getGrowTime(target);
  99.     let moneyAvailable = ns.getServerMoneyAvailable(target);
  100.     let maxMoney = ns.getServerMaxMoney(target);
  101.     let lowerLimit = maxMoney * 0.4;
  102.     let moneyGrowFactor = (moneyAvailable > lowerLimit) ? ns.getServerMaxMoney(target) * 1.1 / ns.getServerMoneyAvailable(target) : 3;
  103.     let growThreads = Math.ceil(ns.growthAnalyze(target, moneyGrowFactor, serverInfo.cpuCores));
  104.     ns.print(ns.nFormat(moneyAvailable, "$0.000a") + "/" + ns.nFormat(maxMoney, "0.000a") + " --> " + ns.nFormat(moneyGrowFactor, "0.00") + " grow threads: " + growThreads);
  105.  
  106.     batch.addAction(new Action(GROW_SCRIPT, growOffset, growThreads));
  107.  
  108.     let securityIncreaseByGrow = ns.growthAnalyzeSecurity(growThreads);
  109.     let secondWeakenThreads = calculateWeakenThreads(ns, securityIncreaseByGrow, serverInfo);
  110.     batch.addAction(new Action(WEAKENING_SCRIPT, weakenOffset, secondWeakenThreads));
  111.  
  112.     let hackTime = ns.getHackTime(target);
  113.     let moneyPerHackThread = ns.hackAnalyze(target);
  114.     let hackThreads = moneyGrowFactor >= 3 ? 1 : Math.floor(0.5 / moneyPerHackThread);  //ns.hackAnalyzeThreads(target, maxMoney / 2);
  115.     let hackSecurityInc = ns.hackAnalyzeSecurity(hackThreads);
  116.    
  117.     let hackOffset = 1.5 * weakenOffset + weakenTime - hackTime;
  118.     batch.addAction(new Action(HACKING_SCRIPT, hackOffset, hackThreads));
  119.  
  120.     await batch.execute();
  121. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement