-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunScriptAll.ts
148 lines (124 loc) · 4.2 KB
/
runScriptAll.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import type { NS } from './NetscriptDefinitions';
import type { KArgs } from './helpers/argParser';
import type { AcceptedArg } from './helpers/getArgHelp';
import rootComputer from './helpers/rootComputer';
import recursiveScan from './helpers/recursiveScan';
import argParser from './helpers/argParser';
const acceptedKArgs: AcceptedArg[] = [
{
fullKeyword: 'max-depth',
shortKeyword: 'd',
type: 'number',
required: false,
default: 1,
description: 'The max depth to search for new computers (recommended to be between 1 and 10).',
},
{
fullKeyword: 'file',
shortKeyword: 'f',
type: 'string',
required: true,
description: 'The file you want to be run on all found computers.',
},
{
fullKeyword: 'restart-scripts',
shortKeyword: 'r',
type: 'flag',
description: 'Whether or not to restart scripts if they are already running on the found computer.',
},
{
fullKeyword: 'verbose',
shortKeyword: 'v',
type: 'flag',
description: 'Whether or not to print out information to the terminal.',
},
{
fullKeyword: 'help',
shortKeyword: 'h',
type: 'flag',
description: 'Displays a help menu when set to true.'
}
];
export function autocomplete(data: { flags: (arg0: string[][]) => void; }, _: string[]) {
data.flags(
acceptedKArgs.flatMap(
(karg) => [karg.fullKeyword, karg.shortKeyword]
).map((karg) => [karg, '']),
);
return [];
}
// -=- Main Program -=-
async function program(ns: NS, kargs: KArgs) {
const maxDepth = kargs['max-depth'] as number;
const verbose = kargs['verbose'] as boolean;
const file = kargs['file'] as string;
const restartScript = kargs['restart-scripts'] as boolean;
const normalTPrint = (...args: any[]) => {
ns.tprint(...args);
}
if (!verbose) {
ns.tprint = (...args: any[]) => {
ns.print(...args);
}
}
let connectedDevices = recursiveScan(ns, +maxDepth);
if (typeof connectedDevices === 'string') {
ns.tprint('WARNING: No computers found.');
return;
}
const duplicateConnectedDevices: { [key: string]: boolean } = {
home: true
}
connectedDevices = connectedDevices.filter((device: string) => {
if (duplicateConnectedDevices[device] !== undefined) return false;
duplicateConnectedDevices[device] = true;
return true;
});
connectedDevices.forEach((uuid: string) => {
// ~ Check if the server is hackable
if (ns.getServerRequiredHackingLevel(uuid) > ns.getPlayer().skills.hacking) return;
// ~ Check if connected devices have been hacked yet
if (!rootComputer(ns, uuid)) return;
// ~ Kill the script if it is already running
const isScriptRunning = ns.scriptRunning(file, uuid);
if (isScriptRunning && restartScript) {
ns.kill(file, uuid)
}
// ~ Check if the script is already running
if (isScriptRunning && !restartScript) return;
// ~ Get max threads script can use
const availableMemory = ns.getServerMaxRam(uuid) - ns.getServerUsedRam(uuid);
const usableThreads = Math.floor(availableMemory / ns.getScriptRam(file))
// ~ Copy the script
ns.scp(
file,
uuid,
)
// ~ Run the script
const status = ns.exec(
file,
uuid,
usableThreads === 0 ? 1 : usableThreads,
);
if (status !== 0) {
ns.tprint(
`INFO: Started ${file} on ${uuid} with ${usableThreads} threads`
)
return;
}
ns.tprint(
`ERROR: starting ${file} on ${uuid} with ${usableThreads} threads`
)
});
ns.tprint('INFO: Finished running script on all computers.');
if (!verbose) {
ns.tprint = (...args: any[]) => {
normalTPrint(...args);
}
}
}
// -=- Run On Script Startup -=-
export async function main(ns: NS) {
const args = arguments[0]['args'];
await argParser(ns, args, acceptedKArgs, program)
}