AutoServerHackV2-2Kays.js

AutoServerHackV2-2Kays.js

Overview


AutoServerHackV2-2Kays.js image 1

When you open the log of this script you will be greeted with a helpful display of information. This display was tuned to the default size of a new log on my computer, hopefully its sized correctly for everyone else. This script has settings which must be configured before it will run correctly. The settings will be explained later. Here I will explain the information included on the display.

Loop Cycle - Displays the amount of times the script has looped. All numbers on the display should format themselves by adding commas when necessary.

Operation - Tells you if the script is currently running "weaken", "grow" or "hack".

Balance Level - Gives you the current balance of the target server as a percentage as well as raw. The color changes based on its position relative to your configuration.

Security Level - Shows the current security status of the server as a percentage as well as raw. The color changes on this one too.

Target Server - Just displays the target server that you selected in configuration.

Ideal Server Balance - This is the target server balance. If the balance falls too far below this it will start running "grow" instead of "hack". You have to choose an ideal server balance in the configuration.

Ideal Server Security - The script will decide to run "weaken" if the security level exceeds this setting.

The horizontal lines at the top and bottom of the display not only help hold it all together, they alternate between red and blue each cycle so it is easier to notice an update.

I hope at least a few people find this script helpful. I just got back into coding, it has only been a couple days, if you run into any issues please report them in the comments. If you discover an issue and fix it yourself I'd still appreciate notice so I can update it here. All criticism will be noted.

Configuration


AutoServerHackV2-2Kays.js image 13

Before you run this script you MUST configure it. There are three major settings which need input. All of the necessary settings will be found near the top of the script.

Target Server @ Line 3: This is where you input the server you are trying to hack. Some servers are case sensitive and it MUST be written how it appears in the terminal if you run "scan-analyze".

Examples: "n00dles", "foodnstuff", "iron-gym", "CSEC"...

Ideal Server Balance @ Line 8: This setting determines how low the balance of the server can get before it stops running "hack". Configuring this correctly and to your preferences will increase the efficiency of the script. If you put a number less than zero or greater than one the script will not function correctly. You must choose a decimal between 0 and 1, 0 means empty and 1 means full.

Ideal Server Security @ Line 9: This setting determines how high the security level can get before the script will begin running "weaken". Your input will be added to the minimum security level. So if you set this to 5 and the minimum security level of the server you are hacking is 2 then the ideal server security will read and function as 7.

In the future I plan on routing all of the settings to an easy to manage place but for now you have to search through the code for them. I added functionality to swap around any of the colors if you want. There are notes beside most lines describing what they do and/or how they work so people can adapt the script for themselves.

After configuration just save the script, make sure you have root access on the target server and run the script. To pull up the display just go to active scripts, click on the script and press log.

Release - AutoServerHackV2-2Kays.js

You can copy the script in the code block below if you want but I highly recommend getting a much cleaner raw copy from this pastebin.[pastebin.com]

