view core.lua @ 16:8a18dd9f2cec

Caching support...
author Aaron@Aaron-PC
date Sun, 15 Aug 2010 17:59:03 -0500
parents 38822958c28a
children 22686cb65c51
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 = {}
local db = {}
local safeRecipes = {}
local ids = {}

RecipeProfit.db = db;

-- Forward Definitions (for local functions)
local get_faction_db, 
      add_note, 
      button_update, 
      find_good_id, 
      safe_cache_vendor,
      get_note_title;

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",
    
    --submitting cached data not yet implemented
    enable_cache = false,
    location_cache = {},
}

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"] = {}
    local nodes = GatherMate.nodeIDs["RecipeProfit"]
    
    for id, note in pairs(RECIPEPROFIT_alliance) do
        safeRecipes[note.item] = true;
        nodes[get_note_title(note, "A")] = id * 2
        GatherMate.nodeTextures["RecipeProfit"][id * 2] = "Interface\\Icons\\INV_Scroll_05"
    end
    
    for id, note in pairs(RECIPEPROFIT_horde) do
        safeRecipes[note.item] = true;
        nodes[get_note_title(note, "H")] = (id - 1) * 2 + 1
        GatherMate.nodeTextures["RecipeProfit"][(id - 1) * 2 + 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 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)
        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     

function RecipeProfit:DoMerge()
    ids = {}
    local selectedDB = get_faction_db();
    
    GatherMate:ClearDB("RecipeProfit")
    for id, note in pairs(selectedDB) do
        x, y = find_good_id(note.x, note.y)
        add_note(x, y, note)
    end
    
    GatherMate:SendMessage("GatherMateDataImport")
    GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
end

function get_note_title(note, factionTag)
    if(not factionTag) then
        _, factionTag = get_faction_db();
    end
    
    return note.item.." - ("..note.vendor.." ".. factionTag ..")";
end

function add_note(x, y, note)
    GatherMate:AddNode(note.map, x / 100, y / 100, "RecipeProfit", get_note_title(note));
end

function get_faction_db()
    local factionAlliance = db.profile.faction == "Alliance" or 
                            db.profile.faction == "default" and UnitFactionGroup("player") == "Alliance";
    if(factionAlliance) then
        return RECIPEPROFIT_alliance, "A";
    else
        return RECIPEPROFIT_horde,    "H";
    end
end

function safe_cache_vendor()
    if(not profile.enable_cache) then
        return
    end
    
    if(not profile.location_cache[UnitName("NPC")]) then
        SetMapToCurrentZone()
        local pos = {}
        pos.x, pos.y = GetPlayerMapPosition("player")
        profile.location_cache[UnitName("NPC")] = pos
    end
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 
        safe_cache_vendor();
        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 find_good_id(x, y)
    if ids[x.." "..y] then
        return find_good_id(x + .01, y)
    end
    
    ids[x.." "..y] = true
    return x, y
end