-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
117 lines (107 loc) · 4.49 KB
/
init.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
111
112
113
114
115
116
117
import Messenger from '/src/messenger'
import ScriptLauncher from '/src/scriptlauncher'
class Init {
constructor(ns, messenger) {
this.messenger = messenger
this.tasks = [
{ name: 'player_manager', enabled: true, running: false, script: '/src/playermanager.js', requirements: 'None.' },
{ name: 'hud', enabled: true, running: false, script: '/utils/hud.js', requirements: 'None.' },
{ name: 'sleeper', enabled: true, running: false, script: '/utils/sleeper.js', requirements: 'None.' },
{ name: 'faction_manager', enabled: false, running: false, script: '/src/factionmanager.js', requirements: 'Runs after BladeManager is running.' },
{ name: 'gang', enabled: false, running: false, script: '/src/gangmanager.js', requirements: 'Need to be in a gang (-54000 karma)' },
{ name: 'corp', enabled: true, running: false, script: '/src/corpmanager.js', requirements: 'Need to form a corp ($150b)' },
{ name: 'bladeburner', enabled: true, running: false, script: '/src/blademanager.js', requirements: 'Need to join Bladeburners (100 each combat stat), runs after PlayerMnanager completes.' },
{ name: 'hacking', enabled: true, running: false, script: '/src/botmaster.js', requirements: 'None.' },
];
}
async init(ns) {
ns.tprint(`Initializing files...`)
ns.rm('money.txt', 'home');
ns.rm('faction_goals.txt', 'home');
ns.rm('affordable_augs.txt', 'home');
ns.rm('/data/rep_goal.txt', 'home');
await ns.write('reserved.txt', 8, "w");
this.tasks = this.tasks.filter(task => task.enabled)
ns.tprint(`Launching scripts...`)
}
async run(ns) {
for (const task of this.tasks) {
if (task.pending && !this.is_pending(ns, task)) {
task.running = true
}
if (task.running == false && this.can_launch(ns, task.name) && !this.is_pending(ns, task)) {
task.pending = true
const result = ns.run(`/src/scriptlauncher.js`, 1, task.script)
ns.tprint(`Tried to launch script ${task.script}: ${result}`)
} else if (task.enabled) {
this.messenger.add_message(`${task.name} pending`, ` Pending: ${task.pending} Running: ${task.running}\n Requirements: ${task.requirements}`)
}
}
}
get finished() {
return this.tasks.filter(task => task.running == false && task.enabled == true).length <= 0
}
is_pending(ns, task) {
if (ns.isRunning(`/src/scriptlauncher.js`, 'home', task.script)) {
return true
} else {
return false
}
}
has_finished_running(ns, task_name) {
const manager_task = this.tasks.filter(task => task.name == task_name)[0]
return (!manager_task.enabled || (!ns.isRunning(manager_task.script, 'home') && manager_task.running))
}
can_launch(ns, task) {
switch (task) {
case 'hacking':
case 'player_manager':
case 'hud':
case 'sleeper':
return true;
case 'faction_manager':
return ns.isRunning('/src/blademanager.js', 'home')
case 'corp':
if (ns.getPlayer().hasCorporation) {
return true
} else {
task.running = true
return false
}
case 'bladeburner':
if (ns.getPlayer().inBladeburner) {
if (this.has_finished_running(ns, 'player_manager')) {
return true
} else {
return false
}
} else {
task.running = true
return false
}
case 'gang':
if (ns.gang.inGang()) {
return true
} else {
task.running = true
return false
}
default:
ns.tprint(`Unrecognized init task: ${task}`)
return false;
}
}
}
/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog("ALL");
const messenger = new Messenger()
const init = new Init(ns, messenger)
await init.init(ns);
while (!init.finished) {
await init.run(ns)
messenger.run(ns);
await ns.sleep(1000);
}
ns.tprint(`All tasks complete, exiting!`)
}