comparison Main.lua @ 5:59400bdcf95c

Record items/costs for merchants. Fix nil error in ParseGUID().
author James D. Callahan III <jcallahan@curse.com>
date Mon, 30 Apr 2012 00:09:59 -0500
parents 24a1d78e2e8c
children e987658a8c7d
comparison
equal deleted inserted replaced
4:24a1d78e2e8c 5:59400bdcf95c
18 18
19 local LibStub = _G.LibStub 19 local LibStub = _G.LibStub
20 local WDP = LibStub("AceAddon-3.0"):NewAddon(ADDON_NAME, "AceEvent-3.0", "AceTimer-3.0") 20 local WDP = LibStub("AceAddon-3.0"):NewAddon(ADDON_NAME, "AceEvent-3.0", "AceTimer-3.0")
21 21
22 local DatamineTT = _G.CreateFrame("GameTooltip", "WDPDatamineTT", _G.UIParent, "GameTooltipTemplate") 22 local DatamineTT = _G.CreateFrame("GameTooltip", "WDPDatamineTT", _G.UIParent, "GameTooltipTemplate")
23 DatamineTT:SetOwner(_G.WorldFrame, "ANCHOR_NONE")
24
23 25
24 ----------------------------------------------------------------------- 26 -----------------------------------------------------------------------
25 -- Local constants. 27 -- Local constants.
26 ----------------------------------------------------------------------- 28 -----------------------------------------------------------------------
27 local DATABASE_DEFAULTS = { 29 local DATABASE_DEFAULTS = {
34 } 36 }
35 37
36 38
37 local EVENT_MAPPING = { 39 local EVENT_MAPPING = {
38 LOOT_OPENED = true, 40 LOOT_OPENED = true,
41 MERCHANT_SHOW = "MERCHANT_UPDATE",
39 MERCHANT_UPDATE = true, 42 MERCHANT_UPDATE = true,
40 PLAYER_TARGET_CHANGED = true, 43 PLAYER_TARGET_CHANGED = true,
41 UNIT_QUEST_LOG_CHANGED = true, 44 UNIT_QUEST_LOG_CHANGED = true,
42 UNIT_SPELLCAST_FAILED = "HandleSpellFailure", 45 UNIT_SPELLCAST_FAILED = "HandleSpellFailure",
43 UNIT_SPELLCAST_FAILED_QUIET = "HandleSpellFailure", 46 UNIT_SPELLCAST_FAILED_QUIET = "HandleSpellFailure",
100 103
101 do 104 do
102 local UNIT_TYPE_BITMASK = 0x007 105 local UNIT_TYPE_BITMASK = 0x007
103 106
104 function WDP:ParseGUID(guid) 107 function WDP:ParseGUID(guid)
108 if not guid then
109 return
110 end
105 local types = private.UNIT_TYPES 111 local types = private.UNIT_TYPES
106 local unit_type = _G.bit.band(tonumber(guid:sub(1, 5)), UNIT_TYPE_BITMASK) 112 local unit_type = _G.bit.band(tonumber(guid:sub(1, 5)), UNIT_TYPE_BITMASK)
107 113
108 if unit_type ~= types.PLAYER or unit_type ~= types.OBJECT or unit_type ~= types.PET then 114 if unit_type ~= types.PLAYER or unit_type ~= types.OBJECT or unit_type ~= types.PET then
109 return unit_type, tonumber(guid:sub(-12, -9), 16) 115 return unit_type, tonumber(guid:sub(-12, -9), 16)
282 end 288 end
283 update_func() 289 update_func()
284 end 290 end
285 291
286 292
293 local POINT_MATCH_PATTERNS = {
294 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING:gsub("%%d", "(%%d+)")), -- May no longer be necessary
295 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3:gsub("%%d", "(%%d+)")), -- May no longer be necessary
296 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_5V5:gsub("%%d", "(%%d+)")), -- May no longer be necessary
297 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_BG:gsub("%%d", "(%%d+)")),
298 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3_BG:gsub("%%d", "(%%d+)")),
299 }
300
301
287 function WDP:MERCHANT_UPDATE() 302 function WDP:MERCHANT_UPDATE()
288 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target")) 303 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
289 304
290 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then 305 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
291 return 306 return
292 end 307 end
293 308 local npc = db.npcs[unit_idnum]
294 for index = 1, _G.GetMerchantNumItems() do 309
295 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(index) 310 if not npc then
311 db.npcs[unit_idnum] = {}
312 npc = db.npcs[unit_idnum]
313 end
314 npc.sells = npc.sells or {}
315
316 for item_index = 1, _G.GetMerchantNumItems() do
317 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index)
318 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index))
319
320 if item_id and item_id > 0 then
321 local price_string = copper_price
322
323 if extended_cost then
324 local bg_points = 0
325 local personal_points = 0
326
327 DatamineTT:ClearLines()
328 DatamineTT:SetMerchantItem(item_index)
329
330 for line_index = 1, DatamineTT:NumLines() do
331 local current_line = _G["WDPDatamineTTTextLeft" .. line_index]
332
333 if not current_line then
334 break
335 end
336 local breakout
337
338 for match_index = 1, #POINT_MATCH_PATTERNS do
339 local match1, match2 = current_line:GetText():match(POINT_MATCH_PATTERNS[match_index])
340 personal_points = personal_points + (match1 or 0)
341 bg_points = bg_points + (match2 or 0)
342
343 if match1 or match2 then
344 breakout = true
345 break
346 end
347 end
348
349 if breakout then
350 break
351 end
352 end
353 local currency_list = {}
354
355 price_string = ("%s:%s:%s"):format(price_string, bg_points, personal_points)
356
357 for cost_index = 1, _G.GetMerchantItemCostInfo(item_index) do
358 local icon_texture, amount_required, currency_link = _G.GetMerchantItemCostItem(item_index, cost_index)
359 local currency_id = currency_link and ItemLinkToID(currency_link) or nil
360
361 if not currency_id or currency_id < 1 then
362 if not icon_texture then
363 return
364 end
365 currency_id = icon_texture:match("[^\\]+$"):lower()
366 end
367 currency_list[#currency_list + 1] = ("(%s:%s)"):format(amount_required, currency_id)
368 end
369
370 for currency_index = 1, #currency_list do
371 price_string = ("%s:%s"):format(price_string, currency_list[currency_index])
372 end
373 end
374 npc.sells[("%s:%s:[%s]"):format(item_id, stack_size, price_string)] = num_available
375 end
296 end 376 end
297 end 377 end
298 378
299 379
300 local GENDER_NAMES = { 380 local GENDER_NAMES = {