-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterhack.js
More file actions
76 lines (65 loc) · 2.74 KB
/
masterhack.js
File metadata and controls
76 lines (65 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {fetchServers, sortServers} from 'serverdata.js'
import {autoKill} from 'autokill.js'
import {autoScp} from 'autoscp.js'
export function isServerHackable(ns, targetHostname) {
let playerHackLvl = ns.getHackingLevel();
let serverRoot = ns.hasRootAccess(targetHostname);
let serverRequiredHack = ns.getServerRequiredHackingLevel(targetHostname);
let serverMaxMoney = ns.getServerMaxMoney(targetHostname);
return serverRoot && playerHackLvl >= serverRequiredHack && serverMaxMoney > 0;
}
/*
distributes the hackclient to hostservers and then launches it from host(s)
*/
export function autoHack(ns, verbose) {
const hackClientScript = "hackclient.js";
const scpSource = "home";
const thisScriptName = ns.getScriptName();
const ramMaxUsage = 0.95;
const thresholdMoneyRatio = 0.5;
const thresholdSecurityDelta = 5.0;
let hostResult = fetchServers(ns, "--host", verbose);
hostResult = sortServers(ns, hostResult, "--ram", false, verbose);
let hackTargetResult = fetchServers(ns, "--hack", verbose);
hackTargetResult = sortServers(ns, hackTargetResult, "--money", false, verbose);
autoScp(ns, hostResult, hackClientScript, scpSource, verbose);
autoKill(ns, hostResult, hackClientScript, verbose);
if(hackTargetResult.length > hostResult.length) {
if(verbose) {
ns.tprintf("%s -> autoHack(verbose=%s) -> hackTargetResult.length=%i > hostResult.length=%i (not enough servers)",
thisScriptName, verbose, hackTargetResult.length, hostResult.length
);
}
}
for(let i = 0; i < hostResult.length; i++) {
let hostName = hostResult[i];
if(i >= hackTargetResult.length) {
if(verbose) {
ns.tprintf("%s -> hostResult.length=%i > hackTargetResult.length=%i (not enough targets)",
thisScriptName, hostResult.length, hackTargetResult.length
);
}
break;
}
let targetName = hackTargetResult[i];
let serverMaxRam = ns.getServerMaxRam(hostName);
let serverRam = ns.getServerUsedRam(hostName);
let serverRamAvailable = (serverMaxRam - serverRam) * ramMaxUsage;
let scriptRam = ns.getScriptRam(hackClientScript);
let threadCount = Math.floor(serverRamAvailable / scriptRam);
let ramUsage = Math.floor(scriptRam * threadCount);
if(threadCount < 1) {
continue;
}
let pidNumber = ns.exec(hackClientScript, hostName, threadCount, targetName, thresholdMoneyRatio, thresholdSecurityDelta);
if(verbose) {
ns.tprintf("%s -> autoHack(verbose=%s) -> %s running pid %i --%i (RAM %s) args[%s %f %f]",
thisScriptName, verbose, hostName, pidNumber, threadCount, ns.formatRam(ramUsage), targetName, thresholdMoneyRatio, thresholdSecurityDelta
);
}
}
}
/** @param {NS} ns */
export async function main(ns) {
autoHack(ns, true);
}