-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgoals.js
83 lines (74 loc) · 1.74 KB
/
goals.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
/** @param {NS} ns */
export async function main(ns) {
}
export class Goal {
/** @param {NS} ns */
constructor(ns, name, condition) {
this.ns = ns;
this.name = name;
this.condition = condition;
this.state = undefined;
}
}
export class Goals {
/** @param {NS} ns */
constructor(ns, logfile, goals) {
this.ns = ns;
this.logfile = logfile;
this.goals = goals;
this.LoadGoals();
}
ResetGoals() {
if (this.ns.fileExists(this.logfile)) {
this.ns.tprint('WARN: Initiating new Goals, resetting ' + this.logfile);
this.ns.rm(this.logfile);
}
for (const goal of this.goals) {
goal.state = undefined;
}
}
LoadGoals() {
if (!this.ns.fileExists(this.logfile))
return;
const data = JSON.parse(this.ns.read(this.logfile));
for (const line of data) {
const goal = this.goals.find(s => s.name == line.name);
if (goal == undefined) {
this.ns.tprint('FAIL: Could not find goal ' + line.name);
continue;
}
goal.state = line.state;
}
}
SaveGoals() {
const data = [];
for (const goal of this.goals) {
data.push({ name: goal.name, state: goal.state });
}
this.ns.write(this.logfile, JSON.stringify(data), 'w');
}
CheckGoals() {
for (const goal of this.goals)
this.CheckGoal(goal);
}
CheckGoal(goal) {
let currentValue = goal.state != true ? goal.condition() : goal.state;
switch (goal.state) {
case undefined:
goal.state = goal.condition();
if (goal.state)
this.ns.tprint('WARN: Goals: Already achieved goal ' + goal.name + ' on script startup');
break;
case false:
if (currentValue) {
goal.state = true;
this.ns.tprint('FAIL: Goals: Achieved goal ' + goal.name);
}
break;
case true:
// Nothing to do here
break;
}
this.SaveGoals();
}
}