forked from jimmy58663/Craftility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCraftility.lua
350 lines (312 loc) · 12.2 KB
/
Craftility.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
local addonName, CraftilityNS = ...
local _G = _G
local ElvUI = nil -- Import: ElvUI if it is loaded when frames are initialized
local E = nil -- Import: ElvUI Engine module when frames are initialized
local S = nil -- Import: ElvUI Skins module when frames are initialized
CraftilityNS.AceSerializer = LibStub:GetLibrary("AceSerializer-3.0")
CraftilityNS.libC = LibStub:GetLibrary("LibCompress")
CraftilityNS.EncodeTable = CraftilityNS.libC:GetAddonEncodeTable()
local Craftility = LibStub("AceAddon-3.0"):NewAddon("Craftility", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
CraftilityNS.Craftility = Craftility
_G.CraftilityNS = CraftilityNS
local OrdersPage = _G.ProfessionsFrame.OrdersPage
Craftility.OrdersSeen = {[Enum.CraftingOrderType.Public] = {}, [Enum.CraftingOrderType.Guild] = {}, [Enum.CraftingOrderType.Personal] = {}}
Craftility.selectedSkillLineAbility = nil
Craftility.searchFavorites = false
Craftility.initialNonPublicSearch = false
local ProfessionSkillLineLookup = {
[129] = 0, --First Aid
[164] = 1, --Blacksmithing
[165] = 2, --Leatherworking
[171] = 3, --Alchemy
[182] = 4, --Herbalism
[184] = 5, --Cooking
[186] = 6, --Mining
[197] = 7, --Tailoring
[202] = 8, --Engineering
[333] = 9, --Enchanting
[356] = 10, --Fishing
[393] = 11, --Skinning
[755] = 12, --Jewelcrafting
[773] = 13, --Inscription
[794] = 14 --Archaeology
}
local CraftingProfessions = {
1, --Blacksmithing
2, --Leatherworking
3, --Alchemy
7, --Tailoring
8, --Engineering
9, --Enchanting
12, --Jewelcrafting
13 --Inscription
}
local soundBytes = {
[SOUNDKIT.AUCTION_WINDOW_OPEN] = "Auction Window Open",
[SOUNDKIT.AUCTION_WINDOW_CLOSE] = "Auction Window Close",
[SOUNDKIT.MONEY_FRAME_OPEN] = "Money Frame Open",
[SOUNDKIT.MONEY_FRAME_CLOSE] = "Money Frame Close",
[SOUNDKIT.RAID_WARNING] = "Raid Warning",
[SOUNDKIT.READY_CHECK] = "Ready Check"
}
local options = {
name = "Craftility",
handler = Craftility,
type = 'group',
args = {
SearchInterval = {
name = "Search Interval",
desc = "The amount of wait time between searches in seconds.",
type = "range",
min = 1,
max = 15,
step = 1,
order = 1,
set = function(_, val) Craftility.db.profile.SearchInterval = val end,
get = function() return Craftility.db.profile.SearchInterval end,
},
SoundByteId = {
name = "Sound Byte",
desc = "The sound that plays when a new public order is found.",
type = "select",
values = soundBytes,
order = 2,
set = function(_, val) Craftility.db.profile.SoundByteId = val end,
get = function() return Craftility.db.profile.SoundByteId end,
},
TestSound = {
name = "Test Sound Byte",
desc = "Plays the select sound byte.",
type = "execute",
order = 3,
func = function() PlaySound(Craftility.db.profile.SoundByteId, "SFX") end,
}
}
}
Craftility.DefaultConfig = {
profile = {
SearchInterval = 2,
SoundByteId = SOUNDKIT.AUCTION_WINDOW_CLOSE
}
}
function Craftility:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("CraftilityDB", self.DefaultConfig, true)
LibStub("AceConfig-3.0"):RegisterOptionsTable("Craftility", options, {"Craftility", "Cfly"})
self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Craftility", "Craftility")
local profiles = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
LibStub("AceConfig-3.0"):RegisterOptionsTable("Craftility_Profiles", profiles)
LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Craftility_Profiles", "Profiles", "Craftility")
end
function Craftility:OnEnable()
self:RegisterEvent("TRADE_SKILL_SHOW")
self:RegisterEvent("CRAFTINGORDERS_SHOW_CUSTOMER")
self:RegisterEvent("CHAT_MSG_SYSTEM")
end
function Craftility:OnDisable()
end
function Craftility:TRADE_SKILL_SHOW()
self:INITIALIZE()
self:UnregisterEvent("TRADE_SKILL_SHOW")
end
function Craftility:CRAFTINGORDERS_SHOW_CUSTOMER()
self:INITIALIZE()
self:UnregisterEvent("CRAFTINGORDERS_SHOW_CUSTOMER")
end
function Craftility:CHAT_MSG_SYSTEM(eventName, message, ...)
if message == "You have received a new Personal Crafting Order." then
FlashClientIcon()
PlaySound(Craftility.db.profile.SoundByteId, "SFX")
RaidNotice_AddMessage(RaidWarningFrame, "!!! NEW PERSONAL CRAFTING ORDER !!!", ChatTypeInfo["RAID_WARNING"])
end
end
function Craftility:INITIALIZE()
if not self.AutoSearchCheckBox then
CraftilityNS:SetProfessionInfo()
self.AutoSearchCheckBox = CreateFrame("CheckButton", "Craftility_AutoSearchCheckBox", OrdersPage.BrowseFrame, "UICheckButtonTemplate")
self.AutoSearchCheckBox:SetSize(26, 26)
self.AutoSearchCheckBox:SetPoint("LEFT", OrdersPage.BrowseFrame.SearchButton, "TOPLEFT", 0, 8)
self.AutoSearchCheckBox.text:SetText("Auto Search Orders")
self.AutoSearchCheckBox:SetScript("OnClick", function()
local checked = Craftility.AutoSearchCheckBox:GetChecked()
if checked then
if Craftility.Timer then
Craftility:CancelAllTimers()
Craftility.Timer = nil
end
Craftility.Timer = Craftility:ScheduleRepeatingTimer("SearchOrders", Craftility.db.profile.SearchInterval)
elseif not checked then
if Craftility.Timer then
Craftility:CancelAllTimers()
Craftility.Timer = nil
end
end
end)
_G.ProfessionsFrame:HookScript("OnHide", function()
Craftility.AutoSearchCheckBox:SetChecked(false)
if Craftility.Timer then
Craftility:CancelAllTimers()
Craftility.Timer = nil
end
end)
if ElvUI == nil then
ElvUI = _G.ElvUI
end
if ElvUI then
E = ElvUI[1]
if E.private.skins.blizzard.tradeskill then
S = E:GetModule("Skins")
S:HandleCheckBox(self.AutoSearchCheckBox)
self.AutoSearchCheckBox:SetSize(24, 24)
self.AutoSearchCheckBox:SetPoint("LEFT", OrdersPage.BrowseFrame.FavoritesSearchButton, "TOPLEFT", -3, 12)
end
end
end
Craftility:SendMessage("CRAFTILITY_INITIALIZE")
end
function Craftility:SearchOrders()
OrdersPage:RequestOrders(self.selectedSkillLineAbility, self.searchFavorites, self.initialNonPublicSearch)
if OrdersPage:GetBrowseType() == 2 then --Buckets are returned
Craftility:RequestOrders(self.selectedSkillLineAbility, self.searchFavorites, self.initialNonPublicSearch)
elseif OrdersPage:GetBrowseType() == 1 then --Orders are returned
local orders = C_CraftingOrders.GetCrafterOrders()
self:ParseOrders(orders)
end
end
function Craftility:ParseOrders(orders)
for i, order in pairs(orders) do
local orderType = order.orderType
local orderID = order.orderID
if not tContains(Craftility.OrdersSeen[orderType], orderID) then
tinsert(Craftility.OrdersSeen[orderType], orderID)
FlashClientIcon()
PlaySound(Craftility.db.profile.SoundByteId, "SFX")
end
end
end
function Craftility:RequestOrders(selectedSkillLineAbility, searchFavorites, initialNonPublicSearch)
local defaultBucketSecondarySort = {
sortType = Enum.CraftingOrderSortType.MaxTip,
reversed = true,
}
local defaultFlatSecondarySort = {
sortType = Enum.CraftingOrderSortType.Tip,
reversed = true,
}
local isFlatSearch = selectedSkillLineAbility ~= nil
local request = {
orderType = Enum.CraftingOrderType.Public,
selectedSkillLineAbility = selectedSkillLineAbility,
searchFavorites = searchFavorites,
initialNonPublicSearch = initialNonPublicSearch,
primarySort = Professions.TranslateSearchSort(OrdersPage.primarySort),
secondarySort = Professions.TranslateSearchSort(OrdersPage.secondarySort) or (isFlatSearch and defaultFlatSecondarySort or defaultBucketSecondarySort),
forCrafter = true,
offset = 0,
callback = C_FunctionContainers.CreateCallback(function(...) Craftility:OrderRequestCallback(...) end),
profession = _G.ProfessionsFrame.professionInfo.profession,
}
C_CraftingOrders.RequestCrafterOrders(request)
end
function Craftility:OrderRequestCallback(orderResult, orderType, displayBuckets, expectMoreRows, offset, isSorted)
if displayBuckets then
local buckets = C_CraftingOrders.GetCrafterBuckets()
Craftility:ParseBuckets(buckets)
else
local orders = C_CraftingOrders.GetCrafterOrders()
Craftility:ParseOrders(orders)
end
end
function Craftility:ParseBuckets(buckets)
for i, bucket in pairs(buckets) do
local selectedSkillLineAbility = bucket.skillLineAbilityID
local searchFavorites = false
local initialNonPublicSearch = false
if bucket.orderType == Enum.CraftingOrderType.Public then
initialNonPublicSearch = false
elseif bucket.orderType == Enum.CraftingOrderType.Guild then
initialNonPublicSearch = true
end
Craftility:RequestOrders(selectedSkillLineAbility, searchFavorites, initialNonPublicSearch)
end
--reset search to defaults
OrdersPage.BrowseFrame.RecipeList:ClearSelectedRecipe()
OrdersPage.selectedRecipe = nil
end
--CraftilityNS
function CraftilityNS:dumpTable(table, maxDepth, currentDepth)
if currentDepth == nil then
currentDepth = 0
end
if maxDepth == nil then
maxDepth = 20
end
if (currentDepth > maxDepth) then
return
end
for k,v in pairs(table) do
if (type(k) == "table") then
CraftilityNS:dumpTable(k, maxDepth, currentDepth+1)
elseif (type(v) == "table") then
print(string.rep(" ", currentDepth)..k..":")
CraftilityNS:dumpTable(v, maxDepth, currentDepth+1)
else
if (type(v) == "function") then
print(string.rep(" ", currentDepth)..k..": <function>")
elseif (type(v) == "userdata") then
print(string.rep(" ", currentDepth)..k..": <userdata>")
else
print(string.rep(" ", currentDepth)..k..": ",v)
end
end
end
end
function CraftilityNS:SetProfessionInfo()
local function GetProfInfo(profIndex)
if profIndex then
local name, icon, skillLevel, maxSkillLevel, numAbilities, spelloffset, skillLine, skillModifier, specializationIndex, specializationOffset, professionName = GetProfessionInfo(profIndex)
local profInfo = {
skillLevel = skillLevel,
skillModifier = skillModifier,
parentProfessionName = name,
parentProfessionID = skillLine,
profession = ProfessionSkillLineLookup[skillLine],
professionName = professionName,
maxSkillLevel = maxSkillLevel,
expansionName = professionName:gsub(' '..name, '')
}
return profInfo
end
end
CraftilityNS.professionInfo = {
prof1 = nil,
prof2 = nil
}
local prof1, prof2= GetProfessions()
CraftilityNS.professionInfo.prof1 = GetProfInfo(prof1)
CraftilityNS.professionInfo.prof2 = GetProfInfo(prof2)
end
function CraftilityNS:IsCraftingProfession(professionInfo)
if tContains(CraftingProfessions, professionInfo.profession) then
return true
else
return false
end
end
function CraftilityNS:SerializeData(data)
local serializedData = self.AceSerializer:Serialize(data)
local compressedData = self.libC:Compress(serializedData)
local encodedData = self.EncodeTable:Encode(compressedData)
return encodedData
end
function CraftilityNS:DeserializeData(encodedData)
local compressedData = self.EncodeTable:Decode(encodedData)
local decompressSuccess, message = self.libC:Decompress(compressedData)
if not decompressSuccess then
error("Craftility: Error decompressing: " .. message)
end
local deserialzeSuccess, data = self.AceSerializer:Deserialize(decompressSuccess)
if not deserialzeSuccess then
error("Craftility: Error deserializing: " .. data)
end
return data
end