-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequilibrium.js
More file actions
36 lines (31 loc) · 895 Bytes
/
equilibrium.js
File metadata and controls
36 lines (31 loc) · 895 Bytes
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
import Core from './core.js';
export default class Equilibrium extends Core {
constructor(initialBalance = 0, max = 100) {
super('Equilibrium');
this.balance = initialBalance;
this.max = max;
this.history = [];
this.visualHistory = [];
this.symbol = '#';
}
getBalance() {
return this.balance;
}
getHistory() {
return this.history;
}
adjust(value) {
this.balance = Math.max(0, Math.min(this.max, value));
this.history.push(this.balance);
this.visualHistory.push({ value: this.balance, symbol: this.symbol });
return this.balance;
}
autoCorrect(targets, weight = 1) {
if (!Array.isArray(targets)) return this.balance;
targets.sort((a, b) => b.priority - a.priority);
for (const target of targets) {
this.adjust(this.balance + (target.value - this.balance) * weight / 10);
}
return this.balance;
}
}