view core.lua @ 13:4e7266527ce5 r13 release

Pre-Release commit. Bugfix on chat commands.
author Aaron Bregger
date Fri, 13 Aug 2010 14:30:44 -0500
parents 5ae9ffe0cc2d
children 38822958c28a
line wrap: on
line source
--[[--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))

    RecipeProfit by -[@project-author@]-
    
    Rev:     @project-revision@
    Updated: @file-date-iso@
    
--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))
    
    http://www.wrathguides.com/
    
--]]--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))

local RecipeProfit = LibStub("AceAddon-3.0"):NewAddon("RecipeProfit", "AceEvent-3.0", "AceConsole-3.0")
local GatherMate = LibStub("AceAddon-3.0"):GetAddon("GatherMate")
local tabletest = {}
 db = {}
local safeRecipes = {}
safer = safeRecipes
RecipeProfit.db = db;

function debugprint(val, indent)
    indent = indent or "";
    if not(type(val) == "table") then
        print("Not table: " .. val)
        return
    end
    
    for k,v in pairs(val) do
        if(type(k) == "table" and type(v) == "table") then
            print(indent .. "table key: {")
            debugprint(k, indent .. "    ")
            print(indent .. "} = {")
            debugprint(v, indent .. "    ")
            print(indent .. "}")
        elseif(type(v) == "table") then                
            print(indent .. k .. " = {")
            debugprint(v, indent .. "    ")
            print(indent .. "}") 
        else
            if(type(v) ~= "boolean") then
                print(indent .. k .. " = " .. v)
            else
                print(indent .. k .. " = " .. (v and "true" or "false"))
            end
        end
    end
end

local defaultProfile = {
    ["show"] = {
        ["RecipeProfit"] = "always",
        ["Herb Gathering"] = "never",
        ["Extract Gas"] = "never",
        ["Fishing"] = "never",
        ["Mining"] = "never",
        ["Treasure"] = "never",
    },
    ["trackShow"] = "active",
}

local options = {
 	type = "group",
	name = "RecipeProfit", -- addon name to import from, don't localize
	handler = {},
	disabled = false,
	args = {
        opt = {
            order = 1,
            name = "Select Database",
            desc = "Show a different database",
            type = "group",
            guiInline = true,
            args = {
                faction = {
                    order = 0,
                    name = "Faction",
                    desc = "Show a different database.",
                    type = "select",
                    values = {
                        ["Alliance"] = "Alliance",
                        ["Horde"]    = "Horde",
                        ["default"]  = "Default",
                    },
                    arg = "faction",
                },
                
                safeBuy = {
                    order = 1,
                    name = "Safe Recipe Buy",
                    desc = "Warn when buying a recipe not on the RecipeProfit list.",
                    type = "select",
                    values = {
                        --["on"] = "On",
                        ["off"]= "Off",
                    },
                    arg = "safebuy",
                },
            },
            get = function(k) return db.profile[k.arg]; end,
            set = function(k, v) db.profile[k.arg] = v; RecipeProfit:DoMerge(); end,
        },
		loadData = {
			order = 8,
			name = "Import Data",
			desc = "Load RecipeProfit and import the data to your database.",
			type = "execute",
			func = function() 
                RecipeProfit:DoMerge()
			end
        },
		loadProfile = {
			order = 9,
			name = "Load RecipeProfit Profile",
			desc = "Loads the RecipeProfit Profile into Gathermate for easy recipe tracking.",
			type = "execute",
			func = function() 
                GatherMate.db.profiles["RecipeProfit"] = GatherMate.db.profiles["RecipeProfit"] or {}
                gmdb = GatherMate.db.profiles["RecipeProfit"]
                for k, v in pairs(defaultProfile) do
                    gmdb[k] = v;
                end
                GatherMate:SendMessage("OnProfileChanged");
                GatherMate:GetModule("Config"):UpdateConfig()
                GatherMate:SendMessage("GatherMateConfigChanged")
                GatherMate.db:SetProfile("RecipeProfit")
			end
        },
    }
}

local defaults = {
    faction = "default",
    safebuy = "on",
}

function RecipeProfit:OnInitialize()
    profile = profile or defaults
    
    for k, v in pairs(defaults) do
        profile[k] = profile[k] or v;
    end
    
    self:RegisterChatCommand("recipeprofit", "ShowOptions")
    self:RegisterChatCommand("rp",           "ShowOptions")
    self:RegisterChatCommand("profit",       "ShowOptions")
    
    db.profile = profile
    db.storage = {}
    
    GatherMate:GetModule("Config"):RegisterModule("RecipeProfit", options)
    GatherMate:RegisterDBType("RecipeProfit", db.storage)
    GatherMate.db.profile.show["RecipeProfit"] = GatherMate.db.profile.show["RecipeProfit"] or "always"
    GatherMate.nodeIDs["RecipeProfit"] = {}
    GatherMate.nodeTextures["RecipeProfit"] = {}
    GatherMate.nodeMinHarvest["RecipeProfit"] = {}
    nodes = GatherMate.nodeIDs["RecipeProfit"]
    
    for id, note in pairs(RECIPEPROFIT_alliance) do
        safeRecipes[note.item] = true;
        nodes[note.item.." - ("..note.vendor.." A)"] = id * 10
        GatherMate.nodeTextures["RecipeProfit"][id * 10] = "Interface\\Icons\\INV_Scroll_05"
    end
    
    for id, note in pairs(RECIPEPROFIT_horde) do
        safeRecipes[note.item] = true;
        nodes[note.item.." - ("..note.vendor.." H)"] = id * 10 + 1
        GatherMate.nodeTextures["RecipeProfit"][id * 10 + 1] = "Interface\\Icons\\INV_Scroll_05"
    end
    
    GatherMate.reverseNodeIDs["RecipeProfit"] = GatherMate:CreateReversedTable(nodes)
    
    GatherMate:GetModule("Config").options.args.display.args.general.args.showGroup.args["showRecipeProfit"] = {
        order = 6,
        name = "Show RecipeProfit nodes.",
        desc = "Toggle showing nodes added by RecipeProfit.",
        type = "select",
        values = {
            ["always"] = "Always show",
            ["never"]  = "Never show",
        },
        arg = "RecipeProfit",
    }
    
    GatherMate:GetModule("Config").options.args.display.args.general.args.iconGroup.args.tracking.args["showRecipeProfit"] = {
        order = 6.5,
        name = "RecipeProfit",
        desc = "Color of the tracking circle.",
        type = "color",
        hasAlpha = true,
        arg = "RecipeProfit",		
    }
    
    GatherMate:GetModule("Config"):UpdateConfig()
    GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
