* add a way to determine and notify if an unrecognised argument was passed
*/
import {
array_get_servers
}
from "lib_servers.js";
import {
array_get_files_to_copy
}
from "lib_ls.js";
import {
float_get_server_ram_free,
array_get_servers_rooted_sorted_by_ram
}
from "lib_ram_server.js";
import {
boolean_can_server_run_script_threads
}
from "lib_ram_script.js";
import {
void_kill_script_named_server_named
}
from "lib_kill.js";
import {
object_parse_arguments
}
from "lib_minimist.js";
// main
export const main = async function(ns) {
// variables
// helper scripts
const object_helpers = object_get_constants().object_helpers;
// threads of "nop.js" required to reserve enough RAM to run "hacker.js"
const integer_threads_nop = integer_get_threads_nop(ns, object_helpers.string_hacker, ns.getScriptName());
// if this server doesn't have enough RAM to run "hacker.js", eject
if (!boolean_can_server_run_script_threads(ns, float_get_server_ram_free(ns, ns.getHostname()), object_helpers.string_nop, integer_threads_nop)) {
const string_message_error = `This server has insufficient RAM to run \"${object_helpers.string_nop}\" with \"${integer_threads_nop}\" thread(s).`;
ns.tprint(`ERROR: ${string_message_error}`);
throw new Error(string_message_error);
}
// name of purchased servers
let string_servers_bought_name = "server";
// maximum amount of jobs to spawn per cycle, used to prevent using up too much IRL RAM
let integer_job_cap = 1000;
// duration between each job
let float_padding_seconds = 1;
// precision of the percentage to steal calculator
let float_precision = 0.001;
// the maximum percentage of cash that should be stolen from a server
let float_steal_cap = 0.9;
// time period used for checking the time in seconds
let float_period_check_seconds = 10;
// target
let string_server_target = "";
// ram utilisiation threshold. upgrade ram when reached.
let float_ram_utilisation_threshold = 0.9;
// argument parsing
// options
const object_arguments = object_parse_arguments(ns.args);
for (const string_argument in object_arguments) {
if (object_arguments.hasOwnProperty(string_argument)) {
const argument_value = object_arguments[string_argument];
switch (string_argument) {
// options
case "c":
// fall-through
case "check-delay":
float_period_check_seconds = argument_value;
break;
case "d":
// fall-through
case "job-delay":
float_padding_seconds = argument_value;
break;
case "i":
// fall-through
case "target":
string_server_target = argument_value;
break;
case "j":
// fall-through
case "job-cap":
integer_job_cap = argument_value;
break;
case "n":
// fall-through
case "server-name":
string_servers_bought_name = argument_value;
break;
case "p":
// fall-through
case "precision":
float_precision = argument_value;
break;
case "r":
// fall-through
case "ram-utilisation":
float_ram_utilisation_threshold = argument_value;
break;
case "s":
// fall-through
case "steal-cap":
float_steal_cap = argument_value;
break;
}
}
}
let array_helpers = [
{
file: object_helpers.string_ram,
threads: 1,
args: [float_period_check_seconds, float_ram_utilisation_threshold]
},
{
file: object_helpers.string_servers,
threads: 1,
args: [float_period_check_seconds, string_servers_bought_name, float_ram_utilisation_threshold]
},
{
file: object_helpers.string_tor,
threads: 1,
args: [float_period_check_seconds]
},
{
file: object_helpers.string_programs,
threads: 1,
args: [float_period_check_seconds, ["BruteSSH.exe", "FTPCrack.exe", "relaySMTP.exe", "HTTPWorm.exe", "SQLInject.exe", "DeepscanV1.exe", "DeepscanV2.exe", "Autolink.exe"]]
},
{
file: object_helpers.string_botnet,
threads: 1,
args: [float_period_check_seconds]
}
];
// flags
for (const string_argument in object_arguments) {
if (object_arguments.hasOwnProperty(string_argument)) {
const argument_value = object_arguments[string_argument];
if (
(string_argument === "a" && argument_value) ||
(string_argument === "ram" && !argument_value)
) {
array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_ram), 1);
}
if (
(string_argument === "e" && argument_value) ||
(string_argument === "servers" && !argument_value)
) {
array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_servers), 1);
}
if (
(string_argument === "o" && argument_value) ||
(string_argument === "tor" && !argument_value)
) {
array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_tor), 1);
}
if (
(string_argument === "g" && argument_value) ||
(string_argument === "programs" && !argument_value)
) {
array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_programs), 1);
}
if (
(string_argument === "b" && argument_value) ||
(string_argument === "botnet" && !argument_value)
) {
array_helpers.splice(integer_get_index_of_file(array_helpers, object_helpers.string_botnet), 1);
}
}
}
// main
// reserve enough RAM for "hacker.js"
ns.exec(object_helpers.string_nop, ns.getHostname(), integer_threads_nop);
void_copy_scripts(ns);
void_script_executor(ns, array_helpers);
// kill "nop.js" scripts to free RAM
void_kill_script_named_server_named(ns, object_helpers.string_nop, ns.getHostname());
ns.spawn(object_helpers.string_hacker, 1, float_period_check_seconds, integer_job_cap, float_precision, float_steal_cap, float_padding_seconds, string_server_target);
};
// functions
const object_get_constants = function() {
return {
// helper scripts
object_helpers: {
string_nop: "nop.js",
string_hacker: "hacker.js",
string_ram: "ram.js",
string_servers: "servers.js",
string_tor: "tor.js",
string_programs: "programs.js",
string_botnet: "botnet.js"
}
};
};
// return the difference in RAM requirements between two scripts
const float_get_ram_difference = function(ns, string_script_0, string_script_1) {
return ns.getScriptRam(string_script_0) - ns.getScriptRam(string_script_1);
};
// return the amount of threads of "nop.js" is required to make up RAM difference between two scripts
const integer_get_threads_nop = function(ns, string_script_0, string_script_1) {
return Math.ceil(float_get_ram_difference(ns, string_script_0, string_script_1) / ns.getScriptRam(object_get_constants().object_helpers.string_nop));
};
// copy all scripts from the current server to all servers
const void_copy_scripts = function(ns) {
const string_host = ns.getHostname();
const array_script_extensions = [".js", ".ns", ".script"];
for (let integer_indices_0 = 0; integer_indices_0 < array_script_extensions.length; ++integer_indices_0) {
const string_extension = array_script_extensions[integer_indices_0];
void_copy_files_to_servers(ns, array_get_files_to_copy(ns, string_host, string_extension), string_host);
}
};
// returns the index of the scripts array which matches the filename input
const integer_get_index_of_file = function(array_scripts, string_file) {
for (let integer_indices_0 = 0; integer_indices_0 < array_scripts.length; ++integer_indices_0) {
const object_script = array_scripts[integer_indices_0];
const string_script_file = object_script.file;
if (string_script_file === string_file) {
return integer_indices_0;
}
}