annotate Modules/Utils.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 = {}
Asa@3 3
Asa@3 4 local AceConsole = LibStub("AceConsole-3.0")
Asa@3 5 AceConsole:Embed(addon)
Asa@3 6
Asa@3 7 addonTable.utils = addon
Asa@3 8
Asa@3 9 function addon:FormatMoney(money)
Asa@6 10 local prefix = ""
Asa@6 11 if money < 0 then
Asa@6 12 prefix = "-"
Asa@6 13 end
Asa@6 14 return prefix .. Altoholic:GetMoneyString(abs(money), WHITE, false)
Asa@6 15 end
Asa@6 16
Asa@8 17 -- This is only here to make sure this doesn't blow up if ReplaceItemCache is never called
Asa@7 18 local item_db = {}
Asa@7 19
Asa@7 20 function addon:ReplaceItemCache(new_cache)
Asa@7 21 item_db = new_cache
Asa@7 22 end
Asa@7 23
Asa@7 24 -- This will be reset every session
Asa@7 25 local tmp_item_cache = {}
Asa@7 26 function addon:GetItemID(itemName)
Asa@7 27 if item_db[itemName] ~= nil then
Asa@7 28 return item_db[itemName]
Asa@7 29 end
Asa@7 30
Asa@7 31 if tmp_item_cache[itemName] == nil then
Asa@7 32 local _, itemLink = GetItemInfo (itemName);
Asa@7 33 if itemLink ~= nil then
Asa@7 34 local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@7 35 tmp_item_cache[itemName] = tonumber(itemID)
Asa@7 36 end
Asa@7 37 end
Asa@7 38
Asa@7 39 return tmp_item_cache[itemName]
Asa@7 40 end
Asa@7 41
Asa@7 42 function addon:SaveItemID(itemName, id)
Asa@7 43 item_db[itemName] = tonumber(id)
Asa@7 44 end
Asa@7 45
Asa@6 46 local SubjectPatterns = {
Asa@6 47 AHCancelled = gsub(AUCTION_REMOVED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 48 AHExpired = gsub(AUCTION_EXPIRED_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 49 AHOutbid = gsub(AUCTION_OUTBID_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 50 AHSuccess = gsub(AUCTION_SOLD_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 51 AHWon = gsub(AUCTION_WON_MAIL_SUBJECT, "%%s", ".*"),
Asa@6 52 CODPayment = gsub(COD_PAYMENT, "%%s", "(.*)"),
Asa@6 53 }
Asa@6 54
Asa@6 55 function addon:GetMailType(msgSubject)
Asa@6 56 if msgSubject then
Asa@6 57 for k, v in pairs(SubjectPatterns) do
Asa@6 58 if msgSubject:find(v) then return k end
Asa@6 59 end
Asa@6 60 end
Asa@6 61 return "NonAHMail"
Asa@3 62 end
Asa@3 63
Asa@3 64 function addon:tcount(tab)
Asa@3 65 local n = #tab
Asa@3 66 if (n == 0) then
Asa@3 67 for _ in pairs(tab) do
Asa@3 68 n = n + 1
Asa@3 69 end
Asa@3 70 end
Asa@3 71 return n
Asa@3 72 end
Asa@3 73
Asa@8 74 function addon:GetSafeLink(link)
Asa@8 75 if link ~= string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@8 76 link = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@8 77 end
Asa@8 78 return link and string.gsub(link, ":0:0:0:0:0:0", "")
Asa@8 79 end
Asa@8 80
Asa@8 81 function addon:GetIDFromLink(link)
Asa@8 82 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@8 83 return tonumber(Id)
Asa@8 84 end
Asa@3 85
Asa@3 86 function addon:GetDebug(info)
Asa@3 87 return true
Asa@3 88 -- return self.db.char.debug
Asa@3 89 end
Asa@3 90
Asa@3 91 function addon:SetDebug(info, input)
Asa@3 92 self:Print("Debugging is now: " .. value)
Asa@3 93 self.db.char.debug = input
Asa@3 94 local value = "off"
Asa@3 95 if input then
Asa@3 96 value = "on"
Asa@3 97 end
Asa@3 98
Asa@3 99 end