annotate Modules/Utils.lua @ 94:4ec8611d9466

Fixed Enchanting. I was not getting the ItemID correctly, so enchants could not be mapped to the scrolls they were to created Changed snatch to only add each item once and to only add a snatch for items you don't have API: Added haveMaterials to the item and need to the reagents that get passed to queue destinations. This is in preparation for building a shopping list module.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 11 Aug 2010 23:48:23 -0700
parents 26f45b6e8d4d
children 53147a647e28
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@94 79 else
Asa@94 80 local _, _, _, _, itemID = string.find(item, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@94 81 tmp_item_cache[item] = tonumber(itemID)
Asa@7 82 end
Asa@7 83 end
Asa@7 84
Asa@86 85 if tmp_item_cache[item] == nil then
Asa@9 86 for link, data in pairs(ItemAuditor.db.factionrealm.items) do
Asa@9 87 local name, itemLink = GetItemInfo (link);
Asa@86 88 if name == item then
Asa@9 89 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@86 90 tmp_item_cache[item] = tonumber(itemID)
Asa@9 91 end
Asa@9 92 end
Asa@9 93 end
Asa@86 94 return tmp_item_cache[item]
Asa@7 95 end
Asa@7 96
Asa@65 97
Asa@63 98 function ItemAuditor:GetLinkFromName(itemName)
Asa@9 99 local itemID = self:GetItemID(itemName)
Asa@9 100 local itemLink
Asa@9 101 if itemID ~= nil then
Asa@9 102 _, itemLink = GetItemInfo(itemID)
Asa@9 103 end
Asa@9 104
Asa@9 105 return itemLink
Asa@9 106 end
Asa@9 107
Asa@65 108 local SubjectPatterns = {
Asa@6 109 AHCancelled = gsub(AUCTION_REMOVED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 110 AHExpired = gsub(AUCTION_EXPIRED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 111 AHOutbid = gsub(AUCTION_OUTBID_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 112 AHSuccess = gsub(AUCTION_SOLD_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 113 AHWon = gsub(AUCTION_WON_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 114 CODPayment = gsub(COD_PAYMENT, "%%s", "(.*)"),
Asa@6 115 }
Asa@6 116
Asa@65 117 function Utils.GetMailType(msgSubject)
Asa@6 118 if msgSubject then
Asa@65 119 for k, v in pairs(SubjectPatterns) do
Asa@6 120 if msgSubject:find(v) then return k end
Asa@6 121 end
Asa@6 122 end
Asa@6 123 return "NonAHMail"
Asa@3 124 end
Asa@3 125
Asa@63 126 function ItemAuditor:tcount(tab)
Asa@3 127 local n = #tab
Asa@3 128 if (n == 0) then
Asa@3 129 for _ in pairs(tab) do
Asa@3 130 n = n + 1
Asa@3 131 end
Asa@3 132 end
Asa@3 133 return n
Asa@3 134 end
Asa@3 135
Asa@63 136 function ItemAuditor:GetDebug(info)
Asa@11 137 return self.db.char.debug
Asa@3 138 end
Asa@3 139
Asa@63 140 function ItemAuditor:SetDebug(info, input)
Asa@11 141
Asa@11 142 ItemAuditor.db.char.debug = input
Asa@3 143 local value = "off"
Asa@3 144 if input then
Asa@3 145 value = "on"
Asa@3 146 end
Asa@11 147 self:Print("Debugging is now: " .. value)
Asa@3 148 end
Asa@65 149
Asa@65 150 -- TODO: Once everything points to the correct Utils method, all of these should be removed
Asa@65 151
Asa@65 152 function ItemAuditor:FormatMoney(copper, color, textOnly)
Asa@65 153 return Utils.FormatMoney(copper, color, textOnly)
Asa@65 154 end
Asa@65 155
Asa@65 156
Asa@65 157 function ItemAuditor:GetMailType(msgSubject)
Asa@65 158 return Utils.GetMailType(msgSubject)
Asa@65 159 end
Asa@65 160
Asa@65 161 function ItemAuditor:GetItemID(itemName)
Asa@65 162 return Utils.GetItemID(itemName)
Asa@65 163 end
Asa@65 164
Asa@65 165 ItemAuditor.parseMoney = Utils.parseMoney
Asa@65 166 ItemAuditor.validateMoney = Utils.validateMoney