forked from kakysha/HonorSpy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhonorspy.lua
executable file
·1074 lines (919 loc) · 32.4 KB
/
honorspy.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
HonorSpy = LibStub("AceAddon-3.0"):NewAddon("HonorSpy", "AceConsole-3.0", "AceHook-3.0", "AceEvent-3.0", "AceComm-3.0", "AceSerializer-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("HonorSpy", true)
local addonName = GetAddOnMetadata("HonorSpy", "Title");
local commPrefix = addonName .. "4";
local paused = false; -- pause all inspections when user opens inspect frame
local playerName = UnitName("player");
local callback = nil
local nameToTest = nil
local startRemovingFakes = false
function HonorSpy:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("HonorSpyDB", {
factionrealm = {
currentPlayerNumber = 0,
currentStandings = {},
lastStandings = {},
last_reset = 0,
minimapButton = {hide = false},
actualCommPrefix = "",
fakePlayers = {},
corruptPlayers = {},
goodPlayers = {},
custom = {
enable = false,
standing = 0,
player_number = 0
}
},
char = {
today_kills = {},
estimated_honor = 0,
original_honor = 0
}
}, true)
self:SecureHook("InspectUnit");
self:SecureHook("UnitPopup_ShowMenu");
self:RegisterEvent("PLAYER_TARGET_CHANGED");
self:RegisterEvent("UPDATE_MOUSEOVER_UNIT");
self:RegisterEvent("INSPECT_HONOR_UPDATE");
self:RegisterEvent("CHAT_MSG_COMBAT_HONOR_GAIN", CHAT_MSG_COMBAT_HONOR_GAIN_EVENT);
ChatFrame_AddMessageEventFilter("CHAT_MSG_COMBAT_HONOR_GAIN", CHAT_MSG_COMBAT_HONOR_GAIN_FILTER);
self:RegisterComm(commPrefix, "OnCommReceive")
self:RegisterEvent("PLAYER_DEAD");
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", FAKE_PLAYERS_FILTER);
DrawMinimapIcon();
HS_wait(5, function() HonorSpy:CheckNeedReset() end)
HonorSpyGUI:PrepareGUI()
PrintWelcomeMsg();
DBHealthCheck()
end
local inspectedPlayers = {}; -- stores last_checked time of all players met
local inspectedPlayerName = nil; -- name of currently inspected player
local function StartInspecting(unitID)
local name, realm = UnitName(unitID);
local level = UnitLevel(unitID);
if (paused or (realm and realm ~= "")) then
return
end
if (name ~= inspectedPlayerName) then -- changed target, clear currently inspected player
ClearInspectPlayer();
inspectedPlayerName = nil;
end
if (name == nil
or name == inspectedPlayerName
or not UnitIsPlayer(unitID)
or not UnitIsFriend("player", unitID)
or not CheckInteractDistance(unitID, 1)
or not CanInspect(unitID)) then
return
end
local player = HonorSpy.db.factionrealm.currentStandings[name] or inspectedPlayers[name];
if (player == nil) then
inspectedPlayers[name] = {last_checked = 0};
player = inspectedPlayers[name];
end
if (GetServerTime() - player.last_checked < 30) then -- 30 seconds until new inspection request
return
end
-- we gonna inspect new player, clear old one
ClearInspectPlayer();
inspectedPlayerName = name;
player.unitID = unitID;
NotifyInspect(unitID);
RequestInspectHonorData();
_, player.rank = GetPVPRankInfo(UnitPVPRank(player.unitID)); -- rank must be get asap while mouse is still over a unit
_, player.class = UnitClass(player.unitID); -- same
end
function HonorSpy:INSPECT_HONOR_UPDATE()
if (inspectedPlayerName == nil or paused or not HasInspectHonorData()) then
return;
end
local player = self.db.factionrealm.currentStandings[inspectedPlayerName] or inspectedPlayers[inspectedPlayerName];
if (player == nil) then return end
if (player.class == nil) then player.class = "nil" end
local todayHK, _, _, _, thisweekHK, thisWeekHonor, _, lastWeekHonor, standing = GetInspectHonorData();
player.thisWeekHonor = thisWeekHonor;
player.lastWeekHonor = lastWeekHonor;
player.standing = standing;
player.rankProgress = GetInspectPVPRankProgress();
ClearInspectPlayer();
NotifyInspect("target"); -- change real target back to player's target, broken by prev NotifyInspect call
ClearInspectPlayer();
player.last_checked = GetServerTime();
player.RP = 0;
if (todayHK >= 15 or thisweekHK >= 15) then
if (player.rank >= 3) then
player.RP = math.ceil((player.rank-2) * 5000 + player.rankProgress * 5000)
elseif (player.rank == 2) then
player.RP = math.ceil(player.rankProgress * 3000 + 2000)
end
if (lastPlayer and lastPlayer.honor == thisWeekHonor and lastPlayer.name ~= inspectedPlayerName) then
return
end
lastPlayer = {name = inspectedPlayerName, honor = thisWeekHonor}
store_player(inspectedPlayerName, player)
broadcast(self:Serialize(inspectedPlayerName, player))
else
self.db.factionrealm.currentStandings[inspectedPlayerName] = nil
end
inspectedPlayers[inspectedPlayerName] = {last_checked = player.last_checked};
inspectedPlayerName = nil;
if callback then
callback()
callback = nil
end
end
-- parse message
-- COMBATLOG_HONORGAIN = "%s dies, honorable kill Rank: %s (Estimated Honor Points: %d)";
-- COMBATLOG_HONORAWARD = "You have been awarded %d honor points.";
local function parseHonorMessage(msg)
local honor_gain_pattern = string.gsub(COMBATLOG_HONORGAIN, "%(", "%%(")
honor_gain_pattern = string.gsub(honor_gain_pattern, "%)", "%%)")
honor_gain_pattern = string.gsub(honor_gain_pattern, "(%%s)", "(.+)")
honor_gain_pattern = string.gsub(honor_gain_pattern, "(%%d)", "(%%d+)")
local victim, rank, est_honor = msg:match(honor_gain_pattern)
if (victim) then
est_honor = math.max(0, math.floor(est_honor * (1-0.10*((HonorSpy.db.char.today_kills[victim] or 1)-1)) + 0.5))
end
local honor_award_pattern = string.gsub(COMBATLOG_HONORAWARD, "(%%d)", "(%%d+)")
local awarded_honor = msg:match(honor_award_pattern)
return victim, est_honor, awarded_honor
end
-- this is called before filter
function CHAT_MSG_COMBAT_HONOR_GAIN_EVENT(e, msg)
local victim, _, awarded_honor = parseHonorMessage(msg)
if victim then
HonorSpy.db.char.today_kills[victim] = (HonorSpy.db.char.today_kills[victim] or 0) + 1
local _, est_honor = parseHonorMessage(msg)
HonorSpy.db.char.estimated_honor = HonorSpy.db.char.estimated_honor + est_honor
elseif awarded_honor then
HonorSpy.db.char.estimated_honor = HonorSpy.db.char.estimated_honor + awarded_honor
end
end
-- this is called after eventg ww
function CHAT_MSG_COMBAT_HONOR_GAIN_FILTER(_s, e, msg, ...)
HonorSpy:CheckNeedReset()
local victim, est_honor, awarded_honor = parseHonorMessage(msg)
if (not victim) then
return
end
return false, format("%s %s: %d, %s: |cff00FF96%d", msg, L["Today Kills"], HonorSpy.db.char.today_kills[victim] or 0, L["Estimated Honor"], est_honor), ...
end
-- INSPECT HOOKS pausing to not mess with native inspect calls
-- pause when use opens target right click menu, as it breaks "inspect" button sometimes
function HonorSpy:UnitPopup_ShowMenu(s, menu, frame, name, id)
if (menu == "PLAYER" and not self:IsHooked(_G["DropDownList1"], "OnHide")) then
self:SecureHookScript(_G["DropDownList1"], "OnHide", "CloseDropDownMenu")
paused = true
return
end
end
function HonorSpy:CloseDropDownMenu()
self:Unhook(_G["DropDownList1"], "OnHide")
paused = false
end
-- pause when use opens inspect frame
function HonorSpy:InspectUnit(unitID)
paused = true;
if (not self:IsHooked(InspectFrame, "OnHide")) then
self:SecureHookScript(InspectFrame, "OnHide", "InspectFrameClose");
end
end
function HonorSpy:InspectFrameClose()
paused = false;
end
-- INSPECTION TRIGGERS
function HonorSpy:UPDATE_MOUSEOVER_UNIT()
StartInspecting("mouseover")
end
function HonorSpy:PLAYER_TARGET_CHANGED()
StartInspecting("target")
end
function HonorSpy:UpdatePlayerData(cb)
if (paused) then
return
end
callback = cb
StartInspecting("player")
end
-- CHAT COMMANDS
local options = {
name = 'HonorSpy',
type = 'group',
args = {
show = {
type = 'execute',
name = L['Show HonorSpy Standings'],
desc = L['Show HonorSpy Standings'],
func = function() HonorSpyGUI:Toggle() end
},
search = {
type = 'input',
name = L['Report specific player standings'],
desc = L['Report specific player standings'],
usage = L['player_name'],
get = false,
set = function(info, playerName) HonorSpy:Report(playerName) end
},
}
}
LibStub("AceConfig-3.0"):RegisterOptionsTable("HonorSpy", options, {"honorspy", "hs"})
function HonorSpy:BuildStandingsTable(sort_by)
local t = { }
local sort_type = 'desc';
local sort_column = 3; -- ThisWeekHonor
if (sort_by == L["Standing"]) then
sort_column = 5;
sort_type = 'asc';
end
if (sort_by == L["Rank"]) then
sort_column = 7;
sort_type = 'desc';
end
for playerName, player in pairs(HonorSpy.db.factionrealm.currentStandings) do
-- Add today honor
thisWeekHonor = player.thisWeekHonor or 0;
if (playerName == UnitName("player")) then
thisWeekHonor = HonorSpy.db.char.estimated_honor;
end
table.insert(t, {playerName, player.class, thisWeekHonor, player.lastWeekHonor or 0, player.standing or 0, player.RP or 0, player.rank or 0, player.last_checked or 0});
end
local sort_func_desc = function(a,b)
return a[sort_column] > b[sort_column];
end
local sort_func_asc = function(a,b)
return a[sort_column] < b[sort_column];
end
if (sort_type == 'desc') then
table.sort(t, sort_func_desc);
else
table.sort(t, sort_func_asc);
end
if (sort_type == 'asc') then
for i = #t, 1, -1 do
if (t[i][sort_column] == 0) then
table.remove(t, i);
end
end
end
return t
end
-- CUSTOM
function HonorSpy:CustomSet(standingValue, totalPlayerValue)
if ( tonumber(standingValue) == nil
or tonumber(totalPlayerValue) == nil
or tonumber(standingValue) <= 0
or tonumber(totalPlayerValue) <= 0
or tonumber(standingValue) > tonumber(totalPlayerValue)
) then
HonorSpy:Print(colorize(L['Must be an integer greater than zero'], "ORANGE"))
return
end
HonorSpy.db.factionrealm.custom.enable = true;
HonorSpy.db.factionrealm.custom.standing = tonumber(standingValue);
HonorSpy.db.factionrealm.custom.player_number = tonumber(totalPlayerValue);
end
function HonorSpy:CustomReset()
HonorSpy.db.factionrealm.custom.enable = false;
HonorSpy.db.factionrealm.custom.standing = 0;
HonorSpy.db.factionrealm.custom.player_number = 0;
end
-- REPORT
function HonorSpy:GetPoolSize(pool_size)
if (HonorSpy.db.factionrealm.custom.enable == true) then
pool_size = HonorSpy.db.factionrealm.custom.player_number
else
local currentPlayerNumber = HonorSpy.db.factionrealm.currentPlayerNumber;
if (currentPlayerNumber and type(currentPlayerNumber) == "number" and currentPlayerNumber > pool_size) then
pool_size = currentPlayerNumber;
end
end
return pool_size;
end
function HonorSpy:GetBracketsByStanding(pool_size)
-- 1 2 3 4 5 6 7 8 9 10 11 12 13 14
local brk = {1, 0.845, 0.697, 0.566, 0.436, 0.327, 0.228, 0.159, 0.100, 0.060, 0.035, 0.020, 0.008, 0.003} -- brackets percentage
local brackets = {}
if (not pool_size) then
return brk
end
pool_size = HonorSpy:GetPoolSize(pool_size);
for i = 14, 1, -1 do
standing = math.floor(brk[i]*pool_size+.5);
honor = HonorSpy:EstimateQuery('Standing', standing);
table.insert(brackets, {i, honor or 0, standing or 0})
end
return brackets
end
function HonorSpy:GetBrackets(pool_size)
-- 1 2 3 4 5 6 7 8 9 10 11 12 13 14
local brk = {1, 0.845, 0.697, 0.566, 0.436, 0.327, 0.228, 0.159, 0.100, 0.060, 0.035, 0.020, 0.008, 0.003} -- brackets percentage
if (not pool_size) then
return brk
end
pool_size = HonorSpy:GetPoolSize(pool_size);
for i = 1,14 do
brk[i] = math.floor(brk[i]*pool_size+.5)
end
return brk
end
function HonorSpy:EstimateQuery(queryType, queryValue)
local tableCurr = { }
local tableLast = { }
local prevRowValue = 0;
for playerName, player in pairs(HonorSpy.db.factionrealm.currentStandings) do
table.insert(tableCurr, {playerName, player.lastWeekHonor or 0, player.standing or 0})
end
for playerName, player in pairs(HonorSpy.db.factionrealm.lastStandings) do
table.insert(tableLast, {playerName, player.lastWeekHonor or 0, player.standing or 0})
end
if (queryType == 'Honor') then
query_column = 2;
get_column = 3;
sort_column = 2;
else
query_column = 3;
get_column = 2;
sort_column = 3;
end
local sort_func_asc = function(a, b)
return a[sort_column] < b[sort_column]
end
table.sort(tableCurr, sort_func_asc)
table.sort(tableLast, sort_func_asc)
local lastRowValue = 0;
local lastRowValueDiff = 0;
for i = 1, #tableLast do
if (tableLast[i][query_column] > 0 and queryValue <= tableLast[i][query_column]) then
lastRowValue = tableLast[i][get_column]
lastRowValueDiff = math.abs(prevRowValue - tableLast[i][query_column]);
break
end
prevRowValue = tableLast[i][query_column];
end
local currRowValue = 0;
local currRowValueDiff = 0;
for i = 1, #tableCurr do
if (tableCurr[i][query_column] > 0 and queryValue <= tableCurr[i][query_column]) then
currRowValue = tableCurr[i][get_column]
currRowValueDiff = math.abs(prevRowValue - tableCurr[i][query_column]);
break
end
prevRowValue = tableCurr[i][query_column];
end
if (lastRowValue == 0 and currRowValue == 0) then
return false
end
if (lastRowValue > 0 and currRowValue > 0) then
if (lastRowValueDiff < currRowValueDiff) then
return lastRowValue;
else
return currRowValue;
end
else
if (lastRowValue > 0) then
return lastRowValue;
end
if (currRowValue > 0) then
return currRowValue;
end
end
end
function HonorSpy:Estimate(playerOfInterest)
local isPlayer = false
if (not playerOfInterest) then
isPlayer = true
playerOfInterest = playerName
end
playerOfInterest = string.utf8upper(string.utf8sub(playerOfInterest, 1, 1))..string.utf8lower(string.utf8sub(playerOfInterest, 2))
local t = HonorSpy:BuildStandingsTable()
local standing = -1;
local index = -1;
local avg_lastchecked = 0;
local pool_size = #t;
for i = 1, pool_size do
if (playerOfInterest == t[i][1]) then
index = i
end
end
if (index == -1) then
return
end;
if (isPlayer == true and HonorSpy.db.factionrealm.custom.enable == true) then
standing = HonorSpy.db.factionrealm.custom.standing
else
local thisWeekHonor = HonorSpy.db.factionrealm.currentStandings[playerOfInterest].thisWeekHonor;
-- Add today honor
if (playerOfInterest == UnitName("player")) then
thisWeekHonor = HonorSpy.db.char.estimated_honor;
end
local estStanding = HonorSpy:EstimateQuery('Honor', thisWeekHonor);
if (estStanding == false) then
standing = index;
else
standing = estStanding;
end
end
local RP = {0, 400} -- RP for each bracket
local Ranks = {0, 2000} -- RP for each rank
local bracket = 1;
local inside_br_progress = 0;
local brk = self:GetBrackets(pool_size)
for i = 2,14 do
if (standing > brk[i]) then
inside_br_progress = (brk[i-1] - standing)/(brk[i-1] - brk[i])
break
end;
bracket = i;
end
if (bracket == 14 and standing == 1) then inside_br_progress = 1 end;
for i = 3,14 do
RP[i] = (i-2) * 1000;
Ranks[i] = (i-2) * 5000;
end
local award = RP[bracket] + 1000 * inside_br_progress;
local RP = HonorSpy.db.factionrealm.currentStandings[playerOfInterest].RP;
local EstRP = math.floor(RP*0.8+award+.5);
local Rank = HonorSpy.db.factionrealm.currentStandings[playerOfInterest].rank;
local EstRank = 14;
local Progress = math.floor(HonorSpy.db.factionrealm.currentStandings[playerOfInterest].rankProgress*100);
local EstProgress = math.floor((EstRP - math.floor(EstRP/5000)*5000) / 5000*100);
for i = 3,14 do
if (EstRP < Ranks[i]) then
EstRank = i-1;
break;
end
end
return pool_size, index, standing, bracket, RP, EstRP, Rank, Progress, EstRank, EstProgress
end
function HonorSpy:Report(playerOfInterest, skipUpdate)
if (not playerOfInterest) then
playerOfInterest = playerName
end
if (playerOfInterest == playerName) then
HonorSpy:UpdatePlayerData() -- will update for next time, this report gonna be for old data
end
playerOfInterest = string.utf8upper(string.utf8sub(playerOfInterest, 1, 1))..string.utf8lower(string.utf8sub(playerOfInterest, 2))
local pool_size, index, standing, bracket, RP, EstRP, Rank, Progress, EstRank, EstProgress = HonorSpy:Estimate(playerOfInterest)
if (not index) then
self:Print(format(L["Player %s not found in table"], playerOfInterest));
return
end
local text = "- HonorSpy: "
if (playerOfInterest ~= playerName) then
text = text .. format("%s <%s>: ", L['Progress of'], playerOfInterest)
end
text = text .. format("%s = %d, %s = %d, %s = %d, %s = %d (%d%%), %s = %d (%d%%)", L["Estimated Standing"], standing, L["Bracket"], bracket, L["Next Week RP"], EstRP, L["Rank"], Rank, Progress, L["Next Week Rank"], EstRank, EstProgress)
SendChatMessage(text, "emote")
end
-- SYNCING --
function table.copy(t)
local u = { }
for k, v in pairs(t) do u[k] = v end
return setmetatable(u, getmetatable(t))
end
function class_exist(className)
if className == "WARRIOR" or
className == "PRIEST" or
className == "SHAMAN" or
className == "WARLOCK" or
className == "MAGE" or
className == "ROGUE" or
className == "HUNTER" or
className == "PALADIN" or
className == "DRUID" then
return true
end
return false
end
function playerIsValid(playerName, player)
if (not player.last_checked or type(player.last_checked) ~= "number"
or player.last_checked > GetServerTime()
or not player.thisWeekHonor or type(player.thisWeekHonor) ~= "number"
or not player.lastWeekHonor or type(player.lastWeekHonor) ~= "number"
or not player.standing or type(player.standing) ~= "number"
or not player.RP or type(player.RP) ~= "number"
or not player.rankProgress or type(player.rankProgress) ~= "number"
or not player.rank or type(player.rank) ~= "number"
or not player.class or not class_exist(player.class)
) then
return false
end
local lastPlayer = HonorSpy.db.factionrealm.lastStandings[playerName];
if (lastPlayer ~= nil) then
if(lastPlayer.lastWeekHonor == player.lastWeekHonor and lastPlayer.standing == player.standing) then
return false
end
else
if (player.last_checked < HonorSpy.db.factionrealm.last_reset + 24*60*60 or player.thisWeekHonor == 0) then
return false
end
end
return true
end
function isFakePlayer(playerName)
if (HonorSpy.db.factionrealm.fakePlayers[playerName]) then
return true
end
return false
end
function store_player(playerName, player)
if (player == nil or playerName == nil or playerName:find("[%d%p%s%c%z]") or isFakePlayer(playerName) or not playerIsValid(playerName, player)) then return end
local player = table.copy(player);
local corruptPlayerCheck = HonorSpy.db.factionrealm.corruptPlayers[playerName];
if (corruptPlayerCheck ~= nil) then
if (corruptPlayerCheck >= player.last_checked) then
return
else
HonorSpy.db.factionrealm.corruptPlayers[playerName] = nil;
end
end
local localPlayer = HonorSpy.db.factionrealm.currentStandings[playerName];
if (localPlayer == nil or localPlayer.last_checked < player.last_checked) then
HonorSpy.db.factionrealm.currentStandings[playerName] = player;
HonorSpy:TestNextFakePlayer();
if (player.standing > HonorSpy.db.factionrealm.currentPlayerNumber) then
HonorSpy.db.factionrealm.currentPlayerNumber = player.standing;
end
end
end
function HonorSpy:OnCommReceive(prefix, message, distribution, sender)
if (distribution ~= "GUILD" and UnitRealmRelationship(sender) ~= 1) then
return -- discard any message from players from different servers (on x-realm BGs)
end
local ok, playerName, player = self:Deserialize(message);
if (not ok) then
return;
end
if (sender == UnitName("player")) then
return; -- Ignore broadcast messages from myself
end
if (playerName == "filtered_players") then
for playerName, player in pairs(player) do
store_player(playerName, player);
end
return
end
store_player(playerName, player);
end
function broadcast(msg, skip_yell)
if (IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and IsInInstance()) then
HonorSpy:SendCommMessage(commPrefix, msg, "INSTANCE_CHAT");
elseif (IsInRaid()) then
HonorSpy:SendCommMessage(commPrefix, msg, "RAID");
end
if (GetGuildInfo("player") ~= nil) then
HonorSpy:SendCommMessage(commPrefix, msg, "GUILD");
end
if (not skip_yell) then
HonorSpy:SendCommMessage(commPrefix, msg, "YELL");
end
end
-- Broadcast on death
local last_send_time = 0;
function HonorSpy:PLAYER_DEAD()
local filtered_players, count = {}, 0;
if (time() - last_send_time < 10*60) then return end;
last_send_time = time();
for playerName, player in pairs(self.db.factionrealm.currentStandings) do
filtered_players[playerName] = player;
count = count + 1;
if (count == 10) then
broadcast(self:Serialize("filtered_players", filtered_players), true)
filtered_players, count = {}, 0;
end
end
if (count > 0) then
broadcast(self:Serialize("filtered_players", filtered_players), true)
end
end
function FAKE_PLAYERS_FILTER(_s, e, msg, ...)
-- not found, fake
if (msg == ERR_FRIEND_NOT_FOUND) then
if (not nameToTest) then
return true
end
HonorSpy.db.factionrealm.currentStandings[nameToTest] = nil
HonorSpy.db.factionrealm.fakePlayers[nameToTest] = true
HonorSpy.db.factionrealm.goodPlayers[nameToTest] = nil
-- HonorSpy:Print("removed non-existing player", nameToTest)
nameToTest = nil
return true
end
-- added or was in friends already, not fake
local friend = msg:match(string.gsub(ERR_FRIEND_ADDED_S, "(%%s)", "(.+)"))
if (not friend) then
friend = msg:match(string.gsub(ERR_FRIEND_ALREADY_S, "(%%s)", "(.+)"))
end
if (friend) then
HonorSpy.db.factionrealm.goodPlayers[friend] = true
HonorSpy.db.factionrealm.fakePlayers[friend] = nil
if (friend == nameToTest) then
HonorSpy:removeTestedFriends()
nameToTest = nil
end
return true
end
end
function HonorSpy:removeTestedFriends()
local limit = C_FriendList.GetNumFriends()
if (type(limit) ~= "number") then
return
end
for i = 1, limit do
local f = C_FriendList.GetFriendInfoByIndex(i)
if (f.notes == "HonorSpy testing") then
C_FriendList.RemoveFriend(f.name)
end
end
end
function HonorSpy:TestNextFakePlayer()
if (nameToTest or not startRemovingFakes) then return end
for playerName, player in pairs(HonorSpy.db.factionrealm.currentStandings) do
if (not HonorSpy.db.factionrealm.fakePlayers[playerName] and not HonorSpy.db.factionrealm.goodPlayers[playerName] and playerName ~= UnitName("player")) then
nameToTest = playerName
break
end
end
if (nameToTest) then
C_FriendList.AddFriend(nameToTest, "HonorSpy testing")
HS_wait(1, function() HonorSpy:TestNextFakePlayer() end)
end
end
-- RESET WEEK
function HonorSpy:Purge(isClick)
inspectedPlayers = {};
if (isClick == true) then
HonorSpy.db.factionrealm.lastStandings={};
else
HonorSpy.db.factionrealm.lastStandings=HonorSpy.db.factionrealm.currentStandings;
end
HonorSpy.db.factionrealm.currentStandings={};
HonorSpy.db.factionrealm.fakePlayers={};
HonorSpy.db.factionrealm.corruptPlayers={};
HonorSpy.db.factionrealm.currentPlayerNumber = 0;
HonorSpy.db.char.original_honor = 0;
HonorSpyGUI:Reset();
HonorSpy:Print(L["All data was purged"]);
end
function getResetTime()
local currentUnixTime = GetServerTime()
local regionId = GetCurrentRegion()
local resetDay = 3 -- wed
local resetHour = 7 -- 7 AM UTC
if (regionId == 1) then -- US + BR + Oceania: 3 PM UTC Tue (7 AM PST Tue)
resetDay = 2
resetHour = 15
elseif (regionId == 2 or regionId == 4 or regionId == 5) then -- Korea, Taiwan, China: 10 PM UTC Mon (7 AM KST Tue)
resetDay = 1
resetHour = 22
elseif (regionId == 3) then -- EU + RU: 7 AM UTC Wed (7 AM UTC Wed)
end
local day = date("!%w", currentUnixTime);
local h = date("!%H", currentUnixTime);
local m = date("!%M", currentUnixTime);
local s = date("!%S", currentUnixTime);
local reset_seconds = resetDay*24*60*60 + resetHour*60*60 -- reset time in seconds from week start
local now_seconds = s + m*60 + h*60*60 + day*24*60*60 -- seconds passed from week start
local week_start = currentUnixTime - now_seconds
local must_reset_on = 0
if (now_seconds - reset_seconds > 0) then -- we passed this week reset time
must_reset_on = week_start + reset_seconds
else -- we not yet passed the reset moment in this week, still on prev week reset time
must_reset_on = week_start - 7*24*60*60 + reset_seconds
end
return must_reset_on
end
function HonorSpy:ResetWeek(isClick)
HonorSpy.db.factionrealm.last_reset = getResetTime();
HonorSpy:Purge(isClick)
HonorSpy:Print(L["Weekly data was reset"]);
end
function HonorSpy:CheckNeedReset(skipUpdate)
if (not skipUpdate) then
HonorSpy:UpdatePlayerData(function() HonorSpy:CheckNeedReset(true) end)
end
-- reset weekly standings
local must_reset_on = getResetTime()
if (HonorSpy.db.factionrealm.last_reset ~= must_reset_on) then
HonorSpy:ResetWeek()
HonorSpy.db.char.original_honor = 0
HonorSpy.db.char.estimated_honor = 0
HonorSpy.db.char.today_kills = {}
end
-- reset daily honor
if (HonorSpy.db.factionrealm.currentStandings[playerName] and HonorSpy.db.char.original_honor ~= HonorSpy.db.factionrealm.currentStandings[playerName].thisWeekHonor) then
HonorSpy.db.char.original_honor = HonorSpy.db.factionrealm.currentStandings[playerName].thisWeekHonor
HonorSpy.db.char.estimated_honor = HonorSpy.db.char.original_honor
HonorSpy.db.char.today_kills = {}
end
end
function HonorSpy:RemoveCorrupt()
local minDataLength = 500
local lastStandings = { }
local currentStandings = { }
for playerName, player in pairs(HonorSpy.db.factionrealm.lastStandings) do
if (player.standing > 0 and player.lastWeekHonor > 0) then
table.insert(lastStandings, {playerName, player.lastWeekHonor or 0, player.standing or 0});
end
end
for playerName, player in pairs(HonorSpy.db.factionrealm.currentStandings) do
if (player.standing > 0 and player.lastWeekHonor > 0) then
table.insert(currentStandings, {playerName, player.lastWeekHonor or 0, player.standing or 0});
end
end
-- Sort
local sort_func_asc = function(a, b)
return a[3] > b[3]
end
if (#lastStandings >= minDataLength) then
table.sort(lastStandings, sort_func_asc)
RemoveRepeatData(lastStandings, HonorSpy.db.factionrealm.lastStandings);
RemoveCorruptData(lastStandings, HonorSpy.db.factionrealm.lastStandings);
RemoveBottomData(lastStandings, HonorSpy.db.factionrealm.lastStandings);
end
if (#currentStandings < minDataLength) then
HonorSpy:Print(L["Requires more than 500 data table records"])
else
table.sort(currentStandings, sort_func_asc)
RemoveRepeatData(currentStandings, HonorSpy.db.factionrealm.currentStandings);
RemoveCorruptData(currentStandings, HonorSpy.db.factionrealm.currentStandings);
RemoveBottomData(currentStandings, HonorSpy.db.factionrealm.currentStandings);
HonorSpy:Print(L["Remove all corrupt data"])
end
end
-- Minimap icon
function DrawMinimapIcon()
LibStub("LibDBIcon-1.0"):Register("HonorSpy", LibStub("LibDataBroker-1.1"):NewDataObject("HonorSpy",
{
type = "data source",
text = addonName,
icon = "Interface\\Icons\\Inv_Misc_Bomb_04",
OnClick = function(self, button)
if (button == "RightButton") then
HonorSpy:Report()
elseif (button == "MiddleButton") then
HonorSpy:Report(UnitIsPlayer("target") and UnitName("target") or nil)
else
HonorSpy:CheckNeedReset()
HonorSpyGUI:Toggle()
end
end,
OnTooltipShow = function(tooltip)
tooltip:AddDoubleLine(format("%s", addonName), format("|cff777777v%s", GetAddOnMetadata(addonName, "Version")));
tooltip:AddLine("|cff777777by Kakysha|r");
tooltip:AddLine("|cFFCFCFCFLeft Click: |r" .. L['Show HonorSpy Standings']);
tooltip:AddLine("|cFFCFCFCFMiddle Click: |r" .. L['Report Target']);
tooltip:AddLine("|cFFCFCFCFRight Click: |r" .. L['Report Me']);
end
}), HonorSpy.db.factionrealm.minimapButton);
end
function PrintWelcomeMsg()
local realm = GetRealmName()
local faction = UnitFactionGroup("player")
local msg = format("|cffAAAAAAversion: %s, bugs & features: github.com/kakysha/honorspy|r\n", GetAddOnMetadata(addonName, "Version"))
if (realm == "Earthshaker" and faction == "Horde") then
msg = msg .. format("You are lucky enough to play with HonorSpy author on one |cffFFFFFF%s |cff209f9brealm! Feel free to mail me (|cff8787edKakysha|cff209f9b) a supportive %s tip or kind word!", realm, GetCoinTextureString(50000))
end
msg = msg .. "欢迎使用 |cff8787edBinkcn|cffFFFFFF 修改版本,该版本针对国服优化,并且可以在仅具备较少本地数据的情况下计算出更准确的下周军衔。"
msg = msg .. "此版本Bug反馈及功能建议请移步:\nhttps://github.com/Binkcn/HonorSpy"
HonorSpy:Print(msg .. "|r")
end
function RemoveRepeatData(tableData, tableStandings)
local _playerName = nil;
local _playerHonor = nil;
local _playerStanding = nil;
local repeatHonor, repearStanding = nil, nil
if (#tableData <= 100) then
return
end
for i=#tableData, 2, -1 do
if ( tableData[i][2] == tableData[i-1][2] and tableData[i][3] == tableData[i-1][3] ) then
repeatHonor = tableData[i][2]
repearStanding = tableData[i][3]
-- remove repeat data.
local j = i
while (tableData[j][2] == repeatHonor and tableData[j][3] == repearStanding) do
_playerName = tableData[j][1]
_playerHonor = tableData[j][2]
_playerStanding = tableData[j][3]
tableStandings[_playerName] = nil
HonorSpy.db.factionrealm.corruptPlayers[_playerName] = GetServerTime();
HonorSpy:Print(format("%s:|cffAAAAAA%s|cffFFFFFF, %s:%s, %s:%s", L["Remove repeat data"], _playerName, L["LstWkHonor"], _playerHonor, L["Standing"], _playerStanding ))
table.remove(tableData, j);
-- move next
if (j >= 2) then
j = j-1
end
end
-- reset array index
i = j
end
end
end
function RemoveCorruptData(tableData, tableStandings)
local _playerName = nil;
local _playerHonor = nil;
local _playerStanding = nil;
if (#tableData <= 100) then
return
end
for i=(#tableData-1), 10, -1 do
_playerName = tableData[i][1]
_playerHonor = tableData[i][2]
_playerStanding = tableData[i][3]
if (tableData[i+1][2] >= tableData[i-1][2]) then
if (_playerHonor >= tableData[i-1][2] and _playerHonor <= tableData[i+1][2]) then
-- pass
else
tableStandings[_playerName] = nil
HonorSpy.db.factionrealm.corruptPlayers[_playerName] = GetServerTime();
HonorSpy:Print(format("%s:|cffAAAAAA%s|cffFFFFFF, %s:%s, %s:%s", L["Remove corrupt data"], _playerName, L["LstWkHonor"], _playerHonor, L["Standing"], _playerStanding ))
table.remove(tableData, i);
RemoveCorruptData(tableData, tableStandings)
break
end
end
end
end
function RemoveBottomData(tableData, tableStandings)
local _playerName = nil;
local _playerHonor = nil;
local _playerStanding = nil;
local minHonorThreshold = 100
local minHonorValue = 100
local maxStandingValue = 0
local checkData = false
if (#tableData <= 100) then
return
end
for i=100, 1, -1 do
_playerName = tableData[i][1]
_playerHonor = tableData[i][2]
_playerStanding = tableData[i][3]
if (checkData == false and _playerHonor <= minHonorThreshold) then
checkData = true
end