Advertisement
Limegirl420

auto hack bitburner5

Jul 11th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | Source Code | 0 0
  1. // Log the time since last augmentation in a text file
  2. async function log(ns) {
  3. const logFile = "farm-log.txt";
  4. const currentTime = new Date();
  5. const time = await ns.getTimeSinceLastAug();
  6. const entry = `${currentTime} - Time Since Last Aug: ${time} seconds`;
  7. ns.write(logFile, `${entry}\n`, "a");
  8. }
  9.  
  10. // Resolve hosts by copying required scripts to them
  11. async function resolveHosts(ns, targetList, hostList) {
  12. const exes = ["hack", "grow", "weaken"]; // Define the exes array here
  13. for (let i = 0; i < targetList.length; i++) {
  14. if (hostList[i] === "NULL") {
  15. for (let j = 0; j < exes.length; j++) {
  16. await ns.scp(exes[j] + ".js", targetList[i]);
  17. }
  18. hostList[i] = targetList[i];
  19. }
  20. }
  21. }
  22.  
  23. // Check if all required scripts exist, exit if any are missing
  24. function scanExes(ns) {
  25. const exes = ["hack", "grow", "weaken"]; // Define the exes array here
  26. for (let i = 0; i < exes.length; i++) {
  27. if (!ns.fileExists(exes[i] + ".js")) {
  28. ns.tprint(`ERROR: ${exes[i]}.js not found`);
  29. ns.exit();
  30. }
  31. }
  32. }
  33.  
  34. // Determine the action priority order for a target server
  35. function resolveActions(ns, target) {
  36. const returnArray = ["", "", ""];
  37. const securityThreshold = 0.9 * ns.getServerMinSecurityLevel(target);
  38. const hackingLevel = ns.getHackingLevel();
  39. const requiredHackingLevel = ns.getServerRequiredHackingLevel(target);
  40.  
  41. if (ns.getServerSecurityLevel(target) > securityThreshold) {
  42. returnArray[0] = "grow.js";
  43. if (hackingLevel >= requiredHackingLevel) {
  44. returnArray[1] = "hack.js";
  45. if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
  46. returnArray[2] = "weaken.js";
  47. }
  48. } else {
  49. returnArray[1] = "weaken.js";
  50. if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
  51. returnArray[2] = "hack.js";
  52. }
  53. }
  54. } else {
  55. returnArray[0] = "hack.js";
  56. if (ns.getServerMoneyAvailable(target) < ns.getServerMaxMoney(target) * 0.75) {
  57. returnArray[1] = "grow.js";
  58. if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
  59. returnArray[2] = "weaken.js";
  60. }
  61. } else {
  62. returnArray[1] = "weaken.js";
  63. if (ns.getServerMoneyAvailable(target) > ns.getServerMaxMoney(target) * 0.2) {
  64. returnArray[2] = "grow.js";
  65. }
  66. }
  67. }
  68. return returnArray;
  69. }
  70. // Made by Potato: potatonumber2, with the help of hydroflame#3661 MGorak caldwell74 hydroxonium missymae#2783 paulcdejean mynameisromayne
  71. // Scan a target server for existing scripts and launch missing ones
  72. async function scanServers(ns, target) {
  73. const scripts = ns.ls(target, ".js");
  74.  
  75. if (!scripts.includes("weaken.js")) {
  76. await ns.scp("weaken.js", target);
  77. }
  78. if (!scripts.includes("hack.js")) {
  79. await ns.scp("hack.js", target);
  80. }
  81. if (!scripts.includes("grow.js")) {
  82. await ns.scp("grow.js", target);
  83. }
  84.  
  85. const status = ns.ps(target);
  86. if (status.length < 3) {
  87. const actions = resolveActions(ns, target);
  88. for (let i = 0; i < actions.length; i++) {
  89. if (actions[i] !== "") {
  90. const scriptName = actions[i];
  91. const scriptPath = target + "/" + scriptName;
  92. const maxThreads = Math.floor(ns.getScriptRam(scriptPath) / ns.getScriptRam(scriptName));
  93.  
  94. if (maxThreads > 0) {
  95. await ns.exec(scriptName, target, maxThreads);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. // Execute the script logic for each host in the hostList
  102. async function runScripts(ns, hostList) {
  103. for (let i = 0; i < hostList.length; i++) {
  104. if (hostList[i] !== "NULL") {
  105. await scanServers(ns, hostList[i]);
  106. }
  107. }
  108. }
  109.  
  110. /**
  111. * Main function to run the autofarm script
  112. * @param {NS} ns - The namespace object
  113. */
  114. export async function main(ns) {
  115. const targetList = ["foodnstuff", "sigma-cosmetics", "joesguns", "nectar-net", "hong-fang-tea", "harakiri-sushi"];
  116. const hostList = ["NULL", "NULL", "NULL", "NULL", "NULL", "NULL"];
  117.  
  118. // Resolve hosts and copy required scripts
  119. await resolveHosts(ns, targetList, hostList);
  120.  
  121. // Check if all required scripts exist
  122. scanExes(ns);
  123.  
  124. while (true) {
  125. // Log the time since last augmentation
  126. await log(ns);
  127.  
  128. // Execute the script logic for each host in the hostList
  129. await runScripts(ns, hostList);
  130.  
  131. // Wait for 1 second before starting the next cycle
  132. await ns.sleep(1000);
  133. }
  134. }
  135.  
Tags: Bitburner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement