annotate Modules/Utils.lua @ 58:bdf3aba93aa9

Refactored the way the window for /ia invested works so I can add more tabs.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sun, 25 Jul 2010 15:51:00 -0700
parents 003de902ae64
children e7d287cc3b02
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@15 2 local addon = _G[addonName]
Asa@3 3
Asa@20 4 function addon: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@58 43 function ItemAuditor.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@58 57 function ItemAuditor.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
Asa@58 65 end
Asa@58 66
Asa@8 67 -- This is only here to make sure this doesn't blow up if ReplaceItemCache is never called
Asa@7 68 local item_db = {}
Asa@7 69
Asa@7 70 function addon:ReplaceItemCache(new_cache)
Asa@7 71 item_db = new_cache
Asa@7 72 end
Asa@7 73
Asa@7 74 -- This will be reset every session
Asa@7 75 local tmp_item_cache = {}
Asa@7 76 function addon:GetItemID(itemName)
Asa@7 77 if item_db[itemName] ~= nil then
Asa@7 78 return item_db[itemName]
Asa@7 79 end
Asa@7 80
Asa@7 81 if tmp_item_cache[itemName] == nil then
Asa@7 82 local _, itemLink = GetItemInfo (itemName);
Asa@7 83 if itemLink ~= nil then
Asa@7 84 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@7 85 tmp_item_cache[itemName] = tonumber(itemID)
Asa@7 86 end
Asa@7 87 end
Asa@7 88
Asa@9 89 if tmp_item_cache[itemName] == nil then
Asa@9 90 for link, data in pairs(ItemAuditor.db.factionrealm.items) do
Asa@9 91 local name, itemLink = GetItemInfo (link);
Asa@9 92 if name == itemName then
Asa@9 93 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 94 tmp_item_cache[itemName] = tonumber(itemID)
Asa@9 95 end
Asa@9 96 end
Asa@9 97
Asa@9 98 end
Asa@9 99
Asa@7 100 return tmp_item_cache[itemName]
Asa@7 101 end
Asa@7 102
Asa@9 103 function addon:GetLinkFromName(itemName)
Asa@9 104 local itemID = self:GetItemID(itemName)
Asa@9 105 local itemLink
Asa@9 106 if itemID ~= nil then
Asa@9 107 _, itemLink = GetItemInfo(itemID)
Asa@9 108 end
Asa@9 109
Asa@9 110 return itemLink
Asa@9 111 end
Asa@9 112
Asa@7 113 function addon:SaveItemID(itemName, id)
Asa@7 114 item_db[itemName] = tonumber(id)
Asa@7 115 end
Asa@7 116
Asa@24 117 addon.SubjectPatterns = {
Asa@6 118 AHCancelled = gsub(AUCTION_REMOVED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 119 AHExpired = gsub(AUCTION_EXPIRED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 120 AHOutbid = gsub(AUCTION_OUTBID_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 121 AHSuccess = gsub(AUCTION_SOLD_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 122 AHWon = gsub(AUCTION_WON_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 123 CODPayment = gsub(COD_PAYMENT, "%%s", "(.*)"),
Asa@6 124 }
Asa@6 125
Asa@6 126 function addon:GetMailType(msgSubject)
Asa@6 127 if msgSubject then
Asa@24 128 for k, v in pairs(self.SubjectPatterns) do
Asa@6 129 if msgSubject:find(v) then return k end
Asa@6 130 end
Asa@6 131 end
Asa@6 132 return "NonAHMail"
Asa@3 133 end
Asa@3 134
Asa@3 135 function addon:tcount(tab)
Asa@3 136 local n = #tab
Asa@3 137 if (n == 0) then
Asa@3 138 for _ in pairs(tab) do
Asa@3 139 n = n + 1
Asa@3 140 end
Asa@3 141 end
Asa@3 142 return n
Asa@3 143 end
Asa@3 144
Asa@3 145 function addon:GetDebug(info)
Asa@11 146 return self.db.char.debug
Asa@3 147 end
Asa@3 148
Asa@3 149 function addon:SetDebug(info, input)
Asa@11 150
Asa@11 151 ItemAuditor.db.char.debug = input
Asa@3 152 local value = "off"
Asa@3 153 if input then
Asa@3 154 value = "on"
Asa@3 155 end
Asa@11 156 self:Print("Debugging is now: " .. value)
Asa@3 157 end