WoWInterface SVN Ranch

[/] [trunk/] [ranch.lua] - Rev 24

Compare with Previous | Blame | View Log

-- Ensure that the required libaries are present.
assert(LibStub, "Ranch requires LibStub")
assert(LibStub:GetLibrary("LibDataBroker-1.1", true), "Ranch requires LibDataBroker-1.1")

-- Create reference to the LDB Libarary
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")

-- Create a local variable to hold the Ranch Icon.
local ranchIcon = "Interface\\Addons\\Ranch\\Ranch.tga"

-- Create a local table to hold references to the options buttons.
local ranchInterface = {}

-- Register the data source.
local dataobj = ldb:NewDataObject("Ranch", {type = "data source", icon = ranchIcon, text = "Ranch"})

-- Create a frame and register the "VARIABLES_LOADED" event on it.
CreateFrame("Frame", "RanchFrame");
RanchFrame:SetScript("OnEvent", function(self, event, ...) getglobal("Ranch_"..event)(event, ...) end);
RanchFrame:RegisterEvent("VARIABLES_LOADED")


-- create local variables for the tracking facility
local ranchtrack = nil
local ranchgoal = nil

-- Event handler for the VARIABLES_LOADED event.
function Ranch_VARIABLES_LOADED(event, ...)

        -- Unregister the VARIABLES_LOADED event. Probably not necessary, but keeps things tidy.
        RanchFrame:UnregisterEvent("VARIABLES_LOADED")
        
        -- Create a GameTooltip for use later. This tooltip can be scanned to detect whether or not an item is BoE.
        CreateFrame( "GameTooltip", "RanchGameTip" );
        RanchGameTip:SetOwner( WorldFrame, "ANCHOR_NONE" );
        RanchGameTip:AddFontStrings(
                RanchGameTip:CreateFontString( "$parentTextLeft1", nil, "GameTooltipText" ),
                RanchGameTip:CreateFontString( "$parentTextRight1", nil, "GameTooltipText" )
                );
        
        -- Create the ranch_perm table if it doesn't exist.
        if ranch_perm == nil then
                ranch_perm = {}
        end
        
        -- Create the ranch_options table if it doesn't exist.
        if ranch_options == nil then
                ranch_options = {}
        end
        
        -- Create a local table to hold the default option values.
        local rtemp_opts = {
                                                ["types"] = {},
                                                ["displayitems"] = true,
                                                ["mergeitems"] = false,
                                                ["questitems"] = true,
                                                ["ldbicon"] = true,
                                                ["ldbtext"] = true,
                                                ["ldbname"] = true,
                                                }

        -- Run through the default settings, copying any that don't currently exist to the user's settings.
        local k, v
        for k, v in pairs(rtemp_opts) do
                if ranch_options[k] == nil then
                        ranch_options[k] = v
                end
        end

        -- If the user has chosen to hide the LDB text, then do so now.
        if not ranch_options["ldbtext"] then
                dataobj.text = ""
        end
        
        -- Create an empty table to hold any temporary items.
        ranch_temp = {}
        
        -- Set the RanchTradeTypes variable
        RanchTradeTypes = 0
        
        -- Create the About Panel.
        RanchAbout = LibStub("tekKonfig-AboutPanel").new(nil, "Ranch")
        
        -- Create the Items Options
        local RanchItemOptions = CreateFrame("Frame", "RanchItemOpts", InterfaceOptionsFramePanelContainer)
        RanchItemOptions.name = "Item Options"
        RanchItemOptions.parent = "Ranch"
        local title, subtitle = LibStub("tekKonfig-Heading").new(RanchItemOptions, "Ranch Item Options", "This panel controls how Ranch deals with certain item types.")
        
        -- Add Display Items option.
        local displayitems = LibStub("tekKonfig-Checkbox").new(RanchItemOptions, nil, "Display BoE items?", "TOPLEFT", subtitle, "BOTTOMLEFT", -2, -8)
        local checksound = displayitems:GetScript("OnClick")
        displayitems:SetScript("OnClick", function(self) checksound(self); ranch_options["displayitems"] = not ranch_options["displayitems"]; end)
        displayitems:SetChecked(ranch_options["displayitems"])

        -- Add Merge Items option.
        local mergeitems = LibStub("tekKonfig-Checkbox").new(RanchItemOptions, nil, "Merge BoE items?", "TOPLEFT", displayitems, "BOTTOMLEFT", 0, -8)
        local checksound = mergeitems:GetScript("OnClick")
        mergeitems:SetScript("OnClick", function(self) checksound(self); ranch_options["mergeitems"] = not ranch_options["mergeitems"]; end)
        mergeitems:SetChecked(ranch_options["mergeitems"])
        
        -- Add Quest Giving Items option
        local questitems = LibStub("tekKonfig-Checkbox").new(RanchItemOptions, nil, "Show items that give quests?", "TOPLEFT", mergeitems, "BOTTOMLEFT", 0, -8)
        local checksound = questitems:GetScript("OnClick")
        questitems:SetScript("OnClick", function(self) checksound(self); ranch_options["questitems"] = not ranch_options["questitems"]; end)
        questitems:SetChecked(ranch_options["questitems"])

        -- Add Display LDB text option
        local ldbtext = LibStub("tekKonfig-Checkbox").new(RanchItemOptions, nil, "Display LDB text when not tracking anything?", "TOPLEFT", questitems, "BOTTOMLEFT", -2, -8)
        local checksound = ldbtext:GetScript("OnClick")
        ldbtext:SetScript("OnClick", function(self) checksound(self); ranch_options["ldbtext"] = not ranch_options["ldbtext"]; Ranch_BAG_UPDATE(nil); end)
        ldbtext:SetChecked(ranch_options["ldbtext"])
        
        -- Add an "OnShow" script to ensure the options are displayed correctly.
        --RanchItemOptions:SetScript("OnShow", function(self) displayitems:SetChecked(ranch_options["displayitems"]); mergeitems:SetChecked(ranch_options["mergeitems"]); questitems:SetChecked(ranch_options["questitems"]) end)
        
        -- Add Items Options to the Interface Menu.
        InterfaceOptions_AddCategory(RanchItemOptions)
        
        -- Create the Trade Goods options
        local RanchTradeOptions = CreateFrame("Frame", "RanchTradeOpts", InterfaceOptionsFramePanelContainer)
        RanchTradeOptions.name = "Trade Goods"
        RanchTradeOptions.parent = "Ranch"
        local title, subtitle = LibStub("tekKonfig-Heading").new(RanchTradeOptions, "Ranch Trade Goods Options", "This panel controls how Ranch deals with Trade Goods.")
        RanchTradeOptions:SetScript("OnShow", function(RanchTradeOptions)
                if RanchTradeTypes < Ranch_tcount(ranch_options["types"]) then
                        RanchTradeTypes = Ranch_tcount(ranch_options["types"])
                        local goods, shown, rows
                        rows = 0
                        for goods, shown in pairs(ranch_options["types"]) do
                                if shown then
                                        local GoodsCheck = LibStub("tekKonfig-Checkbox").new(RanchTradeOptions, nil, "Show "..goods.."?", "TOPLEFT", subtitle, "BOTTOMLEFT", -2, -8 - (rows*26))
                                        rows = rows + 1
                                        local checksound = GoodsCheck:GetScript("OnClick")
                                        GoodsCheck:SetScript("OnClick", function(self) checksound(self); ranch_options["types"][goods] = not ranch_options["types"][goods]; end)
                                        GoodsCheck:SetChecked(ranch_options["types"][goods])
                                end
                        end
                end
        end)
        InterfaceOptions_AddCategory(RanchTradeOptions)
        
        -- Create the LDB feed options
        local RanchLDBOptions = CreateFrame("Frame", "RanchLDBOpts", InterfaceOptionsFramePanelContainer)
        RanchLDBOptions.name = "Data Broker"
        RanchLDBOptions.parent = "Ranch"
        local title, subtitle = LibStub("tekKonfig-Heading").new(RanchLDBOptions, "Ranch Data Broker Options", "This panel controls Ranch's Data Broker feed.")
        ranchInterface.ldb = {}
        
        -- Add Display LDB text option
        ranchInterface.ldb.text = LibStub("tekKonfig-Checkbox").new(RanchLDBOptions, nil, "Display LDB text when not tracking anything?", "TOPLEFT", subtitle, "BOTTOMLEFT", -2, -8)
        local checksound = ranchInterface.ldb.text:GetScript("OnClick")
        ranchInterface.ldb.text:SetScript("OnClick", function(self)
                checksound(self);
                ranch_options["ldbtext"] = not ranch_options["ldbtext"];
                if ranchtrack == nil then
                        if ranch_options["ldbtext"] then
                                dataobj.text = "Ranch";
                        else
                                dataobj.text = "";
                        end;
                end;
        end)
        ranchInterface.ldb.text:SetChecked(ranch_options["ldbtext"])
        
        -- Add Display item icon option
        ranchInterface.ldb.icon = LibStub("tekKonfig-Checkbox").new(RanchLDBOptions, nil, "Change Ranch icon to item icon when tracking?", "TOPLEFT", ranchInterface.ldb.text, "BOTTOMLEFT", 0, -8)
        local checksound = ranchInterface.ldb.icon:GetScript("OnClick")
        ranchInterface.ldb.icon:SetScript("OnClick", function(self) checksound(self); ranch_options["ldbicon"] = not ranch_options["ldbicon"]; Ranch_BAG_UPDATE(nil); if ranch_options["ldbicon"] then ranchInterface.ldb.name:Enable(); else ranchInterface.ldb.name:Disable(); end; end)
        ranchInterface.ldb.icon:SetChecked(ranch_options["ldbicon"])
        
        -- Add Display item name option
        ranchInterface.ldb.name = LibStub("tekKonfig-Checkbox").new(RanchLDBOptions, nil, "Show item name when tracking?", "TOPLEFT", ranchInterface.ldb.icon, "BOTTOMLEFT", 20, -8)
        local checksound = ranchInterface.ldb.name:GetScript("OnClick")
        ranchInterface.ldb.name.tiptext = "This options controls whether the item's name will appear in the data broker text when tracking an item."
        ranchInterface.ldb.name:SetScript("OnClick", function(self) checksound(self); ranch_options["ldbname"] = not ranch_options["ldbname"]; Ranch_BAG_UPDATE(nil); end)
        ranchInterface.ldb.name:SetChecked(ranch_options["ldbname"])

        -- Add the LDB options page to the Interface Options panel.
        InterfaceOptions_AddCategory(RanchLDBOptions)
        
        -- Initialise the slash command handler.
        SLASH_RANCH1 = "/ranch"
        SlashCmdList["RANCH"] = Ranch_Command;
        