end

function RecipeProfit:ShowOptions()
    LibStub("AceConfigDialog-3.0"):Open("GatherMate")
    LibStub("AceConfigDialog-3.0"):SelectGroup("GatherMate", "RecipeProfit")
end

function button_update(self)
    local buttonName = _G[self:GetName().."Name"];
    local link = GetMerchantItemLink(_G[self:GetName().."ItemButton"]:GetID());
    if(not link) then
        return;
    end
    
    local sName, sLink, iRarity, iLevel, iMinLevel, sType, sSubType, iStackCount = GetItemInfo(link)
    
    if(sType == "Recipe" and safeRecipes[sName]) then
        SetItemButtonNameFrameVertexColor(self, 0, 0, 1.0);
        SetItemButtonSlotVertexColor(self, 0, 0, 0.5);
        buttonName:SetText("* " .. sName)
        if(GetItemCount(link, true) == 0) then
            buttonName:SetTextColor(0,1,1);
        elseif(GetItemCount(link, true) < 5) then
            buttonName:SetTextColor(1,0,1);
        else
            buttonName:SetTextColor(1,0,0);
        end
    else
        buttonName:SetTextColor(GameFontNormalSmall:GetTextColor());
    end
end

function RecipeProfit:UpdateButtons(event)
    --print("UpdateButtons", event)
    if(not MerchantFrame:IsVisible()) then
        --print("UpdateButtons - (Event: ", event, ") - MerchantFrame not visible.");
        return;
    end
    
    for i=1, MERCHANT_ITEMS_PER_PAGE, 1 do
        local buttonframe = _G["MerchantItem"..i];
        local index = (((MerchantFrame.page - 1) * MERCHANT_ITEMS_PER_PAGE) + i);
        --print(index)
        if index <= GetMerchantNumItems() then
            button_update(buttonframe)
        --[[else
            --print(i, index, GetMerchantNumItems())
            local itemButton = _G["MerchantItem"..i.."ItemButton"];
            local merchantButton = _G["MerchantItem"..i];
            itemButton.price = nil;
			itemButton.hasItem = nil;
			itemButton:Hide();
			SetItemButtonNameFrameVertexColor(merchantButton, 0.5, 0.5, 0.5);
			SetItemButtonSlotVertexColor(merchantButton,0.4, 0.4, 0.4);
			_G["MerchantItem"..i.."Name"]:SetText("");
			_G["MerchantItem"..i.."MoneyFrame"]:Hide();
			_G["MerchantItem"..i.."AltCurrencyFrame"]:Hide();
            ]]
        end
    end
end

        
function RecipeProfit:OnEnable()

    _G["MerchantPrevPageButton"]:HookScript("OnClick", self.UpdateButtons)
    _G["MerchantNextPageButton"]:HookScript("OnClick", self.UpdateButtons)
    
    self:RegisterEvent("MERCHANT_SHOW",   "UpdateButtons")
    self:RegisterEvent("MERCHANT_UPDATE", "UpdateButtons")
    self:RegisterEvent("BAG_UPDATE",      "UpdateButtons")
    
    RecipeProfit:DoMerge()
end    

local ids = {}
function findGoodId(x, y)
    if ids[x.." "..y] then
        return findGoodId(x + .01, y)
    end
    
    ids[x.." "..y] = true
    return x, y
end    

function RecipeProfit:DoMerge()
    ids = {}
    selectedDB = (db.profile.faction == "Alliance" or 
        db.profile.faction == "default" and UnitFactionGroup("player") == "Alliance") and
        RECIPEPROFIT_alliance or RECIPEPROFIT_horde
    GatherMate:ClearDB("RecipeProfit")
    for id, note in pairs(selectedDB) do
        x, y = findGoodId(note.x, note.y)
        GatherMate:AddNode(note.map, x / 100, y / 100, "RecipeProfit",
            note.item.." - ("..note.vendor.." ".. 
            ((db.profile.faction == "Alliance" or db.profile.faction == "default" and 
            UnitFactionGroup("player") == "Alliance") and "A" or "H") ..")")
    end
    
    GatherMate:SendMessage("GatherMateDataImport")
    GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
end