annotate Modules/Utils.lua @ 92:71053b1f6c1c

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