end

function Ranch_Command(slashargs)
        local startPos, endPos, firstWord, restOfString = string.find(slashargs, "(%w+)[%s%p]*(.*)");
        if firstWord ~= nil then
                firstWord = strlower(firstWord)
        end
        
        if firstWord == "track" then
                if restOfString == "" or restOfString == nil then
                        ranchtrack = nil
                        RanchFrame:UnregisterEvent("BAG_UPDATE");
                        if ranch_options["ldbtext"] then
                                dataobj.text = "Ranch";
                                dataobj.icon = ranchIcon
                        else
                                dataobj.text = "";
                                dataobj.icon = ranchIcon
                        end
                else
                        ranchtrack = restOfString
                        Ranch_BAG_UPDATE(nil)
                        
                        -- Register the BAG_UPDATE event.
                        RanchFrame:RegisterEvent("BAG_UPDATE");
                end
        
        elseif firstWord == "goal" then
                if restOfString == nil then
                        ranchgoal = nil
                        
                elseif ranchtrack == nil then
                        print("You need to set an item to be tracked first!")
                        
                elseif type(tonumber(restOfString)) ~= "number" then
                        print("You must supply the number of items you wish to gather!")
                        
                elseif tonumber(restOfString) < 1 then
                        print("You must set a goal of at least one item to gather!")
                        
                else
                        ranchgoal = {tonumber(restOfString), false}
                
                end
        elseif firstWord == "temp" then
                ranch_temp[Ranch_ItemID(restOfString)] = true
        elseif firstWord == "add" then
                if restOfString ~= nil and restOfString ~= "" then
                        local itemID = Ranch_ItemID(restOfString)
                        ranch_perm[ItemID] = true
                end
        elseif firstWord == "remove" then
                if restOfString ~= nil and restOfString ~= "" then
                        local itemID = Ranch_ItemID(restOfString)
                        local temptable = {}
                        local k,v
                        for k,_ in pairs(ranch_perm) do
                                if k ~= itemID then
                                        temptable[k] = true
                                end
                        end
                        ranch_perm = temptable
                        temptable = nil
                end
        elseif firstWord == "removeall" then
                ranch_perm = {}
        else
                DEFAULT_CHAT_FRAME:AddMessage("Ranch accepts the following arguments:")
                DEFAULT_CHAT_FRAME:AddMessage("track [itemlink]: Tracks an item in the LDB display")
                DEFAULT_CHAT_FRAME:AddMessage("temp [itemlink]: Tracks an item in the LDB tooltip under the 'Temporary' heading. This item will disappear after you logout.")
                DEFAULT_CHAT_FRAME:AddMessage("add [itemlink]: Tracks an item in the LDB tooltip under the 'Custom' heading. This item will only be removed using the 'remove' or 'removeall' commands.")
                DEFAULT_CHAT_FRAME:AddMessage("remove [itemlink]: Removes an item being tracked in the LDB tooltip under the 'Custom' heading.")
                DEFAULT_CHAT_FRAME:AddMessage("removeall: Removes all items being tracked in the LDB tooltip under the 'Custom' heading.")
        end
