forked from kakysha/HonorSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honorspy.lua
executable file
·421 lines (370 loc) · 13.1 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
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;
local VERSION = 1;
local paused = false; -- pause all inspections when user opens inspect frame
local playerName = UnitName("player");
local playerIsInGuild = GetGuildInfo("player") ~= nil
local callback = nil
function HonorSpy:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("HonorSpyDB", {
factionrealm = {
currentStandings = {},
last_reset = 0,
reset_day = 3,
sort = L["Honor"],
minimapButton = {hide = false}
}
}, true)
self:SecureHook("InspectUnit");
self:SecureHook("UnitPopup_ShowMenu");
self:RegisterEvent("PLAYER_TARGET_CHANGED");
self:RegisterEvent("UPDATE_MOUSEOVER_UNIT");
self:RegisterEvent("INSPECT_HONOR_UPDATE");
self:RegisterComm(commPrefix, "OnCommReceive")
self:RegisterEvent("PLAYER_DEAD");
DrawMinimapIcon();
HonorSpy:CheckNeedReset();
HonorSpyGUI:PrepareGUI()
PrintWelcomeMsg();
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);
if (paused or 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) then
return;
end
local player = self.db.factionrealm.currentStandings[inspectedPlayerName] or inspectedPlayers[inspectedPlayerName];
if (player.class == nil) then player.class = "nil" end
local _, _, _, _, 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 (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
self.db.factionrealm.currentStandings[inspectedPlayerName] = player;
broadcast(self:Serialize(inspectedPlayerName, player))
end
inspectedPlayers[inspectedPlayerName] = {last_checked = player.last_checked};
inspectedPlayerName = nil;
if callback then
callback()
callback = nil
end
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()
local t = { }
for playerName, player in pairs(HonorSpy.db.factionrealm.currentStandings) do
table.insert(t, {playerName, player.class, player.thisWeekHonor, player.lastWeekHonor, player.standing, player.RP, player.rank, player.last_checked})
end
local sort_column = 3; -- ThisWeekHonor
if (HonorSpy.db.factionrealm.sort == L["Rank"]) then sort_column = 6; end
table.sort(t, function(a,b)
return a[sort_column] > b[sort_column]
end)
return t
end
-- REPORT
function HonorSpy:GetBrackets(pool_size)
-- 1 2 3 4 5 6 7 8 9 10 11 12 13 14
local brk = {1, 0.858, 0.715, 0.587, 0.477, 0.377, 0.287, 0.207, 0.137, 0.077, 0.037, 0.017, 0.007, 0.002} -- brackets percentage
if (not pool_size) then
return brk
end
for i = 1,14 do
brk[i] = math.floor(brk[i]*pool_size+.5)
end
return brk
end
function HonorSpy:Estimate(playerOfInterest)
if (not playerOfInterest) then
playerOfInterest = playerName
end
playerOfInterest = string.upper(string.sub(playerOfInterest, 1, 1))..string.lower(string.sub(playerOfInterest, 2))
local standing = -1;
local t = HonorSpy:BuildStandingsTable()
local avg_lastchecked = 0;
local pool_size = #t;
for i = 1, pool_size do
if (playerOfInterest == t[i][1]) then
standing = i
end
end
if (standing == -1) then
return
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, 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.upper(string.sub(playerOfInterest, 1, 1))..string.lower(string.sub(playerOfInterest, 2))
local pool_size, standing, bracket, RP, EstRP, Rank, Progress, EstRank, EstProgress = HonorSpy:Estimate(playerOfInterest)
if (not standing) then
self:Print(format(L["Player %s not found in table"], playerOfInterest));
return
end
if (playerOfInterest ~= playerName) then
SendChatMessage(format("- HonorSpy: %s %s", L["Report for player"], playerOfInterest),"emote")
end
SendChatMessage(format("- HonorSpy: %s = %d, %s = %d, %s = %d, %s = %s, %s = %d", L["Pool Size"], pool_size, L["Standing"], standing, L["Bracket"], bracket, L["Current RP"], RP, L["Next Week RP"], EstRP), "emote")
SendChatMessage(format("- HonorSpy: %s = %d (%d%%), %s = %d (%d%%)", L["Current Rank"], Rank, Progress, L["Next Week Rank"], EstRank, EstProgress), "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 store_player(playerName, player)
if (player == nil) then return end
if (player.last_checked < HonorSpy.db.factionrealm.last_reset
or player.last_checked > GetServerTime()
or player.thisWeekHonor == 0
) then
return
end
local player = table.copy(player);
local localPlayer = HonorSpy.db.factionrealm.currentStandings[playerName];
if (localPlayer == nil or localPlayer.last_checked < player.last_checked) then
HonorSpy.db.factionrealm.currentStandings[playerName] = player;
end
end
function HonorSpy:OnCommReceive(prefix, message, distribution, sender)
local ok, playerName, player = self:Deserialize(message);
if (not ok) then
return;
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)
if (UnitInBattleground("player") ~= nil) then
HonorSpy:SendCommMessage(commPrefix, msg, "BATTLEGROUND");
else
HonorSpy:SendCommMessage(commPrefix, msg, "RAID");
end
if (playerIsInGuild) then
HonorSpy:SendCommMessage(commPrefix, msg, "GUILD");
end
end
-- Broadcast on death
local last_send_time = 0;
function HonorSpy:PLAYER_DEAD()
local filtered_players, count = {}, 0;
if (GetServerTime() - last_send_time < 5*60) then return end;
last_send_time = GetServerTime();
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))
filtered_players, count = {}, 0;
end
end
if (count > 0) then
broadcast(self:Serialize("filtered_players", filtered_players))
end
end
-- RESET WEEK
function HonorSpy:Purge()
inspectedPlayers = {};
HonorSpy.db.factionrealm.currentStandings={};
HonorSpyGUI:Reset();
HonorSpy:Print(L["All data was purged"]);
end
function resetWeek(must_reset_on)
HonorSpy.db.factionrealm.last_reset = must_reset_on;
HonorSpy:Purge()
HonorSpy:Print(L["Weekly data was reset"]);
end
function HonorSpy:CheckNeedReset()
local day = date("!%w", GetServerTime());
local h = date("!%H", GetServerTime());
local m = date("!%M", GetServerTime());
local s = date("!%S", GetServerTime());
local days_diff = (7 + (day - HonorSpy.db.factionrealm.reset_day)) - math.floor((7 + (day - HonorSpy.db.factionrealm.reset_day))/7) * 7;
local diff_in_seconds = s + m*60 + h*60*60 + days_diff*24*60*60 - 10*60*60 - 1; -- 10 AM UTC - fixed hour of PvP maintenance
if (diff_in_seconds > 0) then -- it is negative on reset_day untill 10AM
local must_reset_on = GetServerTime()-diff_in_seconds;
if (must_reset_on > HonorSpy.db.factionrealm.last_reset) then resetWeek(must_reset_on) end
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:AddLine(format("%s", addonName));
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|cff209f9b", GetAddOnMetadata("HonorSpy", "Version"))
if (realm == "Flamelash" 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 gold tip or kind word!", realm)
end
HonorSpy:Print(msg .. "|r")
end