-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackingServerManager.js
110 lines (102 loc) · 3.86 KB
/
hackingServerManager.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { TServer } from "src/models.js";
import * as utils from "src/utils.js"
/** @param {NS} ns **/
export async function main(ns) {
let targetSrever = ns.args[0];
// let hostExcepts = ["home"]; // RAM full protection for home
let hostExcepts = [];
let scripts = { "hack": "/src/hack.js", "grow": "/src/grow.js", "weaken": "/src/weaken.js" };
let hackingServer = new TServer(ns, targetSrever);
let tServers = utils.getAllTServersAndHackThem(ns);
TServer.sortByWeight(tServers);
ns.print(`SUCCESS. Server ${hackingServer.name} has been chousen`);
while (true) {
ns.print(`INFO. Start hacking server ${hackingServer.name} `);
let servers = tServers.filter(x => x.hasRootAccess && !hostExcepts.includes(x.name));
let hackType = utils.getHackMethod(hackingServer);
let execTime = hackingServer.getTimes()[hackType];
ns.print(`hackType: ${hackType};execTime: ${execTime}`);
switch (hackType) {
case "grow":
await Grow(ns, hackingServer, servers, scripts);
break;
case "weaken":
await Weaken(ns, hackingServer, servers, scripts);
break;
case "hack":
await Hack(ns, hackingServer, servers, scripts);
break;
default:
ns.print("ERROR. ArgumentOutOfRangeException. %s was out of range.", hackType);
break;
}
let waitTime = execTime + 400;
ns.print(`INFO. Start wait ${(waitTime / 1000 / 60).toFixed(2)} minutes `);
await ns.sleep(waitTime);
ns.print(`INFO. END hacking server ${hackingServer.name} `);
}
}
/** @param {NS} ns **/
async function Grow(ns, hackingServer, hosts, scripts) {
ns.print("INFO. Start Growing for server %s ", hackingServer.name);
let type = "grow";
let script = scripts[type];
let mult = hackingServer.maxMoney / (hackingServer.moneyAvailable == 0 ? 1 : hackingServer.moneyAvailable);
let threadCountToGrow = ns.growthAnalyze(hackingServer.name, mult);
ns.print(`INFO. threadCountToGrow: ${threadCountToGrow}`);
for (const host of hosts) {
if (threadCountToGrow > 0) {
let scriptThreadCount = host.getAvailableCountThreadsForScript(script);
if (scriptThreadCount == 0)
continue;
// RUN SCRIPT
utils.execScript(ns, script, host.name, scriptThreadCount, hackingServer.name);
// ==============
threadCountToGrow -= scriptThreadCount;
}
}
ns.print(`INFO. End Growing execution for server ${hackingServer.name}`);
}
async function Weaken(ns, hackingServer, hosts, scripts) {
ns.print(`INFO. Start Weaken for server ${hackingServer.name}`);
let type = "weaken";
let script = scripts[type];
/**
weaken
Security decreased on 'n00dles' from 1.100 to 1.050 = 0.05
*/
let amount = 0.05;
let threadCountToWeaken = ((hackingServer.securityLevel - hackingServer.minSecurityLevel) / amount).toFixed();
ns.print(`INFO. threadCountToWeaken: ${threadCountToWeaken}`);
for (const host of hosts) {
if (threadCountToWeaken > 0) {
let scriptThreadCount = host.getAvailableCountThreadsForScript(script);
if (scriptThreadCount == 0)
continue;
// RUN SCRIPT
utils.execScript(ns, script, host.name, scriptThreadCount, hackingServer.name);
// ==============
threadCountToWeaken -= scriptThreadCount;
}
}
ns.print(`INFO. End Weaken execution for server ${hackingServer.name}`);
}
async function Hack(ns, hackingServer, hosts, scripts) {
ns.print(`INFO. Start Hack for server ${hackingServer.name}`);
let type = "hack";
let script = scripts[type];
let threadCountToHack = hackingServer.getThreadCountToStealALLMoney() + 1;
ns.print(`INFO. threadCountToHack: ${threadCountToHack}`);
for (const host of hosts) {
if (threadCountToHack > 0) {
let scriptThreadCount = host.getAvailableCountThreadsForScript(script);
if (scriptThreadCount == 0)
continue;
// RUN SCRIPT
utils.execScript(ns, script, host.name, scriptThreadCount, hackingServer.name);
// ==============
threadCountToHack -= scriptThreadCount;
}
}
ns.print(`INFO. End Hack execution for server ${hackingServer.name}`);
}