end

function Ranch_ItemID(itemLink)
        local found, _, itemString = string.find(itemLink, "^|c%x+|H(.+)|h%[.*%]")
        if itemString == nil then
                found, _, itemString = string.find(itemLink, "^c%x+|H(.+)|h%[.*%]")
        end
        if itemString == nil then
                return nil
        else
                local _, itemId, enchantId, jewelId1, jewelId2, jewelId3, jewelId4, suffixId, uniqueId = strsplit(":", itemString)
                return itemId
        end
end

function dataobj:OnTooltipShow()
    local i, j
        local ranch_items = {}
        local ranch_boe = {}
        local ranch_qg = {} -- Quest Giving Items
        local ranch_cash = 0
        local ranch_uncost = false
        local ranch_junk = false
        for i = 0, 4 do
                for j = 1, GetContainerNumSlots(i) do
                        local ContainerItemLink = GetContainerItemLink(i,j)
                        if ContainerItemLink ~= nil then
                                local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemValue = GetItemInfo(ContainerItemLink)                     
                                local itemId = Ranch_ItemID(itemLink)
                                if itemType == "Trade Goods" then
                                        if ranch_items[itemSubType] == nil then
                                                ranch_items[itemSubType] = {}
                                        end
                                        if ranch_items[itemSubType][itemId] == nil then
                                                ranch_items[itemSubType][itemId] = GetItemCount(itemLink)
                                        end
                                elseif itemType == "Gem" then
                                        if ranch_items["Gem"] == nil then
                                                ranch_items["Gem"] = {}
                                        end
                                        if ranch_items["Gem"][itemId] == nil then
                                                ranch_items["Gem"][itemId] = GetItemCount(itemLink)
                                        end
                                elseif itemRarity == 0 then
                                        ranch_junk = true
                                        if itemValue == nil then
                                                ranch_uncost = true
                                        else
                                                ranch_cash = ranch_cash + (itemValue * select(2, GetContainerItemInfo(i,j)))
                                        end
                                elseif itemRarity >=2 then
                                        RanchGameTip:ClearLines();
                                        RanchGameTip:SetBagItem(i,j);
                                        if ((RanchGameTipTextLeft2:GetText() ~= "Soulbound") and (RanchGameTipTextLeft2:GetText() ~= "Quest Item") and RanchGameTipTextLeft2:GetText() ~= "Account Bound") then
                                                if ranch_options["mergeitems"] then
                                                        ranch_boe[itemId] = itemValue
                                                else
                                                        ranch_boe[itemLink] = itemValue
                                                end
                                        end
                                end
                                RanchGameTip:ClearLines();
                                RanchGameTip:SetBagItem(i,j);
                                
                                -- Looking for Quest Giving Items
                                local iRow
                                for iRow = 2, 4 do
                                        if _G["RanchGameTipTextLeft"..iRow] ~= nil then
                                                if _G["RanchGameTipTextLeft"..iRow]:GetText() == ITEM_STARTS_QUEST then
                                                        tinsert(ranch_qg, itemId)
                                                end
                                        end
                                end
                                
                                if ranch_temp[itemId] ~= nil then
                                        ranch_temp[itemId] = GetItemCount(itemLink)
                                end
                        end
                end
        end
        
        local itemGroup, Collection, itemId, itemCount
        self:AddLine("Ranch")
        
        if ranch_options["displayitems"] and Ranch_tcount(ranch_boe) ~= 0 then
                self:AddLine(" ")
                self:AddLine("Items")
                for itemDesc, itemCost in pairs(ranch_boe) do
                        if ranch_options["mergeitems"] then
                                local itemCount = GetItemCount(itemDesc)
                                local _, _, _, textcash = Ranch_FormatCash(itemCost * itemCount)
                                local _, itemLink = GetItemInfo(itemDesc)
                                self:AddDoubleLine(itemLink.." (x"..itemCount..")", textcash)
                        else
                                local _, _, _, textcash = Ranch_FormatCash(itemCost)
                                self:AddDoubleLine(itemDesc, textcash)
                        end
                end
        end
        
        if ranch_options["questitems"] and Ranch_tcount(ranch_qg) ~= 0 then
                self:AddLine(" ")
                self:AddLine("Quest Givers")
                for _, itemDesc in ipairs(ranch_qg) do
                        local _, itemLink = GetItemInfo(itemDesc)
                        self:AddLine(itemLink)
                end
        end
        
        for itemGroup, Collection in pairs(ranch_items) do
                if ranch_options["types"][itemGroup] == nil then
                        ranch_options["types"][itemGroup] = true
                end
                if ranch_options["types"][itemGroup] then
                        self:AddLine(" ")
                        self:AddLine(itemGroup)
                        for itemId, itemCount in pairs(Collection) do
                                _, itemLink = GetItemInfo(itemId)
                                self:AddDoubleLine(itemLink,itemCount)
                        end
                end
        end

        if Ranch_tcount(ranch_perm) > 0 then
                self:AddLine(" ")
                self:AddLine("Custom")
                local i,v
                for i,v in pairs(ranch_perm) do
                        if v then
                                local _, itemLink = GetItemInfo(i)
                                self:AddDoubleLine(itemLink, GetItemCount(itemLink))
                        end
                end
        end
        
        if Ranch_tcount(ranch_temp) > 0 then
                self:AddLine(" ")
                self:AddLine("Temporary")
                local i,v
                for i,v in pairs(ranch_temp) do
                        local _, itemLink = GetItemInfo(i)
                        self:AddDoubleLine(itemLink, GetItemCount(itemLink))
                end
        end
        
        if ranch_junk then
                self:AddLine(" ")
                local textcash
                if ranch_uncost and ranch_cost == 0 then
                        textcash = "??"
                else
                        _, _, _, textcash = Ranch_FormatCash(ranch_cash)
                        if ranch_uncost then
                                textcash = textcash.."+"
                        end
                end
                self:AddDoubleLine("Junk:", textcash)
        end
