Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { weakPerHack, growPerHack } from "utils.ns";
- import { Servers } from "servers.ns";
- export const scriptFiles = [
- "masterFarm.ns",
- "masterFarmWeak.ns",
- "masterFarmGrow.ns",
- "masterFarmHack.ns",
- "utils.ns",
- "useAllRam.ns",
- "servers.ns"
- ];
- class ExtensibleFunction extends Function {
- constructor(f) {
- return Object.setPrototypeOf(f, new.target.prototype);
- }
- }
- export class VirtualNsFunction extends ExtensibleFunction {
- constructor(threads, target, funcName, scriptName) {
- super(() => this.__call__());
- this.threads = threads;
- this.target = target;
- this.funcName = funcName;
- this.scriptName = scriptName;
- this.data = [];
- this.pending = 0;
- }
- notifyIncomming() {
- this.pending += 1;
- }
- add(ns) {
- this.pending = Math.max(0, this.pending - 1);
- this.data.unshift(ns);
- }
- kill() {
- while (this.data && this.data.length > 0) {
- nsExchange.zombies.push(this.data.pop().remoteExit());
- }
- }
- get isFree() {
- return this.data[0] && !this.data[0].inUse;
- }
- get len() {
- return this.data.length + this.pending;
- }
- __call__() {
- if (this.isFree) {
- const ns = this.data.shift();
- ns.inUse = true;
- this.data.push(ns);
- let str = "";
- if (nsExchange.verbose) {
- str = this.funcName[0];
- switch (this.funcName) {
- case "hack":
- str = str.padStart(20);
- break;
- case "grow":
- str = str.padStart(22);
- break;
- default:
- str = str.padStart(24);
- }
- console.log(Date.now(), "start", str.padEnd(50));
- }
- ns[this.funcName](this.target).then(() => {
- ns.inUse = false;
- this.data.splice(this.data.indexOf(ns), 1)
- this.data.unshift(ns);
- nsExchange.verbose && console.log(Date.now(), "hit", (" " + str).padEnd(42));
- }).catch((e) => {
- console.error(e);
- });
- return true;
- } else {
- return false;
- }
- }
- }
- export class VirtualServer {
- constructor(ns, name, target, maxInstances, workerSizes) {
- this.ns = ns;
- this.name = name;
- this.maxInstances = maxInstances;
- this.weak = new VirtualNsFunction(workerSizes.weak, target, "weaken", "masterFarmWeak.ns");
- this.grow = new VirtualNsFunction(workerSizes.grow, target, "grow", "masterFarmGrow.ns");
- this.hack = new VirtualNsFunction(workerSizes.hack, target, "hack", "masterFarmHack.ns");
- }
- async spawnWorker(script, s, virtualFunction, i) {
- let res = await this.ns.exec(script, s, virtualFunction.threads, this.name, s, i);
- if (res) {
- virtualFunction.notifyIncomming();
- }
- return res;
- }
- async addServer(server) {
- this.ns.scp(scriptFiles, server);
- let res = true;
- for (var i = 0; res && this.weak.len < this.maxInstances; i++) {
- console.log(this.weak.len, this.grow.len, this.hack.len);
- let action = this.weak;
- if (this.grow.len < action.len) action = this.grow;
- if (this.hack.len < action.len) action = this.hack;
- res = await this.spawnWorker(action.scriptName, server, action, i);
- console.log(res);
- }
- }
- kill() {
- this.weak.kill();
- this.grow.kill();
- this.hack.kill();
- nsExchange[this.name] = undefined;
- }
- }
- export function sortNumber(a, b) {
- return a - b;
- }
- export async function main(ns) {
- const type = ns.args[0];
- ns.mainFunc = this;
- switch (type) {
- case "-ctrl":
- {
- setTimeout(() => {
- const win = parent["window"];
- win.doc = win["document"];
- win.w = win;
- win.nsExchange = win.nsExchange || {};
- });
- await ns.sleep(100);
- const server = ns.getHostname();
- const prefix = server.slice(0, -1);
- const target = ns.args[1];
- const margin = ns.args[2];
- const name = ns.args[3] || server;
- let mainI;
- let busy = false;
- const actMargin = 1000;
- const rAct = {
- weak: [],
- grow: [],
- hack: []
- };
- let c = 0;
- const tickTime = 500;
- const wInterval = 4000;
- const wTime = ns.getWeakenTime(target) * 1000;
- const gTime = ns.getGrowTime(target) * 1000;
- const hTime = ns.getHackTime(target) * 1000;
- try {
- if (nsExchange[name]) {
- return ns.tprint("Controller name '" + name + "' already in use!");
- }
- nsExchange.verbose = true;
- nsExchange.zombies = [];
- nsExchange.zombies.kill = () => {
- nsExchange.zombies.forEach(_ns => _ns.remoteExit());
- };
- nsExchange.zombies.clear = () => {
- nsExchange.zombies.forEach(_ns => {
- try {
- _ns.exit();
- } catch (e) {
- nsExchange.zombies.splice(nsExchange.zombies.indexOf(_ns), 1);
- }
- });
- };
- const weakSafety = 1.1;
- const farmUnits = Math.floor(margin / ns.hackAnalyzePercent(target));
- const vServer = nsExchange[name] = new VirtualServer(ns, name, target, wTime / wInterval, {
- weak: farmUnits * Math.max(1, weakPerHack(ns, target, margin)),
- grow: farmUnits * Math.max(1, growPerHack(ns, target, margin)),
- hack: farmUnits * Math.max(1, 1)
- });
- ns.tprint("f: " + farmUnits + " h: " + vServer.hack.threads + " g: " + vServer.grow.threads + " w: " + vServer.weak.threads);
- await vServer.addServer(server);
- if (ns.serverExists(prefix + "1")) {
- for (var i = 1; i < 8; i++) {
- const s = prefix + i;
- ns.scp(scriptFiles, s);
- await vServer.addServer(s);
- }
- }
- ns.tprint("Startup complete!");
- console.log(vServer);
- ns.tprint("Starting Main Loop...");
- setTimeout(() => {
- mainI = setInterval(() => {
- if (busy) {
- nsExchange.verbose && console.info("skipping tick...");
- return;
- } else {
- busy = true;
- var s = Date.now();
- try {
- if (rAct.weak.length + 5 < wTime / wInterval && c % (wInterval / tickTime) === 0) {
- if (vServer.weak()) {
- rAct.weak.push(s + wTime);
- rAct.weak.sort(sortNumber);
- }
- }
- s = Date.now();
- if (rAct.weak.length > 0) {
- const possible = rAct.weak.map(t => t - gTime - s).filter(t => 0 <= t && t <= actMargin);
- if (possible.length > 0 && vServer.grow()) {
- rAct.weak.shift();
- rAct.grow.push(s + gTime);
- rAct.grow.sort(sortNumber);
- }
- }
- s = Date.now();
- if (rAct.grow.length > 0) {
- const possible = rAct.grow.map(t => t - hTime - s).filter(t => 0 <= t && t <= actMargin);
- if (possible.length > 0 && vServer.hack()) {
- rAct.grow.shift();
- rAct.hack.push(s + hTime);
- rAct.hack.sort(sortNumber);
- }
- }
- // run gc
- s = Date.now();
- rAct.weak = rAct.weak.filter(t => t - s > 0);
- rAct.grow = rAct.grow.filter(t => t - s > 0);
- rAct.hack = rAct.hack.filter(t => t - s > 0)
- } finally {
- busy = false;
- }
- if (nsExchange.verbose && c % (30000 / tickTime) === 0) {
- console.info(
- (Math.floor(((rAct.weak[0] || NaN) - s) / 1000) + "").padStart(4),
- (Math.floor(((rAct.grow[0] || NaN) - s) / 1000) + "").padStart(4),
- (Math.floor(((rAct.hack[0] || NaN) - s) / 1000) + "").padStart(4),
- rAct
- );
- }
- }
- c = (c + 1) % 1000000;
- }, tickTime);
- }, 1000 - Date.now() % 1000 + tickTime - 1);
- ns.disableLog("sleep");
- while (true) {
- await ns.sleep(5000);
- }
- } catch (e) {
- console.error(e);
- throw e;
- } finally {
- clearInterval(mainI);
- if (nsExchange[name]) {
- nsExchange[name].kill();
- }
- }
- break;
- }
- case "-kill":
- {
- if (ns.args[1] && nsExchange[ns.args[1]]) {
- nsExchange[ns.args[1]].kill();
- }
- await ns.sleep(5000);
- ns.tprint("Done");this.data.push(ns);
- break;
- }
- case "-add":
- {
- await nsExchange[ns.args[2]].addServer(ns.args[1]);
- break;
- }
- case "-all":
- {
- for (var server of new Servers(ns, ns.args[2]).withRoot) {
- await nsExchange[ns.args[1]].addServer(server);
- };
- break;
- }
- default:
- {
- const prefix = type || "Laser-";
- const target = ns.args[1] || "omega-net";
- const margin = ns.args[2] || 90;
- ns.scp(scriptFiles, prefix + "1");
- ns.exec(scriptFiles[0], prefix + "1", 1, "-ctrl", target, margin);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement