forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
199 lines (175 loc) · 5.25 KB
/
util.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { OverlayHandlerRequests, OverlayHandlerResponseTypes } from '../types/event';
import { Job, Role } from '../types/job';
import { callOverlayHandler } from './overlay_plugin_api';
// TODO: it'd be nice to not repeat job names, but at least Record enforces that all are set.
const nameToJobEnum: Record<Job, number> = {
NONE: 0,
GLA: 1,
PGL: 2,
MRD: 3,
LNC: 4,
ARC: 5,
CNJ: 6,
THM: 7,
CRP: 8,
BSM: 9,
ARM: 10,
GSM: 11,
LTW: 12,
WVR: 13,
ALC: 14,
CUL: 15,
MIN: 16,
BTN: 17,
FSH: 18,
PLD: 19,
MNK: 20,
WAR: 21,
DRG: 22,
BRD: 23,
WHM: 24,
BLM: 25,
ACN: 26,
SMN: 27,
SCH: 28,
ROG: 29,
NIN: 30,
MCH: 31,
DRK: 32,
AST: 33,
SAM: 34,
RDM: 35,
BLU: 36,
GNB: 37,
DNC: 38,
RPR: 39,
SGE: 40,
};
const allJobs = Object.keys(nameToJobEnum) as Job[];
const allRoles = ['tank', 'healer', 'dps', 'crafter', 'gatherer', 'none'] as Role[];
const tankJobs: Job[] = ['GLA', 'PLD', 'MRD', 'WAR', 'DRK', 'GNB'];
const healerJobs: Job[] = ['CNJ', 'WHM', 'SCH', 'AST', 'SGE'];
const meleeDpsJobs: Job[] = ['PGL', 'MNK', 'LNC', 'DRG', 'ROG', 'NIN', 'SAM', 'RPR'];
const rangedDpsJobs: Job[] = ['ARC', 'BRD', 'DNC', 'MCH'];
const casterDpsJobs: Job[] = ['BLU', 'RDM', 'BLM', 'SMN', 'ACN', 'THM'];
const dpsJobs: Job[] = [...meleeDpsJobs, ...rangedDpsJobs, ...casterDpsJobs];
const craftingJobs: Job[] = ['CRP', 'BSM', 'ARM', 'GSM', 'LTW', 'WVR', 'ALC', 'CUL'];
const gatheringJobs: Job[] = ['MIN', 'BTN', 'FSH'];
const stunJobs: Job[] = ['BLU', ...tankJobs, ...meleeDpsJobs];
const silenceJobs: Job[] = ['BLU', ...tankJobs, ...rangedDpsJobs];
const sleepJobs: Job[] = ['BLM', 'BLU', ...healerJobs];
const feintJobs: Job[] = [...meleeDpsJobs];
const addleJobs: Job[] = [...casterDpsJobs];
const cleanseJobs: Job[] = ['BLU', 'BRD', ...healerJobs];
const jobToRoleMap: Map<Job, Role> = (() => {
const addToMap = (map: Map<Job, Role>, jobs: Job[], role: Role) => {
jobs.forEach((job) => map.set(job, role));
};
const map: Map<Job, Role> = new Map([['NONE', 'none']]);
addToMap(map, tankJobs, 'tank');
addToMap(map, healerJobs, 'healer');
addToMap(map, dpsJobs, 'dps');
addToMap(map, craftingJobs, 'crafter');
addToMap(map, gatheringJobs, 'gatherer');
return map;
})();
type WatchCombatantParams = {
ids?: number[];
names?: string[];
props?: string[];
delay?: number;
maxDuration?: number;
};
type WatchCombatantFunc = (
params: WatchCombatantParams,
func: (ret: OverlayHandlerResponseTypes['getCombatants']) => boolean,
) => Promise<void>;
type WatchCombatantMapEntry = {
cancel: boolean;
start: number;
};
const watchCombatantMap: WatchCombatantMapEntry[] = [];
const shouldCancelWatch = (
params: WatchCombatantParams,
entry: WatchCombatantMapEntry,
): boolean => {
if (entry.cancel)
return true;
if (params.maxDuration !== undefined && Date.now() - entry.start > params.maxDuration)
return true;
return false;
};
const watchCombatant: WatchCombatantFunc = (params, func) => {
return new Promise<void>((res, rej) => {
const delay = params.delay ?? 1000;
const call: OverlayHandlerRequests['getCombatants'] = {
call: 'getCombatants',
};
if (params.ids)
call.ids = params.ids;
if (params.names)
call.names = params.names;
if (params.props)
call.props = params.props;
const entry: WatchCombatantMapEntry = {
cancel: false,
start: Date.now(),
};
watchCombatantMap.push(entry);
const checkFunc = () => {
if (shouldCancelWatch(params, entry)) {
rej();
return;
}
void callOverlayHandler(call).then((response) => {
if (entry.cancel) {
rej();
return;
}
if (func(response))
res();
else
window.setTimeout(checkFunc, delay);
});
};
window.setTimeout(checkFunc, delay);
});
};
const Util = {
jobEnumToJob: (id: number) => {
const job = allJobs.find((job: Job) => nameToJobEnum[job] === id);
return job ?? 'NONE';
},
jobToJobEnum: (job: Job) => nameToJobEnum[job],
jobToRole: (job: Job) => {
const role = jobToRoleMap.get(job);
return role ?? 'none';
},
getAllRoles: (): readonly Role[] => allRoles,
isTankJob: (job: Job) => tankJobs.includes(job),
isHealerJob: (job: Job) => healerJobs.includes(job),
isMeleeDpsJob: (job: Job) => meleeDpsJobs.includes(job),
isRangedDpsJob: (job: Job) => rangedDpsJobs.includes(job),
isCasterDpsJob: (job: Job) => casterDpsJobs.includes(job),
isDpsJob: (job: Job) => dpsJobs.includes(job),
isCraftingJob: (job: Job) => craftingJobs.includes(job),
isGatheringJob: (job: Job) => gatheringJobs.includes(job),
isCombatJob: (job: Job) => {
return !craftingJobs.includes(job) && !gatheringJobs.includes(job);
},
canStun: (job: Job) => stunJobs.includes(job),
canSilence: (job: Job) => silenceJobs.includes(job),
canSleep: (job: Job) => sleepJobs.includes(job),
canCleanse: (job: Job) => cleanseJobs.includes(job),
canFeint: (job: Job) => feintJobs.includes(job),
canAddle: (job: Job) => addleJobs.includes(job),
watchCombatant: watchCombatant,
clearWatchCombatants: () => {
while (watchCombatantMap.length > 0) {
const watch = watchCombatantMap.pop();
if (watch)
watch.cancel = true;
}
},
} as const;
export default Util;