comparison Core.lua @ 3:bbcf81868171

Code cleanup.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 22 May 2010 11:34:19 -0700
parents 6c87720c301c
children c940b527ccab
comparison
equal deleted inserted replaced
2:e9a1646beaa4 3:bbcf81868171
1 local addon = LibStub("AceAddon-3.0"):NewAddon("ItemAuditor", "AceConsole-3.0", "AceEvent-3.0", "AceBucket-3.0") 1 local addonName, addonTable = ...;
2 _G[addonName] = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceBucket-3.0")
3 local addon = _G[addonName]
2 4
3 ItemAuditor = addon 5 local utils = addonTable.utils
6
4 7
5 local WHITE = "|cFFFFFFFF" 8 local WHITE = "|cFFFFFFFF"
6 local RED = "|cFFFF0000" 9 local RED = "|cFFFF0000"
7 local GREEN = "|cFF00FF00" 10 local GREEN = "|cFF00FF00"
8 local YELLOW = "|cFFFFFF00" 11 local YELLOW = "|cFFFFFF00"
23 26
24 self.db.char.debug = true 27 self.db.char.debug = true
25 28
26 self:RegisterOptions() 29 self:RegisterOptions()
27 30
28 self:RegisterEvent("MAIL_SHOW") 31 self:RegisterEvent("PLAYER_ENTERING_WORLD")
29 self:WatchBags()
30 end
31
32 local function IA_tcount(tab)
33 local n = #tab
34 if (n == 0) then
35 for _ in pairs(tab) do
36 n = n + 1
37 end
38 end
39 return n
40 end
41
42
43 local options = {
44 name = "ItemAuditor",
45 handler = ItemAuditor,
46 type = 'group',
47 args = {
48 debug = {
49 type = "toggle",
50 name = "Debug",
51 desc = "Toggles debug messages in chat",
52 get = "GetDebug",
53 set = "SetDebug"
54 },
55 dump = {
56 type = "execute",
57 name = "dump",
58 desc = "dumps IA database",
59 func = "DumpInfo",
60 },
61 options = {
62 type = "execute",
63 name = "options",
64 desc = "Show Blizzard's options GUI",
65 func = "ShowOptionsGUI",
66 guiHidden = true,
67 },
68 },
69 }
70
71
72 function addon:DumpInfo()
73 self:Print("self.db.char")
74 DevTools_Dump(self.db.char)
75 self:Print("self.db.factionrealm")
76 DevTools_Dump(self.db.factionrealm)
77 end
78
79 function addon:RegisterOptions()
80 self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("ItemAuditor", "ItemAuditor")
81
82 LibStub("AceConfig-3.0"):RegisterOptionsTable("ItemAuditor", options, {"ia"})
83 end
84
85 function addon:GetMessage(info)
86 return self.message
87 end
88
89 function addon:SetMessage(info, newValue)
90 self.message = newValue
91 end
92
93
94 function addon:ShowOptionsGUI()
95 InterfaceOptionsFrame_OpenToCategory(self.optionsFrame)
96 end
97
98 function addon:GetDebug(info)
99 return self.db.char.debug
100 end
101
102 function addon:SetDebug(info, input)
103 self.db.char.debug = input
104 local value = "off"
105 if input then
106 value = "on"
107 end
108 self:Print("Debugging is now: " .. value)
109 end
110
111
112 -- ================ DEBUG ================
113 addon.OriginalRegisterEvent = addon.RegisterEvent
114 addon.OriginalUnregisterEvent = addon.UnregisterEvent
115
116 function addon:RegisterEvent(event, callback, arg)
117 self:Debug("RegisterEvent " .. event )
118 if arg ~= nil then
119 addon:OriginalRegisterEvent(event, callback, arg)
120 elseif callback ~= nil then
121 addon:OriginalRegisterEvent(event, callback)
122 else
123 addon:OriginalRegisterEvent(event)
124 end
125 end
126
127 function addon:UnregisterEvent(event)
128 self:Debug("UnregisterEvent " .. event )
129 addon:OriginalUnregisterEvent (event)
130 end
131
132 -- ================ DEBUG ================
133
134 function addon:FormatMoney(money)
135 return Altoholic:GetMoneyString(money, WHITE, false)
136 end 32 end
137 33
138 function addon:GetCurrentInventory() 34 function addon:GetCurrentInventory()
139 local i = {} 35 local i = {}
140 local link 36 local link
221 117
222 end 118 end
223 return results 119 return results
224 end 120 end
225 121
226 function addon:MAIL_SHOW()
227 self:Debug("MAIL_SHOW")
228 self.lastMailScan = self:ScanMail()
229 self:UnregisterEvent("MAIL_SHOW")
230 self:RegisterEvent("MAIL_CLOSED")
231 self:RegisterEvent("MAIL_INBOX_UPDATE")
232 self:Debug("MAIL_SHOW complete")
233 end
234
235 function addon:MAIL_CLOSED()
236 addon:UnregisterEvent("MAIL_CLOSED")
237 self:UnregisterEvent("MAIL_INBOX_UPDATE")
238 self:RegisterEvent("MAIL_SHOW")
239 end
240
241 function addon:MAIL_INBOX_UPDATE()
242 local newScan = addon:ScanMail()
243 local diff
244 for item, total in pairs(self.lastMailScan) do
245
246 if newScan[item] == nil then
247 newScan[item] = 0
248 end
249 diff = total - newScan[item]
250 if diff ~= 0 then
251 self:SaveValue(item, diff)
252 end
253
254 end
255
256 self.lastMailScan = newScan
257 end
258
259 function addon:SaveValue(item, value) 122 function addon:SaveValue(item, value)
260 local item_account = self.db.factionrealm.item_account 123 local item_account = self.db.factionrealm.item_account
261 if item_account[item] == nil then 124 if item_account[item] == nil then
262 item_account[item] = 0 125 item_account[item] = 0
263 end 126 end
266 if item_account[item] >= 0 then 129 if item_account[item] >= 0 then
267 item_account[item] = nil 130 item_account[item] = nil
268 end 131 end
269 end 132 end
270 133
271 function addon:OnEnable() 134 function addon:WatchBags(delay)
272 self:Debug("Hello, world! OnEnable")
273 end
274
275 function addon:Debug(msg)
276 if self.db.char.debug then
277 self:Print(msg)
278 end
279 end
280
281 function addon:WatchBags()
282 if self.watch_handle == nil then 135 if self.watch_handle == nil then
136 delay = delay or 0.2
283 self.lastInventory = self:GetCurrentInventory() 137 self.lastInventory = self:GetCurrentInventory()
284 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, 0.2, "UpdateAudit") 138 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, delay, "UpdateAudit")
285 end 139 end
286 end 140 end
287 141
288 function addon:UnwatchBags() 142 function addon:UnwatchBags()
289 if self.watch_handle ~= nil then 143 if self.watch_handle ~= nil then
290 self:UnregisterBucket(self.watch_handle) 144 self:UnregisterBucket(self.watch_handle)
291 self.watch_handle = nil 145 self.watch_handle = nil
292 end 146 end
293 end 147 end
294 148
295 function addon:UpdateAudit() 149
296 self:Debug("UpdateAudit")
297 local currentInventory = self:GetCurrentInventory()
298 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
299 -- this is only here for debugging
300 self.lastdiff = diff
301
302 if abs(diff.money) > 0 and IA_tcount(diff.items) == 1 then
303 self:Debug("purchase or sale")
304
305 for itemName, count in pairs(diff.items) do
306 self:SaveValue(itemName, diff.money)
307 end
308 elseif IA_tcount(diff.items) > 1 then
309 local positive, negative = {}, {}
310 local positiveCount, negativeCount = 0, 0
311 for item, count in pairs(diff.items) do
312 if count > 0 then
313 positive[item] = count
314 positiveCount = positiveCount + count
315 elseif count < 0 then
316 negative[item] = count
317 negativeCount = negativeCount + abs(count)
318 end
319 end
320
321 if IA_tcount(positive) > 0 and IA_tcount(negative) > 0 then
322 -- we must have created/converted something
323 self:Debug("conversion")
324 local totalChange = 0
325 for itemName, change in pairs(negative) do
326 local _, itemCost, count = self:GetItemCost(itemName, change)
327 self:SaveValue(itemName, abs(itemCost * change))
328
329 totalChange = totalChange + abs(itemCost * change)
330 end
331
332 self:Debug("totalChange")
333 self:Debug(totalChange)
334
335 local valuePerItem = totalChange / positiveCount
336 self:Debug(valuePerItem )
337 for itemName, change in pairs(positive) do
338 self:Debug(itemName)
339 self:Debug(0-abs(valuePerItem * change))
340 self:SaveValue(itemName, 0-abs(valuePerItem * change))
341 end
342 end
343 end
344
345 self.lastInventory = currentInventory
346 end
347 150
348 function addon:GetItemCost(itemName, countModifier) 151 function addon:GetItemCost(itemName, countModifier)
349 local invested = abs(self.db.factionrealm.item_account[itemName] or 0) 152 local invested = abs(self.db.factionrealm.item_account[itemName] or 0)
350 153
351 if invested > 0 then 154 if invested > 0 then
355 if countModifier ~= nil then 158 if countModifier ~= nil then
356 count = count - countModifier 159 count = count - countModifier
357 end 160 end
358 if count == 0 then 161 if count == 0 then
359 self.db.factionrealm.item_account[itemName] = nil 162 self.db.factionrealm.item_account[itemName] = nil
360 self:Print("You ran out of " .. itemName .. "and never recovered " .. self:FormatMoney(invested)) 163 self:Print("You ran out of " .. itemName .. "and never recovered " .. utils:FormatMoney(invested))
361 return 0, 0, 0 164 return 0, 0, 0
362 end 165 end
363 return ceil(invested), ceil(invested/count), count 166 return ceil(invested), ceil(invested/count), count
364 end 167 end
365 return 0, 0, 0 168 return 0, 0, 0
366 end 169 end
367
368 function addon:ShowTooltip(tip, link, num)
369 if (link == nil) then
370 return;
371 end
372
373 local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, _, _, _, _, itemVendorPrice = GetItemInfo (link);
374 -- local _, _, Color, Ltype, Id, Enchant, Gem1, Gem2, Gem3, Gem4, Suffix, Unique, LinkLvl, Name = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
375
376 local investedTotal, investedPerItem, count = self:GetItemCost(itemName)
377
378 local AHCut = 0.05
379 local keep = 1 - AHCut
380
381 if investedTotal > 0 then
382 tip:AddDoubleLine("\124cffffffffIA: Total Invested", self:FormatMoney(investedTotal));
383 tip:AddDoubleLine("\124cffffffffIA: Invested/Item (" .. count .. ")", self:FormatMoney(ceil(investedPerItem)));
384 tip:AddDoubleLine("\124cffffffffIA: Minimum faction AH Price: ", self:FormatMoney(ceil(investedPerItem/keep)))
385 tip:Show()
386 end
387 end
388
389 local function ShowTipWithPricing(tip, link, num)
390 addon:ShowTooltip(tip, link, num)
391 end
392
393 hooksecurefunc (GameTooltip, "SetBagItem",
394 function(tip, bag, slot)
395 local _, num = GetContainerItemInfo(bag, slot);
396 ShowTipWithPricing (tip, GetContainerItemLink(bag, slot), num);
397 end
398 );
399
400
401 hooksecurefunc (GameTooltip, "SetAuctionItem",
402 function (tip, type, index)
403 ShowTipWithPricing (tip, GetAuctionItemLink(type, index));
404 end
405 );
406
407 hooksecurefunc (GameTooltip, "SetAuctionSellItem",
408 function (tip)
409 local name, _, count = GetAuctionSellItemInfo();
410 local __, link = GetItemInfo(name);
411 ShowTipWithPricing (tip, link, num);
412 end
413 );
414
415
416 hooksecurefunc (GameTooltip, "SetLootItem",
417 function (tip, slot)
418 if LootSlotIsItem(slot) then
419 local link, _, num = GetLootSlotLink(slot);
420 ShowTipWithPricing (tip, link, num);
421 end
422 end
423 );
424
425 hooksecurefunc (GameTooltip, "SetLootRollItem",
426 function (tip, slot)
427 local _, _, num = GetLootRollItemInfo(slot);
428 ShowTipWithPricing (tip, GetLootRollItemLink(slot), num);
429 end
430 );
431
432
433 hooksecurefunc (GameTooltip, "SetInventoryItem",
434 function (tip, unit, slot)
435 ShowTipWithPricing (tip, GetInventoryItemLink(unit, slot), GetInventoryItemCount(unit, slot));
436 end
437 );
438
439 hooksecurefunc (GameTooltip, "SetGuildBankItem",
440 function (tip, tab, slot)
441 local _, num = GetGuildBankItemInfo(tab, slot);
442 ShowTipWithPricing (tip, GetGuildBankItemLink(tab, slot), num);
443 end
444 );
445
446 hooksecurefunc (GameTooltip, "SetTradeSkillItem",
447 function (tip, skill, id)
448 local link = GetTradeSkillItemLink(skill);
449 local num = GetTradeSkillNumMade(skill);
450 if id then
451 link = GetTradeSkillReagentItemLink(skill, id);
452 num = select (3, GetTradeSkillReagentInfo(skill, id));
453 end
454
455 ShowTipWithPricing (tip, link, num);
456 end
457 );
458
459 hooksecurefunc (GameTooltip, "SetTradePlayerItem",
460 function (tip, id)
461 local _, _, num = GetTradePlayerItemInfo(id);
462 ShowTipWithPricing (tip, GetTradePlayerItemLink(id), num);
463 end
464 );
465
466 hooksecurefunc (GameTooltip, "SetTradeTargetItem",
467 function (tip, id)
468 local _, _, num = GetTradeTargetItemInfo(id);
469 ShowTipWithPricing (tip, GetTradeTargetItemLink(id), num);
470 end
471 );
472
473 hooksecurefunc (GameTooltip, "SetQuestItem",
474 function (tip, type, index)
475 local _, _, num = GetQuestItemInfo(type, index);
476 ShowTipWithPricing (tip, GetQuestItemLink(type, index), num);
477 end
478 );
479
480 hooksecurefunc (GameTooltip, "SetQuestLogItem",
481 function (tip, type, index)
482 local num, _;
483 if type == "choice" then
484 _, _, num = GetQuestLogChoiceInfo(index);
485 else
486 _, _, num = GetQuestLogRewardInfo(index)
487 end
488
489 ShowTipWithPricing (tip, GetQuestLogItemLink(type, index), num);
490 end
491 );
492
493 hooksecurefunc (GameTooltip, "SetInboxItem",
494 function (tip, index, attachIndex)
495 local _, _, num = GetInboxItem(index, attachIndex);
496 ShowTipWithPricing (tip, GetInboxItemLink(index, attachIndex), num);
497 end
498 );
499
500 hooksecurefunc (GameTooltip, "SetSendMailItem",
501 function (tip, id)
502 local name, _, num = GetSendMailItem(id)
503 local name, link = GetItemInfo(name);
504 ShowTipWithPricing (tip, link, num);
505 end
506 );
507
508 hooksecurefunc (GameTooltip, "SetHyperlink",
509 function (tip, itemstring, num)
510 local name, link = GetItemInfo (itemstring);
511 ShowTipWithPricing (tip, link, num);
512 end
513 );
514
515 hooksecurefunc (ItemRefTooltip, "SetHyperlink",
516 function (tip, itemstring)
517 local name, link = GetItemInfo (itemstring);
518 ShowTipWithPricing (tip, link);
519 end
520 );