-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lua
164 lines (135 loc) · 4.46 KB
/
core.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
--- LFGHelper - prunes locked activities from the LFG window
-- @author: Sammy James
-- @copyright: MIT
local ADDON_NAME = ...
local ADDON_TITLE = select(2, GetAddOnInfo(ADDON_NAME))
local C_LFGList = C_LFGList
local _G = _G
local LFGHelper = LibStub("AceAddon-3.0"):NewAddon(ADDON_NAME, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0",
"AceHook-3.0")
LFGHelper.m_Lockouts = {}
local function StringHash(text)
local counter = 1
local len = string.len(text)
for i = 1, len, 3 do
counter = math.fmod(counter * 8161, 4294967279) + -- 2^32 - 17: Prime!
(string.byte(text, i) * 16776193) +
((string.byte(text, i + 1) or (len - i + 256)) * 8372226) +
((string.byte(text, i + 2) or (len - i + 256)) * 3932164)
end
return math.fmod(counter, 4294967291) -- 2^32 - 5: Prime (and different from the prime in the loop)
end
local function MakeKey(name, max_players, heroic)
return StringHash(name .. max_players .. tostring(heroic))
end
function LFGHelper:OnEnable()
self:RegisterEvent("PLAYER_ENTERING_WORLD", "OnEnteringWorld")
self:RegisterEvent("UPDATE_INSTANCE_INFO", "OnUpdateInstanceInfo")
self:RegisterEvent("LFG_LIST_AVAILABILITY_UPDATE", "OnLfgAvailable")
self:ScheduleRepeatingTimer("UpdateLockoutData", 60)
self:RegisterHook()
self:UpdateLockoutData()
self:BuildLookupTable()
end
function LFGHelper:OnDisable()
self:CancelAllTimers()
end
function LFGHelper:OnEnteringWorld(_, ...)
self:UpdateLockoutData()
end
function LFGHelper:OnUpdateInstanceInfo(_, ...)
self:UpdateLockoutData()
end
function LFGHelper:OnLfgAvailable(_)
self:BuildLookupTable()
end
function LFGHelper:RegisterHook()
if _G.LFGListingActivityView_UpdateActivities == nil then
self:Print("Update Activities function doesn't exist waiting for 10 seconds...")
self:ScheduleTimer(function(...)
self:RegisterHook()
end, 10)
else
self:SecureHook("LFGListingActivityView_UpdateActivities")
self:Print("Successfully hooked Update Activities")
end
end
function LFGHelper:UpdateLockoutData()
self.m_Lockouts = {}
local num_saved = GetNumSavedInstances()
for i = 1, num_saved do
local name, id, _, _, _, _, _, is_raid, max_players, _, _, _, _ = GetSavedInstanceInfo(i)
-- treat any saved instance that isn't a raid as a heroic dungeon for now
local key = MakeKey(name, max_players, not is_raid)
self.m_Lockouts[key] = id
end
end
function LFGHelper:BuildLookupTable()
self.m_LookupTable = {}
self.m_ReverseLookupTable = {}
local activity_handler = function(self, activity_id, heroic)
local info = C_LFGList.GetActivityInfoTable(activity_id);
local name = info.shortName ~= "" and info.shortName or info.fullName;
local key = MakeKey(name, info.maxNumPlayers, heroic)
self.m_LookupTable[activity_id] = key
self.m_ReverseLookupTable[key] = activity_id
end
local categories = C_LFGList.GetAvailableCategories()
for _, category_id in ipairs(categories) do
do
local activities = C_LFGList.GetAvailableActivities(category_id, 0);
if #activities > 0 then
for _, activity_id in ipairs(activities) do
activity_handler(self, activity_id, false)
end
end
end
local activity_groups = C_LFGList.GetAvailableActivityGroups(category_id);
for _, group_id in ipairs(activity_groups) do
local activities = C_LFGList.GetAvailableActivities(category_id, group_id);
if #activities > 0 then
local name, order_index = C_LFGList.GetActivityGroupInfo(group_id);
for _, activity_id in ipairs(activities) do
activity_handler(self, activity_id, order_index == 2)
end
end
end
end
end
function LFGHelper:LFGListingActivityView_UpdateActivities(widget, category_id)
local to_remove = {}
local dp = widget.ScrollBox:GetDataProvider()
dp:ForEach(function(node)
local data = node:GetData()
if data.buttonType == 2 then
local key = self.m_LookupTable[data.activityID]
if self.m_Lockouts[key] then
table.insert(to_remove, node)
end
end
end)
local node_remove = function(parent, child)
for index, node2 in ipairs(parent.nodes) do
if node2 == child then
local removed = table.remove(parent.nodes, index);
parent:Invalidate();
return removed;
end
end
end
local dp_remove = function(dp, node)
local index, node2 = dp:FindIndex(node);
if node2 then
local parent = node2.parent;
assert(parent ~= nil);
if parent then
node_remove(parent, node)
end
end
end
for _, v in pairs(to_remove) do
dp_remove(dp, v)
end
dp:Invalidate()
widget.ScrollBox:FullUpdate(true)
end