annotate Modules/Events.lua @ 8:0271e781b154

Working on converting the database to store items as links instead of names.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 23 Jun 2010 23:47:48 -0700
parents bbba2fae0f69
children 374dd1a90d02
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@3 2 local addon = _G[addonName]
Asa@3 3
Asa@3 4 local utils = addonTable.utils
Asa@3 5
Asa@3 6 function addon:PLAYER_ENTERING_WORLD()
Asa@8 7 addon:ConvertItems()
Asa@8 8 DevTools_Dump(ItemAuditor.db.factionrealm.items)
Asa@8 9
Asa@3 10 self:RegisterEvent("MAIL_SHOW")
Asa@4 11 self:RegisterEvent("UNIT_SPELLCAST_START")
Asa@3 12 self:WatchBags()
Asa@3 13 end
Asa@3 14
Asa@3 15 function addon:MAIL_SHOW()
Asa@3 16 self:Debug("MAIL_SHOW")
Asa@3 17 self.lastMailScan = self:ScanMail()
Asa@7 18
Asa@3 19 self:UnregisterEvent("MAIL_SHOW")
Asa@3 20 self:RegisterEvent("MAIL_CLOSED")
Asa@3 21 self:RegisterEvent("MAIL_INBOX_UPDATE")
Asa@3 22 self:Debug("MAIL_SHOW complete")
Asa@3 23 end
Asa@3 24
Asa@3 25 function addon:MAIL_CLOSED()
Asa@3 26 addon:UnregisterEvent("MAIL_CLOSED")
Asa@7 27 self:MAIL_INBOX_UPDATE()
Asa@3 28 self:UnregisterEvent("MAIL_INBOX_UPDATE")
Asa@3 29 self:RegisterEvent("MAIL_SHOW")
Asa@3 30 end
Asa@3 31
Asa@3 32 function addon:MAIL_INBOX_UPDATE()
Asa@3 33 local newScan = addon:ScanMail()
Asa@3 34 local diff
Asa@6 35 for mailType, collection in pairs(self.lastMailScan) do
Asa@7 36 newScan[mailType] = (newScan[mailType] or {})
Asa@6 37 for item, total in pairs(collection) do
Asa@3 38
Asa@6 39 diff = total - (newScan[mailType][item] or 0)
Asa@6 40 if diff ~= 0 then
Asa@6 41 self:SaveValue(item, diff)
Asa@6 42 end
Asa@6 43
Asa@3 44 end
Asa@3 45 end
Asa@3 46
Asa@3 47 self.lastMailScan = newScan
Asa@3 48 end
Asa@3 49
Asa@4 50 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 51 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 52 self:UnwatchBags()
Asa@4 53 self:UpdateCurrentInventory()
Asa@4 54 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 55 self:RegisterEvent("LOOT_CLOSED")
Asa@3 56 end
Asa@3 57 end
Asa@3 58
Asa@4 59 --[[
Asa@4 60 The item should be destroyed before this point, so the last inventory check
Asa@4 61 needs to be kept so it can be combined with the up coming loot.
Asa@4 62 ]]
Asa@4 63 function addon:LOOT_CLOSED()
Asa@4 64 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 65 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 66 local inventory = self.lastInventory
Asa@4 67 self:WatchBags()
Asa@4 68 self.lastInventory = inventory
Asa@4 69 end
Asa@3 70
Asa@4 71 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 72 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 73 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 74 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 75 self:WatchBags()
Asa@4 76 end
Asa@4 77 end
Asa@4 78
Asa@4 79 function addon:UpdateCurrentInventory()
Asa@4 80 self.lastInventory = self:GetCurrentInventory()
Asa@3 81 end
Asa@3 82
Asa@3 83 function addon:UpdateAudit()
Asa@7 84 -- self:Debug("UpdateAudit")
Asa@3 85 local currentInventory = self:GetCurrentInventory()
Asa@3 86 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 87 -- this is only here for debugging
Asa@3 88 self.lastdiff = diff
Asa@3 89
Asa@5 90 local positive, negative = {}, {}
Asa@5 91 local positiveCount, negativeCount = 0, 0
Asa@5 92 for item, count in pairs(diff.items) do
Asa@5 93 if count > 0 then
Asa@5 94 positive[item] = count
Asa@5 95 positiveCount = positiveCount + count
Asa@5 96 elseif count < 0 then
Asa@5 97 negative[item] = count
Asa@5 98 negativeCount = negativeCount + abs(count)
Asa@5 99 end
Asa@5 100 end
Asa@5 101
Asa@5 102 if diff.money > 0 and utils:tcount(positive) > 0 and utils:tcount(negative) == 0 then
Asa@7 103 -- self:Debug("loot")
Asa@7 104 elseif utils:tcount(diff.items) == 1 then
Asa@8 105 self:Debug("purchase or sale")
Asa@3 106
Asa@3 107 for itemName, count in pairs(diff.items) do
Asa@3 108 self:SaveValue(itemName, diff.money)
Asa@3 109 end
Asa@3 110 elseif utils:tcount(diff.items) > 1 then
Asa@3 111
Asa@3 112 if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then
Asa@3 113 -- we must have created/converted something
Asa@7 114 -- self:Debug("conversion")
Asa@3 115 local totalChange = 0
Asa@3 116 for itemName, change in pairs(negative) do
Asa@3 117 local _, itemCost, count = self:GetItemCost(itemName, change)
Asa@3 118 self:SaveValue(itemName, abs(itemCost * change))
Asa@3 119
Asa@3 120 totalChange = totalChange + abs(itemCost * change)
Asa@3 121 end
Asa@3 122
Asa@7 123 local valuePerItem = totalChange / positiveCount
Asa@3 124
Asa@3 125 for itemName, change in pairs(positive) do
Asa@3 126 self:SaveValue(itemName, 0-abs(valuePerItem * change))
Asa@3 127 end
Asa@3 128 end
Asa@3 129 end
Asa@3 130
Asa@3 131 self.lastInventory = currentInventory
Asa@4 132 addon:WatchBags()
Asa@3 133 end