annotate core.lua @ 16:8a18dd9f2cec

Caching support...
author Aaron@Aaron-PC
date Sun, 15 Aug 2010 17:59:03 -0500
parents 38822958c28a
children 22686cb65c51
rev   line source
Aaron@4 1 --[[--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))
Aaron@4 2
Aaron@4 3 RecipeProfit by -[@project-author@]-
Aaron@4 4
Aaron@4 5 Rev: @project-revision@
Aaron@4 6 Updated: @file-date-iso@
Aaron@4 7
Aaron@4 8 --))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))
Aaron@4 9
Aaron@4 10 http://www.wrathguides.com/
Aaron@4 11
Aaron@4 12 --]]--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))
Aaron@4 13
Aaron@12 14 local RecipeProfit = LibStub("AceAddon-3.0"):NewAddon("RecipeProfit", "AceEvent-3.0", "AceConsole-3.0")
Aaron@0 15 local GatherMate = LibStub("AceAddon-3.0"):GetAddon("GatherMate")
Aaron@0 16 local tabletest = {}
Aaron@16 17 local db = {}
Aaron@1 18 local safeRecipes = {}
Aaron@16 19 local ids = {}
Aaron@16 20
Aaron@0 21 RecipeProfit.db = db;
Aaron@0 22
Aaron@16 23 -- Forward Definitions (for local functions)
Aaron@16 24 local get_faction_db,
Aaron@16 25 add_note,
Aaron@16 26 button_update,
Aaron@16 27 find_good_id,
Aaron@16 28 safe_cache_vendor,
Aaron@16 29 get_note_title;
Aaron@16 30
Aaron@0 31 function debugprint(val, indent)
Aaron@0 32 indent = indent or "";
Aaron@0 33 if not(type(val) == "table") then
Aaron@0 34 print("Not table: " .. val)
Aaron@0 35 return
Aaron@0 36 end
Aaron@0 37
Aaron@0 38 for k,v in pairs(val) do
Aaron@0 39 if(type(k) == "table" and type(v) == "table") then
Aaron@0 40 print(indent .. "table key: {")
Aaron@0 41 debugprint(k, indent .. " ")
Aaron@0 42 print(indent .. "} = {")
Aaron@0 43 debugprint(v, indent .. " ")
Aaron@0 44 print(indent .. "}")
Aaron@12 45 elseif(type(v) == "table") then
Aaron@0 46 print(indent .. k .. " = {")
Aaron@0 47 debugprint(v, indent .. " ")
Aaron@0 48 print(indent .. "}")
Aaron@0 49 else
Aaron@12 50 if(type(v) ~= "boolean") then
Aaron@12 51 print(indent .. k .. " = " .. v)
Aaron@12 52 else
Aaron@12 53 print(indent .. k .. " = " .. (v and "true" or "false"))
Aaron@12 54 end
Aaron@0 55 end
Aaron@0 56 end
Aaron@0 57 end
Aaron@8 58
Aaron@7 59 local defaultProfile = {
Aaron@7 60 ["show"] = {
Aaron@7 61 ["RecipeProfit"] = "always",
Aaron@7 62 ["Herb Gathering"] = "never",
Aaron@7 63 ["Extract Gas"] = "never",
Aaron@7 64 ["Fishing"] = "never",
Aaron@7 65 ["Mining"] = "never",
Aaron@7 66 ["Treasure"] = "never",
Aaron@7 67 },
Aaron@8 68 ["trackShow"] = "active",
Aaron@7 69 }
Aaron@0 70
Aaron@0 71 local options = {
Aaron@0 72 type = "group",
Aaron@0 73 name = "RecipeProfit", -- addon name to import from, don't localize
Aaron@0 74 handler = {},
Aaron@0 75 disabled = false,
Aaron@0 76 args = {
Aaron@1 77 opt = {
Aaron@0 78 order = 1,
Aaron@0 79 name = "Select Database",
Aaron@0 80 desc = "Show a different database",
Aaron@0 81 type = "group",
Aaron@0 82 guiInline = true,
Aaron@0 83 args = {
Aaron@0 84 faction = {
Aaron@0 85 order = 0,
Aaron@0 86 name = "Faction",
Aaron@0 87 desc = "Show a different database.",
Aaron@0 88 type = "select",
Aaron@0 89 values = {
Aaron@0 90 ["Alliance"] = "Alliance",
Aaron@0 91 ["Horde"] = "Horde",
Aaron@1 92 ["default"] = "Default",
Aaron@0 93 },
Aaron@0 94 arg = "faction",
Aaron@0 95 },
Aaron@1 96
Aaron@1 97 safeBuy = {
Aaron@1 98 order = 1,
Aaron@1 99 name = "Safe Recipe Buy",
Aaron@1 100 desc = "Warn when buying a recipe not on the RecipeProfit list.",
Aaron@1 101 type = "select",
Aaron@1 102 values = {
Aaron@7 103 --["on"] = "On",
Aaron@1 104 ["off"]= "Off",
Aaron@1 105 },
Aaron@1 106 arg = "safebuy",
Aaron@1 107 },
Aaron@0 108 },
Aaron@1 109 get = function(k) return db.profile[k.arg]; end,
Aaron@0 110 set = function(k, v) db.profile[k.arg] = v; RecipeProfit:DoMerge(); end,
Aaron@0 111 },
Aaron@0 112 loadData = {
Aaron@0 113 order = 8,
Aaron@0 114 name = "Import Data",
Aaron@0 115 desc = "Load RecipeProfit and import the data to your database.",
Aaron@0 116 type = "execute",
Aaron@0 117 func = function()
Aaron@0 118 RecipeProfit:DoMerge()
Aaron@0 119 end
Aaron@0 120 },
Aaron@7 121 loadProfile = {
Aaron@7 122 order = 9,
Aaron@7 123 name = "Load RecipeProfit Profile",
Aaron@7 124 desc = "Loads the RecipeProfit Profile into Gathermate for easy recipe tracking.",
Aaron@7 125 type = "execute",
Aaron@7 126 func = function()
Aaron@7 127 GatherMate.db.profiles["RecipeProfit"] = GatherMate.db.profiles["RecipeProfit"] or {}
Aaron@7 128 gmdb = GatherMate.db.profiles["RecipeProfit"]
Aaron@7 129 for k, v in pairs(defaultProfile) do
Aaron@7 130 gmdb[k] = v;
Aaron@7 131 end
Aaron@7 132 GatherMate:SendMessage("OnProfileChanged");
Aaron@7 133 GatherMate:GetModule("Config"):UpdateConfig()
Aaron@7 134 GatherMate:SendMessage("GatherMateConfigChanged")
Aaron@7 135 GatherMate.db:SetProfile("RecipeProfit")
Aaron@7 136 end
Aaron@7 137 },
Aaron@0 138 }
Aaron@0 139 }
Aaron@0 140
Aaron@0 141 local defaults = {
Aaron@1 142 faction = "default",
Aaron@1 143 safebuy = "on",
Aaron@16 144
Aaron@16 145 --submitting cached data not yet implemented
Aaron@16 146 enable_cache = false,
Aaron@16 147 location_cache = {},
Aaron@0 148 }
Aaron@0 149
Aaron@0 150 function RecipeProfit:OnInitialize()
Aaron@0 151 profile = profile or defaults
Aaron@7 152
Aaron@1 153 for k, v in pairs(defaults) do
Aaron@1 154 profile[k] = profile[k] or v;
Aaron@1 155 end
Aaron@1 156
Aaron@12 157 self:RegisterChatCommand("recipeprofit", "ShowOptions")
Aaron@12 158 self:RegisterChatCommand("rp", "ShowOptions")
Aaron@12 159 self:RegisterChatCommand("profit", "ShowOptions")
Aaron@12 160
Aaron@0 161 db.profile = profile
Aaron@0 162 db.storage = {}
Aaron@0 163
Aaron@0 164 GatherMate:GetModule("Config"):RegisterModule("RecipeProfit", options)
Aaron@0 165 GatherMate:RegisterDBType("RecipeProfit", db.storage)
Aaron@8 166 GatherMate.db.profile.show["RecipeProfit"] = GatherMate.db.profile.show["RecipeProfit"] or "always"
Aaron@0 167 GatherMate.nodeIDs["RecipeProfit"] = {}
Aaron@0 168 GatherMate.nodeTextures["RecipeProfit"] = {}
Aaron@0 169 GatherMate.nodeMinHarvest["RecipeProfit"] = {}
Aaron@16 170 local nodes = GatherMate.nodeIDs["RecipeProfit"]
Aaron@0 171
Aaron@0 172 for id, note in pairs(RECIPEPROFIT_alliance) do
Aaron@1 173 safeRecipes[note.item] = true;
Aaron@16 174 nodes[get_note_title(note, "A")] = id * 2
Aaron@16 175 GatherMate.nodeTextures["RecipeProfit"][id * 2] = "Interface\\Icons\\INV_Scroll_05"
Aaron@0 176 end
Aaron@0 177
Aaron@0 178 for id, note in pairs(RECIPEPROFIT_horde) do
Aaron@1 179 safeRecipes[note.item] = true;
Aaron@16 180 nodes[get_note_title(note, "H")] = (id - 1) * 2 + 1
Aaron@16 181 GatherMate.nodeTextures["RecipeProfit"][(id - 1) * 2 + 1] = "Interface\\Icons\\INV_Scroll_05"
Aaron@0 182 end
Aaron@0 183
Aaron@0 184 GatherMate.reverseNodeIDs["RecipeProfit"] = GatherMate:CreateReversedTable(nodes)
Aaron@8 185
Aaron@8 186 GatherMate:GetModule("Config").options.args.display.args.general.args.showGroup.args["showRecipeProfit"] = {
Aaron@8 187 order = 6,
Aaron@8 188 name = "Show RecipeProfit nodes.",
Aaron@8 189 desc = "Toggle showing nodes added by RecipeProfit.",
Aaron@8 190 type = "select",
Aaron@8 191 values = {
Aaron@8 192 ["always"] = "Always show",
Aaron@8 193 ["never"] = "Never show",
Aaron@8 194 },
Aaron@8 195 arg = "RecipeProfit",
Aaron@8 196 }
Aaron@8 197
Aaron@8 198 GatherMate:GetModule("Config").options.args.display.args.general.args.iconGroup.args.tracking.args["showRecipeProfit"] = {
Aaron@8 199 order = 6.5,
Aaron@8 200 name = "RecipeProfit",
Aaron@8 201 desc = "Color of the tracking circle.",
Aaron@8 202 type = "color",
Aaron@8 203 hasAlpha = true,
Aaron@8 204 arg = "RecipeProfit",
Aaron@8 205 }
Aaron@8 206
Aaron@0 207 GatherMate:GetModule("Config"):UpdateConfig()
Aaron@0 208 GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
Aaron@0 209 end
Aaron@0 210
Aaron@12 211 function RecipeProfit:ShowOptions()
Aaron@13 212 LibStub("AceConfigDialog-3.0"):Open("GatherMate")
Aaron@13 213 LibStub("AceConfigDialog-3.0"):SelectGroup("GatherMate", "RecipeProfit")
Aaron@12 214 end
Aaron@12 215
Aaron@16 216 function RecipeProfit:UpdateButtons(event, ...)
Aaron@16 217 --print("UpdateButtons", event)
Aaron@16 218 if(not MerchantFrame:IsVisible()) then
Aaron@16 219 --print("UpdateButtons - (Event: ", event, ") - MerchantFrame not visible.");
Aaron@16 220 return;
Aaron@16 221 end
Aaron@16 222
Aaron@16 223 for i=1, MERCHANT_ITEMS_PER_PAGE, 1 do
Aaron@16 224 local buttonframe = _G["MerchantItem"..i];
Aaron@16 225 local index = (((MerchantFrame.page - 1) * MERCHANT_ITEMS_PER_PAGE) + i);
Aaron@16 226 --print(index)
Aaron@16 227 if index <= GetMerchantNumItems() then
Aaron@16 228 button_update(buttonframe)
Aaron@16 229 end
Aaron@16 230 end
Aaron@16 231 end
Aaron@16 232
Aaron@16 233
Aaron@16 234 function RecipeProfit:OnEnable()
Aaron@16 235
Aaron@16 236 _G["MerchantPrevPageButton"]:HookScript("OnClick", self.UpdateButtons)
Aaron@16 237 _G["MerchantNextPageButton"]:HookScript("OnClick", self.UpdateButtons)
Aaron@16 238
Aaron@16 239 self:RegisterEvent("MERCHANT_SHOW", "UpdateButtons")
Aaron@16 240 self:RegisterEvent("MERCHANT_UPDATE", "UpdateButtons")
Aaron@16 241 self:RegisterEvent("BAG_UPDATE", "UpdateButtons")
Aaron@16 242
Aaron@16 243 RecipeProfit:DoMerge()
Aaron@16 244 end
Aaron@16 245
Aaron@16 246 function RecipeProfit:DoMerge()
Aaron@16 247 ids = {}
Aaron@16 248 local selectedDB = get_faction_db();
Aaron@16 249
Aaron@16 250 GatherMate:ClearDB("RecipeProfit")
Aaron@16 251 for id, note in pairs(selectedDB) do
Aaron@16 252 x, y = find_good_id(note.x, note.y)
Aaron@16 253 add_note(x, y, note)
Aaron@16 254 end
Aaron@16 255
Aaron@16 256 GatherMate:SendMessage("GatherMateDataImport")
Aaron@16 257 GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
Aaron@16 258 end
Aaron@16 259
Aaron@16 260 function get_note_title(note, factionTag)
Aaron@16 261 if(not factionTag) then
Aaron@16 262 _, factionTag = get_faction_db();
Aaron@16 263 end
Aaron@16 264
Aaron@16 265 return note.item.." - ("..note.vendor.." ".. factionTag ..")";
Aaron@16 266 end
Aaron@16 267
Aaron@16 268 function add_note(x, y, note)
Aaron@16 269 GatherMate:AddNode(note.map, x / 100, y / 100, "RecipeProfit", get_note_title(note));
Aaron@16 270 end
Aaron@16 271
Aaron@16 272 function get_faction_db()
Aaron@16 273 local factionAlliance = db.profile.faction == "Alliance" or
Aaron@16 274 db.profile.faction == "default" and UnitFactionGroup("player") == "Alliance";
Aaron@16 275 if(factionAlliance) then
Aaron@16 276 return RECIPEPROFIT_alliance, "A";
Aaron@16 277 else
Aaron@16 278 return RECIPEPROFIT_horde, "H";
Aaron@16 279 end
Aaron@16 280 end
Aaron@16 281
Aaron@16 282 function safe_cache_vendor()
Aaron@16 283 if(not profile.enable_cache) then
Aaron@16 284 return
Aaron@16 285 end
Aaron@16 286
Aaron@16 287 if(not profile.location_cache[UnitName("NPC")]) then
Aaron@16 288 SetMapToCurrentZone()
Aaron@16 289 local pos = {}
Aaron@16 290 pos.x, pos.y = GetPlayerMapPosition("player")
Aaron@16 291 profile.location_cache[UnitName("NPC")] = pos
Aaron@16 292 end
Aaron@16 293 end
Aaron@15 294
Aaron@9 295 function button_update(self)
Aaron@9 296 local buttonName = _G[self:GetName().."Name"];
Aaron@9 297 local link = GetMerchantItemLink(_G[self:GetName().."ItemButton"]:GetID());
Aaron@9 298 if(not link) then
Aaron@9 299 return;
Aaron@9 300 end
Aaron@9 301
Aaron@9 302 local sName, sLink, iRarity, iLevel, iMinLevel, sType, sSubType, iStackCount = GetItemInfo(link)
Aaron@9 303
Aaron@16 304 if(sType == "Recipe" and safeRecipes[sName]) then
Aaron@16 305 safe_cache_vendor();
Aaron@9 306 SetItemButtonNameFrameVertexColor(self, 0, 0, 1.0);
Aaron@9 307 SetItemButtonSlotVertexColor(self, 0, 0, 0.5);
Aaron@9 308 buttonName:SetText("* " .. sName)
Aaron@9 309 if(GetItemCount(link, true) == 0) then
Aaron@9 310 buttonName:SetTextColor(0,1,1);
Aaron@9 311 elseif(GetItemCount(link, true) < 5) then
Aaron@9 312 buttonName:SetTextColor(1,0,1);
Aaron@9 313 else
Aaron@9 314 buttonName:SetTextColor(1,0,0);
Aaron@9 315 end
Aaron@9 316 else
Aaron@9 317 buttonName:SetTextColor(GameFontNormalSmall:GetTextColor());
Aaron@9 318 end
Aaron@9 319 end
Aaron@9 320
Aaron@16 321 function find_good_id(x, y)
Aaron@0 322 if ids[x.." "..y] then
Aaron@16 323 return find_good_id(x + .01, y)
Aaron@0 324 end
Aaron@0 325
Aaron@0 326 ids[x.." "..y] = true
Aaron@0 327 return x, y
Aaron@16 328 end