forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combo_tracker.ts
133 lines (113 loc) · 3.33 KB
/
combo_tracker.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
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
import { EventEmitter } from 'eventemitter3';
import {
kComboActions,
kComboBreakers,
kComboBreakersCn,
kComboBreakersKo,
kComboDelay,
} from './constants';
import { FfxivRegion } from './jobs';
import { Player } from './player';
type StartMap = {
[s: string]: {
id: string;
next: StartMap;
};
};
export type ComboCallback = (id: string | undefined, combo: ComboTracker) => void;
/**
* Track combos that the current player uses.
*
* Emit `combo` event for each combo/comboBreakers skill
* - when cast in combo, skill => its HexID
* - when cast out of combo/cast comboBreakers, skill => undefined
*/
export class ComboTracker extends EventEmitter<{ combo: ComboCallback }> {
player: Player;
comboDelayMs: number;
comboTimer?: number;
comboBreakers: readonly string[];
startMap: StartMap;
considerNext: StartMap;
isFinalSkill: boolean;
constructor(
{ comboBreakers, player, comboDelayMs }: {
player: Player;
comboBreakers: readonly string[];
comboDelayMs: number;
},
) {
super();
this.player = player;
this.comboDelayMs = comboDelayMs;
this.comboTimer = undefined;
this.comboBreakers = comboBreakers;
// A tree of nodes.
this.startMap = {}; // {} key => { id: str, next: { key => node } }
this.considerNext = this.startMap;
this.isFinalSkill = false;
// register events
this.player.on('action/you', (id) => this.HandleAbility(id));
this.player.on('hp', ({ hp }) => {
if (hp === 0)
this.AbortCombo();
});
// Combos are job specific.
this.player.on('job', () => this.AbortCombo());
}
AddCombo(skillList: string[]): void {
let nextMap: StartMap = this.startMap;
skillList.forEach((id) => {
const node = {
id: id,
next: {},
};
let nextEntry = nextMap[id];
if (!nextEntry)
nextEntry = nextMap[id] = node;
nextMap = nextEntry.next;
});
}
HandleAbility(id: string): void {
if (id in this.considerNext) {
this.StateTransition(id, this.considerNext[id]);
return;
}
if (this.comboBreakers.includes(id))
this.AbortCombo(id);
}
StateTransition(id?: string, nextState?: StartMap[string]): void {
window.clearTimeout(this.comboTimer);
this.comboTimer = undefined;
this.isFinalSkill = (nextState && Object.keys(nextState.next).length === 0) ?? false;
if (!nextState || this.isFinalSkill) {
this.considerNext = this.startMap;
} else {
this.considerNext = Object.assign({}, this.startMap, nextState?.next);
this.comboTimer = window.setTimeout(() => {
this.AbortCombo();
}, this.comboDelayMs);
}
// If not aborting, then this is a valid combo skill.
this.emit('combo', nextState ? id : undefined, this);
}
AbortCombo(id?: string): void {
this.StateTransition(id);
}
static setup(ffxivRegion: FfxivRegion, player: Player): ComboTracker {
let breakers;
if (ffxivRegion === 'ko')
breakers = kComboBreakersKo;
else if (ffxivRegion === 'cn')
breakers = kComboBreakersCn;
else
breakers = kComboBreakers;
const comboTracker = new ComboTracker({
player: player,
comboBreakers: breakers,
comboDelayMs: kComboDelay * 1000,
});
kComboActions.forEach((skillList) => comboTracker.AddCombo(skillList));
return comboTracker;
}
}