Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Log the time since last augmentation in a text file
- async function log(ns) {
- const logFile = "farm-log.txt";
- const currentTime = new Date();
- const time = await ns.getTimeSinceLastAug();
- const entry = `${currentTime} - Time Since Last Aug: ${time} seconds`;
- ns.write(logFile, `${entry}\n`, "a");
- }
- // Resolve hosts by copying required scripts to them
- async function resolveHosts(ns, targetList, hostList) {
- const exes = ["hack", "grow", "weaken"]; // Define the exes array here
- for (let i = 0; i < targetList.length; i++) {
- if (hostList[i] === "NULL") {
- for (let j = 0; j < exes.length; j++) {
- await ns.scp(exes[j] + ".js", targetList[i]);
- }
- hostList[i] = targetList[i];
- }
- }
- }
- // Check if all required scripts exist, exit if any are missing
- function scanExes(ns) {
- const exes = ["hack", "grow", "weaken"]; // Define the exes array here
- for (let i = 0; i < exes.length; i++) {
- if (!ns.fileExists(exes[i] + ".js")) {
- ns.tprint(`ERROR: ${exes[i]}.js not found`);
- ns.exit();
- }
- }
- }
- // Determine the action priority order for a target server
- function resolveActions(ns, target) {
- const returnArray = ["", "", ""];
- const securityThreshold = 0.9 * ns.getServerMinSecurityLevel(target);
- const hackingLevel = ns.getHackingLevel();
- const requiredHackingLevel = ns.getServerRequiredHackingLevel(target);
- if (ns.getServerSecurityLevel(target) > securityThreshold) {
- returnArray[0] = "grow.js";
- if (hackingLevel >= requiredHackingLevel) {
- returnArray[1] = "hack.js";
- if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
- returnArray[2] = "weaken.js";
- }
- } else {
- returnArray[1] = "weaken.js";
- if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
- returnArray[2] = "hack.js";
- }
- }
- } else {
- returnArray[0] = "hack.js";
- if (ns.getServerMoneyAvailable(target) < ns.getServerMaxMoney(target) * 0.75) {
- returnArray[1] = "grow.js";
- if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
- returnArray[2] = "weaken.js";
- }
- } else {
- returnArray[1] = "weaken.js";
- if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
- returnArray[2] = "grow.js";
- }
- }
- }
- return returnArray;
- }
- // Made by Potato: potatonumber2, with the help of hydroflame#3661 MGorak caldwell74 hydroxonium missymae#2783 paulcdejean mynameisromayne
- // Scan a target server for existing scripts and launch missing ones
- async function scanServers(ns, target) {
- const scripts = ns.ls(target, ".js");
- if (!scripts.includes("weaken.js")) {
- await ns.scp("weaken.js", target);
- }
- if (!scripts.includes("hack.js")) {
- await ns.scp("hack.js", target);
- }
- if (!scripts.includes("grow.js")) {
- await ns.scp("grow.js", target);
- }
- const status = ns.ps(target);
- if (status.length < 3) {
- const actions = resolveActions(ns, target);
- for (let i = 0; i < actions.length; i++) {
- if (actions[i] !== "") {
- const scriptName = actions[i];
- const scriptPath = target + "/" + scriptName;
- const maxThreads = Math.floor(ns.getScriptRam(scriptPath) / ns.getScriptRam(scriptName));
- if (maxThreads > 0) {
- await ns.exec(scriptName, target, maxThreads);
- }
- }
- }
- }
- }
- // Execute the script logic for each host in the hostList
- async function runScripts(ns, hostList) {
- for (let i = 0; i < hostList.length; i++) {
- if (hostList[i] !== "NULL") {
- await scanServers(ns, hostList[i]);
- }
- }
- }
- /**
- * Main function to run the autofarm script
- * @param {NS} ns - The namespace object
- */
- export async function main(ns) {
- const targetList = ["foodnstuff", "sigma-cosmetics", "joesguns", "nectar-net", "hong-fang-tea", "harakiri-sushi"];
- const hostList = ["NULL", "NULL", "NULL", "NULL", "NULL", "NULL"];
- // Resolve hosts and copy required scripts
- await resolveHosts(ns, targetList, hostList);
- // Check if all required scripts exist
- scanExes(ns);
- while (true) {
- // Log the time since last augmentation
- await log(ns);
- // Execute the script logic for each host in the hostList
- await runScripts(ns, hostList);
- // Wait for 1 second before starting the next cycle
- await ns.sleep(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement