-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComms.lua
545 lines (431 loc) · 15.3 KB
/
Comms.lua
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
local ADDON_NAME, namespace = ...
local L = namespace.L
local CONST_ADDON = "GHOSTRECON"
local CONST_CHAT_PREFIX_COLOR = { ["r"] = 1, ["g"] = 1, ["b"] = 0 }
local CONST_CHAT_PREFIX = "Ghost: Recon - "
GhostRecon.knownUsers = { }
local outboundMessages = { }
local nextSendTime = nil
local queries = { }
local inCombat
local versionWarningIssued
local function GetSpellInfo(spellID)
if not spellID then
return nil
end
local spellInfo = C_Spell.GetSpellInfo(spellID)
if spellInfo then
return spellInfo.name, nil, spellInfo.iconID, spellInfo.castTime, spellInfo.minRange, spellInfo.maxRange, spellInfo.spellID, spellInfo.originalIconID
end
end
local function CsvDecode(text, sep)
local rc = { }
local curText
if sep == nil then
sep = ","
end
curText = text..sep
while true do
local posn = string.find(curText, "%"..sep)
if posn then
local curElement = string.sub(curText, 1, posn-1)
table.insert(rc, curElement)
curText = string.sub(curText, posn+1, string.len(curText))
else
break
end
end
return unpack(rc)
end
function GhostRecon:TellUser(msg, r, g, b, force)
if r ~= nil and g == nil and b == nil and force == nil then
force = r
end
-- if not inCombat or force then
if GhostReconDB.Settings.ShowMessages or force then
local out = string.format("%s%s", self:ColoredText(CONST_CHAT_PREFIX, CONST_CHAT_PREFIX_COLOR), msg)
DEFAULT_CHAT_FRAME:AddMessage(out, r, g, b)
end
-- end
end
function GhostRecon:SendSpellInfoNotification(spellId, spellName, zone, isRemovable)
if IsInGuild() and GhostRecon.useComms and GhostReconDB.Settings.GuildSync then
if spellId ~= nil and
spellName ~= nil and
zone ~= nil then
local msg
msg = string.format("SPELLINFO,%d,%s,%s", spellId, spellName, zone)
if isRemovable then
msg = msg..",1"
else
msg = msg..",0"
end
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
end
end
function GhostRecon:SendHealInfoNotification(spellId, spellName, zone, healType)
if IsInGuild() and GhostRecon.useComms and GhostReconDB.Settings.GuildSync then
if spellId ~= nil and
spellName ~= nil and
zone ~= nil then
local msg
msg = string.format("HEALINFO,%d,%s,%s,%d", spellId, spellName, zone, healType)
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
end
end
function GhostRecon:SendNotification(notifyType, mobGuid, mobName, spellId, spellName, zone, success, inInstance)
if IsInGuild() and GhostRecon.useComms and GhostReconDB.Settings.GuildSync then
if notifyType ~= nil and
mobGuid ~= nil and
mobName ~= nil and
spellId ~= nil and
spellName ~= nil and
zone ~= nil then
local msg
msg = string.format("%s,%s,%s,%d,%s,%s", string.upper(notifyType), mobGuid, mobName, spellId, spellName, zone)
if success then
msg = msg..",1"
else
msg = msg..",0"
end
if inInstance then
msg = msg..",1"
else
msg = msg..",0"
end
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
end
end
function GhostRecon:SendQuery(mobGuid, mobName, zone)
if IsInGuild() and GhostRecon.useComms and GhostReconDB.Settings.GuildSync then
local info = { }
info.mobGuid = mobGuid
info.mobName = mobName
info.zone = zone
info.spellCount = 0
info.respondants = { }
info.askTime = GetTime()
queries[mobGuid] = info
-- send the query
local msg = string.format("QUERY,%s,%s,%s", mobGuid, mobName, zone)
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
end
function GhostRecon:SendHello()
if IsInGuild() then
C_ChatInfo.SendAddonMessage(CONST_ADDON, "HELLO", "GUILD")
end
end
function GhostRecon:SendWelcome()
if IsInGuild() then
C_ChatInfo.SendAddonMessage(CONST_ADDON, "WELCOME", "GUILD")
end
end
function GhostRecon:SendVersion()
if IsInGuild() then
C_ChatInfo.SendAddonMessage(CONST_ADDON, "VERSION,"..C_AddOns.GetAddOnMetadata("GhostRecon", "Version"), "GUILD")
end
end
local function ReceiveControlNotification(sender, mobGuid, mobName, spellId, spellName, zone, success, inInstance)
spellId = tonumber(spellId)
success = tonumber(success)
inInstance = tonumber(inInstance)
if inInstance == 1 then
GhostReconDB.Instances[zone] = GhostReconDB.Instances[zone] or {}
GhostReconDB.Instances[zone].isInstance = true
end
if success == 0 then
success = nil
else
success = true
end
if inInstance == 1 or not GhostReconDB.Settings.InstancesOnly then
if GhostRecon:AddControl(zone, mobGuid, mobName, spellId, success) then
if spellId then
local link = C_Spell.GetSpellLink(spellId)
if link then
if success then
GhostRecon:TellUser(string.format(L["Received control information: %s works on %s (%s). Sent by %s."], link, mobName, zone, sender), 0.3, 1, 0.3)
else
GhostRecon:TellUser(string.format(L["Received control information: %s fails on %s (%s). Sent by %s."], link, mobName, zone, sender), 1, 0.3, 0.3)
end
end
end
end
end
end
local function ReceiveAbilityNotification(sender, mobGuid, mobName, spellId, spellName, zone, inInstance)
spellId = tonumber(spellId)
inInstance = tonumber(inInstance)
if spellId ~= nil and mobGuid ~= nil then
if not GhostRecon.IgnoredAbilities[spellId] then
if inInstance == 1 then
GhostReconDB.Instances[zone] = GhostReconDB.Instances[zone] or { }
GhostReconDB.Instances[zone].isInstance = true
end
if inInstance == 1 or not GhostReconDB.Settings.InstancesOnly then
if GhostRecon:AddAbility(zone, mobGuid, mobName, spellId) then
local link = C_Spell.GetSpellLink(spellId)
GhostRecon:TellUser(string.format(L["Received spell/ability information: %s has a spell/ability called %s (%s). Sent by %s."], mobName, link, zone, sender), 0.3, 0.3, 1)
if GhostRecon.abilityBar:IsVisible() then
if GhostRecon.abilityBar.mobName == mobName then
GhostRecon:RefreshSpells("target")
end
end
end
end
end
end
end
local function ReceiveQueryRequest(sender, mobGuid, mobName, zone)
local spellCount = 0
local whereInfo = GhostReconDB.Instances[zone] or { }
local mobInfo = whereInfo[mobName] or { }
if mobInfo.spells then
for i, _ in pairs(mobInfo.spells) do
spellCount = spellCount + 1
end
end
if spellCount > 0 then
local msg = string.format("RESPONSE,%s,%s,%s,%d", mobGuid, mobName, zone, spellCount)
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
end
local function ReceiveQueryResponse(sender, mobGuid, mobName, zone, spellCount)
local info = queries[mobGuid]
if info then
info.respondants[sender] = tonumber(spellCount)
end
end
local function ReceiveSpellInfoNotification(sender, spellId, spellName, zone, removable, inInstance)
if spellId ~= nil and spellName ~= nil then
spellId = tonumber(spellId)
inInstance = tonumber(inInstance)
removable = tonumber(removable)
if spellId then
local link = C_Spell.GetSpellLink(spellId)
if link then
GhostReconDB.SpellInfo = GhostReconDB.SpellInfo or {}
GhostReconDB.SpellInfo[spellId] = GhostReconDB.SpellInfo[spellId] or {}
if not GhostReconDB.SpellInfo[spellId].removable then
GhostRecon:TellUser(string.format(L["Received spell information: %s is removable. Sent by %s."], link, sender), 0.3, 1, 0.3)
end
if removable == 1 then
GhostReconDB.SpellInfo[spellId].removable = true
else
GhostReconDB.SpellInfo[spellId].removable = false
end
end
end
end
end
local function ReceiveHealInfoNotification(sender, spellId, spellName, zone, healType, inInstance)
if spellId ~= nil and spellName ~= nil then
spellId = tonumber(spellId)
inInstance = tonumber(inInstance)
healType = tonumber(healType)
if spellId then
local link = C_Spell.GetSpellLink(spellId)
if link then
GhostReconDB.SpellInfo = GhostReconDB.SpellInfo or {}
GhostReconDB.SpellInfo[spellId] = GhostReconDB.SpellInfo[spellId] or {}
if GhostReconDB.SpellInfo[spellId].healType == nil then
if healType == GhostRecon.CONST_HEAL_SELF then
GhostRecon:TellUser(string.format(L["Received heal information: %s is a self-healing spell. Sent by %s."], link, sender), 0.3, 1, 0.3)
elseif healType == GhostRecon.CONST_HEAL_OTHERS then
GhostRecon:TellUser(string.format(L["Received heal information: %s is a general healing spell. Sent by %s."], link, sender), 0.3, 1, 0.3)
end
GhostReconDB.SpellInfo[spellId].healType = healType
elseif GhostReconDB.SpellInfo[spellId].healType == GhostRecon.CONST_HEAL_SELF then
if healType == GhostRecon.CONST_HEAL_OTHERS then
GhostReconDB.SpellInfo[spellId].healType = GhostRecon.CONST_HEAL_OTHERS
GhostRecon:TellUser(string.format(L["Received heal information: %s is a general healing spell. Sent by %s."], link, sender), 0.3, 1, 0.3)
end
end
end
end
end
end
local function ReceiveSendRequest(sender, whoShouldSend, mobGuid, mobName, zone)
if whoShouldSend == UnitName("player") then
local whereInfo = GhostReconDB.Instances[zone] or { }
local mobInfo = whereInfo[mobName] or { }
-- mob CC effects
for ccType, spellList in pairs(GhostRecon.ccList) do
local value = mobInfo[ccType] and mobInfo[ccType].value
local chosenSpellId = 0
local spellName = ""
for spellId, _ in pairs(spellList) do
chosenSpellId = spellId
break
end
if value ~= nil and chosenSpellId ~= nil then
local name = GetSpellInfo(chosenSpellId)
if name then spellName = name end
if value == GhostRecon.CC_NO then
GhostRecon:SendNotification("CONTROL", mobGuid, mobName, chosenSpellId, spellName, zone, false, whereInfo.isInstance or false)
elseif value == GhostRecon.CC_YES then
GhostRecon:SendNotification("CONTROL", mobGuid, mobName, chosenSpellId, spellName, zone, true, whereInfo.isInstance or false)
elseif value == GhostRecon.CC_SOMETIMES then
GhostRecon:SendNotification("CONTROL", mobGuid, mobName, chosenSpellId, spellName, zone, false, whereInfo.isInstance or false)
GhostRecon:SendNotification("CONTROL", mobGuid, mobName, chosenSpellId, spellName, zone, true, whereInfo.isInstance or false)
end
end
end
-- mob abilities
local spellsFound = { }
if mobInfo.spells then
for i, _ in pairs(mobInfo.spells) do
local name = GetSpellInfo(i)
GhostRecon:SendNotification("ABILITY", mobGuid, mobName, i, name, zone, true, whereInfo.isInstance or false)
-- additional spell information
if GhostReconDB.SpellInfo and GhostReconDB.SpellInfo[i] then
GhostRecon:SendSpellInfoNotification(i, name, zone, GhostReconDB.SpellInfo[i].removable)
end
end
end
end
end
local function ReceiveVersionCheck(sender, versionString)
local major, minor, revision = CsvDecode(versionString, ".")
local myMajor, myMinor, myRevision = CsvDecode(C_AddOns.GetAddOnMetadata("GhostRecon", "Version"), ".")
local total = tonumber(major) * 10000 + tonumber(minor) * 100 + tonumber(revision)
local myTotal = tonumber(myMajor) * 10000 + tonumber(myMinor) * 100 + tonumber(myRevision)
GhostRecon.knownUsers[sender] = versionString
if not versionWarningIssued and myTotal < total then
GhostRecon:TellUser(string.format(L["%s has version %s of Ghost: Recon - time for you to update!"], sender, versionString), 1, 0, 0, true)
versionWarningIssued = true
end
end
function GhostRecon:RequestVersion(sender)
if IsInGuild() then
C_ChatInfo.SendAddonMessage(CONST_ADDON, "SENDVERSION", "GUILD")
end
end
local notifyHandlers = {
["CONTROL"] = ReceiveControlNotification,
["ABILITY"] = ReceiveAbilityNotification,
["SPELLINFO"] = ReceiveSpellInfoNotification,
["HEALINFO"] = ReceiveHealInfoNotification,
["QUERY"] = ReceiveQueryRequest,
["RESPONSE"] = ReceiveQueryResponse,
["SEND"] = ReceiveSendRequest,
["VERSION"] = ReceiveVersionCheck,
["SENDVERSION"] = function()
GhostRecon:SendVersion()
end,
["HELLO"] = function()
GhostRecon:SendWelcome()
GhostRecon.useComms = true
end,
["WELCOME"] = function()
GhostRecon:TellUser(L["Switching communications on - other guild members are using Ghost: Recon."])
GhostRecon.useComms = true
end,
}
local function OnReceiveNotification(f, event, addOn, msg, distribution, sender)
if GhostReconDB.Settings.Active then
if addOn == CONST_ADDON and sender ~= UnitName("player") then
if not GhostRecon.knownUsers[sender] then
GhostRecon.knownUsers[sender] = true
end
if GhostReconDB.Settings.GuildSync then
local notifyType = CsvDecode(msg)
local func = notifyHandlers[notifyType]
if func then
func(sender, select(2, CsvDecode(msg)))
end
end
end
end
end
local function OnEvent(f, event, ...)
if event == "PLAYER_REGEN_ENABLED" then
inCombat = false
elseif event == "PLAYER_REGEN_DISABLED" then
inCombat = true
elseif event == "CHAT_MSG_ADDON" then
OnReceiveNotification(f, event, ...)
elseif event == "ZONE_CHANGED" or
event == "ZONE_CHANGED_INDOORS" or
event == "ZONE_CHANGED_NEW_AREA" then
GhostRecon:SendVersion()
elseif event == "PLAYER_ENTERING_WORLD" then
GhostRecon:SendHello()
end
end
local function OnUpdate()
-- send pending outgoing messages to keep spam under
-- control in order to avoid disconnections
if nextSendTime ~= nil then
local curTime = GetTime()
local msgSent
if curTime >= nextSendTime then
for msg, _ in pairs(outboundMessages) do
if msgSent then
nextSendTime = curTime + 0.3333333333
break
else
C_ChatInfo.SendAddonMessage(CONST_ADDON, msg, "GUILD")
outboundMessages[msg] = nil
nextSendTime = nil
msgSent = true
end
end
end
end
-- check query responses
for key, info in pairs(queries) do
local diff = GetTime() - info.askTime
if diff >= 5 then
-- find a sender and get them to tell us
local chosenSender
local highCount = 0
for who, count in pairs(info.respondants) do
if count > highCount then
highCount = count
chosenSender = who
end
end
if chosenSender then
-- format a message to tell them to send the info
local msg = string.format("SEND,%s,%s,%s,%s", chosenSender, info.mobGuid, info.mobName, info.zone)
outboundMessages[msg] = true
if nextSendTime == nil then
nextSendTime = GetTime()
end
end
-- remove the entry - if we didn't get any responses
-- by this time, we'll assume we won't get any at all
queries[key] = nil
end
end
end
local frame = CreateFrame("Frame")
frame:SetScript("OnEvent", OnEvent)
frame:SetScript("OnUpdate", OnUpdate)
frame:RegisterEvent("CHAT_MSG_ADDON")
frame:RegisterEvent("PLAYER_REGEN_ENABLED")
frame:RegisterEvent("PLAYER_REGEN_DISABLED")
frame:RegisterEvent("ZONE_CHANGED")
frame:RegisterEvent("ZONE_CHANGED_INDOORS")
frame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")