-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGargulAutoRoll.lua
278 lines (245 loc) · 10.5 KB
/
GargulAutoRoll.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
ADDON_VERSION = "v3.8"
DEBUG = false
DEBUG_MSG = "|c00967FD2[DEBUG]|r"
MSG = "|c00967FD2[GargulAutoRoll]|r"
if DEBUG then
REQUESTED = 0
REFRESHED = 0
RENDERED = 0
LOOTED = 0
EXCHANGED = 0
end
GargulAutoRoll = CreateFrame("Frame", "AutoRollMainFrame", UIParent, "BasicFrameTemplateWithInset")
GargulAutoRoll:Hide()
GargulAutoRoll:RegisterEvent("ADDON_LOADED")
GargulAutoRoll:RegisterEvent("PLAYER_ENTERING_WORLD")
GargulAutoRoll:RegisterEvent("BANKFRAME_OPENED")
GargulAutoRoll:RegisterEvent("BANKFRAME_CLOSED")
GargulAutoRoll:RegisterEvent("TRADE_CLOSED")
GargulAutoRoll:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
if DEBUG then GargulAutoRoll:RegisterEvent("CHAT_MSG_SAY") end
do
local defaults = {
rules = {},
playerClass = UnitClass("player"),
bankItems = {},
enabled = true, -- RollListener
["hide"] = false, -- MinimapButton visibility
["minimapPos"] = 110, -- MinimapButton position
height = 390,
width = 400
}
local function SetGameVersion()
GargulAutoRoll.IsClassic = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
GargulAutoRoll.IsSoD = GargulAutoRoll.IsClassic and C_Seasons.HasActiveSeason() and (C_Seasons.GetActiveSeason() == Enum.SeasonID.SeasonOfDiscovery)
end
local function LoadSavedSettings()
GargulAutoRollDB = GargulAutoRollDB or {}
for key,value in pairs(defaults) do
if (GargulAutoRollDB[key] == nil) then
GargulAutoRollDB[key] = value
end
end
end
-- Function to register events if the addon is enabled
local function RegisterRaidEvents()
GargulAutoRoll:RegisterEvent("CHAT_MSG_RAID")
GargulAutoRoll:RegisterEvent("CHAT_MSG_RAID_LEADER")
GargulAutoRoll:RegisterEvent("CHAT_MSG_RAID_WARNING")
end
-- Function to unregister events if the addon is disabled
local function UnregisterRaidEvents()
GargulAutoRoll:UnregisterEvent("CHAT_MSG_RAID")
GargulAutoRoll:UnregisterEvent("CHAT_MSG_RAID_LEADER")
GargulAutoRoll:UnregisterEvent("CHAT_MSG_RAID_WARNING")
end
-- Enable auto rolling
function GargulAutoRoll:EnableRollListener()
GargulAutoRollDB.enabled = true
RegisterRaidEvents()
print(MSG, "Auto rolling Enabled")
GargulAutoRoll.statusButton:SetText("|cff00ff00Enabled|r")
end
-- Disable auto rolling
function GargulAutoRoll:DisableRollListener()
GargulAutoRollDB.enabled = false
UnregisterRaidEvents()
print(MSG, "Auto rolling Disabled")
GargulAutoRoll.statusButton:SetText("|cffA9A9A9Disabled|r")
end
local function InitializeRollListener()
if GargulAutoRollDB.enabled == true then
GargulAutoRoll:EnableRollListener()
else
GargulAutoRoll:DisableRollListener()
end
end
local function PrintWelcomeMessage()
print("|c00967FD2GargulAutoRoll " .. ADDON_VERSION .. " by Daga_WildGrowth.|r Type |cff00ff00/gar|r to get started!")
end
local function HandleAutoroll(message, sender)
if GargulAutoRollDB.enabled == true then
local itemLink, itemId = message:match("roll on (|c.-|Hitem:(%d+).-|h|r)")
if itemId then
itemId = tonumber(itemId)
if GargulAutoRollDB.rules and GargulAutoRollDB.rules[itemId] == Utils.ROLL.MS then
print(MSG, "Rolling MS for " .. itemLink)
RandomRoll(1, 100)
elseif GargulAutoRollDB.rules and GargulAutoRollDB.rules[itemId] == Utils.ROLL.OS then
print(MSG, "Rolling OS for " .. itemLink)
RandomRoll(1, 99)
end
end
end
end
local function HandleItemAwarded(message)
local itemLink, awardedPlayer = message:match("(|c.-|Hitem:%d+.-|h|r) was awarded to ([^%.%s]+%-?[^%.%s]*)%.?")
if itemLink and awardedPlayer then
if DEBUG then print(DEBUG_MSG, "[ItemAwarded]", itemLink, awardedPlayer) end
local itemId = tonumber(itemLink:match("Hitem:(%d+)"))
local strippedPlayerName = awardedPlayer:match("([^%-%.]+)")
if DEBUG then print(DEBUG_MSG, "[ItemAwarded]", itemId, strippedPlayerName) end
if strippedPlayerName == UnitName("player") then
if GargulAutoRollDB.rules[itemId] then
GargulAutoRoll:SaveRuleAsync(itemLink, "pass")
GargulAutoRoll.Interface:RefreshLootedItems(2, itemId)
end
end
end
end
local function HandleItemGiven(message)
local itemLink, givenPlayer = message:match("gave (|c.-|Hitem:%d+.-|h|r) to ([^%.%s]+%-?[^%.%s]*)%.?")
if itemLink and givenPlayer then
if DEBUG then print(DEBUG_MSG, "[ItemGiven]", itemLink, givenPlayer) end
local itemId = tonumber(itemLink:match("Hitem:(%d+)"))
local strippedPlayerName = givenPlayer:match("([^%-]+)%-?.*")
if DEBUG then print(DEBUG_MSG, "[ItemGiven]", itemId, strippedPlayerName) end
if strippedPlayerName == UnitName("player") then
if GargulAutoRollDB.rules[itemId] then
GargulAutoRoll:SaveRuleAsync(itemLink, "pass")
GargulAutoRoll.Interface:RefreshLootedItems(2, itemId)
end
end
end
end
local function OnRaidMessage(self, event, message, sender, ...)
HandleAutoroll(message, sender)
HandleItemAwarded(message)
HandleItemGiven(message)
end
GargulAutoRoll:SetScript("OnEvent", function(self, event, addon, ...)
GargulAutoRoll.isInitialized = false
--if DEBUG then print(DEBUG_MSG, event, addon or "") end
if event == "ADDON_LOADED" then
if addon == "GargulAutoRoll" then
PrintWelcomeMessage()
GargulAutoRoll:UnregisterEvent("ADDON_LOADED")
end
return
elseif event == "PLAYER_ENTERING_WORLD" then
if not GargulAutoRoll.IsInitialized then
SetGameVersion()
LoadSavedSettings()
GargulAutoRoll.Interface:Initialize()
GargulAutoRoll.Minimap:Initialize()
InitializeRollListener()
if not GargulAutoRoll.IsSoD then GargulAutoRoll.AtlasLoot.Import() end
GargulAutoRoll.IsInitialized = true
end
GargulAutoRoll.playerInstance = GetInstanceInfo()
if DEBUG then print(DEBUG_MSG, "[GetPlayerInstance]", GargulAutoRoll.playerInstance) end
return
elseif event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER" or event == "CHAT_MSG_RAID_WARNING" or (DEBUG and event == "CHAT_MSG_SAY") then
OnRaidMessage(self, event, addon, ...)
return
elseif event == "BANKFRAME_OPENED" then
Utils:StoreBankItems()
GargulAutoRoll:RegisterEvent("BAG_UPDATE_DELAYED")
return
elseif event == "BANKFRAME_CLOSED" then
GargulAutoRoll:UnregisterEvent("BAG_UPDATE_DELAYED")
return
elseif event == "BAG_UPDATE_DELAYED" then
GargulAutoRoll.Interface:RefreshLootedItems(2)
return
elseif event == "TRADE_CLOSED" then
GargulAutoRoll.Interface:RefreshLootedItems(2)
return
elseif event == "PLAYER_EQUIPMENT_CHANGED" then
GargulAutoRoll.Interface:RefreshLootedItems(2)
return
end
end)
function GargulAutoRoll:SaveRule(itemLink, rule)
local itemId = Utils:GetItemIdFromLink(itemLink)
if itemId then
if rule == nil or rule == "search" then
if GargulAutoRollDB.rules[itemId] then
GargulAutoRoll.Interface:HighlightNone(itemId)
GargulAutoRollDB.rules[itemId] = nil
print(MSG, "Removed", itemLink)
else
print(MSG, itemLink, "is not in the list")
end
elseif rule == "ms" then
GargulAutoRoll.Interface:HighlightNeedButton(itemId)
GargulAutoRollDB.rules[itemId] = Utils:getRuleValue(rule)
print(MSG, "Rolling", rule:upper(), "for", itemLink)
elseif rule == "os" then
GargulAutoRoll.Interface:HighlightGreedButton(itemId)
GargulAutoRollDB.rules[itemId] = Utils:getRuleValue(rule)
print(MSG, "Rolling", rule:upper(), "for", itemLink)
elseif rule == "pass" then
GargulAutoRoll.Interface:HighlightPassButton(itemId)
GargulAutoRollDB.rules[itemId] = Utils:getRuleValue(rule)
print(MSG, "Rolling", rule:upper(), "for", itemLink)
end
end
end
function GargulAutoRoll:SaveRuleAsync(itemLink, rule)
local itemId = Utils:GetItemIdFromLink(itemLink)
Utils:GetItemInfoAsync(itemId, function(itemName, itemLink, itemRarity, itemIcon)
if DEBUG then print(DEBUG_MSG, "[SaveRuleAsync] Requested item", itemId) end
if itemLink then
local searchResults = {}
searchResults[itemId] = {
id = itemId,
name = itemName,
link = itemLink,
rarity = itemRarity,
icon = itemIcon,
rule = GargulAutoRollDB.rules[itemId] or Utils.ROLL.SEARCH
}
GargulAutoRoll:SaveRule(itemLink, rule)
GargulAutoRoll.Interface:RefreshWithItems(searchResults)
else
if DEBUG then print(DEBUG_MSG, "[SaveRuleAsync] Item not found:", itemId) end
end
end)
end
function GargulAutoRoll:ToggleShow()
if GargulAutoRoll:IsShown() then
GargulAutoRoll:Hide()
else
GargulAutoRoll:Show()
GargulAutoRoll.inputBox:Clear()
GargulAutoRoll.Interface:RefreshWithItems()
end
end
function GargulAutoRoll:PrintHelp()
print(MSG, "Help")
print(" Show/Hide addon interface:")
print(" /gar")
print(" Add item rules:")
print(" /gar ms [item-link]")
print(" /gar os [item-link]")
print(" /gar pass [item-link]")
print(" Remove item rules:")
print(" /gar remove [item-link]")
print(" List item rules:")
print(" /gar rules")
print(" Show/Hide minimap button:")
print(" /gar minimap")
end
end
return GargulAutoRoll