Repository navigation
Expand file tree
/
Copy pathQueue.lua
More file actions
3809 lines (3590 loc) · 170 KB
/
Copy pathQueue.lua
File metadata and controls
3809 lines (3590 loc) · 170 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Manners -- the candidate queue: who is owed a favour, what the game keeps
-- refusing, the never-offer list, friends and guildmates, and BuildQueue,
-- which puts everybody the prompt could offer in order.
local ns = select(2, ...)
local L = ns.L
local addon = ns.addon
local issecretvalue = _G.issecretvalue
local InCombatLockdown = _G.InCombatLockdown
local GetTime = _G.GetTime
-- Core.lua's and Range.lua's, which load first.
local plain, safecall, caps, MANA = ns.plain, ns.safecall, ns.caps, ns.MANA
local ShortName, SafeForMacro = ns.ShortName, ns.SafeForMacro
local UnitHasBuff, UnitHasMana, MANA_CLASSES = ns.UnitHasBuff, ns.UnitHasMana, ns.MANA_CLASSES
local IsBuffableUnit, InRange, ShoutReach, SameParty = ns.IsBuffableUnit, ns.InRange, ns.ShoutReach, ns.SameParty
local prox, Resting = ns.proximity, ns.Resting
---------------------------------------------------------------------------
-- candidate queue
---------------------------------------------------------------------------
-- People who buffed us: [name] = { expires, at, guid?, class?, pvp? }. `at` is
-- when the favour was noticed, which the grace window and LiveExpiry count
-- from; `pvp` the PvP flag last read off them (see "flagged for PvP").
local owed = {}
-- [name .. "\0" .. buffKey] = expiry for a buff we just tried on them, so
-- casting Fortitude does not stop the walk reaching Divine Spirit; and
-- [name .. "\0*"] = expiry for the whole person (a right-press skip, or a press
-- that reached nobody), so somebody behind a pillar does not walk the list.
local tried = {}
-- [name] = what the game has been refusing on this person (NoteRefusal). In
-- memory only, on purpose: whatever the server held against them (a phase, a
-- duel, a rule nobody names) rarely outlives a /reload or a new login.
local refusals = {}
ns.lastGave = {} -- [name] = buffKey, for rotating when auras cannot be read
-- What landed on whom, and when: a cast the game confirmed, kept so that a buff
-- somebody wears with nobody named as its caster (WoW Forever names no caster
-- on another player's aura) can still be told for ours. One time per buff key
-- per name: a blessing cast after your Oath must not make the Oath look like
-- somebody else's. Memory only, bounded.
do
-- giftAt: the same, for the casts that were gifts (`gift`: the ledger's
-- "Gave", returning no favour of theirs), which a buff from them in their
-- time answers (ns.GaveLately). A return written later takes its place.
local gaveAt, giftAt, gaveCount = {}, {}, 0
function ns.NoteGave(name, buffKey, gift)
if not name or not buffKey then return end
local entry = gaveAt[name]
if not entry then
if gaveCount >= 300 then
wipe(gaveAt)
wipe(giftAt)
gaveCount = 0
end
gaveCount = gaveCount + 1
entry = {}
gaveAt[name] = entry
end
local now = GetTime()
entry[buffKey] = now
local gifts = giftAt[name]
if gift then
if not gifts then
gifts = {}
giftAt[name] = gifts
end
gifts[buffKey] = now
elseif gifts then
gifts[buffKey] = nil
end
end
-- Whether what they wear of `buffKey`, with `remaining` seconds on a buff
-- that lasts `length`, can be the cast noted for them: that cast ran for as
-- long as `length` and no more, so a buff that runs longer was cast again
-- since, by somebody else (a few seconds are allowed for the clocks).
function ns.GaveSince(name, buffKey, remaining, length)
local entry = name and gaveAt[name]
local at = entry and entry[buffKey]
if not at then return false end
if type(remaining) ~= "number" or type(length) ~= "number" then return false end
return at + length + 5 >= GetTime() + remaining
end
-- A buff Manners saw land on `name` as a gift that still has more than its
-- top-up lead to run, by the cast's own time (no aura read): its key, or
-- nil. `leadOf(length)` is the lead for a buff that long. The longer of a
-- buff's two lengths (a group version can last longer), so a cast is never
-- taken to have run out early. For a buff back from somebody you buffed
-- first (Favours.lua), which the aura alone cannot say on WoW Forever. A
-- cast that returned their favour is no gift: a buff from them after it is
-- a new favour.
function ns.GaveLately(name, now, leadOf)
local entry = name and giftAt[name]
if not entry then return nil end
local class = ns.caps and ns.caps.class
for key, at in pairs(entry) do
local buff = ns.FindBuff(class, key)
local single, group = ns.BuffSeconds(buff)
local length = math.max(single or 0, group or 0)
if length > 0 and at + length - leadOf(length) > now then return key end
end
return nil
end
end
ns.owed, ns.tried, ns.refusals = owed, tried, refusals
-- The key a group version is blocked under for the one it was aimed at, beside
-- the buff's own: name .. "\0" .. buff key .. this (NoteRefusal writes it,
-- GroupBuffs.lua's Build asks it).
ns.GROUP_HOLD = "\1group"
-- When a debt really runs out: the stamp it was filed with, or its age against
-- the window as it stands now, whichever comes first -- so lowering "Remember a
-- buff for" applies to debts already owed. Every reader asks this.
local function LiveExpiry(entry)
local db = addon.db and addon.db.profile
local window = db and db.timing and db.timing.reciprocateWindow
if type(entry.at) ~= "number" or type(window) ~= "number" then return entry.expires end
return math.min(entry.expires, entry.at + window)
end
ns.DebtExpiry = LiveExpiry
-- GetTime() is time since boot, so it restarts after a reboot and differs per
-- machine: debts go out on the wall clock and are rebased on the way back in,
-- `at` included. db.char, not the profile: a debt is owed to a character.
local function SaveDebts()
local store = addon.db and addon.db.char
local wall = plain(time and time())
if not store or type(wall) ~= "number" then return end
-- Switched off means the file is cleared, not merely not written.
if addon.db.profile and addon.db.profile.timing.keepDebts == false then
store.debts = nil
return
end
local now, out = GetTime(), nil
for name, entry in pairs(owed) do
local expires = LiveExpiry(entry)
if expires > now then
out = out or {}
-- The class and the PvP flag are all the tokenless fallback judges by. The
-- guid is not kept: nothing reads it back.
out[name] = {
expires = wall + (expires - now),
at = wall - (now - entry.at),
class = entry.class,
pvp = entry.pvp == true or nil,
-- And their level, for the rank the fallback's macro names.
level = type(entry.level) == "number" and entry.level or nil,
}
end
end
store.debts = out -- nil when empty, so AceDB prunes the section on logout
end
ns.SaveDebts = SaveDebts
local function RestoreDebts()
local store = addon.db and addon.db.char
local saved = store and store.debts
local wall = plain(time and time())
if type(saved) ~= "table" or type(wall) ~= "number" then return end
-- Honoured on the way in too: a file can outlive the setting (a profile
-- switched between logins, or changed on another character).
if addon.db.profile and addon.db.profile.timing.keepDebts == false then
store.debts = nil
return
end
local now = GetTime()
local window = (addon.db and addon.db.profile.timing.reciprocateWindow) or 120
for name, entry in pairs(saved) do
if type(entry) == "table" and type(entry.expires) == "number"
and type(entry.at) == "number" and SafeForMacro(name) then
-- Clamped to the window as it stands now, counted from the favour
-- as LiveExpiry does. A stamp from the future (the clock set back
-- since it was written) counts as now, or the skew would be added
-- to the window.
local at = math.min(entry.at, wall)
local left = math.min(entry.expires, at + window) - wall
if left > 0 then
-- `at` goes negative just after a reboot, correctly: now - at
-- is still the real age of the debt.
owed[name] = {
expires = now + left,
at = now - (wall - at),
class = type(entry.class) == "string" and entry.class or nil,
pvp = entry.pvp == true or nil,
level = type(entry.level) == "number" and entry.level >= 1 and entry.level or nil,
}
end
end
end
end
ns.RestoreDebts = RestoreDebts
-- AceDB fires this from its PLAYER_LOGOUT handler, before it strips the
-- defaults, so writing here is safe.
function addon:SaveDebts()
ns.Guard("SaveDebts", SaveDebts)
end
-- The only writers and reader of the two key shapes in `tried`, in a block of
-- their own for the main chunk's 200 locals.
do
-- One writer under both shapes. keepLonger extends a block and never
-- shortens it, so the two-second rewind of a press that went nowhere cannot
-- cancel a right-press skip at the full retry cooldown.
local function Block(key, seconds, keepLonger)
if not seconds then
local db = addon.db and addon.db.profile
seconds = (db and db.timing.retryCooldown) or 12
end
local expiry = GetTime() + seconds
local standing = tried[key]
if keepLonger and standing and standing > expiry then return end
tried[key] = expiry
end
function ns.MarkAttempted(name, buffKey, seconds, keepLonger)
if not name or not buffKey then return end
Block(name .. "\0" .. buffKey, seconds, keepLonger)
end
function ns.BlockPerson(name, seconds, keepLonger)
if not name then return end
Block(name .. "\0*", seconds, keepLonger)
end
-- Lets go of the retry block a press wrote, and only that: a block that
-- stands longer (a right-click's skip of an hour) is somebody's choice and
-- stays.
function ns.ReleaseRetry(name, buffKey)
if not name or not buffKey then return end
local key = name .. "\0" .. buffKey
local standing = tried[key]
local db = addon.db and addon.db.profile
local retry = (db and db.timing.retryCooldown) or 12
if standing and standing <= GetTime() + retry then tried[key] = 0 end
end
-- How long a right-click on the prompt, or Not now in its menu, leaves
-- somebody (or your own buff) alone: its own setting, apart from the retry
-- cooldown, which is for a cast that failed.
function ns.SkipSeconds()
local db = addon.db and addon.db.profile
return (db and db.timing.skipSeconds) or (db and db.timing.retryCooldown) or 12
end
-- The same for one of your own buffs: its own length in minutes (a skipped
-- food is often skipped for the evening), and 0 follows skipSeconds.
function ns.OwnSkipSeconds()
local db = addon.db and addon.db.profile
local minutes = db and tonumber(db.timing.ownSkipMinutes)
if minutes and minutes > 0 then return minutes * 60 end
return ns.SkipSeconds()
end
-- A skip of one of your own: Well Fed comes from any food, so a skipped food is every food.
function ns.SkipOwn(name, key, seconds)
local spell = ns.FindOwnSpell(key)
local family = spell and spell.family
if family and family.food and family.spells then
for _, each in ipairs(family.spells) do ns.MarkAttempted(name, each.key, seconds) end
else
ns.MarkAttempted(name, key, seconds)
end
end
-- The keys one person's blocks are filed under, built once per person and
-- kept, since IsBlocked is asked for everybody on every scan. The
-- whole-person key sits under a table key no buff can have.
local blockKeys, blockKeyCount = {}, 0
local WHOLE_PERSON = {}
local function BlockKeys(name)
local keys = blockKeys[name]
if keys then return keys end
-- Bounded: a city puts hundreds of people through the scan in a session.
if blockKeyCount >= 500 then
wipe(blockKeys)
blockKeyCount = 0
end
keys = { [WHOLE_PERSON] = name .. "\0*" }
blockKeys[name] = keys
blockKeyCount = blockKeyCount + 1
return keys
end
-- The whole-person key is always consulted, and so is a back-off after
-- refusals in a row, a block on the whole person too.
function ns.IsBlocked(name, buffKey, now)
if not name then return false end
now = now or GetTime()
local refused = refusals[name]
if refused and refused.blockUntil > now then return true end
if next(tried) == nil then return false end
local keys = BlockKeys(name)
local person = tried[keys[WHOLE_PERSON]]
if person and person > now then return true end
if not buffKey then return false end
local key = keys[buffKey]
if not key then
key = name .. "\0" .. buffKey
keys[buffKey] = key
end
local one = tried[key]
return one ~= nil and one > now
end
end
-- What the game keeps refusing, per person, and what follows. The spoken line
-- is held for a while after a refusal: a macro runs every line even when its
-- /cast fails, so a thank-you went out over a buff that never landed, once per
-- press (beta.8). And the person backs off further with each refusal in a row:
-- somebody the game will never let you buff came straight back after two
-- seconds, forever. A cast that lands forgets it all (PruneSettled).
do
-- How long each refusal in a row keeps the person off the prompt. The first
-- is the two seconds RewindClick has always written, so one stray refusal
-- costs nothing new. Somebody who buffed you backs off more gently:
-- returning the favour is the point, and the favour itself stays owed.
local STEPS = { 2, 30, 300 }
local OWED_STEPS = { 2, 20, 60 }
-- The refusal in a row that says so in chat, once per run.
local TELL_AT = 3
-- How long the spoken line stays held after a press came to nothing, and
-- past the end of any back-off.
local QUIET_SECONDS = 30
-- A run of refusals this long over is forgotten, and the sweep lets the
-- person go: somebody never seen again must not be kept all session.
local FORGET_SECONDS = 600
local CAP = 200
-- How close an error line and a refusal must be to be about the same cast.
local ERROR_SECONDS = 1
local lastError, lastErrorAt = nil, 0
-- Who the newest refusal was about, for an error line arriving just after.
local newest
-- Whom the player last picked as their target on purpose: what
-- PLAYER_TARGET_CHANGED last handed LiftBackoff, nil once they cleared it.
-- Not whoever the prompt's own macro left targeted, which the event ignores.
local pointedAt
-- Errors about the caster rather than the person (out of mana, moving, the
-- cooldown, still on a flying mount): they hold the spoken line, since
-- nothing landed, but say nothing about whether the game will refuse this
-- person next time. Named by the client's own global strings, since the
-- text is localised.
local CASTER_SIDE = { "ERR_OUT_OF_MANA", "SPELL_FAILED_MOVING", "SPELL_FAILED_NOT_READY",
"ERR_SPELL_COOLDOWN", "ERR_ABILITY_COOLDOWN", "SPELL_FAILED_SPELL_IN_PROGRESS",
"SPELL_FAILED_SILENCED", "SPELL_FAILED_STUNNED", "SPELL_FAILED_CASTER_DEAD",
"SPELL_FAILED_INTERRUPTED", "SPELL_FAILED_NOT_MOUNTED" }
-- Refusals the press asks about again before it lets a line go
-- (Prompt/Press.lua, HoldLine): the range, the mana, a cooldown or a cast
-- in progress, the target dead. A press that carried no line and was
-- refused for one of these leaves nothing to hold: the next press says the
-- line only if it finds that cleared.
local RECHECKED = { "ERR_OUT_OF_RANGE", "SPELL_FAILED_OUT_OF_RANGE", "ERR_OUT_OF_MANA",
"SPELL_FAILED_NOT_READY", "ERR_SPELL_COOLDOWN", "ERR_ABILITY_COOLDOWN",
"SPELL_FAILED_SPELL_IN_PROGRESS", "SPELL_FAILED_TARGETS_DEAD" }
-- The game itself said they are out of range, which the scan cannot always
-- learn on this client: long enough for the prompt not to put them straight
-- back up. A floor under the first step of their own (WHERE), not a step.
local OUT_OF_RANGE = { "ERR_OUT_OF_RANGE", "SPELL_FAILED_OUT_OF_RANGE" }
local OUT_OF_RANGE_SECONDS = 10
-- Refusals about where they stand, not who they are: out of sight round a
-- pillar, or out of range. A step puts them right, so they are a run of
-- their own (`away` on the record), never past half a minute, no step of
-- the person's run (`count`, whose third step is five minutes) and never
-- the chat line that the game keeps refusing them. Counted in with the
-- person's run, a healer behind a pillar was off the prompt for five
-- minutes after three presses (review 2026-11-15). Line of sight no scan
-- can read, and range not always, so they still back off, a little more
-- each time: somebody who stays forty yards off is not pressed at every
-- few seconds.
local WHERE = { "SPELL_FAILED_LINE_OF_SIGHT", "ERR_OUT_OF_RANGE", "SPELL_FAILED_OUT_OF_RANGE" }
local WHERE_STEPS = { 5, 15, 30 }
-- The game said this buff cannot land on them, and nothing about them: a
-- stronger one of the same kind is on them already (a campfire's or a
-- scroll's, which no aura read of ours knows to hold it off), or they are
-- too low a level for the rank. Retrying in two seconds, or in half a
-- minute, never helps: the buff is held off that person for ten minutes and
-- everything else about them stays as it was.
local THIS_BUFF = { "SPELL_FAILED_AURA_BOUNCED", "SPELL_FAILED_LOWLEVEL" }
local THIS_BUFF_SECONDS = 600
-- Core.lua's table of the buffs so held, which its Castable reads.
local heldOff = ns.heldOff
-- The same buff refused on the same person twice running, with nothing about
-- them changed between (no aura added to them or taken off), and no reason
-- from the game or one of these about them: held off that person as above,
-- whether or not they are your target (1.11.25 on Forever: five presses on
-- one target in a minute, each refused, each put straight back up). Range,
-- sight, mana and moving are about the caster or the place and hold nobody.
local AGAIN = { "SPELL_FAILED_BAD_TARGETS", "SPELL_FAILED_TARGET_AURASTATE" }
-- [name] = { key = buff key, at = when }: the first such refusal, waiting for
-- a second; [name] = { [buff key] = true }: the holds that rule made, which a
-- new aura on them lifts (ns.AuraChangedOn). A THIS_BUFF hold is the game's
-- own word and stays.
local firstRefusal, heldAgain = {}, {}
-- The newest error's string id (GetGameMessageInfo, Clicks.lua), for the
-- trail: its words may name somebody, its id never does.
local lastErrorId
-- Whether the game's words are one of these global strings.
local function OneOf(message, keys)
if type(message) ~= "string" then return false end
local bare = message:gsub("%.$", "")
for _, key in ipairs(keys) do
local text = plain(_G[key])
if type(text) == "string" and text:gsub("%.$", "") == bare then return true end
end
return false
end
local function AboutTheCaster(message)
return OneOf(message, CASTER_SIDE)
end
-- Said whether or not chat lines are on: it is the only thing that says
-- why somebody vanished from the prompt.
local function Tell(name, why)
-- Your own buff (SelfEntry) by what it is: your name in the third
-- person reads as somebody else who shares it. Targeting yourself
-- lifts it, as targeting anybody does (LiftBackoff).
if ns.IsPlayerName(name) then
addon:Print(type(why) == "string"
and L["|cffff8080the game keeps refusing your own buff|r (it said: %s) -- it will not be offered to you for a while; target yourself to try again."]
:format((why:gsub("%.$", "")))
or L["|cffff8080the game keeps refusing your own buff|r -- it will not be offered to you for a while; target yourself to try again."])
return
end
if type(why) == "string" then
addon:Print(L["|cffff8080the game keeps refusing buffs on %s|r (it said: %s) -- they will not be offered for a while; target them to try again."]
:format(name, (why:gsub("%.$", ""))))
else
addon:Print(L["|cffff8080the game keeps refusing buffs on %s|r -- they will not be offered for a while; target them to try again."]
:format(name))
end
end
function ns.SweepRefusals(now)
for name, r in pairs(refusals) do
if r.blockUntil <= now and r.quietUntil <= now and now - r.last > FORGET_SECONDS then
refusals[name] = nil
end
end
-- And the buffs held off one person that have run their ten minutes.
for name, held in pairs(heldOff) do
for key, lifts in pairs(held) do
if lifts <= now then held[key] = nil end
end
if next(held) == nil then heldOff[name] = nil end
end
-- A first refusal never repeated, and the holds of a second whose ten
-- minutes ran out above.
for name, first in pairs(firstRefusal) do
if now - first.at > FORGET_SECONDS then firstRefusal[name] = nil end
end
for name, mine in pairs(heldAgain) do
local held = heldOff[name]
for key in pairs(mine) do
if not (held and held[key]) then mine[key] = nil end
end
if next(mine) == nil then heldAgain[name] = nil end
end
if ns.refusalWatch and next(firstRefusal) == nil and next(heldAgain) == nil then ns.refusalWatch = nil end
end
-- One buff held off one person as the wrong one for them (Core.lua,
-- heldOff, which Castable reads). Bounded as the refusals are.
local function HoldOff(name, buffKey, lifts)
local held = heldOff[name]
if not held then
local count = 0
for _ in pairs(heldOff) do count = count + 1 end
if count >= CAP then wipe(heldOff) end
held = {}
heldOff[name] = held
end
held[buffKey] = lifts
end
-- Said once, when the hold is made, whether or not chat lines are on: it is
-- the only thing that says why they left the prompt, and how to try again.
local function TellAgain(name, buffKey)
local buff = ns.FindBuff(caps.class, buffKey) or ns.FindOwnSpell(buffKey)
local label = buff and ns.BuffName(buff) or buffKey
if ns.IsPlayerName(name) then
addon:Print(L["|cffff8080the game refused %s on you twice running|r -- it is not offered to you for ten minutes, or until a buff on you changes. To try again now: Who to buff, Never offer, Offer held-back buffs again."]
:format(label))
else
addon:Print(L["|cffff8080the game refused %s on %s twice running|r, with nothing about them changed between -- it is not offered to them for ten minutes, or until a buff on them changes. To try again now: Who to buff, Never offer, Offer held-back buffs again."]
:format(label, name))
end
end
-- A refusal of one buff on one person that is no reason to back them off
-- (AGAIN, above): the first is remembered, the second holds the buff off them.
-- Returns true when it held.
local function RefusedAgain(name, buffKey, why, now)
if why ~= nil and not OneOf(why, AGAIN) then
firstRefusal[name] = nil
return false
end
local first = firstRefusal[name]
if not (first and first.key == buffKey) then
firstRefusal[name] = { key = buffKey, at = now }
ns.refusalWatch = true
return false
end
firstRefusal[name] = nil
ns.MarkAttempted(name, buffKey, THIS_BUFF_SECONDS, true)
HoldOff(name, buffKey, now + THIS_BUFF_SECONDS)
local mine = heldAgain[name] or {}
heldAgain[name] = mine
mine[buffKey] = true
ns.refusalWatch = true
ns.Trail("held", "refused twice running with nothing changed: " .. tostring(buffKey)
.. " held off them for ten minutes", name)
TellAgain(name, buffKey)
return true
end
-- One hold the rule above made, taken back.
local function LiftAgain(name, buffKey)
local mine = heldAgain[name]
if not (mine and mine[buffKey]) then return end
mine[buffKey] = nil
if next(mine) == nil then heldAgain[name] = nil end
local held = heldOff[name]
if held then
held[buffKey] = nil
if next(held) == nil then heldOff[name] = nil end
end
ns.MarkAttempted(name, buffKey, 0)
end
-- The game's reason, arriving after the refusal it explains (Forever sends
-- UI_ERROR_MESSAGE after UNIT_SPELLCAST_FAILED for a cast the server refused):
-- what the refusal would have done with it, done now. `late` is what the
-- refusal left (NoteRefusal): the buff, the group, the count and the block it
-- found, and whether it held the buff as refused twice.
local function LateReason(name, r, late, message, now)
ns.Trail("refused", "the game's reason came after the refusal: " .. tostring(lastErrorId or "an error with no id"), name)
if late.key and OneOf(message, THIS_BUFF) then
-- No count towards backing them off, as THIS_BUFF in NoteRefusal.
r.count, r.blockUntil = late.count, late.block
local what = late.members and (late.key .. ns.GROUP_HOLD) or late.key
ns.MarkAttempted(name, what, THIS_BUFF_SECONDS, true)
if not late.members then HoldOff(name, late.key, now + THIS_BUFF_SECONDS) end
firstRefusal[name] = nil
return
end
-- About the caster or the place after all: nothing about them to hold.
if not OneOf(message, AGAIN) then
firstRefusal[name] = nil
if late.again then LiftAgain(name, late.key) end
end
end
-- UNIT_AURA on `unit` while a refusal is remembered (Favours.lua, asked only
-- while ns.refusalWatch): an aura added to them or taken off is something
-- changed, so the first refusal is forgotten and the holds it led to lifted.
-- An update of an aura already there (a timer, a stack) is not.
function ns.AuraChangedOn(unit, info)
if type(info) == "table" and not (issecretvalue and issecretvalue(info)) then
if plain(info.isFullUpdate) ~= true and plain(info.addedAuras) == nil
and plain(info.removedAuraInstanceIDs) == nil then return end
end
local name = ns.UnitFullName(unit)
if not name then return end
if firstRefusal[name] then firstRefusal[name] = nil end
local mine = heldAgain[name]
if mine then
for key in pairs(mine) do LiftAgain(name, key) end
ns.Trail("held", "a buff on them changed: what was held off them is offered again", name)
end
if next(firstRefusal) == nil and next(heldAgain) == nil then ns.refusalWatch = nil end
end
-- Whether any buff is held off anybody, for the button that offers them again.
function ns.AnyHeldOff()
return next(heldOff) ~= nil
end
-- Who to buff, Never offer, Offer held-back buffs again: every buff held off
-- anybody (the game's "a more powerful spell", too low a level, or refused
-- twice running) offered again now, and the refusals behind them forgotten.
function ns.OfferHeldAgain()
for name, held in pairs(heldOff) do
for key in pairs(held) do
ns.MarkAttempted(name, key, 0)
if ns.GROUP_HOLD then ns.MarkAttempted(name, key .. ns.GROUP_HOLD, 0) end
end
end
wipe(heldOff)
wipe(heldAgain)
wipe(firstRefusal)
ns.refusalWatch = nil
ns.Trail("held", "the player offered every held-back buff again")
addon:Print(L["Buffs held off people are offered again."])
if ns.Prompt and ns.Prompt.Refresh then ns.Guard("offer held buffs again", ns.Prompt.Refresh, ns.Prompt) end
end
-- One person's record, room made for it first. Counted here, not tallied: a
-- new record is rare, and a count cannot drift from the table.
local function Record(name, now)
local r = refusals[name]
if r then return r end
local held, oldest, at = 0, nil, nil
for who, rec in pairs(refusals) do
held = held + 1
if not at or rec.last < at then oldest, at = who, rec.last end
end
-- Full even so: the person refused longest ago goes.
if held >= CAP then refusals[oldest] = nil end
r = { count = 0, last = now, blockUntil = 0, quietUntil = 0 }
refusals[name] = r
return r
end
-- A refusal about where they stand (WHERE): a step of their own run, held
-- under OUT_OF_RANGE_SECONDS' floor when the game said range. Their own
-- target, picked out by the player, is spared as the person's run spares it
-- (see NoteRefusal). `hold` is whether the spoken line is held.
local function StoodAway(name, r, why, now, hold)
r.away = (r.away or 0) + 1
if name == pointedAt and ns.UnitFullName("target") == pointedAt then return end
local seconds = WHERE_STEPS[math.min(r.away, #WHERE_STEPS)]
if seconds < OUT_OF_RANGE_SECONDS and OneOf(why, OUT_OF_RANGE) then seconds = OUT_OF_RANGE_SECONDS end
if now + seconds > r.blockUntil then r.blockUntil = now + seconds end
-- The spoken line held past it, as for the person's run.
local quiet = r.blockUntil + QUIET_SECONDS
if hold and quiet > r.quietUntil then r.quietUntil = quiet end
end
-- Every error line the game raises. One arriving just after a refusal that
-- had no reason of its own is taken as that reason. `id` is the message's
-- string id where the client gives one (Clicks.lua, UI_ERROR_MESSAGE). When
-- it says where they stand (WHERE), the step that refusal took on the
-- person's run is taken back and taken on their own instead (`undo`, written
-- by that step): the error line can come a moment after the refusal.
function ns.NoteGameError(message, id)
if type(message) ~= "string" then return end
local now = GetTime()
lastError, lastErrorAt, lastErrorId = message, now, type(id) == "string" and id or nil
local r = newest and refusals[newest]
if r and r.why == nil and now - r.last <= ERROR_SECONDS and not AboutTheCaster(message) then
local undo = r.undo
if undo and undo.at == r.last and OneOf(message, WHERE) then
r.undo = nil
r.count = r.count - 1
r.blockUntil, r.quietUntil = undo.blockUntil, undo.quietUntil
if r.count < TELL_AT then r.said = nil end
StoodAway(newest, r, message, now, undo.hold)
-- Nor does it count towards the same buff refused twice (LateReason).
local late = r.late
r.late = nil
if late and not OneOf(message, AGAIN) then
firstRefusal[newest] = nil
if late.again then LiftAgain(newest, late.key) end
end
return
end
r.why = message
end
-- And acted on as the refusal would have, had it come first.
local late = r and r.late
if late and now - late.at <= ERROR_SECONDS then
r.late = nil
LateReason(newest, r, late, message, now)
end
end
-- A press on this person came to nothing: an error inside the press's
-- window, or a refusal after it was sent. `why` is the game's own words
-- where the caller has them. `quietOnly` holds the spoken line and no more,
-- for a press the game never answered at all: nothing refused anybody
-- there, so it is no evidence the game will refuse them next time.
-- `spoke` is false for a press whose macro carried no line (PostClick
-- records it): refused for something the next press asks again
-- (RECHECKED), or not refused at all (quietOnly), that holds no line, or
-- the press that lands once they are back in reach would go out silent
-- too. The back-off stands either way.
function ns.NoteRefusal(name, why, quietOnly, spoke, buffKey, members)
if not name then return end
local now = GetTime()
if why == nil and lastError and now - lastErrorAt <= ERROR_SECONDS then why = lastError end
ns.Trail("refused", "the game refused: " .. tostring(why or "no reason given"), name)
local r = Record(name, now)
local hold = spoke ~= false or (not quietOnly and not OneOf(why, RECHECKED))
-- Never shortened: a quiet note must not cut the longer hold a back-off
-- below wrote.
if hold and now + QUIET_SECONDS > r.quietUntil then r.quietUntil = now + QUIET_SECONDS end
newest = name
if quietOnly or AboutTheCaster(why) then
r.last = now
return
end
-- This buff on this person, not the person: held off for a good while,
-- and no count towards backing them off from everything.
if buffKey and OneOf(why, THIS_BUFF) then
r.last = now
-- A group cast (`members`): what cannot land is the group version
-- aimed at this one, so that is what is held, off them as the one a
-- group cast is aimed at (GroupBuffs.lua, Build). The single buff
-- is held off nobody: until the right-buff audit it was held off
-- everybody the cast covered, and one member too low for Arcane
-- Brilliance cost the whole party its Intellect for ten minutes.
local held = members and (buffKey .. ns.GROUP_HOLD) or buffKey
ns.MarkAttempted(name, held, THIS_BUFF_SECONDS, true)
-- And told to the walk as the wrong buff for them, not as a press to
-- wait out (Core.lua, Castable): a paladin's walk goes on to the
-- next blessing.
if not members then HoldOff(name, buffKey, now + THIS_BUFF_SECONDS) end
return
end
-- A run long over was swept (SweepRefusals), so this starts again at one.
r.last = now
-- Where they stand, not who they are: a run of its own (WHERE).
if OneOf(why, WHERE) then
r.undo = nil
StoodAway(name, r, why, now, hold)
return
end
-- What this step is about to write, for an error line just after it
-- saying where they stand (NoteGameError). Only with no reason yet.
r.undo = why == nil and { at = now, blockUntil = r.blockUntil, quietUntil = r.quietUntil, hold = hold } or nil
r.count = r.count + 1
r.why = type(why) == "string" and why or nil
-- What a reason arriving after this refusal acts on (LateReason): the
-- count and the block as they stood before it.
r.late = why == nil and { at = now, key = buffKey, members = members, count = r.count - 1,
block = r.blockUntil } or nil
-- The same buff refused on them twice running: held off, target or not.
if buffKey and not members and RefusedAgain(name, buffKey, why, now) and r.late then r.late.again = true end
-- Somebody the player has picked out and still has selected is not backed
-- off: the lift is a change of target (LiftBackoff), and selecting the
-- person already selected sends none, so the prompt would turn from them
-- for good. The refusal is counted, so the next one, once they are no
-- longer the target, backs them off at the step it has reached. Only a
-- target the player chose: the macro's own /target (a hand-back switched
-- off, or nothing targeted before it) leaves its person targeted too, and
-- they must still be let go. Nothing to say in chat either: they stay offered.
if name == pointedAt and ns.UnitFullName("target") == name then return end
local debt = owed[name]
local steps = (debt and LiveExpiry(debt) > now) and OWED_STEPS or STEPS
local seconds = steps[math.min(r.count, #steps)]
if now + seconds > r.blockUntil then r.blockUntil = now + seconds end
-- The line stays held past the back-off: somebody the game keeps refusing
-- comes back when it runs out, and the first press then would thank them
-- over yet another refused cast. A cast that lands clears it (NoteLanded).
if hold and r.blockUntil + QUIET_SECONDS > r.quietUntil then r.quietUntil = r.blockUntil + QUIET_SECONDS end
if r.count >= TELL_AT and not r.said then
r.said = true
if r.why or not (C_Timer and C_Timer.After) then
Tell(name, r.why)
else
-- The error line can arrive a moment after the refusal it explains,
-- and when it says where they stand this was no step of the
-- person's run at all (NoteGameError takes `said` back).
C_Timer.After(0.3, function()
if r.said then ns.Guard("refusal line", Tell, name, r.why) end
end)
end
end
end
-- A cast on them landed: nothing the game refused before stands.
function ns.NoteLanded(name)
if name then refusals[name] = nil end
end
function ns.SpeechHeld(name, now)
local r = name and refusals[name]
return r ~= nil and r.quietUntil > (now or GetTime())
end
-- The player pointed at them on purpose (PLAYER_TARGET_CHANGED): the
-- back-off lifts, the count stays, so another refusal backs off again. `name`
-- is nil when the target was cleared, and the choice is remembered either way
-- (see NoteRefusal).
function ns.LiftBackoff(name)
pointedAt = name
local r = name and refusals[name]
if r then r.blockUntil = 0 end
end
-- For /manners debug: who is backed off or kept quiet, and for how long.
-- Seconds rather than a clock time: the minimap clock may be the realm's.
function ns.RefusalLines(now)
local out = {}
for name, r in pairs(refusals) do
if r.blockUntil > now then
out[#out + 1] = L["backed off for %ds more: |cffffffff%s|r -- refused %d times in a row"]
:format(math.ceil(r.blockUntil - now), name, r.count)
elseif r.quietUntil > now then
out[#out + 1] = L["no spoken line for %ds more: |cffffffff%s|r -- the game refused a cast on them"]
:format(math.ceil(r.quietUntil - now), name)
end
end
-- And each buff held off one person, by the key the buff is filed under.
for name, held in pairs(heldOff) do
for key, lifts in pairs(held) do
if lifts > now then
out[#out + 1] = L["%s held off for %ds more: |cffffffff%s|r -- the game refused it on them"]
:format(key, math.ceil(lifts - now), name)
end
end
end
table.sort(out)
return out
end
end
-- The debt is paid. Written through, so a reload cannot raise it again.
function ns.SettleFavour(name)
if not name then return end
owed[name] = nil
SaveDebts()
end
---------------------------------------------------------------------------
-- the never-offer list
--
-- People the player has said never to offer anything to, filed under the same
-- name debts are, in the profile. It reaches the queue at its next rebuild (in
-- a fight, the end of it); nothing here touches the button.
---------------------------------------------------------------------------
-- Defined with the favour ledger's other hook further down, and declared here
-- so the never-offer list below can tell the ledger about a favour it lets go.
local TellLedger
-- What a never-offer entry holds: true for a name the player typed (and for
-- every list saved before there was a marker), NEVER_FILED for one filed from a
-- name the game gave. Core's ClampSettings keeps both.
local NEVER_FILED = "filed"
local function NeverSet()
local db = addon.db and addon.db.profile
local never = db and db.never
if type(never) ~= "table" then return nil end
return never
end
local function CleanName(name)
if type(name) ~= "string" then return nil end
name = name:gsub("%s+", " "):match("^%s*(.-)%s*$")
if name == "" then return nil end
return name
end
-- Whether a listed name matches, regardless of case, since names are typed by
-- hand. Folded by the client's strcmputf8i where there is one (string.lower
-- leaves accented capitals alone); plain lower is the fallback, and all there
-- is to it for two names in A to Z. The scan asks this of every asker it
-- remembers against every request on every pass, so the pcall is kept for a
-- name that needs it.
local function SameName(a, b)
if not b then return false end
if a == b then return true end
local fold = _G.strcmputf8i
if type(fold) == "function" and (a:find("[\128-\255]") or b:find("[\128-\255]")) then
local ok, cmp = pcall(fold, a, b)
if ok and type(cmp) == "number" then return cmp == 0 end
end
return a:lower() == b:lower()
end
ns.SameName = SameName
-- Whether a sorts before b, case folded the same way SameName folds it, so a
-- name typed in small letters sorts among the capitalised ones in any script.
-- Not full collation (Ё still sorts apart from Е). Ties fall back to lower and
-- then to bytes, so the order is total, which table.sort needs.
local function NameBefore(a, b)
local fold = _G.strcmputf8i
if type(fold) == "function" then
local ok, cmp = pcall(fold, a, b)
if ok and type(cmp) == "number" and cmp ~= 0 then return cmp < 0 end
end
local la, lb = a:lower(), b:lower()
if la ~= lb then return la < lb end
return a < b
end
-- What the list walk has already answered, by name: the entry, or false. The
-- scan asks about everybody 2.5 times a second, two case-folded compares per
-- entry. Checked against a copy of the list rather than cleared by an editor,
-- because imports, profile switches and the scenarios write the table
-- directly; a different fold function throws the answers away too. `scan` is
-- the answers while BuildQueue walks the units.
local neverSeen = {
list = nil, fold = nil, size = 0, copy = {}, verdict = {}, count = 0,
max = 1000, scan = nil,
}
-- The answers, valid for the list as it stands now. nil when there is no list.
local function NeverVerdicts()
local never = NeverSet()
if not never then return nil end
local copy, fold = neverSeen.copy, _G.strcmputf8i
local fresh = neverSeen.list == never and neverSeen.fold == fold
if fresh then
local size = 0
for key, flag in pairs(never) do
if copy[key] ~= flag then fresh = false break end
size = size + 1
end
if size ~= neverSeen.size then fresh = false end
end
if not fresh then
wipe(copy)
local size = 0
for key, flag in pairs(never) do
copy[key] = flag
size = size + 1
end
neverSeen.list, neverSeen.fold, neverSeen.size = never, fold, size
wipe(neverSeen.verdict)
neverSeen.count = 0
end
return neverSeen.verdict
end
-- The entry on the list that names this person, or nil: exact, then regardless
-- of case, then -- for an entry the player typed -- with the realm taken off the
-- name (as the prompt shows it), never the other way round. An entry filed from
-- a name the game gave (the prompt, the ledger) is marked NEVER_FILED and
-- matches that name alone: a same-realm "Anna" on the list is not the Anna of
-- another realm, who is filed "Anna-Realm" and shows on the prompt as "Anna".
-- An exact entry always beats a realm-less one. `verdict` is NeverVerdicts'
-- table, passed only by the scan; anybody else walks the list.
local function ListedAs(name, verdict)
local never = NeverSet()
if not never or type(name) ~= "string" or next(never) == nil then return nil end
local held = never[name]
if held == true or held == NEVER_FILED then return name end
if verdict then
local known = verdict[name]
if known ~= nil then return known or nil end
end
local short = ShortName(name)
local found, typed
for key, flag in pairs(never) do
if type(key) == "string" then
if SameName(key, name) then found = key break end
if flag == true and not typed and SameName(key, short) then typed = key end
end
end
found = found or typed
if verdict then
-- Bounded like the other per-person caches.
if neverSeen.count >= neverSeen.max then
wipe(verdict)
neverSeen.count = 0
end
verdict[name] = found or false
neverSeen.count = neverSeen.count + 1
end
return found
end
ns.ListedAs = ListedAs
-- The scan's answers while it walks; between scans the same answers, checked
-- against the list as it stands (NeverVerdicts, table lookups alone), so an
-- edit is honoured at once. The repaint and the options page ask about the
-- same few names over and over, and a list walk per ask was most of the cost.
function ns.IsNeverOffered(name)
return ListedAs(name, neverSeen.scan or NeverVerdicts()) ~= nil
end
-- Whether somebody is on your /ignore list, the game's own "leave me alone":
-- by token (or name: IsIgnored takes either), else by GUID. Neither call
-- throws on a plain string, and each answers a plain yes or no, so nothing is
-- pcalled; false on a client without them. Unlike the never-offer list, a
-- favour is no exception: nobody you ignore is thanked or offered one back
-- (Favours.lua, Sight and the combat log), and no passer-by is offered at
-- all. A group member stays offered: buffing a teammate you ignore still
-- helps the group.
function ns.Ignored(unit, guid)
local friends = _G.C_FriendList
if type(friends) ~= "table" then return false end