forked from OverlayPlugin/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombo_tracker.ts
161 lines (140 loc) · 4.22 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { EventEmitter } from 'eventemitter3';
import {
kComboActions,
kComboBreakers,
kComboBreakers620,
kComboBreakers630,
kComboDelay,
} from './constants';
import { PartialFieldMatches } from './event_emitter';
import { FfxivVersion } 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, matches) => this.HandleAbility(id, matches));
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, matches: PartialFieldMatches<'Ability'>): void {
if (matches.targetIndex !== '0')
return;
if (id in this.considerNext && matches.targetId !== 'E0000000') {
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(ffxivVersion: FfxivVersion, player: Player): ComboTracker {
let breakers;
if (ffxivVersion < 630)
breakers = kComboBreakers620;
else if (ffxivVersion < 640)
breakers = kComboBreakers630;
else
breakers = kComboBreakers;
const comboTracker = new ComboTracker({
player: player,
comboBreakers: breakers,
comboDelayMs: kComboDelay * 1000,
});
const normalise = (raw: (string | string[])[][]): string[][] => {
const queue = [...raw];
const result = [];
while (queue.length) {
const item = queue.shift();
if (typeof item === 'undefined')
continue;
if (item?.every((i) => typeof i === 'string')) {
result.push(item as string[]);
continue;
}
const idx = item.findIndex((i) => Array.isArray(i));
const arr = item[idx] as string[];
for (const i of arr) {
const copy = [...item];
copy[idx] = i;
queue.push(copy);
}
}
return result;
};
const normalised = normalise(kComboActions);
for (const skillList of normalised) {
comboTracker.AddCombo(skillList);
}
return comboTracker;
}
}