annotate Modules/Utils.lua @ 157:091bae7bfca0

Fixed a bug in calculating purchase/sale where the item ID was being used instead of the number of items that changed.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 15 Dec 2010 22:49:28 -0800
parents 53147a647e28
children
rev   line source
Asa@63 1 local ItemAuditor = select(2, ...)
Asa@63 2 local Utils = ItemAuditor:NewModule("Utils")
Asa@3 3
Asa@65 4 function Utils.FormatMoney(copper, color, textOnly)
Asa@20 5 color = color or "|cFFFFFFFF"
Asa@6 6 local prefix = ""
Asa@101 7 copper = copper or 0
Asa@16 8 if copper < 0 then
Asa@6 9 prefix = "-"
Asa@16 10 copper = abs(copper)
Asa@6 11 end
Asa@20 12
Asa@20 13 local copperTexture = COPPER_AMOUNT_TEXTURE
Asa@20 14 local silverTexture = SILVER_AMOUNT_TEXTURE
Asa@20 15 local goldTexture = GOLD_AMOUNT_TEXTURE
Asa@20 16 if textOnly then
Asa@20 17 copperTexture = '%dc'
Asa@20 18 silverTexture = '%ds'
Asa@20 19 goldTexture = '%dg'
Asa@20 20 end
Asa@16 21
Asa@16 22 local gold = floor( copper / 10000 );
Asa@16 23 copper = mod(copper, 10000)
Asa@16 24 local silver = floor( copper / 100 );
Asa@16 25 copper = mod(copper, 100)
Asa@16 26
Asa@16 27
Asa@20 28 copper = color .. format(copperTexture, copper, 13, 13)
Asa@16 29 if silver > 0 or gold > 0 then
Asa@20 30 silver = color.. format(silverTexture, silver, 13, 13) .. ' '
Asa@16 31 else
Asa@16 32 silver = ""
Asa@16 33 end
Asa@16 34 if gold > 0 then
Asa@20 35 gold = color.. format(goldTexture, gold, 13, 13) .. ' '
Asa@16 36 else
Asa@16 37 gold = ""
Asa@16 38 end
Asa@16 39
Asa@16 40 return format("%s%s%s%s", prefix, gold, silver, copper)
Asa@6 41 end
Asa@6 42
Asa@58 43 -- Copied from QuickAuctions
Asa@65 44 function Utils.validateMoney(value)
Asa@58 45 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@58 46 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@58 47 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@58 48
Asa@58 49 if( not gold and not silver and not copper ) then
Asa@58 50 return false;
Asa@58 51 -- return L["Invalid monney format entered, should be \"#g#s#c\", \"25g4s50c\" is 25 gold, 4 silver, 50 copper."]
Asa@58 52 end
Asa@58 53
Asa@58 54 return true
Asa@58 55 end
Asa@58 56
Asa@58 57 -- Copied from QuickAuctions
Asa@65 58 function Utils.parseMoney(value)
Asa@58 59 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@58 60 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@58 61 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@58 62
Asa@58 63 -- Convert it all into copper
Asa@58 64 return (copper or 0) + ((gold or 0) * COPPER_PER_GOLD) + ((silver or 0) * COPPER_PER_SILVER)
Asa@58 65 end
Asa@58 66
Asa@7 67
Asa@7 68 local tmp_item_cache = {}
Asa@86 69 function Utils.GetItemID(item)
Asa@86 70 if not item then
Asa@86 71 return nil
Asa@86 72 end
Asa@86 73
Asa@86 74 if tmp_item_cache[item] == nil then
Asa@86 75 -- Whether item is a link or a name, both should return the full link
Asa@86 76 local _, itemLink = GetItemInfo (item);
Asa@7 77 if itemLink ~= nil then
Asa@7 78 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@86 79 tmp_item_cache[item] = tonumber(itemID)
Asa@94 80 else
Asa@94 81 local _, _, _, _, itemID = string.find(item, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@94 82 tmp_item_cache[item] = tonumber(itemID)
Asa@7 83 end
Asa@7 84 end
Asa@7 85
Asa@86 86 if tmp_item_cache[item] == nil then
Asa@9 87 for link, data in pairs(ItemAuditor.db.factionrealm.items) do
Asa@9 88 local name, itemLink = GetItemInfo (link);
Asa@86 89 if name == item then
Asa@9 90 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@86 91 tmp_item_cache[item] = tonumber(itemID)
Asa@9 92 end
Asa@9 93 end
Asa@9 94 end
Asa@86 95 return tmp_item_cache[item]
Asa@7 96 end
Asa@7 97
Asa@65 98
Asa@63 99 function ItemAuditor:GetLinkFromName(itemName)
Asa@9 100 local itemID = self:GetItemID(itemName)
Asa@9 101 local itemLink
Asa@9 102 if itemID ~= nil then
Asa@9 103 _, itemLink = GetItemInfo(itemID)
Asa@9 104 end
Asa@9 105
Asa@9 106 return itemLink
Asa@9 107 end
Asa@9 108
Asa@65 109 local SubjectPatterns = {
Asa@6 110 AHCancelled = gsub(AUCTION_REMOVED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 111 AHExpired = gsub(AUCTION_EXPIRED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 112 AHOutbid = gsub(AUCTION_OUTBID_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 113 AHSuccess = gsub(AUCTION_SOLD_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 114 AHWon = gsub(AUCTION_WON_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 115 CODPayment = gsub(COD_PAYMENT, "%%s", "(.*)"),
Asa@6 116 }
Asa@6 117
Asa@65 118 function Utils.GetMailType(msgSubject)
Asa@6 119 if msgSubject then
Asa@65 120 for k, v in pairs(SubjectPatterns) do
Asa@6 121 if msgSubject:find(v) then return k end
Asa@6 122 end
Asa@6 123 end
Asa@6 124 return "NonAHMail"
Asa@3 125 end
Asa@3 126
Asa@63 127 function ItemAuditor:tcount(tab)
Asa@3 128 local n = #tab
Asa@3 129 if (n == 0) then
Asa@3 130 for _ in pairs(tab) do
Asa@3 131 n = n + 1
Asa@3 132 end
Asa@3 133 end
Asa@3 134 return n
Asa@3 135 end
Asa@3 136
Asa@63 137 function ItemAuditor:GetDebug(info)
Asa@11 138 return self.db.char.debug
Asa@3 139 end
Asa@3 140
Asa@63 141 function ItemAuditor:SetDebug(info, input)
Asa@11 142
Asa@11 143 ItemAuditor.db.char.debug = input
Asa@3 144 local value = "off"
Asa@3 145 if input then
Asa@3 146 value = "on"
Asa@3 147 end
Asa@11 148 self:Print("Debugging is now: " .. value)
Asa@3 149 end
Asa@65 150
Asa@65 151 -- TODO: Once everything points to the correct Utils method, all of these should be removed
Asa@65 152
Asa@65 153 function ItemAuditor:FormatMoney(copper, color, textOnly)
Asa@65 154 return Utils.FormatMoney(copper, color, textOnly)
Asa@65 155 end
Asa@65 156
Asa@65 157
Asa@65 158 function ItemAuditor:GetMailType(msgSubject)
Asa@65 159 return Utils.GetMailType(msgSubject)
Asa@65 160 end
Asa@65 161
Asa@65 162 function ItemAuditor:GetItemID(itemName)
Asa@65 163 return Utils.GetItemID(itemName)
Asa@65 164 end
Asa@65 165
Asa@65 166 ItemAuditor.parseMoney = Utils.parseMoney
Asa@65 167 ItemAuditor.validateMoney = Utils.validateMoney