annotate core.lua @ 20:bdd440ac55be v1.02

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