-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
385 lines (337 loc) · 14.6 KB
/
index.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
'use strict'
const enabled = true
const debug = false
const AbnormalManager = require('./lib/abnormal')
const CooldownManager = require('./lib/cooldown')
const EntityManager = require('./lib/entity')
const PartyManager = require('./lib/party')
const Notify = require('./lib/notify')
const isDefined = x => typeof x !== 'undefined'
const isArray = x => x instanceof Array
const isError = x => x instanceof Error
const toArray = x => isArray(x) ? x : (isDefined(x) ? [x] : [])
const toSet = x => new Set(toArray(x))
const thisIfGreater = (x, y) => (x > y) ? x : false
//const thisIfSmaller = (x, y) => (x < y) ? x : false
//const skillGroup = x => Math.floor(x / 10000)
//Using Math.ceil is more accurate than rounding it, for bigInt durations.
//const roundFractionalBigIntMs = (uts) => (uts / 1000n) + (uts < 0n ? ((uts % 1000n) <= -500n ? 1n : 0n) : (uts % 1000n >= 500n ? 1n : 0n))
const ceilFractionalBigIntMs = (uts) => (uts / 1000n) + ((uts % 1000n) !== 0n ? 1n : 0n)
const msRemainingBigInt = (uts, nowMs) => uts - BigInt(nowMs)
const sRemainingBigInt = (uts, nowMs) => ceilFractionalBigIntMs(msRemainingBigInt(uts, nowMs))
const matchExpiringBigInt = (set, uts, nowMs) => set.has(sRemainingBigInt(uts, nowMs))
const msRemaining = (uts, nowMs) => uts - nowMs
const sRemaining = (uts, nowMs) => Math.round(msRemaining(uts, nowMs) / 1000)
const matchExpiring = (set, uts, nowMs) => set.has(sRemaining(uts, nowMs))
function tryIt(func) {
try {
return func()
} catch (e) {
return e
}
}
function logError(message) {
console.error(Array.isArray(message) ? message.join('\n') : message)
}
function BattleNotify(mod) {
const abMan = new AbnormalManager(mod, debug)
const cooldown = new CooldownManager(mod)
const entities = new EntityManager(mod, debug)
const party = new PartyManager(mod)
const notify = new Notify(mod, debug)
const conditions = new Conditions()
const targets = new Targets()
const events = new Set()
const combat = () => entities.myEntity().combat
const enrage = () => entities.myBoss().enraged
let interval = null
mod.hook('S_LOGIN', 'event', () => {
if (enabled) {
interval = mod.setInterval(checkEvents, 500)
refreshConfig()
}
})
mod.hook('S_RETURN_TO_LOBBY', 'event', () => {
mod.clearInterval(interval)
})
mod.hook('S_PRIVATE_CHAT', 'event', () => {
if (!debug) return
mod.setTimeout(refreshConfig, 5)
})
function Conditions() {
function AbnormalConditions() {
const checkAdded = (lastMatch, { added } = {}) => added
const checkRemoved = (lastMatch, { removed } = {}) => removed
function AddedOrRefreshed({ requiredStacks } = {}) {
this.requiredStacks = requiredStacks
return checkAddedOrRefreshed.bind(this)
}
function checkAddedOrRefreshed(lastMatch, { stacks = 0, added, refreshed } = {}) {
if (stacks > this.requiredStacks)
return refreshed || added
}
function Refreshed({ requiredStacks } = {}) {
this.requiredStacks = requiredStacks
return checkRefreshed.bind(this)
}
function checkRefreshed(lastMatch, { stacks = 0, refreshed, added } = {}) {
if (stacks > this.requiredStacks)
return refreshed || added
}
function Expiring({ timesToMatch } = {}) {
this.timesToMatch = timesToMatch
return checkExpiring.bind(this)
}
function checkExpiring(lastMatch, { expires = 0n, added, refreshed } = {}) {
const nowMs = Date.now()
if (matchExpiringBigInt(this.timesToMatch, expires, nowMs))
return (refreshed || added || 0n) + sRemainingBigInt(expires, nowMs)
}
function Missing({ rewarnTimeout } = {}) {
this.rewarnTimeout = rewarnTimeout * 1000n
return checkMissing.bind(this)
}
function checkMissing(lastMatch, { added, refreshed } = {}) {
if (added || refreshed) return
const nowMs = Date.now()
return thisIfGreater(BigInt(nowMs), lastMatch + this.rewarnTimeout)
}
function MissingDuringCombat({ rewarnTimeout } = {}) {
this.rewarnTimeout = rewarnTimeout * 1000n
return checkMissingDuringCombat.bind(this)
}
function checkMissingDuringCombat(lastMatch, { added, refreshed } = {}) {
if (added || refreshed || !combat()) return
const nowMs = Date.now()
return thisIfGreater(BigInt(nowMs), lastMatch + this.rewarnTimeout)
}
this.added = (x) => checkAdded
this.removed = (x) => checkRemoved
this.addedorrefreshed = (x) => new AddedOrRefreshed(x)
this.refreshed = (x) => new Refreshed(x)
this.expiring = (x) => new Expiring(x)
this.missing = (x) => new Missing(x)
this.missingduringcombat = (x) => new MissingDuringCombat(x)
}
function CooldownConditions() {
function Expiring({ timesToMatch } = {}) {
this.timesToMatch = timesToMatch
return checkExpiring.bind(this)
}
function checkExpiring(lastMatch, { expires } = {}) {
const nowMs = Date.now()
if (matchExpiring(this.timesToMatch, expires, nowMs))
return expires - sRemaining(expires, nowMs)
}
function ExpiringDuringCombat({ timesToMatch } = {}) {
this.timesToMatch = timesToMatch
return checkExpiringDuringCombat.bind(this)
}
function checkExpiringDuringCombat(lastMatch, { expires = 0 } = {}) {
if (combat())
return checkExpiring.call(this, ...arguments)
}
function ExpiringDuringEnrage({ timesToMatch } = {}) {
this.timesToMatch = timesToMatch
return checkExpiringDuringEnrage.bind(this)
}
function checkExpiringDuringEnrage(lastMatch, { expires = 0 } = {}) {
if (enrage)
return checkExpiringDuringCombat.call(this, ...arguments)
}
function Ready({ rewarnTimeout } = {}) {
this.rewarnTimeout = rewarnTimeout * 1000
return checkReady.bind(this)
}
function checkReady(lastMatch, { expires = 0 } = {}) {
const nowMs = Date.now()
if (nowMs > expires)
return thisIfGreater(nowMs, lastMatch + this.rewarnTimeout)
}
function ReadyDuringCombat({ rewarnTimeout } = {}) {
this.rewarnTimeout = rewarnTimeout * 1000
return checkReadyDuringCombat.bind(this)
}
function checkReadyDuringCombat(lastMatch, { expires = 0 } = {}) {
if (combat())
return checkReady.call(this, ...arguments)
}
function ReadyDuringEnrage({ rewarnTimeout } = {}) {
this.rewarnTimeout = rewarnTimeout * 1000
return checkReadyDuringEnrage.bind(this)
}
function checkReadyDuringEnrage(lastMatch, { expires = 0 } = {}) {
if (enrage())
return checkReadyDuringCombat.call(this, ...arguments)
}
this.expiring = (x) => new Expiring(x)
this.expiringduringcombat = (x) => new ExpiringDuringCombat(x)
this.expiringduringenrage = (x) => new ExpiringDuringEnrage(x)
this.ready = (x) => new Ready(x)
this.readyduringcombat = (x) => new ReadyDuringCombat(x)
this.readyduringenrage = (x) => new ReadyDuringEnrage(x)
}
this.cooldown = new CooldownConditions()
this.abnormal = new AbnormalConditions()
}
function Targets() {
function AbnormalTargets() {
this.self = () => [entities.myCid()]
this.myboss = () => [entities.myBossId()]
this.party = () => party.members()
.filter(cid => cid !== entities.myCid())
.filter(cid => cid !== '0')
this.partyincludingself = () => party.members()
.filter(cid => cid !== '0')
}
function CooldownTargets(skills, items) {
skills = Array.from(skills)
items = Array.from(items)
return () =>
skills.map(id => cooldown.skill(id))
.concat(items.map(id => cooldown.item(id)))
}
this.cooldown = CooldownTargets
this.abnormal = new AbnormalTargets()
}
function AbnormalEvent(data) {
const type = data.type.toLowerCase()
const target = data.target.toLowerCase()
const getTargets = targets.abnormal[target]
if (debug) console.log(`BN => [ABNORMALITY EVENT.getTargets] ${target} => ${JSON.stringify(getTargets(), (k, v) => typeof v === 'bigint' ? v.toString() : v)}`)
const event = {}
const args = event.args = {
timesToMatch: toSet((isDefined(data.time_remaining) && data.time_remaining !== 0) ? (isArray(data.time_remaining) ? data.time_remaining.map(el => BigInt(el)) : BigInt(data.time_remaining)) : 6n),
rewarnTimeout: ((isDefined(data.rewarn_timeout) && data.rewarn_timeout !== 0) ? BigInt(data.rewarn_timeout) : 5n),
requiredStacks: data.required_stacks || 1
}
event.abnormalities = toSet(data.abnormalities)
event.condition = conditions.abnormal[type](args)
event.message = data.message
event.lastMatches = new Map()
event.matchAll = type.includes('missing')
this.check = function () {
getTargets()
.map(id => tryIt(() => checkAbnormalEvent(id, event)))
.filter(isError)
.forEach(err => logError([
`[battle-notify] AbnormalEvent.check: error while checking event`,
`event: ${JSON.stringify(event || {}, (k, v) => typeof v === 'bigint' ? `${v.toString()}n` : v)}`,
err.stack
]))
}
}
function checkAbnormalEvent(entityId, event) {
//if (debug) console.log(`BN => INTERVAL [ABNORMALITY checkAbnormalEvent] ${entityId} => ${JSON.stringify(event, (k, v) => typeof v === 'bigint' ? v.toString() : v)}`)
if (!entityId) return
const entity = entities.get(entityId)
if (entity.dead) return
entityId = entityId.toString()
if (!event.lastMatches.has(entityId))
event.lastMatches.set(entityId, 0n)
const results = new Set()
let info
let currentMatch = 0n
for (const abnormal of event.abnormalities) {
const lastMatch = event.lastMatches.get(entityId)
const abnormalInfo = abMan.get(entityId, abnormal)
const match = event.condition(lastMatch, abnormalInfo)
if (match && match !== lastMatch) {
currentMatch = match
info = abnormalInfo
results.add(true)
} else results.add(false)
}
if (event.matchAll && results.has(false) || !results.has(true)) return
notify.abnormal(event.message, entity, info)
event.lastMatches.set(entityId, currentMatch)
}
function CooldownEvent(data) {
data.skills = toArray(data.skills)
data.items = toArray(data.items)
const type = data.type.toLowerCase()
const getTargets = targets.cooldown(data.skills, data.items)
const event = {}
const args = event.args = {
timesToMatch: toSet(data.time_remaining || 6),
rewarnTimeout: data.rewarn_timeout || 5
}
event.condition = conditions.cooldown[type](args)
event.message = data.message
event.lastMatches = new Map()
this.check = function () {
getTargets()
.map(info =>
tryIt(() => checkCooldownEvent(info, event)))
.filter(isError)
.forEach(err => logError([
`[battle-notify] CooldownEvent.check: error while checking event`,
`event: ${JSON.stringify(event || {})}`,
err.stack
]))
}
}
function checkCooldownEvent(info, event) {
const id = info.item ? info.item : info.skill
if (!event.lastMatches.has(id))
event.lastMatches.set(id, 0)
const lastMatch = event.lastMatches.get(id)
const match = event.condition(lastMatch, info)
if (match && match !== lastMatch) {
notify.cooldown(event.message, info)
event.lastMatches.set(id, match)
}
}
function ResetEvent(data) {
cooldown.onReset(toArray(data.skills), info => {
notify.skillReset(data.message, info)
})
this.check = function () { }
}
function refreshConfig() {
events.clear()
cooldown.clearResetHooks()
loadEvents('./config/' + entities.self().class)
loadEvents('./config/common')
loadStyling('./config/common_styling.js')
}
function loadStyling(path) {
const data = require(path)
if (!data) return
notify.setDefaults(data)
}
function loadEvent(event) {
let type
if (event.abnormalities)
type = AbnormalEvent
else if (event.type && event.type.toLowerCase() === 'reset')
type = ResetEvent
else if (event.skills || event.items)
type = CooldownEvent
return new type(event)
}
function loadEvents(path) {
const data = require(path)
toArray(data)
.forEach(event => {
const result = tryIt(() => loadEvent(event))
if (isError(result)) {
logError([
`[battle-notify] loadEvents error while loading event from ${path}`,
`event: ${JSON.stringify(event)}`,
result.stack
])
return
}
events.add(result)
})
}
function checkEvents() {
events.forEach(e => e.check())
}
if (debug) {
//mod.send('C_CHAT', 1, { "channel": 11, "message": "<FONT></FONT>" })
//notify.testColors()
}
}
exports.NetworkMod = BattleNotify;