comparison core.lua @ 16:8a18dd9f2cec

Caching support...
author Aaron@Aaron-PC
date Sun, 15 Aug 2010 17:59:03 -0500
parents 38822958c28a
children 22686cb65c51
comparison
equal deleted inserted replaced
15:38822958c28a 16:8a18dd9f2cec
12 --]]--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--)) 12 --]]--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))--))
13 13
14 local RecipeProfit = LibStub("AceAddon-3.0"):NewAddon("RecipeProfit", "AceEvent-3.0", "AceConsole-3.0") 14 local RecipeProfit = LibStub("AceAddon-3.0"):NewAddon("RecipeProfit", "AceEvent-3.0", "AceConsole-3.0")
15 local GatherMate = LibStub("AceAddon-3.0"):GetAddon("GatherMate") 15 local GatherMate = LibStub("AceAddon-3.0"):GetAddon("GatherMate")
16 local tabletest = {} 16 local tabletest = {}
17 db = {} 17 local db = {}
18 local safeRecipes = {} 18 local safeRecipes = {}
19 safer = safeRecipes 19 local ids = {}
20
20 RecipeProfit.db = db; 21 RecipeProfit.db = db;
22
23 -- Forward Definitions (for local functions)
24 local get_faction_db,
25 add_note,
26 button_update,
27 find_good_id,
28 safe_cache_vendor,
29 get_note_title;
21 30
22 function debugprint(val, indent) 31 function debugprint(val, indent)
23 indent = indent or ""; 32 indent = indent or "";
24 if not(type(val) == "table") then 33 if not(type(val) == "table") then
25 print("Not table: " .. val) 34 print("Not table: " .. val)
130 } 139 }
131 140
132 local defaults = { 141 local defaults = {
133 faction = "default", 142 faction = "default",
134 safebuy = "on", 143 safebuy = "on",
144
145 --submitting cached data not yet implemented
146 enable_cache = false,
147 location_cache = {},
135 } 148 }
136 149
137 function RecipeProfit:OnInitialize() 150 function RecipeProfit:OnInitialize()
138 profile = profile or defaults 151 profile = profile or defaults
139 152
152 GatherMate:RegisterDBType("RecipeProfit", db.storage) 165 GatherMate:RegisterDBType("RecipeProfit", db.storage)
153 GatherMate.db.profile.show["RecipeProfit"] = GatherMate.db.profile.show["RecipeProfit"] or "always" 166 GatherMate.db.profile.show["RecipeProfit"] = GatherMate.db.profile.show["RecipeProfit"] or "always"
154 GatherMate.nodeIDs["RecipeProfit"] = {} 167 GatherMate.nodeIDs["RecipeProfit"] = {}
155 GatherMate.nodeTextures["RecipeProfit"] = {} 168 GatherMate.nodeTextures["RecipeProfit"] = {}
156 GatherMate.nodeMinHarvest["RecipeProfit"] = {} 169 GatherMate.nodeMinHarvest["RecipeProfit"] = {}
157 nodes = GatherMate.nodeIDs["RecipeProfit"] 170 local nodes = GatherMate.nodeIDs["RecipeProfit"]
158 171
159 for id, note in pairs(RECIPEPROFIT_alliance) do 172 for id, note in pairs(RECIPEPROFIT_alliance) do
160 safeRecipes[note.item] = true; 173 safeRecipes[note.item] = true;
161 nodes[note.item.." - ("..note.vendor.." A)"] = id * 10 174 nodes[get_note_title(note, "A")] = id * 2
162 GatherMate.nodeTextures["RecipeProfit"][id * 10] = "Interface\\Icons\\INV_Scroll_05" 175 GatherMate.nodeTextures["RecipeProfit"][id * 2] = "Interface\\Icons\\INV_Scroll_05"
163 end 176 end
164 177
165 for id, note in pairs(RECIPEPROFIT_horde) do 178 for id, note in pairs(RECIPEPROFIT_horde) do
166 safeRecipes[note.item] = true; 179 safeRecipes[note.item] = true;
167 nodes[note.item.." - ("..note.vendor.." H)"] = id * 10 + 1 180 nodes[get_note_title(note, "H")] = (id - 1) * 2 + 1
168 GatherMate.nodeTextures["RecipeProfit"][id * 10 + 1] = "Interface\\Icons\\INV_Scroll_05" 181 GatherMate.nodeTextures["RecipeProfit"][(id - 1) * 2 + 1] = "Interface\\Icons\\INV_Scroll_05"
169 end 182 end
170 183
171 GatherMate.reverseNodeIDs["RecipeProfit"] = GatherMate:CreateReversedTable(nodes) 184 GatherMate.reverseNodeIDs["RecipeProfit"] = GatherMate:CreateReversedTable(nodes)
172 185
173 GatherMate:GetModule("Config").options.args.display.args.general.args.showGroup.args["showRecipeProfit"] = { 186 GatherMate:GetModule("Config").options.args.display.args.general.args.showGroup.args["showRecipeProfit"] = {
198 function RecipeProfit:ShowOptions() 211 function RecipeProfit:ShowOptions()
199 LibStub("AceConfigDialog-3.0"):Open("GatherMate") 212 LibStub("AceConfigDialog-3.0"):Open("GatherMate")
200 LibStub("AceConfigDialog-3.0"):SelectGroup("GatherMate", "RecipeProfit") 213 LibStub("AceConfigDialog-3.0"):SelectGroup("GatherMate", "RecipeProfit")
201 end 214 end
202 215
216 function RecipeProfit:UpdateButtons(event, ...)
217 --print("UpdateButtons", event)
218 if(not MerchantFrame:IsVisible()) then
219 --print("UpdateButtons - (Event: ", event, ") - MerchantFrame not visible.");
220 return;
221 end
222
223 for i=1, MERCHANT_ITEMS_PER_PAGE, 1 do
224 local buttonframe = _G["MerchantItem"..i];
225 local index = (((MerchantFrame.page - 1) * MERCHANT_ITEMS_PER_PAGE) + i);
226 --print(index)
227 if index <= GetMerchantNumItems() then
228 button_update(buttonframe)
229 end
230 end
231 end
232
233
234 function RecipeProfit:OnEnable()
235
236 _G["MerchantPrevPageButton"]:HookScript("OnClick", self.UpdateButtons)
237 _G["MerchantNextPageButton"]:HookScript("OnClick", self.UpdateButtons)
238
239 self:RegisterEvent("MERCHANT_SHOW", "UpdateButtons")
240 self:RegisterEvent("MERCHANT_UPDATE", "UpdateButtons")
241 self:RegisterEvent("BAG_UPDATE", "UpdateButtons")
242
243 RecipeProfit:DoMerge()
244 end
245
246 function RecipeProfit:DoMerge()
247 ids = {}
248 local selectedDB = get_faction_db();
249
250 GatherMate:ClearDB("RecipeProfit")
251 for id, note in pairs(selectedDB) do
252 x, y = find_good_id(note.x, note.y)
253 add_note(x, y, note)
254 end
255
256 GatherMate:SendMessage("GatherMateDataImport")
257 GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
258 end
259
260 function get_note_title(note, factionTag)
261 if(not factionTag) then
262 _, factionTag = get_faction_db();
263 end
264
265 return note.item.." - ("..note.vendor.." ".. factionTag ..")";
266 end
267
268 function add_note(x, y, note)
269 GatherMate:AddNode(note.map, x / 100, y / 100, "RecipeProfit", get_note_title(note));
270 end
271
272 function get_faction_db()
273 local factionAlliance = db.profile.faction == "Alliance" or
274 db.profile.faction == "default" and UnitFactionGroup("player") == "Alliance";
275 if(factionAlliance) then
276 return RECIPEPROFIT_alliance, "A";
277 else
278 return RECIPEPROFIT_horde, "H";
279 end
280 end
281
282 function safe_cache_vendor()
283 if(not profile.enable_cache) then
284 return
285 end
286
287 if(not profile.location_cache[UnitName("NPC")]) then
288 SetMapToCurrentZone()
289 local pos = {}
290 pos.x, pos.y = GetPlayerMapPosition("player")
291 profile.location_cache[UnitName("NPC")] = pos
292 end
293 end
203 294
204 function button_update(self) 295 function button_update(self)
205 local buttonName = _G[self:GetName().."Name"]; 296 local buttonName = _G[self:GetName().."Name"];
206 local link = GetMerchantItemLink(_G[self:GetName().."ItemButton"]:GetID()); 297 local link = GetMerchantItemLink(_G[self:GetName().."ItemButton"]:GetID());
207 if(not link) then 298 if(not link) then
208 return; 299 return;
209 end 300 end
210 301
211 local sName, sLink, iRarity, iLevel, iMinLevel, sType, sSubType, iStackCount = GetItemInfo(link) 302 local sName, sLink, iRarity, iLevel, iMinLevel, sType, sSubType, iStackCount = GetItemInfo(link)
212 303
213 if(sType == "Recipe" and safeRecipes[sName]) then 304 if(sType == "Recipe" and safeRecipes[sName]) then
305 safe_cache_vendor();
214 SetItemButtonNameFrameVertexColor(self, 0, 0, 1.0); 306 SetItemButtonNameFrameVertexColor(self, 0, 0, 1.0);
215 SetItemButtonSlotVertexColor(self, 0, 0, 0.5); 307 SetItemButtonSlotVertexColor(self, 0, 0, 0.5);
216 buttonName:SetText("* " .. sName) 308 buttonName:SetText("* " .. sName)
217 if(GetItemCount(link, true) == 0) then 309 if(GetItemCount(link, true) == 0) then
218 buttonName:SetTextColor(0,1,1); 310 buttonName:SetTextColor(0,1,1);
224 else 316 else
225 buttonName:SetTextColor(GameFontNormalSmall:GetTextColor()); 317 buttonName:SetTextColor(GameFontNormalSmall:GetTextColor());
226 end 318 end
227 end 319 end
228 320
229 function RecipeProfit:UpdateButtons(event) 321 function find_good_id(x, y)
230 --print("UpdateButtons", event)
231 if(not MerchantFrame:IsVisible()) then
232 --print("UpdateButtons - (Event: ", event, ") - MerchantFrame not visible.");
233 return;
234 end
235
236 for i=1, MERCHANT_ITEMS_PER_PAGE, 1 do
237 local buttonframe = _G["MerchantItem"..i];
238 local index = (((MerchantFrame.page - 1) * MERCHANT_ITEMS_PER_PAGE) + i);
239 --print(index)
240 if index <= GetMerchantNumItems() then
241 button_update(buttonframe)
242 --[[else
243 --print(i, index, GetMerchantNumItems())
244 local itemButton = _G["MerchantItem"..i.."ItemButton"];
245 local merchantButton = _G["MerchantItem"..i];
246 itemButton.price = nil;
247 itemButton.hasItem = nil;
248 itemButton:Hide();
249 SetItemButtonNameFrameVertexColor(merchantButton, 0.5, 0.5, 0.5);
250 SetItemButtonSlotVertexColor(merchantButton,0.4, 0.4, 0.4);
251 _G["MerchantItem"..i.."Name"]:SetText("");
252 _G["MerchantItem"..i.."MoneyFrame"]:Hide();
253 _G["MerchantItem"..i.."AltCurrencyFrame"]:Hide();
254 ]]
255 end
256 end
257 end
258
259
260 function RecipeProfit:OnEnable()
261
262 _G["MerchantPrevPageButton"]:HookScript("OnClick", self.UpdateButtons)
263 _G["MerchantNextPageButton"]:HookScript("OnClick", self.UpdateButtons)
264
265 self:RegisterEvent("MERCHANT_SHOW", "UpdateButtons")
266 self:RegisterEvent("MERCHANT_UPDATE", "UpdateButtons")
267 self:RegisterEvent("BAG_UPDATE", "UpdateButtons")
268
269 RecipeProfit:DoMerge()
270 end
271
272 local ids = {}
273 function findGoodId(x, y)
274 if ids[x.." "..y] then 322 if ids[x.." "..y] then
275 return findGoodId(x + .01, y) 323 return find_good_id(x + .01, y)
276 end 324 end
277 325
278 ids[x.." "..y] = true 326 ids[x.." "..y] = true
279 return x, y 327 return x, y
280 end 328 end
281
282 function RecipeProfit:DoMerge()
283 ids = {}
284 selectedDB = (db.profile.faction == "Alliance" or
285 db.profile.faction == "default" and UnitFactionGroup("player") == "Alliance") and
286 RECIPEPROFIT_alliance or RECIPEPROFIT_horde
287 GatherMate:ClearDB("RecipeProfit")
288 for id, note in pairs(selectedDB) do
289 x, y = findGoodId(note.x, note.y)
290 GatherMate:AddNode(note.map, x / 100, y / 100, "RecipeProfit",
291 note.item.." - ("..note.vendor.." "..
292 ((db.profile.faction == "Alliance" or db.profile.faction == "default" and
293 UnitFactionGroup("player") == "Alliance") and "A" or "H") ..")")
294 end
295
296 GatherMate:SendMessage("GatherMateDataImport")
297 GatherMate:GetModule("Config"):SendMessage("GatherMateConfigChanged")
298 end