-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-orchestrator.js
More file actions
355 lines (321 loc) · 11.3 KB
/
smart-orchestrator.js
File metadata and controls
355 lines (321 loc) · 11.3 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/** smart-orchestrator.js
*
* INTELLIGENT SCRIPT ORCHESTRATOR
*
* Dynamically loads/unloads scripts based on need
* Saves RAM by running scripts only when necessary
* Prioritizes money-making and essential automation
*
* Features:
* - Continuous scripts: Run always (batch-manager, server-scanner)
* - Periodic scripts: Run every X minutes (hacknet, programs)
* - One-time scripts: Run once when conditions met
* - Smart RAM allocation
* - Auto-restart failed scripts
*
* Usage: run smart-orchestrator.js
*
* @param {NS} ns
*/
export async function main(ns) {
ns.disableLog("ALL");
ns.tail();
const CONFIG = {
checkInterval: 10000, // Check every 10 seconds
// Script categories with run frequencies
scripts: {
// CRITICAL: Always running (money making)
continuous: [
{
name: "Money Maker",
scripts: [
{ file: "batch/batch-manager.js", minRAM: 16, fallback: "bootstrap-income.js" },
{ file: "bootstrap-income.js", maxRAM: 15 } // Only if <16GB RAM
]
},
{
name: "Server Scanner",
script: "modules/server-scanner.js",
requireSF: null
}
],
// PERIODIC: Run every X minutes
periodic: [
{
name: "Hacknet Manager",
script: "hacknet-farm-manager.js",
interval: 300000, // 5 minutes
requireSF: null
},
{
name: "Program Buyer",
script: "modules/program-buyer.js",
interval: 1800000, // 30 minutes (programs don't change often)
requireSF: 4,
runOnce: true // Buy all programs then stop checking
},
{
name: "Stock Trader",
script: "modules/stock-trader.js",
interval: 6000, // 6 seconds
requireSF: 8
}
],
// ONE-TIME: Run once when conditions met, then stop
oneTime: [
{
name: "Corporation Setup",
script: "modules/corporation-manager.js",
condition: (ns) => {
try {
return ns.corporation && ns.corporation.hasCorporation();
} catch { return false; }
},
requireSF: 3
},
{
name: "Gang Setup",
script: "modules/gang-manager.js",
condition: (ns) => {
try {
return ns.gang && ns.gang.inGang();
} catch { return false; }
},
requireSF: 2
}
],
// BACKGROUND: Run in background, low priority
background: [
{
name: "Bladeburner",
script: "modules/bladeburner-automation.js",
requireSF: [6, 7],
condition: (ns) => {
try {
return ns.bladeburner && ns.bladeburner.inBladeburner();
} catch { return false; }
}
}
]
}
};
// State tracking
const state = {
running: new Map(), // pid -> script info
lastRun: new Map(), // script -> last run time
oneTimeCompleted: new Set(),
programsBought: false
};
// Helper: Check if we have a Source File
function hasSF(sfNum) {
const player = ns.getPlayer();
if (!player.sourceFiles) return false;
if (Array.isArray(sfNum)) {
return sfNum.some(n => player.sourceFiles.some(sf => sf.n === n));
}
return player.sourceFiles.some(sf => sf.n === sfNum);
}
// Helper: Get free RAM
function getFreeRAM() {
return ns.getServerMaxRam("home") - ns.getServerUsedRam("home");
}
// Helper: Check if script is running
function isRunning(scriptPath) {
for (const [pid, info] of state.running) {
if (info.script === scriptPath && ns.isRunning(pid)) {
return true;
}
}
return false;
}
// Helper: Start a script
function startScript(scriptPath, displayName) {
try {
const scriptRAM = ns.getScriptRam(scriptPath);
const freeRAM = getFreeRAM();
if (scriptRAM > freeRAM) {
return { success: false, reason: "insufficient_ram" };
}
const pid = ns.run(scriptPath, 1);
if (pid > 0) {
state.running.set(pid, {
script: scriptPath,
name: displayName,
startTime: Date.now()
});
return { success: true, pid: pid };
}
return { success: false, reason: "failed_to_start" };
} catch (e) {
return { success: false, reason: e.message };
}
}
// Helper: Stop a script
function stopScript(scriptPath) {
for (const [pid, info] of state.running) {
if (info.script === scriptPath) {
ns.kill(pid);
state.running.delete(pid);
return true;
}
}
return false;
}
// Clean up dead processes
function cleanupDeadProcesses() {
for (const [pid, info] of state.running) {
if (!ns.isRunning(pid)) {
state.running.delete(pid);
}
}
}
ns.print("═════════════════════════════════════════════════════════");
ns.print("🧠 SMART ORCHESTRATOR - Intelligent Script Management");
ns.print("═════════════════════════════════════════════════════════");
ns.print("");
// Main loop
while (true) {
cleanupDeadProcesses();
const homeRAM = ns.getServerMaxRam("home");
const freeRAM = getFreeRAM();
const now = Date.now();
ns.clearLog();
ns.print("═════════════════════════════════════════════════════════");
ns.print("🧠 SMART ORCHESTRATOR");
ns.print("═════════════════════════════════════════════════════════");
ns.print("");
ns.print(`RAM: ${ns.formatRam(homeRAM - freeRAM)} / ${ns.formatRam(homeRAM)} (${ns.formatRam(freeRAM)} free)`);
ns.print(`Running: ${state.running.size} scripts`);
ns.print("");
// PRIORITY 1: Continuous scripts (money making!)
ns.print("🔴 CONTINUOUS (Always Running):");
for (const item of CONFIG.scripts.continuous) {
if (item.scripts) {
// Multiple script options (like money maker with fallback)
let started = false;
for (const scriptOpt of item.scripts) {
// Check RAM constraints
if (scriptOpt.minRAM && homeRAM < scriptOpt.minRAM) continue;
if (scriptOpt.maxRAM && homeRAM > scriptOpt.maxRAM) continue;
if (!isRunning(scriptOpt.file)) {
const result = startScript(scriptOpt.file, item.name);
if (result.success) {
ns.print(` ✓ Started: ${item.name} (${scriptOpt.file})`);
started = true;
break;
}
} else {
ns.print(` ✓ Running: ${item.name}`);
started = true;
break;
}
}
if (!started) {
ns.print(` ⏳ Waiting: ${item.name} (need more RAM)`);
}
} else {
// Single script
if (item.requireSF && !hasSF(item.requireSF)) continue;
if (!isRunning(item.script)) {
const result = startScript(item.script, item.name);
if (result.success) {
ns.print(` ✓ Started: ${item.name}`);
} else {
ns.print(` ⏳ Waiting: ${item.name} (${result.reason})`);
}
} else {
ns.print(` ✓ Running: ${item.name}`);
}
}
}
ns.print("");
// PRIORITY 2: Periodic scripts
ns.print("🟡 PERIODIC (Scheduled):");
for (const item of CONFIG.scripts.periodic) {
if (item.requireSF && !hasSF(item.requireSF)) continue;
// Check if it's a one-time script that's completed
if (item.runOnce && state.oneTimeCompleted.has(item.script)) {
ns.print(` ✓ Completed: ${item.name}`);
continue;
}
const lastRun = state.lastRun.get(item.script) || 0;
const timeSince = now - lastRun;
const shouldRun = timeSince >= item.interval;
if (shouldRun && !isRunning(item.script)) {
const result = startScript(item.script, item.name);
if (result.success) {
state.lastRun.set(item.script, now);
ns.print(` ✓ Started: ${item.name}`);
// Mark as completed if it's run-once
if (item.runOnce) {
state.oneTimeCompleted.add(item.script);
}
} else {
ns.print(` ⏳ Waiting: ${item.name} (${result.reason})`);
}
} else if (isRunning(item.script)) {
ns.print(` ▶️ Running: ${item.name}`);
} else {
const nextRun = Math.ceil((item.interval - timeSince) / 1000);
ns.print(` ⏰ Next run: ${item.name} in ${nextRun}s`);
}
}
ns.print("");
// PRIORITY 3: One-time scripts
ns.print("🟢 ONE-TIME (Condition-based):");
for (const item of CONFIG.scripts.oneTime) {
if (item.requireSF && !hasSF(item.requireSF)) continue;
if (state.oneTimeCompleted.has(item.script)) {
ns.print(` ✓ Completed: ${item.name}`);
continue;
}
const conditionMet = item.condition(ns);
if (conditionMet && !isRunning(item.script)) {
const result = startScript(item.script, item.name);
if (result.success) {
ns.print(` ✓ Started: ${item.name}`);
state.oneTimeCompleted.add(item.script);
} else {
ns.print(` ⏳ Waiting: ${item.name} (${result.reason})`);
}
} else if (isRunning(item.script)) {
ns.print(` ▶️ Running: ${item.name}`);
} else if (!conditionMet) {
ns.print(` ⏸️ Waiting: ${item.name} (condition not met)`);
}
}
ns.print("");
// PRIORITY 4: Background scripts (lowest priority)
if (freeRAM > 4) {
ns.print("🔵 BACKGROUND (Low Priority):");
for (const item of CONFIG.scripts.background) {
if (item.requireSF && !hasSF(item.requireSF)) continue;
if (!item.condition(ns)) {
ns.print(` ⏸️ Waiting: ${item.name} (condition not met)`);
continue;
}
if (!isRunning(item.script)) {
const result = startScript(item.script, item.name);
if (result.success) {
ns.print(` ✓ Started: ${item.name}`);
} else {
ns.print(` ⏳ Waiting: ${item.name} (${result.reason})`);
}
} else {
ns.print(` ✓ Running: ${item.name}`);
}
}
ns.print("");
}
// Status summary
ns.print("─────────────────────────────────────────────────────────");
ns.print("📊 Script Status:");
for (const [pid, info] of state.running) {
const runtime = Math.floor((now - info.startTime) / 1000);
ns.print(` ▶️ ${info.name}: ${runtime}s`);
}
ns.print("");
ns.print(`Next check in ${CONFIG.checkInterval / 1000}s...`);
await ns.sleep(CONFIG.checkInterval);
}
}