export async function main(ns) //Main Script { const s = "SETTING"; //Target Server [!!! SETTING !!!] **Which server are we hacking??? (case sensitive)** var ab = ns.getServerMoneyAvailable(s); //Available Server Balance var cs = ns.getServerSecurityLevel(s); //Current Server Security const mb = ns.getServerMaxMoney(s); //Maximum Server Balance const ms = ns.getServerMinSecurityLevel(s); //Minimum Server Security const ib = mb * SETTING; //Ideal Server Balance [!!! SETTING !!!] **How full should the balance remain??? (0-1)** const is = ms + SETTING; //Ideal Server Security [!!! SETTING !!!] **How many levels above minimum security before** const ibp = ((ib * 100) / mb); //Ideal Server Balance as a percentage. **we weaken instead of hacking the server??? (0-100)** const isp = ((is - ms) * 100) / (100 - ms); //Ideal Server Security as a percentage. const ba = "\x1b[30m"; //Black Color const re = "\x1b[31m"; //Red Color const gr = "\x1b[32m"; //Green Color const ye = "\x1b[33m"; //Yellow Color const bu = "\x1b[34m"; //Blue Color const ma = "\x1b[35m"; //Magenta Color const cy = "\x1b[36m"; //Cyan Color const wh = "\x1b[37m"; //White Color var fl = 0; //Initial Flash Value var l = 0; //Loop Cycle Number function con(t, c) //Recreates the local print function to allow for variable output color. { ns.print(c + t); //Prints colorized text. } function r(wgoh, c) //Script for gathering server information and cleanly displaying it in the console. { function fu() //Function to switch between return values each time it is called. { if (fl == 0) //If flash equals 0... { fl = 1; //then change it to 1.. return re; //and return re } else if (fl == 1) //If flash equals 1... { fl = 0; //then change it to 0... return bu; //and return bu } } function f(x) //Function that adds commas to long numbers. { x = x.toString(); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x)) x = x.replace(pattern, "$1,$2"); return x; } var sb = ((ab * 100) / (mb)); //Calculates how full the server balance is as a percentage. var ss = ((cs - ms) * 100) / (100 - ms); //Calculates how secure the server is as a percentage. function getBalCo() //Decides which output color is appropriate based on current server balance. { if (ab < ib) //If server balance is less than ideal balance... { return re; //then return color red. } else if (ab < (ib + 5)) //If server balance is less than ideal balance plus five... { return ye; //then return color yellow. } else //If none of the previous statements are true... { return gr; //then return color green. } } function getSecCo() //Decides which output color is appropriate based on the servers current security level. { if (cs < is) //If server security level is less than ideal server security... { return gr; //then return color green. } else if (cs = is) //If server security level equals ideal server security... { return ye; //then return color yellow. } else //If none of the previous statements are true... { return gr; //then return color green. } } con("---------------------------------------------------", fu()); //Prints a line across the console, color alternating red and blue. con(" ", fu()); //This is essential a page break. con(" Server Status:", ma) //Prints "Server Status:", color magenta. con(" ", ba); //This is essential a page break. con(" Loop Cycle: #" + f(l), cy); //Prints the current loop cycle in the console, color cyan. con(" Operation: " + wgoh, c); //Prints the last operation in console. Weaken, grow or hack in red, yellow or green color. con(" Balance Level: " + Math.round(sb) + "%, $" + f(Math.round(ab)), getBalCo()); //Prints complete and rounded server balance information to console with a variable color to display status. con(" Security Level: " + Math.round(ss) + "%, " + f(Math.round(1000 * cs / 1000)), getSecCo()); //Prints complete and rounded security information to console with a variable color to display status. con(" ", ba); //This is essential a page break. con(" Current Settings:", ma) //Prints "Current Settings:", color magenta. con(" ", ba); //This is essential a page break. con(" Target Server: " + s, cy); //Prints the target server in console, color cyan. con(" Ideal Server Balance: " + Math.round(ibp) + "%, $" + f(Math.round(ib)), ye); //Prints complete and rounded ideal server balance to console, color yellow. con(" Ideal Server Security: " + Math.round(isp) + "%, " + f(Math.round(1000 * is / 1000)), ye); //Prints complete and rounded ideal server security to console, color yellow. con(" ", ba); //This is essential a page break. con(" (AutoServerHack.js, Written By 2Kays)", ba); //Unnecessary self promotion, delete it if you must </3. con("---------------------------------------------------", fu()); //Prints a line across the console, color alternating red and blue. } while (true) //Infinite Loop { var ab = ns.getServerMoneyAvailable(s); //Available Balance **I had to redefine these values inside the** var cs = ns.getServerSecurityLevel(s); //Current Security **loop so that it would be updated each cycle.** if (cs > is) //If server security level is greater than ideal server security level... { r("Weaken", re); //Calls for the r function above. Telling the function that our last operation was the weaken command and that it should display as red. await ns.weaken(s); //then run the weaken command on the server. } else if (ab < ib) //If not and if server available balance is less than ideal server balance... { r("Grow", ye); //Calls for the r function above. Telling the function that our last operation was the grow command and that it should display as yellow. await ns.grow(s); //then run the grow command on the server. } else //If none of the previous statements are true... { r("Hack", gr); //Calls for the r function above. Telling the function that our last operation was the hack command and that it should display as green. await ns.hack(s); //then run the hack command on the server. } l = l + 1; //Updates the loop counter. } }

Source: https://steamcommunity.com/sharedfiles/filedetails/?id=2968252827					

More Bitburner guilds