forked from E1ila/RaidLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaidLogger.lua
executable file
·1777 lines (1574 loc) · 62.3 KB
/
RaidLogger.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
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
--
-- Created by IntelliJ IDEA.
-- User: kof
-- Date: 11/04/2019
-- Time: 18:36
--
local VERSION = 2.0012
local MIN_RAID_PLAYERS = 10
local ADDON_NAME = "RaidLogger"
local FONT_NAME = "Fonts\\FRIZQT__.TTF"
local ADDON_PREFIX = "RaidLogger"
-- local HOURGLASS_SAND_NAME = "Linen Cloth"
local HOURGLASS_SAND_NAME = "Hourglass Sand"
local TRACKED_INSTANCES = {
[1] = "The Molten Core",
[2] = "Blackwing Lair",
[3] = "Onyxia's Lair",
[4] = "Ahn'Qiraj",
[5] = "Naxxramas",
-- [5] = "Zul'Gurub",
-- [6] = "Ruins of Ahn'Qiraj",
-- [8] = "Ragefire Chasm",
}
local CLASS_COLOR = {
["Druid"] = "|cffFF7D0A",
["Hunter"] = "|cffA9D271",
["Mage"] = "|cff40C7EB",
["Paladin"] = "|cffF58CBA",
["Priest"] = "|cffFFFFFF",
["Rogue"] = "|cffFFF569",
["Shaman"] = "|cff0070DE",
["Warlock"] = "|cff8787ED",
["Warrior"] = "|cffC79C6E",
["Unknown"] = "|cff888888",
}
local IGNORED_ITEMS = {
[1] = "Elementium Ore",
[2] = "Nexus Crystal"
}
local COLOR_INSTANCE = "|cffff33ff"
local STATE_ATTENDED = "a"
local STATE_BENCHED = "b"
local STATE_NOSHOW = "n"
local STATE_LATE = "l"
local STATE_SKIP = "s"
local QUALITY_POOR = 0 -- gray
local QUALITY_COMMON = 1 -- white
local QUALITY_UNCOMMON = 2 -- green
local QUALITY_RARE = 3 -- blue
local QUALITY_EPIC = 4 -- purple
local QUALITY_LEGENDARY = 5 -- orange
local QUALITY_TEXT = {
[0] = "Poor",
[1] = "Common",
[2] = "Uncommon",
[3] = "Rare",
[4] = "Epic",
[5] = "Legendary",
}
local SYNC_LOOT = "loot"
local SYNC_COUNCIL = "council"
local SYNC_COUNCIL_WHO = "council?"
local SYNC_VOTE = "vote"
local SYNC_SUGGEST = "suggest"
local SYNC_PING = "ping"
local SYNC_PONG = "pong"
local SYNC_CHECK = "check"
local SYNC_CHECK_REPLY = "check-reply"
local SYNC_RESEND = "resend"
local SYNC_END = "end"
local SYNC_COOLDOWN_SECONDS = 60
local NEXT_SYNC_CHECK_MIN_SECONDS = 60
local NEXT_SYNC_CHECK_RANDOM_SECONDS = 60
local NEXT_SYNC_CHECK_SOON_MIN_SECONDS = 10
local NEXT_SYNC_CHECK_SOON_RANDOM_SECONDS = 10
local DROPDOWN_DISENCHANT_NAME = "-- Disenchant --"
local DROPDOWN_BANK_NAME = "-- Bank --"
local BUFF_CHECK_SECONDS = 60
local lastBuffCheck = 0
local editRaid = nil
local editRaidIndex = nil
local lastCouncilSync = 0
local votingEnabled = false
local nextSyncCheck = 0
local lastSync = 0
local firstSyncMismatch = 0
local lootMismatchs = 0
local outOfSync = false
local syncingNow = false
RaidLoggerDelayedMessages = {}
RaidLoggerPendingLoot = {}
RaidLoggerStore = {
raids = {},
activeRaid = nil,
players = {},
}
RaidLogger = {}
local function questionOp(cond, trueValue, falseValue)
if cond then
return trueValue
end
return falseValue
end
local function out(text)
print(" |cff0088ff<|cff00bbffRaidLogger|cff0088ff>|r "..text)
end
local function debug(text)
if RaidLoggerStore.debug then
print(" |cff0088ff<|cff00bbffRaidLogger|cff0088ff>|r |cff009999DEBUG |cff999999"..text)
end
end
local function err(text)
out(""..text)
end
local function normalizeLink(link)
-- remove player level from item link
local parts = {_G.string.split(":", link)}
parts[10] = "_"
return table.concat(parts, ":")
end
local function removeRealmName(playerRealmName)
local nameParts = {_G.string.split("-", playerRealmName)}
return nameParts[1]
end
local function splitCsv(text, sep)
local result = {}
for word in string.gmatch(text, '([^,]+)') do
table.insert(result, word)
end
return result
end
local function tableTextLookup(table, text)
for _, value in ipairs(table) do
if value == text then
return true
end
end
return false
end
-- checks if a value exists in a list
local function HasValue(tab, val)
for _, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
-- removes a value from a list, if exists
local function RemoveValue(tab, val)
for index, value in ipairs(tab) do
if value == val then
table.remove(tab, index)
return true
end
end
return false
end
local function PlaceLinkInChatEditBox(itemLink)
-- Copy itemLink into ChatFrame
local chatFrame = SELECTED_DOCK_FRAME
local editbox = chatFrame.editBox
if editbox then
if editbox:HasFocus() then
editbox:SetText(editbox:GetText()..itemLink);
else
editbox:SetFocus(true);
editbox:SetText(itemLink);
end
end
end
local function AttStatusToString(status)
if status == STATE_ATTENDED then return "Attended" end
if status == STATE_BENCHED then return "Benched" end
if status == STATE_LATE then return "Late" end
if status == STATE_SKIP then return "Skip from logs" end
if status == STATE_NOSHOW then return "No Show" end
return "??"..status
end
local function AttStatusFromString(text)
if text == "Attended" then return STATE_ATTENDED end
if text == "Benched" then return STATE_BENCHED end
if text == "Late" then return STATE_LATE end
if text == "Skip from logs" then return STATE_SKIP end
if text == "No Show" then return STATE_NOSHOW end
return "??"
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- LOGGER LOGIC
local function InTrackedInstance()
if not IsInInstance() then return nil end
local zone = GetZoneText()
if tableTextLookup(TRACKED_INSTANCES, zone) then return zone end
return nil
end
local function ConcatPlayers(tab, filter)
local st = ""
for name, state in pairs(tab) do
if state == filter then
st = st .. CLASS_COLOR[RaidLoggerStore.players[name] or "Unknown"] .. name .. "|r "
end
end
return st
end
local function TitleCase(first, rest)
return string.upper(first) .. string.lower(rest)
end
local function FixPlayerName(player)
return TitleCase(string.sub(player, 1, 1), string.sub(player, 2))
end
local function ColorName(who)
return CLASS_COLOR[RaidLoggerStore.players[who] or "Unknown"] .. who .. "|r"
end
local function GetNumRaidMembers()
local count = 0
for i = 1, MAX_RAID_MEMBERS do
name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(i)
if name then
count = count + 1
end
end
return count
end
local function EndRaidReminder()
err(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
err(" DO NOT FORGET TO END THE RAID !")
err(" /rl end")
err(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
end
local function ItemStringFromLink(itemLink)
local startIndex, _ = string.find(itemLink, "item")
local _, endIndex = string.find(itemLink, "h%[")
return string.sub(itemLink, startIndex, endIndex-3)
end
-- loot can be itemId
local function LogLoot(who, loot, quantity, ts, tradedTo, votes, status, lootid)
-- local vStartIndex, vEndIndex, vLinkColor, vItemCode, vItemEnchantCode, vItemSubCode, vUnknownCode, vItemName = strfind(loot, "|c(%x+)|Hitem:(%d+):(%d+):(%d+):(%d+)|h%[([^%]]+)%]|h|r");
local itemName, itemLink, quality, _, _, itemType, _, _, _, _, vendorPrice = GetItemInfo(loot);
if not itemLink then
debug("Adding item to RaidLoggerPendingLoot - "..who..","..loot..","..quantity)
tinsert(RaidLoggerPendingLoot, {who, loot, quantity or 1, ts, tradedTo, votes, status, lootid})
return
end
itemLink = normalizeLink(itemLink)
local itemString = ItemStringFromLink(itemLink)
lootid = lootid or (#RaidLoggerStore.activeRaid.loot + 1)
-- debug("Checking dup of - "..lootid..","..itemString)
for i = #RaidLoggerStore.activeRaid.loot, 1, -1 do
local loggedItem = RaidLoggerStore.activeRaid.loot[i]
-- debug("Checking dup with - "..loggedItem.lootid..","..loggedItem.itemString)
if loggedItem.lootid == lootid then
if loggedItem.itemString == itemString then
debug("Found matching loot entry")
else
out("|cffff0000Loot log isn't synced!")
end
return
end
end
if who and itemName == HOURGLASS_SAND_NAME then
if not RaidLoggerStore.activeRaid.sands then RaidLoggerStore.activeRaid.sands = {} end
RaidLoggerStore.activeRaid.sands[who] = (RaidLoggerStore.activeRaid.sands[who] or 0) + 1
end
local ignoredItem = tableTextLookup(IGNORED_ITEMS, itemName)
if who and quality >= QUALITY_EPIC and not ignoredItem then
out("Logged loot: " .. ColorName(who) .. " received " .. itemLink)
local entry = {
player = who,
item = itemName,
ts = ts or time(),
link = itemLink,
quality = quality,
quantity = quantity,
votes = votes or {},
status = status or 0,
lootid = lootid,
itemString = itemString,
tradedTo = tradedTo,
}
table.insert(RaidLoggerStore.activeRaid.loot, entry)
RaidLogger_RaidWindow_LootTab:Refresh()
if not ts then
local count = #RaidLoggerStore.activeRaid.loot
RaidLogger:PostLootEntry(entry, count.."/"..count, 3, nil)
end
end
end
local LootMsgStrings = {
_G.LOOT_ITEM_MULTIPLE, -- %s receives loot: %sx%d.
_G.LOOT_ITEM, -- %s receives loot: %s.
}
local LootSelfMsgStrings = {
_G.LOOT_ITEM_SELF_MULTIPLE, -- You receive loot: %sx%d.
_G.LOOT_ITEM_SELF, -- You receive loot: %s.
}
function RaidLogger:ParseLootMessage(msg, zone)
for _, st in ipairs(LootMsgStrings) do
local player, link, quantity = RaidLoggerDeformat(msg, st)
if player and link then
LogLoot(player, link, (quantity or 1))
end
end
for _, st in ipairs(LootSelfMsgStrings) do
local link, quantity = RaidLoggerDeformat(msg, st)
if link then
local myName = UnitName("player")
LogLoot(myName, link, (quantity or 1))
end
end
end
function RaidLogger_Commands(msg)
-- local _, _, cmd, arg1 = string.find(msg, "([%w]+)%s*(.*)$");
local cmd, arg1 = _G.string.split(" ", msg)
cmd = string.upper(cmd)
-- out("cmd '" .. cmd .. "'")
if not cmd or #cmd == 0 then
RaidLoggerStore.windowShown = not RaidLoggerStore.windowShown
if RaidLoggerStore.windowShown then
RaidLogger:ChooseLastRaid()
RaidLogger_RaidWindow:Refresh()
RaidLogger_RaidWindow:Show()
else
RaidLogger_RaidWindow:Hide()
end
elseif "S" == cmd or "START" == cmd then
local zone = nil
if arg1 and #arg1 > 1 then
zone = string.sub(msg, #cmd + 2)
debug("Custom zone '"..zone.."'")
end
RaidLogger:UpdateRaid(zone)
elseif "H" == cmd or "HELP" == cmd then
out("Commands: ")
out(" |cFF00FF00/rl|r - show UI")
out(" |cFF00FF00/rl |cFF00ff95a|cFF00FF00dd <player>|r - manually log an attended player.")
out(" |cFF00FF00/rl |cFF00ff95b|cFF00FF00ench <player>|r - log a benched player.")
out(" |cFF00FF00/rl log <itemlink> <receiver>|r - manually add looted item.")
out(" |cFF00FF00/rl de|r - marks last distributed loot item as disenchanted.")
out(" |cFF00FF00/rl os|r - marks last distributed loot as an off-spec item.")
out(" |cFF00FF00/rl discard|r - discard current raid, do this to ignore current raid.")
out(" |cFF00FF00/rl end|r - save and close raid, do this when raid ended.")
out(" |cFF00FF00/rl p|r - print active raid, if any.")
out(" |cFF00FF00/rl start|r - start logging a raid or update existing one.")
out(" |cFF00FF00/rl sand <channel>|r - print a list of players who picked [Hourglass Sand]. Channel can be raid/yell/guild or empty for say.")
out(" |cFF00FF00/rl ping|r - check who's on your sync channel.")
out(" |cFF00FF00/rl sync <password>|r - sets sync channel. Leave <password> empty to print the password.")
out(" |cFF00FF00/rl resync <player>|r - requests for full loot re-sync from <player>, make sure he's in your sync channel.")
elseif "LOG" == cmd then
if not RaidLoggerStore.activeRaid then
out("No active raid!")
return
end
local startIndex, _ = string.find(arg1, "%|c");
local _, endIndex = string.find(arg1, "%]%|h%|r");
local itemLink = string.sub(arg1, startIndex, endIndex);
if itemLink and GetItemInfo(itemLink) then
if ((endIndex + 2 ) <= (#arg1)) then
local player = string.sub(arg1, endIndex + 2, #arg1)
if player then
LogLoot(player, itemLink, 1)
else
out("Incorrect usage of command, write |cff00ff00/rl log [ITEM_LINK] [RECEIVER_NAME]")
end
end
else
out("Incorrect usage of command, write |cff00ff00/rl log [ITEM_LINK] [RECEIVER_NAME]")
end
elseif "COUNCIL" == cmd then
if arg1 and string.len(arg1) > 0 then
if arg1 == "disable" then
out("Loot council disabled.");
RaidLoggerStore.council = nil
else
RaidLogger:SetLootCouncil(FixPlayerName(arg1))
end
else
err("Missing player name!")
end
elseif "SYNC" == cmd then
if arg1 and string.len(arg1) > 0 then
if string.len(arg1) > 6 then
return out("Password is too long! Max length is 6 characters.")
end
RaidLoggerStore.sync = arg1
ReloadUI()
else
out("Current sync password: |cff00ff00" .. RaidLoggerStore.sync)
end
elseif "CHECK" == cmd then
lootMismatchs = 0
RaidLogger:Post(0, nil, SYNC_CHECK, VERSION)
elseif "RESYNC" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLoggerStore.activeRaid.loot = {}
lastSync = 0
out("Requesting full item resync from "..arg1)
RaidLogger:FullResync(arg1)
else
err("Missing sync target! Write /rl resync <PLAYER_NAME>")
end
elseif "RESEND" == cmd then
if arg1 and string.len(arg1) > 0 then
local entry = RaidLoggerStore.activeRaid.loot[tonumber(arg1)]
local count = #RaidLoggerStore.activeRaid.loot
RaidLogger:PostLootEntry(entry, count.."/"..count, 1, nil)
else
err("Missing sync target! Write /rl resync <PLAYER_NAME>")
end
elseif "PING" == cmd then
out("Sending PING query...")
RaidLogger:Post(0, sender, SYNC_PING)
elseif "SAND" == cmd then
if not editRaid.sands then
out("No sands log in selected raid.")
return
end
local sorted = {}
for name in pairs(editRaid.sands) do table.insert(sorted, name) end
if #sorted == 0 then
out("No sands were logged in selected raid.")
return
end
table.sort(sorted)
local output = "The following players have looted |cffffffff|Hitem:19183::::::::60:::::::|h[Hourglass Sand]|h|r: "
for i = 1, #sorted do
local name = sorted[i]
output = output .. name .. " x" .. editRaid.sands[name]
if i < #sorted then output = output .. ", " end
end
SendChatMessage(output, arg1)
elseif "CLEAR" == cmd then
RaidLoggerStore.activeRaid.loot = {}
RaidLogger_RaidWindow_LootTab:Refresh()
elseif "BENCH" == cmd or "B" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLogger:LogBenched(FixPlayerName(arg1))
else
err("Missing player name!")
end
elseif "NOSHOW" == cmd or "NS" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLogger:LogNoShow(FixPlayerName(arg1))
else
err("Missing player name!")
end
elseif "LATE" == cmd or "L" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLogger:LogLateShow(FixPlayerName(arg1))
else
err("Missing player name!")
end
elseif "REMOVE" == cmd or "R" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLogger:RemoveFromLog(FixPlayerName(arg1))
else
err("Missing player name!")
end
elseif "ADD" == cmd or "A" == cmd then
if arg1 and string.len(arg1) > 0 then
RaidLogger:LogAttended(FixPlayerName(arg1))
else
err("Missing player name!")
end
elseif "P" == cmd then
if RaidLoggerStore.activeRaid then
out("Raid started at " .. COLOR_INSTANCE .. RaidLoggerStore.activeRaid.date)
if RaidLoggerStore.activeRaid.zone then
out("Zone " .. COLOR_INSTANCE .. RaidLoggerStore.activeRaid.zone)
end
out("Attended " .. ConcatPlayers(RaidLoggerStore.activeRaid.players, STATE_ATTENDED))
out("Benched " .. ConcatPlayers(RaidLoggerStore.activeRaid.players, STATE_BENCHED))
out("No-show " .. ConcatPlayers(RaidLoggerStore.activeRaid.players, STATE_NOSHOW))
out("Late " .. ConcatPlayers(RaidLoggerStore.activeRaid.players, STATE_LATE))
out("Skip " .. ConcatPlayers(RaidLoggerStore.activeRaid.players, STATE_SKIP))
else
out("No active raid.")
end
elseif "DISCARD" == cmd then
RaidLogger:DiscardRaid()
elseif "VERSION" == cmd or "V" == cmd then
out("Version |cFFFFFF00" .. VERSION)
elseif "DEBUG" == cmd then
RaidLoggerStore.debug = not RaidLoggerStore.debug
out("Debug mode: " .. tostring(RaidLoggerStore.debug))
elseif "END" == cmd then
out("Raid ended, saving.")
RaidLogger:EndRaid()
RaidLogger:Post(0, nil, SYNC_END)
end
end
function RaidLogger:StartRaid()
-- flush previous raid
RaidLogger:EndRaid()
RaidLoggerStore.activeRaid = {
date = date("%y-%m-%d %H:%M"),
startTime = time(),
players = {},
zone = nil,
loot = {},
buffs = {},
}
if not RaidLoggerStore.players then
RaidLoggerStore.players = {}
end
LoggingCombat(true) -- start combat logging
out("Started a new raid.")
local roster = {}
for i=1,GetNumGuildMembers() do
local name,rank,_,level,clas = GetGuildRosterInfo(i)
if level >= 40 then
roster[name] = {level, clas, rank}
end
end
RaidLoggerStore.guildRoster = roster
RaidLogger:ChooseLastRaid()
RaidLogger_RaidWindow:Refresh()
RaidLogger_RaidWindow_Buttons_LootTab:Clicked()
nextSyncCheck = 1 -- check with other raiders if there's loot going on
end
function RaidLogger:DiscardRaid()
if RaidLoggerStore.activeRaid then
out("Raid has been discarded.")
RaidLoggerStore.activeRaid = nil
RaidLogger:ChooseLastRaid()
RaidLogger_RaidWindow:Refresh()
else
out("No active raid.")
end
end
function RaidLogger:EndRaid()
if RaidLoggerStore.activeRaid then
RaidLoggerStore.activeRaid.endTime = time()
if not RaidLoggerStore.activeRaid.zone then
RaidLoggerStore.activeRaid.zone = "Unknown"
end
table.insert(RaidLoggerStore.raids, RaidLoggerStore.activeRaid)
out("Ended raid to " .. COLOR_INSTANCE .. RaidLoggerStore.activeRaid.zone)
end
RaidLoggerStore.activeRaid = nil
LoggingCombat(false) -- stop combat logging
RaidLogger:ChooseLastRaid()
RaidLogger_RaidWindow:Refresh()
end
function RaidLogger:SetLootCouncil(player)
if not RaidLoggerStore.council then
RaidLoggerStore.council = {}
end
if RaidLoggerStore.council[player] then
out("Removing " .. ColorName(player) .. " from loot council.")
RaidLoggerStore.council[player] = nil
else
out("Adding " .. ColorName(player) .. " to loot council.")
RaidLoggerStore.council[player] = true
end
self:AnnounceLootCouncil()
RaidLogger_RaidWindow_LootTab:Refresh()
end
function RaidLogger:PackLootCouncil()
if not RaidLoggerStore.council then return "" end
local names = {}
for name, _ in pairs(RaidLoggerStore.council) do
tinsert(names, name)
end
table.sort(names)
return table.concat(names, "|")
end
function RaidLogger:AnnounceLootCouncil(packedCouncil)
RaidLogger:Post(0, nil, SYNC_COUNCIL, packedCouncil or self:PackLootCouncil())
end
function RaidLogger:LogBenched(player)
out("Logging bench for " .. ColorName(player))
RaidLoggerStore.activeRaid.players[player] = STATE_BENCHED
RaidLogger_RaidWindow_PlayersTab:Refresh()
end
function RaidLogger:LogAttended(player)
if RaidLoggerStore.activeRaid.players[player] ~= STATE_ATTENDED then
out("Logging attendance for " .. ColorName(player))
RaidLoggerStore.activeRaid.players[player] = STATE_ATTENDED
RaidLogger_RaidWindow_PlayersTab:Refresh()
RaidLogger_RaidWindow_LootTab:Refresh() -- refresh player list
end
end
function RaidLogger:LogNoShow(player)
out("Logging no-show for " .. ColorName(player))
RaidLoggerStore.activeRaid.players[player] = STATE_NOSHOW
RaidLogger_RaidWindow_PlayersTab:Refresh()
end
function RaidLogger:LogLateShow(player)
out("Logging late show for " .. ColorName(player))
RaidLoggerStore.activeRaid.players[player] = STATE_LATE
RaidLogger_RaidWindow_PlayersTab:Refresh()
end
function RaidLogger:RemoveFromLog(player)
out("Removing " .. ColorName(player) .. " from log")
RaidLoggerStore.activeRaid.players[player] = nil
RaidLogger_RaidWindow_PlayersTab:Refresh()
end
function RaidLogger:UpdateRaid(forceZone)
local raidSize = GetNumRaidMembers()
if raidSize == 0 then
out("Not in a raid!")
return
end
-- out("Updating raid...")
if not RaidLoggerStore.activeRaid then
RaidLogger:StartRaid();
end
if forceZone and #forceZone > 2 then
RaidLoggerStore.activeRaid.zone = forceZone
end
-- save zone
if not RaidLoggerStore.activeRaid.zone then
local zone = InTrackedInstance()
if zone then
RaidLoggerStore.activeRaid.zone = zone
RaidLogger_RaidWindow:Refresh()
out("Zone: " .. COLOR_INSTANCE .. zone)
else
err("Zone " .. COLOR_INSTANCE .. GetZoneText() .. "|r couldn't be identified!")
end
end
-- merge current player list with previous list
for i = 1, raidSize do
local name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(i)
if name then
RaidLogger:LogAttended(name)
RaidLoggerStore.players[name] = class
end
end
-- out("Attendance updated.")
end
function RaidLogger:ChooseLastRaid()
editRaidIndex = nil
if not RaidLoggerStore.activeRaid then
if RaidLoggerStore.raids and #RaidLoggerStore.raids > 0 then
editRaidIndex = #RaidLoggerStore.raids
editRaid = RaidLoggerStore.raids[editRaidIndex]
else
editRaid = nil
end
else
editRaid = RaidLoggerStore.activeRaid
end
if editRaid then
if not editRaid.players then editRaid.players = {} end
end
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- SYNC
function RaidLogger:OnAddonMessage(text, channel, sender, target)
sender = removeRealmName(sender)
if sender == UnitName("player") then return end
local parts = splitCsv(text)
debug("SYNC IN - ["..sender.."]: "..text)
local function VerifyLoot(parts)
if not RaidLoggerStore.activeRaid then
out("Couldn't set vote, no active raid")
return nil
end
local lootid = tonumber(parts[2])
local entry = self:FindEntry(lootid)
if not entry then
out("|cffffff00Couldn't find loot "..lootid)
nextSyncCheck = 1 -- sync now, if possible
return nil
end
if entry.itemString ~= parts[3] then
out("|cffffff00Wrong item found with id "..lootid..", expected "..parts[3].." but got "..entry.itemString)
nextSyncCheck = 1 -- sync now, if possible
return nil
end
return entry
end
if parts[1] == SYNC_LOOT then
-- 2-receiver, 3-itemString, 4-quantity, 5-ts, 6-index, 7-tradedTo, 8-status, 9-votes
if RaidLoggerStore.activeRaid then
local version = tonumber(parts[2])
if not version then
debug("|cffff0000"..sender.." is using an old version, ignoring loot message")
return
end
local t = time() - 10
-- local zone = #parts >= 12 and parts[12]
local _votes = parts[11]
local status = tonumber(parts[10])
local tradedTo = parts[9]
local lootid = tonumber(parts[8])
local ts = tonumber(parts[7])
local quantity = tonumber(parts[6])
local itemString = parts[5]
local who = parts[4]
local lootProgress = parts[3] -- 1/4 2/4 3/4 4/4
-- if zone and zone ~= RaidLoggerStore.activeRaid.zone then
-- return -- ignore loot reports from a different zone, it may have different loot count
-- end
local lootCountBefore = #RaidLoggerStore.activeRaid.loot
local shouldAdd = true
for i = #RaidLoggerStore.activeRaid.loot, 1, -1 do
local loggedItem = RaidLoggerStore.activeRaid.loot[i]
if loggedItem.lootid == lootid then
if loggedItem.itemString == itemString then
shouldAdd = false
debug("Found matching loot entry")
break -- found it
else
shouldAdd = false
out("|cffff0000Loot log isn't synced!")
outOfSync = true
break
end
end
end
if shouldAdd then
if tradedTo == "_" then tradedTo = nil end
local votes = {}
if _votes and #_votes > 0 then
local votesParts = {_G.string.split("|", _votes)}
for _, vote in ipairs(votesParts) do
local voteParts = {_G.string.split("-", vote)}
votes[voteParts[1]] = tonumber(voteParts[2])
end
end
debug("who="..who.." itemString="..itemString.." quantity="..quantity.." ts="..ts.." tradedTo="..tostring(tradedTo).." lootid="..(lootid or "nil"))
LogLoot(who, itemString, quantity, ts, tradedTo, votes, status, lootid)
end
local progressParts = {_G.string.split("/", lootProgress)}
if outOfSync and progressParts[1] == progressParts[2] then
syncingNow = false
-- last item, using lootCountBefore because item may have been sent to query
-- and RaidLoggerStore.activeRaid.loot hasn't changed yet
if lootCountBefore + 1 == tonumber(progressParts[2]) then
outOfSync = false
lootMismatchs = 0
firstSyncMismatch = 0
else
err("Resync ended, but we still don't have the same number of items as "..sender)
end
end
else
out("|cffffff00Received loot sync, but no active raid - ignoring")
end
elseif parts[1] == SYNC_COUNCIL then
local currentCouncil = self:PackLootCouncil()
if currentCouncil == parts[2] then return end -- council not changed
if not parts[2] or #parts[2] == 0 then
RaidLoggerStore.council = nil
out("Received loot council disable from "..sender)
else
RaidLoggerStore.council = {}
local names = {_G.string.split("|", parts[2])}
for _, name in pairs(names) do
RaidLoggerStore.council[name] = true
end
out("Received new loot council from "..sender..": "..parts[2])
RaidLogger_RaidWindow_LootTab:Refresh()
end
elseif parts[1] == SYNC_COUNCIL_WHO then
local currentCouncil = self:PackLootCouncil()
if #currentCouncil > 0 then
self:AnnounceLootCouncil(currentCouncil)
end
elseif parts[1] == SYNC_PING then
out("Received PING from "..sender)
self:Post(0, sender, SYNC_PONG, VERSION)
elseif parts[1] == SYNC_PONG then
out("Received PONG from |cff88ff00"..sender.."|r version |cff88ff00"..parts[2])
elseif parts[1] == SYNC_VOTE then
local entry = VerifyLoot(parts)
if not entry then return end
if entry.votes[sender] == tonumber(parts[4]) then return end -- vote already recorded
entry.votes[sender] = tonumber(parts[4])
local voteStr = "|cffff0000NO|r"
if entry.votes[sender] == 1 then voteStr = "|cff00ff00YES|r" end
out(sender.." voted "..voteStr.." to give "..entry.link.." to "..entry.tradedTo)
self:CheckVotes(entry)
elseif parts[1] == SYNC_SUGGEST then
local entry = VerifyLoot(parts)
if not entry then return end
if entry.tradedTo == parts[4] then return end -- tradeTo already recorded
entry.tradedTo = parts[4]
local row = self:FindRow(entry.lootid)
RaidLogger_RaidWindow_LootTab:TradedToChanged(row, entry)
if entry.tradedTo == DROPDOWN_DISENCHANT_NAME then
out(sender.." suggests to disenchant "..entry.link)
elseif entry.tradedTo == DROPDOWN_BANK_NAME then
out(sender.." suggests to send "..entry.link.." to guild bank")
else
out(sender.." suggests to give "..entry.link.." to "..entry.tradedTo)
end
elseif parts[1] == SYNC_CHECK then
if not parts[2] then
out("|cffffff00"..sender.." is using an old version of RaidLogger, this may lead to unexpected behaviour")
else
local ver = tonumber(parts[2])
if ver < VERSION then
out("|cffffff00"..sender.." is using an old version of RaidLogger ("..ver.."), this may lead to unexpected behaviour")
elseif ver > VERSION then
out("|cffffff00You are using an old version of RaidLogger, this may lead to unexpected behaviour!")
end
end
lootMismatchs = 0
self:ScheduleSyncCheck() -- reset check timer
if RaidLoggerStore.activeRaid and RaidLoggerStore.activeRaid.loot and not syncingNow then
self:Post(1, nil, SYNC_CHECK_REPLY, #RaidLoggerStore.activeRaid.loot, RaidLoggerStore.activeRaid.zone or "")
end
elseif parts[1] == SYNC_CHECK_REPLY then
local zone = parts[3]
local lootCount = tonumber(parts[2])
-- start a raid
if not RaidLoggerStore.activeRaid or RaidLoggerStore.activeRaid.zone ~= parts[3] or syncingNow then
return
end
if lootCount > #RaidLoggerStore.activeRaid.loot then -- and time() - lastSync > SYNC_COOLDOWN_SECONDS
lootMismatchs = lootMismatchs + 1
-- someone else has more loot than us!
if firstSyncMismatch == 0 then
-- check loot again in 10 sec to make sure it didn't just take time for loot msg to get to us
firstSyncMismatch = time()
self:ScheduleSyncCheckSoon()
debug("Not in sync first time, will check again soon")
else
outOfSync = true
out("|cffff0000WARNING: You are not in sync! "..sender.." has "..lootCount.." items while you have only "..tostring(#RaidLoggerStore.activeRaid.loot)..". Write |cffffff00/rl resync "..sender.."|cffff0000 to rebuild item list from "..sender)
end
end
elseif parts[1] == SYNC_RESEND then
self:ResendLoot(sender)
elseif parts[1] == SYNC_END then
if RaidLoggerStore.activeRaid then
RaidLogger:EndRaid()
end
end
end
function RaidLogger:FullResync(from)
syncingNow = true
lastSync = time()
RaidLoggerStore.activeRaid.loot = {}
self:Post(0, from, SYNC_RESEND)
end
function RaidLogger:ScheduleSyncCheck()
nextSyncCheck = time() + NEXT_SYNC_CHECK_MIN_SECONDS + math.random(NEXT_SYNC_CHECK_RANDOM_SECONDS)
end
function RaidLogger:ScheduleSyncCheckSoon()
nextSyncCheck = time() + NEXT_SYNC_CHECK_SOON_MIN_SECONDS + math.random(NEXT_SYNC_CHECK_SOON_RANDOM_SECONDS)
end
function RaidLogger:Post(delaySeconds, toWho, ...)
tinsert(RaidLoggerDelayedMessages, {
["time"] = time() + delaySeconds,
["msg"] = table.concat({...}, ","),
["to"] = toWho,
})
end
function RaidLogger:ResendLoot(sendTo, index)
if index and string.len(index) > 0 then
local entry = RaidLoggerStore.activeRaid.loot[tonumber(index)]
RaidLogger:PostLootEntry(entry, index.."/"..#RaidLoggerStore.activeRaid.loot, 1, sendTo)
else
for i, entry in ipairs(RaidLoggerStore.activeRaid.loot) do
RaidLogger:PostLootEntry(entry, i.."/"..#RaidLoggerStore.activeRaid.loot, i, sendTo)
end
end
end
function RaidLogger:CheckVotes(entry)
local raidPlayers = {}
for i = 1, GetNumGroupMembers() do
if UnitIsConnected("raid"..i) then
local name = UnitName("raid"..i)
raidPlayers[name] = true
end
end
local sum = 0
local max = 0
local veto = {}
for name, enabled in pairs(RaidLoggerStore.council) do
if enabled and raidPlayers[name] then
max = max + 1
if entry.votes[name] == 1 then
sum = sum + 1
elseif entry.votes[name] == 0 then
tinsert(veto, name)
end
end
end
if sum > max then
out("Very strange, got more votes than max possible for "..entry.link)
end
if sum >= max and entry.status ~= 1 then
entry.status = 1
out("Loot "..entry.link.." |cff00ff00AGREED|r to be given to "..entry.tradedTo.." with "..sum.." / "..max.." votes.")
elseif #veto > 0 then
entry.status = -1
end