-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSync.lua
More file actions
511 lines (442 loc) · 14.3 KB
/
Sync.lua
File metadata and controls
511 lines (442 loc) · 14.3 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
local addonName, BBT = ...
BBT = BBT or _G.BigBotTracker or {}
_G.BigBotTracker = BBT
BBT.Sync = BBT.Sync or {}
local Sync = BBT.Sync
local Util = BBT.Util
local Storage = BBT.Storage
local Normalizer = BBT.Normalizer
local PREFIX = "BigBotTrack"
local PROTOCOL_VERSION = 2
local FEATURE_VERSION = 4
local MAX_PAYLOAD_BYTES = 240
local OBSERVATION_WINDOW_SECONDS = 1800
Sync.queue = Sync.queue or {}
Sync.queued = Sync.queued or {}
Sync.lastSendAt = Sync.lastSendAt or 0
Sync.status = Sync.status or "Off"
local function getSettings()
local settings = Storage.GetSettings()
return settings.sync
end
local function sanitizeSettings()
local settings = getSettings()
settings.channelName = nil
settings.prefix = nil
settings.includeGuild = settings.includeGuild ~= false
settings.includeGroup = settings.includeGroup ~= false
settings.sendInterval = tonumber(settings.sendInterval) or 12
settings.candidateCooldown = tonumber(settings.candidateCooldown) or 90
settings.maxQueueSize = tonumber(settings.maxQueueSize) or 30
return settings
end
local function isEnumSuccess(enumTable, result)
if result == nil or result == true then
return true
end
if result == false then
return false
end
if enumTable and enumTable.Success ~= nil then
return result == enumTable.Success
end
return true
end
local function ensurePrefixRegistered()
if not C_ChatInfo or type(C_ChatInfo.RegisterAddonMessagePrefix) ~= "function" then
Sync.status = "Addon channel API unavailable"
return false
end
if type(C_ChatInfo.IsAddonMessagePrefixRegistered) == "function" then
local ok, registered = pcall(C_ChatInfo.IsAddonMessagePrefixRegistered, PREFIX)
if ok and registered then
Sync.prefixRegistered = true
return true
end
end
local ok, result1, result2 = pcall(C_ChatInfo.RegisterAddonMessagePrefix, PREFIX)
if not ok then
Sync.status = "Addon prefix registration failed"
return false
end
local result = result2 ~= nil and result2 or result1
Sync.prefixRegistered = isEnumSuccess(Enum and Enum.RegisterAddonMessagePrefixResult, result)
if not Sync.prefixRegistered then
Sync.status = "Addon prefix registration failed"
end
return Sync.prefixRegistered
end
local function getGroupTransport()
if type(IsInGroup) ~= "function" then
return nil
end
local instanceCategory = LE_PARTY_CATEGORY_INSTANCE or 2
if IsInGroup(instanceCategory) then
return "INSTANCE_CHAT"
end
if type(IsInRaid) == "function" and IsInRaid() then
return "RAID"
end
if IsInGroup() then
return "PARTY"
end
return nil
end
local function appendTransport(transports, chatType)
if chatType then
transports[#transports + 1] = chatType
end
end
local function formatTransports(transports)
local labels = {
GUILD = "guild",
PARTY = "party",
RAID = "raid",
INSTANCE_CHAT = "instance",
}
local formatted = {}
for _, transport in ipairs(transports or {}) do
formatted[#formatted + 1] = labels[transport] or transport:lower()
end
return table.concat(formatted, "/")
end
local function getEligibleTransports()
local settings = sanitizeSettings()
local transports = {}
if not settings.enabled then
return transports
end
if settings.includeGuild and type(IsInGuild) == "function" and IsInGuild() then
appendTransport(transports, "GUILD")
end
if settings.includeGroup then
appendTransport(transports, getGroupTransport())
end
return transports
end
local function updateStatus()
local settings = sanitizeSettings()
if not settings.enabled then
Sync.status = "Off"
return
end
local transports = getEligibleTransports()
if #transports == 0 then
Sync.status = "Waiting for guild/group"
else
Sync.status = "Ready: " .. formatTransports(transports)
end
end
local function split(value, delimiter)
local parts = {}
delimiter = delimiter or "|"
value = tostring(value or "")
local startIndex = 1
while true do
local delimiterStart, delimiterEnd = value:find(delimiter, startIndex, true)
if not delimiterStart then
parts[#parts + 1] = value:sub(startIndex)
break
end
parts[#parts + 1] = value:sub(startIndex, delimiterStart - 1)
startIndex = delimiterEnd + 1
end
return parts
end
local function encodeNumber(value, multiplier)
value = tonumber(value) or 0
multiplier = multiplier or 1
return tostring(math.floor(value * multiplier + 0.5))
end
local function decodePercent(value)
return (tonumber(value) or 0) / 100
end
local function observationWindow(timestamp)
timestamp = tonumber(timestamp) or Util.GetNow()
return math.floor(timestamp / OBSERVATION_WINDOW_SECONDS)
end
local function topHashes(map, limit)
local rows = {}
for hash, count in pairs(map or {}) do
rows[#rows + 1] = { hash = hash, count = count }
end
table.sort(rows, function(left, right)
if left.count == right.count then
return left.hash < right.hash
end
return left.count > right.count
end)
local encoded = {}
for index = 1, math.min(limit or 2, #rows) do
encoded[#encoded + 1] = rows[index].hash .. ":" .. tostring(rows[index].count)
end
return table.concat(encoded, ",")
end
local function parseHashes(value)
local hashes = {}
for pair in tostring(value or ""):gmatch("[^,]+") do
local hash, count = pair:match("^([^:]+):(%d+)$")
if hash and count then
hashes[hash] = tonumber(count) or 0
end
end
return hashes
end
local function getPeerId(sender)
local identity = Util.NormalizeIdentity(sender or "unknown")
return Normalizer.Hash(identity.fullKey)
end
function Sync.GetLocalPeerId()
return getPeerId(Util.GetPlayerFullName())
end
function Sync.SerializeCandidate(candidate)
if not candidate or not candidate.displayName then
return nil
end
local score = candidate.score or {}
local timing = candidate.timing or {}
local content = candidate.content or {}
local behavior = candidate.behavior or {}
local firstSeen = math.floor(candidate.firstSeen or 0)
local lastSeen = math.floor(candidate.lastSeen or 0)
for hashLimit = 2, 0, -1 do
local payload = table.concat({
"C",
tostring(PROTOCOL_VERSION),
tostring(candidate.featureVersion or FEATURE_VERSION),
candidate.displayName,
tostring(firstSeen),
tostring(lastSeen),
tostring(observationWindow(firstSeen)),
tostring(observationWindow(lastSeen)),
tostring(candidate.totalMessages or 0),
encodeNumber(timing.averageInterval or 0),
encodeNumber(timing.robustCoefficientVariation or timing.coefficientVariation or 0, 100),
encodeNumber(timing.globalEntropy or 1, 100),
encodeNumber(timing.lowestRollingEntropy or 1, 100),
encodeNumber(content.templateReusePercent or 0),
encodeNumber(content.shingleReusePercent or 0),
tostring(timing.cadenceSwitchCount or 0),
encodeNumber(behavior.postsPerHour or 0, 10),
encodeNumber(score.confidence or 0),
topHashes(content.templateCounts, hashLimit),
topHashes(content.shingleCounts, hashLimit),
}, "|")
if #payload <= MAX_PAYLOAD_BYTES then
return payload
end
end
return nil
end
function Sync.ParseCapsule(message, sender)
if #tostring(message or "") > MAX_PAYLOAD_BYTES then
return nil, "oversized"
end
local parts = split(message, "|")
if parts[1] ~= "C" or tonumber(parts[2]) ~= PROTOCOL_VERSION then
return nil, "version"
end
if not parts[4] or parts[4] == "" then
return nil, "identity"
end
local capsule = {
peerId = getPeerId(sender),
featureVersion = tonumber(parts[3]) or 1,
fullName = parts[4],
firstSeen = tonumber(parts[5]) or 0,
lastSeen = tonumber(parts[6]) or 0,
firstWindow = tonumber(parts[7]) or 0,
lastWindow = tonumber(parts[8]) or 0,
messageCount = tonumber(parts[9]) or 0,
averageInterval = tonumber(parts[10]) or 0,
robustCoefficientVariation = decodePercent(parts[11]),
coefficientVariation = decodePercent(parts[11]),
globalEntropy = decodePercent(parts[12]),
rollingEntropy = decodePercent(parts[13]),
templateReusePercent = tonumber(parts[14]) or 0,
shingleReusePercent = tonumber(parts[15]) or 0,
cadenceSwitchCount = tonumber(parts[16]) or 0,
postsPerHour = (tonumber(parts[17]) or 0) / 10,
confidence = tonumber(parts[18]) or 0,
templateHashes = parseHashes(parts[19]),
shingleHashes = parseHashes(parts[20]),
receivedAt = Util.GetNow(),
}
if capsule.firstSeen <= 0 or capsule.lastSeen < capsule.firstSeen then
return nil, "timestamp"
end
return capsule, nil
end
function Sync.QueueCandidate(candidate)
if not BBT.DB or not candidate or not candidate.fullKey then
return
end
local settings = sanitizeSettings()
if not settings.enabled then
return
end
if (candidate.totalMessages or 0) < 3 then
return
end
local now = Util.GetNow()
if candidate.lastSyncQueuedAt and now - candidate.lastSyncQueuedAt < settings.candidateCooldown then
return
end
candidate.lastSyncQueuedAt = now
if not Sync.queued[candidate.fullKey] then
if #Sync.queue >= settings.maxQueueSize then
local dropped = table.remove(Sync.queue, 1)
if dropped then
Sync.queued[dropped] = nil
end
end
Sync.queue[#Sync.queue + 1] = candidate.fullKey
Sync.queued[candidate.fullKey] = true
end
end
local function findCandidateByFullKey(fullKey)
for _, candidate in ipairs(Storage.GetAllCandidates()) do
if candidate.fullKey == fullKey then
return candidate
end
end
return nil
end
function Sync.SetEnabled(enabled)
local settings = sanitizeSettings()
settings.enabled = enabled == true
if not settings.enabled then
Sync.queue = {}
Sync.queued = {}
end
updateStatus()
end
function Sync.JoinChannel()
sanitizeSettings()
updateStatus()
return false
end
function Sync.SendNext()
local settings = sanitizeSettings()
if not settings.enabled then
updateStatus()
return
end
if not C_ChatInfo or type(C_ChatInfo.SendAddonMessage) ~= "function" then
Sync.status = "Addon channel API unavailable"
return
end
if not ensurePrefixRegistered() then
return
end
local transports = getEligibleTransports()
if #transports == 0 then
updateStatus()
return
end
local now = Util.GetNow()
if now - (Sync.lastSendAt or 0) < settings.sendInterval then
return
end
local fullKey = table.remove(Sync.queue, 1)
if not fullKey then
updateStatus()
return
end
Sync.queued[fullKey] = nil
local candidate = findCandidateByFullKey(fullKey)
if not candidate then
return
end
local payload = Sync.SerializeCandidate(candidate)
if not payload then
return
end
local sent = 0
for _, transport in ipairs(transports) do
local ok, result1, result2 = pcall(C_ChatInfo.SendAddonMessage, PREFIX, payload, transport)
local result = result2 ~= nil and result2 or result1
if ok and isEnumSuccess(Enum and Enum.SendAddonMessageResult, result) then
sent = sent + 1
else
Util.Debug("Sync send failed on " .. tostring(transport) .. ".")
end
end
if sent > 0 then
Sync.status = "Last sent via " .. formatTransports(transports)
Sync.lastSendAt = now
else
Sync.status = "Send throttled or failed"
end
end
function Sync.HandleAddonMessage(prefix, message, channel, sender)
local settings = sanitizeSettings()
if not settings.enabled or prefix ~= PREFIX then
return
end
if Util.IsSelf(sender) then
return
end
local allowedChannel = channel == "GUILD" or channel == "PARTY" or channel == "RAID" or channel == "INSTANCE_CHAT"
if not allowedChannel then
return
end
local capsule, reason = Sync.ParseCapsule(message, sender)
if not capsule then
Util.Debug("Rejected sync packet: " .. tostring(reason))
return
end
local candidate, mergeReason = Storage.MergeNetworkEvidence(capsule)
if candidate then
Sync.status = "Received evidence for " .. candidate.displayName
else
Util.Debug("Rejected network evidence: " .. tostring(mergeReason))
end
end
function Sync.ShowFirstRunNotice()
local settings = sanitizeSettings()
if settings.firstRunNoticeShown then
return
end
settings.firstRunNoticeShown = true
settings.enabled = false
if StaticPopupDialogs and StaticPopup_Show then
StaticPopupDialogs.BIGBOTTRACKER_SYNC_NOTICE = {
text = "Big Bot Tracker can share compact evidence summaries through hidden WoW addon channels with guild or group members who also run it. It does not join custom chat channels and does not share raw chat text.",
button1 = "Enable Sync",
button2 = "Keep Local",
OnAccept = function()
Sync.SetEnabled(true)
end,
OnCancel = function()
Sync.SetEnabled(false)
end,
timeout = 0,
whileDead = true,
hideOnEscape = false,
preferredIndex = 3,
}
StaticPopup_Show("BIGBOTTRACKER_SYNC_NOTICE")
else
Sync.SetEnabled(false)
end
end
function Sync.Initialize()
sanitizeSettings()
ensurePrefixRegistered()
updateStatus()
end
function Sync.Start()
if not BBT.DB then
return
end
sanitizeSettings()
Sync.ShowFirstRunNotice()
if getSettings().enabled then
ensurePrefixRegistered()
end
updateStatus()
end
function Sync.OnUpdate()
Sync.SendNext()
end