end     

function Ranch_FormatCash(cash)
        local gold = floor(cash/10000)
        local silver = floor((cash-(gold*10000))/100)
        local copper = cash - (silver * 100) - (gold * 10000)
        local textcash = ""
        if gold > 0 then
                textcash = "|cFFFFFFFF"..gold.."|r|cffffd700g|r|cFFFFFFFF, |r"
        end
        if silver > 0 then
                textcash = textcash.."|cFFFFFFFF"..silver.."|r|cffc7c7cfs|r|cFFFFFFFF, |r"
        end
        textcash = textcash.."|cFFFFFFFF"..copper.."|r|cffeda55fc|r"
        return gold, silver, copper, textcash
end

function Ranch_BAG_UPDATE(...)
        if ranchtrack ~= nil then
                local itemIcon = GetItemIcon(ranchtrack)
                if itemIcon ~= nil and ranch_options["ldbicon"] then
                        dataobj.icon = itemIcon
                else
                        dataobj.icon = ranchIcon
                end
                local itemCount = GetItemCount(ranchtrack)
                dataobj.text = "|r|"..ranchtrack.." - "..itemCount;
                if ranchgoal ~= nil then
                        if itemCount < ranchgoal[1] and ranchgoal[2] then
                                ranchgoal[2] = false
                                
                        elseif itemCount >= ranchgoal[1] and not ranchgoal[2] then
                                 RaidNotice_AddMessage(RaidBossEmoteFrame, "You now have "..itemCount.." of "..ranchtrack.."!", ChatTypeInfo["GUILD"])
                                 ranchgoal[2] = true
                        end
                end
        end
end

 -- tcount: count table members even if they're not indexed by numbers
 -- From the Table Helpers page by Mikk on WoWWiki (http://www.wowwiki.com/Table_Helpers)
 function Ranch_tcount(tab)
   local n=0;
   for _ in pairs(tab) do
     n=n+1;
   end
   return n;
 end
 

Compare with Previous | Blame