forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combat_state.ts
48 lines (42 loc) · 1.51 KB
/
combat_state.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
import { DamageTracker } from './damage_tracker';
// Handles when to start and stop combat.
// Outside callers (e.g. DamageTracker) can call StartCombat/StopCombat as needed from
// various sources (lines, events) and this will apply hysteresis to only emit
// OnStartEncounter/OnStopEncounter events back to DamageTracker on edge transitions.
export class CombatState {
public startTime?: number;
public stopTime?: number;
constructor(private damageTracker: DamageTracker) {
this.Reset();
}
Reset(): void {
this.startTime = undefined;
this.stopTime = undefined;
}
StartCombat(timestamp: number): void {
// Wiping / in combat state / damage are all racy with each other.
// One potential ordering:
// -in combat: false
// -wipe
// -belated death/damage <-- this damage shouldn't start
// -damage (early pull) <-- this damage should
// -in combat: true
// Therefore, suppress "start combat" after wipes within a short
// period of time. Gross.
if (this.startTime !== undefined)
return;
const kMinimumSecondsAfterWipe = 5;
if (this.stopTime && timestamp - this.stopTime < 1000 * kMinimumSecondsAfterWipe)
return;
this.startTime = timestamp;
this.stopTime = undefined;
this.damageTracker.OnStartEncounter(timestamp);
}
StopCombat(timestamp: number): void {
if (this.stopTime !== undefined)
return;
this.startTime = undefined;
this.stopTime = timestamp;
this.damageTracker.OnStopEncounter(timestamp);
}
}