Repository navigation
Expand file tree
/
Copy pathAutoThanks.lua
More file actions
267 lines (265 loc) · 6.86 KB
/
Copy pathAutoThanks.lua
File metadata and controls
267 lines (265 loc) · 6.86 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
-- SPDX-License-Identifier: GPL-3.0-only
-- Native aura observations only; no network event can request an emote.
local QT = _G.QuestTogether
local EMOTES = { "thank", "bow", "applaud" }
local MAX_AURAS, MAX_PENDING, MAX_COOLDOWNS = 256, 4, 64
local CASTER_COOLDOWN, GLOBAL_COOLDOWN, EXPIRY = 300, 15, 10
local function Number(a, value)
local n = a:SafeToNumber(value)
if n and n == n and n >= 0 and n < math.huge then
return n
end
end
local function Text(a, value, limit)
if
a:CanAccessValue(value)
and type(value) == "string"
and #value > 0
and #value <= limit
and not value:find("[%z\1-\31\127]")
then
return value
end
end
local function Call(a, method, ...)
local fn = a.API and a.API[method]
if type(fn) ~= "function" then
return
end
local ok, value = pcall(fn, ...)
if ok then
return value
end
end
local function Active(a)
return a.isEnabled
and not a.isLoggingOut
and not a.isLeavingWorld
and not a.isRunningTests
and not a.suppressLocalAnnouncementDisplayDuringTests
and a:GetOption("autoThankBuffs") == true
end
local function Restricted(a)
return a:IsRuntimeRestricted() or a:IsRuntimeRestrictionTypeActive("chat")
end
local function Snapshot(a)
local rows = Call(a, "GetAutoThanksAuras")
if not a:CanAccessTable(rows) or type(rows) ~= "table" or #rows > MAX_AURAS then
return
end
local byID, ordered = {}, {}
for i = 1, #rows do
local row = rows[i]
if not a:CanAccessTable(row) or type(row) ~= "table" then
return
end
local id = Number(a, row.id)
if not id or id == 0 or id ~= math.floor(id) or byID[id] then
return
end
local guid = Text(a, row.guid, 64)
if guid and not guid:match("^Player%-%d+%-%x+$") then
guid = nil
end
local clean = {
id = id,
guid = guid,
duration = Number(a, row.duration),
unit = Text(a, row.unit, 40),
name = Text(a, row.name, 120),
}
byID[id], ordered[#ordered + 1] = clean, clean
end
return byID, ordered
end
local function ClearObservation(s)
s.baseline, s.pending, s.dirty = nil, {}, true
end
local function Random(a, low, high)
local value = Number(a, Call(a, "Random", low, high))
if value and value >= low and value <= high and value == math.floor(value) then
return value
end
return low
end
local function PendingAura(s, pending)
local found
for id in pairs(pending.ids) do
local aura = s.baseline and s.baseline[id]
if aura and aura.guid == pending.guid and aura.duration and aura.duration >= 60 then
if not found or id < found.id then
found = aura
end
else
pending.ids[id] = nil
end
end
return found
end
function QT:ResetAutoThanks()
self.autoThanksState = nil
end
function QT:SuspendAutoThanks()
local s = rawget(self, "autoThanksState")
if s then
ClearObservation(s)
end
end
function QT:PLAYER_REGEN_DISABLED()
self:DismissManagedWindowsForCombat()
self:SuspendAutoThanks()
end
function QT:InitializeAutoThanks()
self:ResetAutoThanks()
if not Active(self) then
return false
end
self.autoThanksState = { profile = self.db and self.db.profile, pending = {}, cooldowns = {}, dirty = true }
return true
end
function QT:UNIT_AURA(_, unit)
if not self:CanAccessValue(unit) or unit ~= "player" then
return
end
local s = rawget(self, "autoThanksState")
if s then
if Restricted(self) then
ClearObservation(s)
else
s.dirty = true
end
end
end
function QT:UpdateAutoThanks()
local s = rawget(self, "autoThanksState")
if not s then
return
end
if not Active(self) or s.profile ~= (self.db and self.db.profile) then
self:ResetAutoThanks()
return
end
local now = Number(self, Call(self, "GetTime"))
if not now or s.lastTime and now < s.lastTime then
ClearObservation(s)
s.cooldowns, s.nextAllowed, s.lastTime = {}, nil, now
return
end
s.lastTime = now
if Restricted(self) then
ClearObservation(s)
return
end
local cooldownCount = 0
for guid, untilTime in pairs(s.cooldowns) do
if now >= untilTime then
s.cooldowns[guid] = nil
else
cooldownCount = cooldownCount + 1
end
end
if s.dirty then
s.dirty = false
local current, ordered = Snapshot(self)
if not current then
-- A later complete sample is a silent baseline, never a set of new buffs.
s.baseline, s.pending = nil, {}
return
end
local previous = s.baseline
s.baseline = current
if previous and (not s.nextAllowed or now >= s.nextAllowed) and cooldownCount < MAX_COOLDOWNS then
local pendingCount = 0
for _ in pairs(s.pending) do
pendingCount = pendingCount + 1
end
for _, row in ipairs(ordered) do
if
not previous[row.id]
and row.guid
and row.duration
and row.duration >= 60
and not s.cooldowns[row.guid]
then
local pending = s.pending[row.guid]
if pending then
pending.ids[row.id] = true
elseif pendingCount < MAX_PENDING then
s.pending[row.guid] = {
id = row.id,
ids = { [row.id] = true },
guid = row.guid,
due = now + Random(self, 2000, 4000) / 1000,
expires = now + EXPIRY,
}
pendingCount = pendingCount + 1
end
end
end
end
end
local candidate
for guid, pending in pairs(s.pending) do
if now > pending.expires or not PendingAura(s, pending) then
s.pending[guid] = nil
elseif
now >= pending.due
and (
not candidate
or pending.due < candidate.due
or pending.due == candidate.due and pending.id < candidate.id
)
then
candidate = pending
end
end
if not candidate then
return
end
s.pending[candidate.guid] = nil
if s.nextAllowed and now < s.nextAllowed or s.cooldowns[candidate.guid] then
return
end
-- One fresh scan at action time handles a lost/coalesced removal event or
-- newly inaccessible aura data. It never creates more gratitude candidates.
local current = Snapshot(self)
if not current then
ClearObservation(s)
return
end
s.baseline = current
local aura = PendingAura(s, candidate)
if not aura then
return
end
-- Tokens can be recycled during the pause. Native identity is checked again
-- without changing the player's selected target or retaining foreign tables.
local target = Text(self, Call(self, "GetAutoThanksTarget", candidate.guid, aura.unit, aura.name), 120)
if
not target
or rawget(self, "autoThanksState") ~= s
or s.profile ~= (self.db and self.db.profile)
or not Active(self)
or Restricted(self)
then
return
end
local checked, ignored = pcall(self.IsIgnoredPlayerName, self, target)
if not checked or not self:CanAccessValue(ignored) or ignored ~= false then
return
end
local emote = EMOTES[Random(self, 1, #EMOTES)]
if
rawget(self, "autoThanksState") ~= s
or s.profile ~= (self.db and self.db.profile)
or not Active(self)
or Restricted(self)
then
return
end
-- Reserve the cooldown before crossing the native boundary. Failed attempts
-- are never retried as a burst of social actions.
s.nextAllowed, s.cooldowns[candidate.guid] = now + GLOBAL_COOLDOWN, now + CASTER_COOLDOWN
s.pending = {}
Call(self, "PerformAutoThanks", emote, target)
end