annotate Main.lua @ 21:a82c5d1134db

Record reputation gains from NPC deaths.
author James D. Callahan III <jcallahan@curse.com>
date Wed, 09 May 2012 09:34:49 -0500
parents dae28edbbd61
children 7e4ce6371608
rev   line source
jcallahan@0 1 -----------------------------------------------------------------------
jcallahan@0 2 -- Upvalued Lua API.
jcallahan@0 3 -----------------------------------------------------------------------
jcallahan@0 4 local _G = getfenv(0)
jcallahan@0 5
jcallahan@0 6 local pairs = _G.pairs
jcallahan@1 7 local tonumber = _G.tonumber
jcallahan@1 8
jcallahan@1 9 local bit = _G.bit
jcallahan@1 10 local math = _G.math
jcallahan@1 11 local table = _G.table
jcallahan@1 12
jcallahan@0 13
jcallahan@0 14 -----------------------------------------------------------------------
jcallahan@0 15 -- AddOn namespace.
jcallahan@0 16 -----------------------------------------------------------------------
jcallahan@0 17 local ADDON_NAME, private = ...
jcallahan@0 18
jcallahan@0 19 local LibStub = _G.LibStub
jcallahan@0 20 local WDP = LibStub("AceAddon-3.0"):NewAddon(ADDON_NAME, "AceEvent-3.0", "AceTimer-3.0")
jcallahan@0 21
jcallahan@4 22 local DatamineTT = _G.CreateFrame("GameTooltip", "WDPDatamineTT", _G.UIParent, "GameTooltipTemplate")
jcallahan@5 23 DatamineTT:SetOwner(_G.WorldFrame, "ANCHOR_NONE")
jcallahan@5 24
jcallahan@0 25
jcallahan@0 26 -----------------------------------------------------------------------
jcallahan@0 27 -- Local constants.
jcallahan@0 28 -----------------------------------------------------------------------
jcallahan@0 29 local DATABASE_DEFAULTS = {
jcallahan@0 30 global = {
jcallahan@0 31 items = {},
jcallahan@0 32 npcs = {},
jcallahan@0 33 objects = {},
jcallahan@0 34 quests = {},
jcallahan@17 35 zones = {},
jcallahan@0 36 }
jcallahan@0 37 }
jcallahan@0 38
jcallahan@0 39
jcallahan@1 40 local EVENT_MAPPING = {
jcallahan@18 41 COMBAT_TEXT_UPDATE = true,
jcallahan@13 42 LOOT_CLOSED = true,
jcallahan@1 43 LOOT_OPENED = true,
jcallahan@7 44 MERCHANT_SHOW = "UpdateMerchantItems",
jcallahan@7 45 MERCHANT_UPDATE = "UpdateMerchantItems",
jcallahan@2 46 PLAYER_TARGET_CHANGED = true,
jcallahan@9 47 QUEST_COMPLETE = true,
jcallahan@9 48 QUEST_DETAIL = true,
jcallahan@9 49 QUEST_LOG_UPDATE = true,
jcallahan@4 50 UNIT_QUEST_LOG_CHANGED = true,
jcallahan@1 51 UNIT_SPELLCAST_FAILED = "HandleSpellFailure",
jcallahan@1 52 UNIT_SPELLCAST_FAILED_QUIET = "HandleSpellFailure",
jcallahan@1 53 UNIT_SPELLCAST_INTERRUPTED = "HandleSpellFailure",
jcallahan@1 54 UNIT_SPELLCAST_SENT = true,
jcallahan@1 55 UNIT_SPELLCAST_SUCCEEDED = true,
jcallahan@0 56 }
jcallahan@0 57
jcallahan@4 58
jcallahan@1 59 local AF = private.ACTION_TYPE_FLAGS
jcallahan@0 60
jcallahan@4 61
jcallahan@0 62 -----------------------------------------------------------------------
jcallahan@0 63 -- Local variables.
jcallahan@0 64 -----------------------------------------------------------------------
jcallahan@0 65 local db
jcallahan@0 66 local durability_timer_handle
jcallahan@2 67 local target_location_timer_handle
jcallahan@1 68 local action_data = {}
jcallahan@20 69 local faction_names = {}
jcallahan@0 70
jcallahan@1 71
jcallahan@1 72 -----------------------------------------------------------------------
jcallahan@1 73 -- Helper Functions.
jcallahan@1 74 -----------------------------------------------------------------------
jcallahan@19 75 local function DBEntry(data_type, unit_id)
jcallahan@19 76 if not data_type or not unit_id then
jcallahan@6 77 return
jcallahan@6 78 end
jcallahan@19 79 local unit = db[data_type][unit_id]
jcallahan@6 80
jcallahan@10 81 if not unit then
jcallahan@19 82 db[data_type][unit_id] = {}
jcallahan@19 83 unit = db[data_type][unit_id]
jcallahan@6 84 end
jcallahan@10 85 return unit
jcallahan@6 86 end
jcallahan@6 87
jcallahan@6 88
jcallahan@1 89 local function CurrentLocationData()
jcallahan@1 90 local map_level = _G.GetCurrentMapDungeonLevel() or 0
jcallahan@1 91 local x, y = _G.GetPlayerMapPosition("player")
jcallahan@1 92
jcallahan@1 93 x = x or 0
jcallahan@1 94 y = y or 0
jcallahan@1 95
jcallahan@1 96 if x == 0 and y == 0 then
jcallahan@1 97 for level_index = 1, _G.GetNumDungeonMapLevels() do
jcallahan@1 98 _G.SetDungeonMapLevel(level_index)
jcallahan@1 99 x, y = _G.GetPlayerMapPosition("player")
jcallahan@1 100
jcallahan@1 101 if x and y and (x > 0 or y > 0) then
jcallahan@1 102 _G.SetDungeonMapLevel(map_level)
jcallahan@1 103 map_level = level_index
jcallahan@1 104 break
jcallahan@1 105 end
jcallahan@1 106 end
jcallahan@1 107 end
jcallahan@1 108
jcallahan@1 109 if _G.DungeonUsesTerrainMap() then
jcallahan@1 110 map_level = map_level - 1
jcallahan@1 111 end
jcallahan@8 112 local _, instance_type = _G.IsInInstance()
jcallahan@8 113 return _G.GetRealZoneText(), ("%.2f"):format(x * 100), ("%.2f"):format(y * 100), map_level or 0, instance_type:upper()
jcallahan@1 114 end
jcallahan@1 115
jcallahan@1 116
jcallahan@1 117 local function ItemLinkToID(item_link)
jcallahan@1 118 if not item_link then
jcallahan@1 119 return
jcallahan@1 120 end
jcallahan@7 121 return tonumber(item_link:match("item:(%d+)"))
jcallahan@1 122 end
jcallahan@0 123
jcallahan@4 124
jcallahan@4 125 do
jcallahan@4 126 local UNIT_TYPE_BITMASK = 0x007
jcallahan@4 127
jcallahan@4 128 function WDP:ParseGUID(guid)
jcallahan@5 129 if not guid then
jcallahan@5 130 return
jcallahan@5 131 end
jcallahan@4 132 local types = private.UNIT_TYPES
jcallahan@4 133 local unit_type = _G.bit.band(tonumber(guid:sub(1, 5)), UNIT_TYPE_BITMASK)
jcallahan@4 134
jcallahan@10 135 if unit_type ~= types.PLAYER and unit_type ~= types.PET then
jcallahan@4 136 return unit_type, tonumber(guid:sub(-12, -9), 16)
jcallahan@4 137 end
jcallahan@4 138
jcallahan@4 139 return unit_type
jcallahan@4 140 end
jcallahan@4 141 end -- do-block
jcallahan@4 142
jcallahan@4 143
jcallahan@10 144 local function UpdateObjectLocation(identifier)
jcallahan@10 145 if not identifier then
jcallahan@10 146 return
jcallahan@10 147 end
jcallahan@10 148 local zone_name, x, y, map_level, instance_type = CurrentLocationData()
jcallahan@19 149 local object = DBEntry("objects", identifier)
jcallahan@11 150 object.locations = object.locations or {}
jcallahan@10 151
jcallahan@11 152 if not object.locations[zone_name] then
jcallahan@11 153 object.locations[zone_name] = {}
jcallahan@10 154 end
jcallahan@11 155 object.locations[zone_name][("%s:%s:%s:%s"):format(instance_type, map_level, x, y)] = true
jcallahan@10 156 end
jcallahan@10 157
jcallahan@10 158
jcallahan@19 159 local function HandleItemUse(item_link, bag_index, slot_index)
jcallahan@19 160 if not item_link then
jcallahan@19 161 return
jcallahan@19 162 end
jcallahan@19 163 local item_id = ItemLinkToID(item_link)
jcallahan@19 164
jcallahan@19 165 if not bag_index or not slot_index then
jcallahan@19 166 for new_bag_index = 0, _G.NUM_BAG_FRAMES do
jcallahan@19 167 for new_slot_index = 1, _G.GetContainerNumSlots(new_bag_index) do
jcallahan@19 168 if item_id == ItemLinkToID(_G.GetContainerItemLink(new_bag_index, new_slot_index)) then
jcallahan@19 169 bag_index = new_bag_index
jcallahan@19 170 slot_index = new_slot_index
jcallahan@19 171 break
jcallahan@19 172 end
jcallahan@19 173 end
jcallahan@19 174 end
jcallahan@19 175 end
jcallahan@19 176
jcallahan@19 177 if not bag_index or not slot_index then
jcallahan@19 178 return
jcallahan@19 179 end
jcallahan@19 180 local _, _, _, _, _, is_lootable = _G.GetContainerItemInfo(bag_index, slot_index)
jcallahan@19 181
jcallahan@19 182 if not is_lootable then
jcallahan@19 183 return
jcallahan@19 184 end
jcallahan@19 185 DatamineTT:ClearLines()
jcallahan@19 186 DatamineTT:SetBagItem(bag_index, slot_index)
jcallahan@19 187
jcallahan@19 188 for line_index = 1, DatamineTT:NumLines() do
jcallahan@19 189 local current_line = _G["WDPDatamineTTTextLeft" .. line_index]
jcallahan@19 190
jcallahan@19 191 if not current_line then
jcallahan@19 192 break
jcallahan@19 193 end
jcallahan@19 194
jcallahan@19 195 if current_line:GetText() == _G.ITEM_OPENABLE then
jcallahan@19 196 table.wipe(action_data)
jcallahan@19 197 action_data.type = AF.ITEM
jcallahan@19 198 action_data.item_id = item_id
jcallahan@19 199 action_data.loot_type = "contains"
jcallahan@19 200 break
jcallahan@19 201 end
jcallahan@19 202 end
jcallahan@19 203 end
jcallahan@19 204
jcallahan@19 205
jcallahan@20 206 local function UpdateFactionNames()
jcallahan@20 207 for faction_index = 1, 1000 do
jcallahan@20 208 local faction_name, _, _, _, _, _, _, _, is_header = _G.GetFactionInfo(faction_index)
jcallahan@20 209
jcallahan@20 210 if faction_name and not is_header then
jcallahan@20 211 faction_names[faction_name] = true
jcallahan@20 212 elseif not faction_name then
jcallahan@20 213 break
jcallahan@20 214 end
jcallahan@20 215 end
jcallahan@20 216 end
jcallahan@20 217
jcallahan@20 218
jcallahan@0 219 -----------------------------------------------------------------------
jcallahan@0 220 -- Methods.
jcallahan@0 221 -----------------------------------------------------------------------
jcallahan@0 222 function WDP:OnInitialize()
jcallahan@0 223 db = LibStub("AceDB-3.0"):New("WoWDBProfilerData", DATABASE_DEFAULTS, "Default").global
jcallahan@14 224
jcallahan@14 225 local raw_db = _G["WoWDBProfilerData"]
jcallahan@14 226
jcallahan@18 227 local build_num = tonumber(private.build_num)
jcallahan@14 228
jcallahan@14 229 if raw_db.build_num and raw_db.build_num < build_num then
jcallahan@14 230 for entry in pairs(DATABASE_DEFAULTS.global) do
jcallahan@14 231 db[entry] = {}
jcallahan@14 232 end
jcallahan@14 233 raw_db.build_num = build_num
jcallahan@14 234 elseif not raw_db.build_num then
jcallahan@14 235 raw_db.build_num = build_num
jcallahan@14 236 end
jcallahan@0 237 end
jcallahan@0 238
jcallahan@0 239
jcallahan@0 240 function WDP:OnEnable()
jcallahan@0 241 for event_name, mapping in pairs(EVENT_MAPPING) do
jcallahan@1 242 self:RegisterEvent(event_name, (_G.type(mapping) ~= "boolean") and mapping or nil)
jcallahan@0 243 end
jcallahan@0 244 durability_timer_handle = self:ScheduleRepeatingTimer("ProcessDurability", 30)
jcallahan@2 245 target_location_timer_handle = self:ScheduleRepeatingTimer("UpdateTargetLocation", 0.2)
jcallahan@19 246
jcallahan@19 247 _G.hooksecurefunc("UseContainerItem", function(bag_index, slot_index, target_unit)
jcallahan@19 248 if target_unit then
jcallahan@19 249 return
jcallahan@19 250 end
jcallahan@19 251 HandleItemUse(_G.GetContainerItemLink(bag_index, slot_index), bag_index, slot_index)
jcallahan@19 252 end)
jcallahan@19 253
jcallahan@19 254 _G.hooksecurefunc("UseItemByName", function(identifier, target_unit)
jcallahan@19 255 if target_unit then
jcallahan@19 256 return
jcallahan@19 257 end
jcallahan@19 258 local _, item_link = _G.GetItemInfo(identifier)
jcallahan@19 259 HandleItemUse(item_link)
jcallahan@19 260 end)
jcallahan@0 261 end
jcallahan@0 262
jcallahan@0 263
jcallahan@0 264 local function RecordDurability(item_id, durability)
jcallahan@0 265 if not durability or durability <= 0 then
jcallahan@0 266 return
jcallahan@0 267 end
jcallahan@0 268
jcallahan@0 269 if not db.items[item_id] then
jcallahan@0 270 db.items[item_id] = {}
jcallahan@0 271 end
jcallahan@0 272 db.items[item_id].durability = durability
jcallahan@0 273 end
jcallahan@0 274
jcallahan@0 275
jcallahan@0 276 function WDP:ProcessDurability()
jcallahan@0 277 for slot_index = 0, _G.INVSLOT_LAST_EQUIPPED do
jcallahan@1 278 local item_id = _G.GetInventoryItemID("player", slot_index)
jcallahan@0 279
jcallahan@0 280 if item_id and item_id > 0 then
jcallahan@1 281 local _, max_durability = _G.GetInventoryItemDurability(slot_index)
jcallahan@0 282 RecordDurability(item_id, max_durability)
jcallahan@0 283 end
jcallahan@0 284 end
jcallahan@0 285
jcallahan@0 286 for bag_index = 0, _G.NUM_BAG_SLOTS do
jcallahan@0 287 for slot_index = 1, _G.GetContainerNumSlots(bag_index) do
jcallahan@1 288 local item_id = _G.GetContainerItemID(bag_index, slot_index)
jcallahan@0 289
jcallahan@0 290 if item_id and item_id > 0 then
jcallahan@1 291 local _, max_durability = _G.GetContainerItemDurability(bag_index, slot_index)
jcallahan@0 292 RecordDurability(item_id, max_durability)
jcallahan@0 293 end
jcallahan@0 294 end
jcallahan@0 295 end
jcallahan@0 296 end
jcallahan@0 297
jcallahan@0 298
jcallahan@2 299 function WDP:UpdateTargetLocation()
jcallahan@2 300 if not _G.UnitExists("target") or _G.UnitPlayerControlled("target") or _G.UnitIsTapped("target") then
jcallahan@2 301 return
jcallahan@2 302 end
jcallahan@2 303
jcallahan@2 304 for index = 1, 4 do
jcallahan@2 305 if not _G.CheckInteractDistance("target", index) then
jcallahan@2 306 return
jcallahan@2 307 end
jcallahan@2 308 end
jcallahan@2 309
jcallahan@2 310 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
jcallahan@2 311
jcallahan@2 312 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
jcallahan@2 313 return
jcallahan@2 314 end
jcallahan@8 315 local zone_name, x, y, map_level, instance_type = CurrentLocationData()
jcallahan@19 316 local npc_data = DBEntry("npcs", unit_idnum).stats[("level_%d"):format(_G.UnitLevel("target"))]
jcallahan@6 317 npc_data.locations = npc_data.locations or {}
jcallahan@2 318
jcallahan@2 319 if not npc_data.locations[zone_name] then
jcallahan@2 320 npc_data.locations[zone_name] = {}
jcallahan@2 321 end
jcallahan@8 322 npc_data.locations[zone_name][("%s:%s:%s:%s"):format(instance_type, map_level, x, y)] = true
jcallahan@2 323 end
jcallahan@2 324
jcallahan@2 325
jcallahan@0 326 -----------------------------------------------------------------------
jcallahan@0 327 -- Event handlers.
jcallahan@0 328 -----------------------------------------------------------------------
jcallahan@18 329 function WDP:COMBAT_TEXT_UPDATE(event, message_type, faction_name, amount)
jcallahan@21 330 local npc = DBEntry("npcs", action_data.id_num)
jcallahan@21 331
jcallahan@21 332 if not npc then
jcallahan@21 333 return
jcallahan@21 334 end
jcallahan@21 335 npc.stats[action_data.npc_level].reputations = npc.stats[action_data.npc_level].reputations or {}
jcallahan@21 336 npc.stats[action_data.npc_level].reputations[faction_name] = amount
jcallahan@18 337 end
jcallahan@18 338
jcallahan@18 339
jcallahan@13 340 function WDP:LOOT_CLOSED()
jcallahan@18 341 -- table.wipe(action_data)
jcallahan@0 342 end
jcallahan@0 343
jcallahan@0 344
jcallahan@13 345 do
jcallahan@13 346 local re_gold = _G.GOLD_AMOUNT:gsub("%%d", "(%%d+)")
jcallahan@13 347 local re_silver = _G.SILVER_AMOUNT:gsub("%%d", "(%%d+)")
jcallahan@13 348 local re_copper = _G.COPPER_AMOUNT:gsub("%%d", "(%%d+)")
jcallahan@13 349
jcallahan@13 350
jcallahan@13 351 local function _moneyMatch(money, re)
jcallahan@13 352 return money:match(re) or 0
jcallahan@1 353 end
jcallahan@1 354
jcallahan@0 355
jcallahan@13 356 local function _toCopper(money)
jcallahan@13 357 if not money then
jcallahan@13 358 return 0
jcallahan@13 359 end
jcallahan@0 360
jcallahan@13 361 return _moneyMatch(money, re_gold) * 10000 + _moneyMatch(money, re_silver) * 100 + _moneyMatch(money, re_copper)
jcallahan@1 362 end
jcallahan@1 363
jcallahan@1 364
jcallahan@13 365 local LOOT_VERIFY_FUNCS = {
jcallahan@16 366 [AF.ITEM] = function()
jcallahan@16 367 local locked_item_id
jcallahan@16 368
jcallahan@16 369 for bag_index = 0, _G.NUM_BAG_FRAMES do
jcallahan@16 370 for slot_index = 1, _G.GetContainerNumSlots(bag_index) do
jcallahan@16 371 local _, _, is_locked = _G.GetContainerItemInfo(bag_index, slot_index)
jcallahan@16 372
jcallahan@16 373 if is_locked then
jcallahan@16 374 locked_item_id = ItemLinkToID(_G.GetContainerItemLink(bag_index, slot_index))
jcallahan@16 375 end
jcallahan@16 376 end
jcallahan@16 377 end
jcallahan@16 378
jcallahan@16 379 if not locked_item_id or (action_data.item_id and action_data.item_id ~= locked_item_id) then
jcallahan@16 380 return false
jcallahan@16 381 end
jcallahan@16 382 action_data.item_id = locked_item_id
jcallahan@16 383 return true
jcallahan@16 384 end,
jcallahan@13 385 [AF.NPC] = function()
jcallahan@17 386 if not _G.UnitExists("target") or _G.UnitIsFriend("player", "target") or _G.UnitIsPlayer("target") or _G.UnitPlayerControlled("target") then
jcallahan@15 387 return false
jcallahan@13 388 end
jcallahan@15 389 local unit_type, id_num = WDP:ParseGUID(_G.UnitGUID("target"))
jcallahan@15 390 action_data.id_num = id_num
jcallahan@13 391 return true
jcallahan@13 392 end,
jcallahan@14 393 [AF.OBJECT] = true,
jcallahan@17 394 [AF.ZONE] = function()
jcallahan@17 395 return action_data.loot_type and _G.IsFishingLoot()
jcallahan@17 396 end,
jcallahan@13 397 }
jcallahan@13 398
jcallahan@13 399
jcallahan@13 400 local LOOT_UPDATE_FUNCS = {
jcallahan@16 401 [AF.ITEM] = function()
jcallahan@19 402 local item = DBEntry("items", action_data.item_id)
jcallahan@19 403 local loot_type = action_data.loot_type or "drops"
jcallahan@16 404 item[loot_type] = item[loot_type] or {}
jcallahan@16 405
jcallahan@17 406 for index = 1, #action_data.loot_list do
jcallahan@17 407 table.insert(item[loot_type], action_data.loot_list[index])
jcallahan@16 408 end
jcallahan@16 409 end,
jcallahan@13 410 [AF.NPC] = function()
jcallahan@19 411 local npc = DBEntry("npcs", action_data.id_num)
jcallahan@15 412
jcallahan@15 413 if not npc then
jcallahan@15 414 return
jcallahan@15 415 end
jcallahan@13 416 local loot_type = action_data.loot_type or "drops"
jcallahan@13 417 npc[loot_type] = npc[loot_type] or {}
jcallahan@13 418
jcallahan@17 419 for index = 1, #action_data.loot_list do
jcallahan@17 420 table.insert(npc[loot_type], action_data.loot_list[index])
jcallahan@13 421 end
jcallahan@13 422 end,
jcallahan@13 423 [AF.OBJECT] = function()
jcallahan@19 424 local object = DBEntry("objects", action_data.identifier)
jcallahan@13 425 object.drops = object.drops or {}
jcallahan@13 426
jcallahan@17 427 for index = 1, #action_data.loot_list do
jcallahan@17 428 table.insert(object.drops, action_data.loot_list[index])
jcallahan@17 429 end
jcallahan@17 430 end,
jcallahan@17 431 [AF.ZONE] = function()
jcallahan@17 432 local loot_type = action_data.loot_type or "drops"
jcallahan@19 433 local zone = DBEntry("zones", action_data.zone)
jcallahan@17 434 zone[loot_type] = zone[loot_type] or {}
jcallahan@17 435
jcallahan@17 436 local location_data = ("%s:%s:%s:%s"):format(action_data.instance_type, action_data.map_level, action_data.x, action_data.y)
jcallahan@17 437 local loot_data = zone[loot_type][location_data]
jcallahan@17 438
jcallahan@17 439 if not loot_data then
jcallahan@17 440 zone[loot_type][location_data] = {}
jcallahan@17 441 loot_data = zone[loot_type][location_data]
jcallahan@17 442 end
jcallahan@17 443
jcallahan@17 444 for index = 1, #action_data.loot_list do
jcallahan@17 445 table.insert(loot_data, action_data.loot_list[index])
jcallahan@13 446 end
jcallahan@13 447 end,
jcallahan@13 448 }
jcallahan@13 449
jcallahan@13 450
jcallahan@13 451 function WDP:LOOT_OPENED()
jcallahan@18 452 if action_data.looting then
jcallahan@18 453 return
jcallahan@18 454 end
jcallahan@18 455
jcallahan@13 456 if not action_data.type then
jcallahan@13 457 action_data.type = AF.NPC
jcallahan@1 458 end
jcallahan@13 459 local verify_func = LOOT_VERIFY_FUNCS[action_data.type]
jcallahan@13 460 local update_func = LOOT_UPDATE_FUNCS[action_data.type]
jcallahan@13 461
jcallahan@14 462 if not verify_func or not update_func then
jcallahan@13 463 return
jcallahan@13 464 end
jcallahan@13 465
jcallahan@14 466 if _G.type(verify_func) == "function" and not verify_func() then
jcallahan@14 467 return
jcallahan@14 468 end
jcallahan@18 469 -- TODO: Remove this check once the MoP client goes live
jcallahan@18 470 local wow_version = private.wow_version
jcallahan@13 471 local loot_registry = {}
jcallahan@17 472 action_data.loot_list = {}
jcallahan@18 473 action_data.looting = true
jcallahan@13 474
jcallahan@18 475 if wow_version == "5.0.1" then
jcallahan@18 476 for loot_slot = 1, _G.GetNumLootItems() do
jcallahan@18 477 local icon_texture, item_text, quantity, quality, locked = _G.GetLootSlotInfo(loot_slot)
jcallahan@13 478
jcallahan@18 479 local slot_type = _G.GetLootSlotType(loot_slot)
jcallahan@18 480
jcallahan@18 481 if slot_type == _G.LOOT_SLOT_ITEM then
jcallahan@18 482 local item_id = ItemLinkToID(_G.GetLootSlotLink(loot_slot))
jcallahan@18 483 loot_registry[item_id] = (loot_registry[item_id]) or 0 + quantity
jcallahan@18 484 elseif slot_type == _G.LOOT_SLOT_MONEY then
jcallahan@18 485 table.insert(action_data.loot_list, ("money:%d"):format(_toCopper(item_text)))
jcallahan@18 486 elseif slot_type == _G.LOOT_SLOT_CURRENCY then
jcallahan@18 487 table.insert(action_data.loot_list, ("currency:%d:%s"):format(quantity, icon_texture:match("[^\\]+$"):lower()))
jcallahan@18 488 end
jcallahan@18 489 end
jcallahan@18 490 else
jcallahan@18 491 for loot_slot = 1, _G.GetNumLootItems() do
jcallahan@18 492 local icon_texture, item_text, quantity, quality, locked = _G.GetLootSlotInfo(loot_slot)
jcallahan@18 493 if _G.LootSlotIsItem(loot_slot) then
jcallahan@18 494 local item_id = ItemLinkToID(_G.GetLootSlotLink(loot_slot))
jcallahan@18 495 loot_registry[item_id] = (loot_registry[item_id]) or 0 + quantity
jcallahan@18 496 elseif _G.LootSlotIsCoin(loot_slot) then
jcallahan@18 497 table.insert(action_data.loot_list, ("money:%d"):format(_toCopper(item_text)))
jcallahan@18 498 elseif _G.LootSlotIsCurrency(loot_slot) then
jcallahan@18 499 table.insert(action_data.loot_list, ("currency:%d:%s"):format(quantity, icon_texture:match("[^\\]+$"):lower()))
jcallahan@18 500 end
jcallahan@13 501 end
jcallahan@13 502 end
jcallahan@13 503
jcallahan@13 504 for item_id, quantity in pairs(loot_registry) do
jcallahan@17 505 table.insert(action_data.loot_list, ("%d:%d"):format(item_id, quantity))
jcallahan@13 506 end
jcallahan@13 507 update_func()
jcallahan@1 508 end
jcallahan@13 509 end -- do-block
jcallahan@0 510
jcallahan@0 511
jcallahan@5 512 local POINT_MATCH_PATTERNS = {
jcallahan@5 513 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING:gsub("%%d", "(%%d+)")), -- May no longer be necessary
jcallahan@5 514 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3:gsub("%%d", "(%%d+)")), -- May no longer be necessary
jcallahan@5 515 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_5V5:gsub("%%d", "(%%d+)")), -- May no longer be necessary
jcallahan@5 516 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_BG:gsub("%%d", "(%%d+)")),
jcallahan@5 517 ("^%s$"):format(_G.ITEM_REQ_ARENA_RATING_3V3_BG:gsub("%%d", "(%%d+)")),
jcallahan@5 518 }
jcallahan@5 519
jcallahan@5 520
jcallahan@7 521 function WDP:UpdateMerchantItems()
jcallahan@4 522 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
jcallahan@4 523
jcallahan@4 524 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
jcallahan@4 525 return
jcallahan@4 526 end
jcallahan@19 527 local merchant = DBEntry("npcs", unit_idnum)
jcallahan@6 528 merchant.sells = merchant.sells or {}
jcallahan@5 529
jcallahan@5 530 for item_index = 1, _G.GetMerchantNumItems() do
jcallahan@5 531 local _, _, copper_price, stack_size, num_available, _, extended_cost = _G.GetMerchantItemInfo(item_index)
jcallahan@5 532 local item_id = ItemLinkToID(_G.GetMerchantItemLink(item_index))
jcallahan@5 533
jcallahan@5 534 if item_id and item_id > 0 then
jcallahan@5 535 local price_string = copper_price
jcallahan@5 536
jcallahan@5 537 if extended_cost then
jcallahan@5 538 local bg_points = 0
jcallahan@5 539 local personal_points = 0
jcallahan@5 540
jcallahan@5 541 DatamineTT:ClearLines()
jcallahan@5 542 DatamineTT:SetMerchantItem(item_index)
jcallahan@5 543
jcallahan@5 544 for line_index = 1, DatamineTT:NumLines() do
jcallahan@5 545 local current_line = _G["WDPDatamineTTTextLeft" .. line_index]
jcallahan@5 546
jcallahan@5 547 if not current_line then
jcallahan@5 548 break
jcallahan@5 549 end
jcallahan@5 550 local breakout
jcallahan@5 551
jcallahan@5 552 for match_index = 1, #POINT_MATCH_PATTERNS do
jcallahan@5 553 local match1, match2 = current_line:GetText():match(POINT_MATCH_PATTERNS[match_index])
jcallahan@5 554 personal_points = personal_points + (match1 or 0)
jcallahan@5 555 bg_points = bg_points + (match2 or 0)
jcallahan@5 556
jcallahan@5 557 if match1 or match2 then
jcallahan@5 558 breakout = true
jcallahan@5 559 break
jcallahan@5 560 end
jcallahan@5 561 end
jcallahan@5 562
jcallahan@5 563 if breakout then
jcallahan@5 564 break
jcallahan@5 565 end
jcallahan@5 566 end
jcallahan@5 567 local currency_list = {}
jcallahan@5 568
jcallahan@5 569 price_string = ("%s:%s:%s"):format(price_string, bg_points, personal_points)
jcallahan@5 570
jcallahan@5 571 for cost_index = 1, _G.GetMerchantItemCostInfo(item_index) do
jcallahan@5 572 local icon_texture, amount_required, currency_link = _G.GetMerchantItemCostItem(item_index, cost_index)
jcallahan@5 573 local currency_id = currency_link and ItemLinkToID(currency_link) or nil
jcallahan@5 574
jcallahan@5 575 if not currency_id or currency_id < 1 then
jcallahan@5 576 if not icon_texture then
jcallahan@5 577 return
jcallahan@5 578 end
jcallahan@5 579 currency_id = icon_texture:match("[^\\]+$"):lower()
jcallahan@5 580 end
jcallahan@5 581 currency_list[#currency_list + 1] = ("(%s:%s)"):format(amount_required, currency_id)
jcallahan@5 582 end
jcallahan@5 583
jcallahan@5 584 for currency_index = 1, #currency_list do
jcallahan@5 585 price_string = ("%s:%s"):format(price_string, currency_list[currency_index])
jcallahan@5 586 end
jcallahan@5 587 end
jcallahan@6 588 merchant.sells[("%s:%s:[%s]"):format(item_id, stack_size, price_string)] = num_available
jcallahan@5 589 end
jcallahan@4 590 end
jcallahan@14 591
jcallahan@14 592 if _G.CanMerchantRepair() then
jcallahan@14 593 merchant.can_repair = true
jcallahan@14 594 end
jcallahan@4 595 end
jcallahan@4 596
jcallahan@4 597
jcallahan@18 598 do
jcallahan@18 599 local GENDER_NAMES = {
jcallahan@18 600 "UNKNOWN",
jcallahan@18 601 "MALE",
jcallahan@18 602 "FEMALE",
jcallahan@18 603 }
jcallahan@2 604
jcallahan@2 605
jcallahan@18 606 local REACTION_NAMES = {
jcallahan@18 607 "HATED",
jcallahan@18 608 "HOSTILE",
jcallahan@18 609 "UNFRIENDLY",
jcallahan@18 610 "NEUTRAL",
jcallahan@18 611 "FRIENDLY",
jcallahan@18 612 "HONORED",
jcallahan@18 613 "REVERED",
jcallahan@18 614 "EXALTED",
jcallahan@18 615 }
jcallahan@2 616
jcallahan@2 617
jcallahan@18 618 local POWER_TYPE_NAMES = {
jcallahan@18 619 ["0"] = "MANA",
jcallahan@18 620 ["1"] = "RAGE",
jcallahan@18 621 ["2"] = "FOCUS",
jcallahan@18 622 ["3"] = "ENERGY",
jcallahan@18 623 ["6"] = "RUNIC_POWER",
jcallahan@18 624 }
jcallahan@2 625
jcallahan@2 626
jcallahan@18 627 function WDP:PLAYER_TARGET_CHANGED()
jcallahan@18 628 if not _G.UnitExists("target") or _G.UnitPlayerControlled("target") then
jcallahan@18 629 return
jcallahan@18 630 end
jcallahan@18 631 local unit_type, unit_idnum = self:ParseGUID(_G.UnitGUID("target"))
jcallahan@2 632
jcallahan@18 633 if unit_type ~= private.UNIT_TYPES.NPC or not unit_idnum then
jcallahan@18 634 return
jcallahan@18 635 end
jcallahan@19 636 local npc = DBEntry("npcs", unit_idnum)
jcallahan@18 637 local _, class_token = _G.UnitClass("target")
jcallahan@18 638 npc.class = class_token
jcallahan@20 639
jcallahan@20 640 UpdateFactionNames()
jcallahan@20 641 DatamineTT:ClearLines()
jcallahan@20 642 DatamineTT:SetUnit("target")
jcallahan@20 643
jcallahan@20 644 for line_index = 1, DatamineTT:NumLines() do
jcallahan@20 645 local current_line = _G["WDPDatamineTTTextLeft" .. line_index]
jcallahan@20 646
jcallahan@20 647 if not current_line then
jcallahan@20 648 break
jcallahan@20 649 end
jcallahan@20 650 local line_text = current_line:GetText()
jcallahan@20 651
jcallahan@20 652 if faction_names[line_text] then
jcallahan@20 653 npc.faction = line_text
jcallahan@20 654 break
jcallahan@20 655 end
jcallahan@20 656 end
jcallahan@18 657 npc.gender = GENDER_NAMES[_G.UnitSex("target")] or "UNDEFINED"
jcallahan@18 658 npc.is_pvp = _G.UnitIsPVP("target") and true or nil
jcallahan@18 659 npc.reaction = ("%s:%s:%s"):format(_G.UnitLevel("player"), _G.UnitFactionGroup("player"), REACTION_NAMES[_G.UnitReaction("player", "target")])
jcallahan@18 660 npc.stats = npc.stats or {}
jcallahan@2 661
jcallahan@18 662 local npc_level = ("level_%d"):format(_G.UnitLevel("target"))
jcallahan@3 663
jcallahan@18 664 if not npc.stats[npc_level] then
jcallahan@18 665 npc.stats[npc_level] = {
jcallahan@18 666 max_health = _G.UnitHealthMax("target"),
jcallahan@18 667 }
jcallahan@3 668
jcallahan@18 669 local max_power = _G.UnitManaMax("target")
jcallahan@18 670
jcallahan@18 671 if max_power > 0 then
jcallahan@18 672 local power_type = _G.UnitPowerType("target")
jcallahan@18 673 npc.stats[npc_level].power = ("%s:%d"):format(POWER_TYPE_NAMES[_G.tostring(power_type)] or power_type, max_power)
jcallahan@18 674 end
jcallahan@3 675 end
jcallahan@21 676 table.wipe(action_data)
jcallahan@21 677 action_data.type = AF.NPC
jcallahan@21 678 action_data.id_num = unit_idnum
jcallahan@21 679 action_data.npc_level = npc_level
jcallahan@2 680 end
jcallahan@18 681 end -- do-block
jcallahan@2 682
jcallahan@12 683 do
jcallahan@12 684 local function UpdateQuestJuncture(point)
jcallahan@12 685 local unit_name = _G.UnitName("questnpc")
jcallahan@9 686
jcallahan@12 687 if not unit_name then
jcallahan@12 688 return
jcallahan@12 689 end
jcallahan@12 690 local unit_type, unit_id = WDP:ParseGUID(_G.UnitGUID("questnpc"))
jcallahan@9 691
jcallahan@12 692 if unit_type == private.UNIT_TYPES.OBJECT then
jcallahan@12 693 UpdateObjectLocation(unit_id)
jcallahan@12 694 end
jcallahan@19 695 local quest = DBEntry("quests", _G.GetQuestID())
jcallahan@12 696 quest[point] = quest[point] or {}
jcallahan@12 697 quest[point][("%s:%d"):format(private.UNIT_TYPE_NAMES[unit_type + 1], unit_id)] = true
jcallahan@12 698 end
jcallahan@10 699
jcallahan@12 700
jcallahan@12 701 function WDP:QUEST_COMPLETE()
jcallahan@12 702 UpdateQuestJuncture("end")
jcallahan@10 703 end
jcallahan@10 704
jcallahan@12 705
jcallahan@12 706 function WDP:QUEST_DETAIL()
jcallahan@12 707 UpdateQuestJuncture("begin")
jcallahan@10 708 end
jcallahan@12 709 end -- do-block
jcallahan@9 710
jcallahan@9 711
jcallahan@4 712 function WDP:QUEST_LOG_UPDATE()
jcallahan@4 713 self:UnregisterEvent("QUEST_LOG_UPDATE")
jcallahan@4 714 end
jcallahan@4 715
jcallahan@4 716
jcallahan@4 717 function WDP:UNIT_QUEST_LOG_CHANGED(event, unit_id)
jcallahan@4 718 if unit_id ~= "player" then
jcallahan@4 719 return
jcallahan@4 720 end
jcallahan@4 721 self:RegisterEvent("QUEST_LOG_UPDATE")
jcallahan@4 722 end
jcallahan@4 723
jcallahan@4 724
jcallahan@1 725 function WDP:UNIT_SPELLCAST_SENT(event_name, unit_id, spell_name, spell_rank, target_name, spell_line)
jcallahan@1 726 if private.tracked_line or unit_id ~= "player" then
jcallahan@1 727 return
jcallahan@1 728 end
jcallahan@1 729 local spell_label = private.SPELL_LABELS_BY_NAME[spell_name]
jcallahan@1 730
jcallahan@1 731 if not spell_label then
jcallahan@1 732 return
jcallahan@1 733 end
jcallahan@18 734 table.wipe(action_data)
jcallahan@1 735
jcallahan@1 736 local tt_item_name, tt_item_link = _G.GameTooltip:GetItem()
jcallahan@1 737 local tt_unit_name, tt_unit_id = _G.GameTooltip:GetUnit()
jcallahan@1 738
jcallahan@1 739 if not tt_unit_name and _G.UnitName("target") == target_name then
jcallahan@1 740 tt_unit_name = target_name
jcallahan@1 741 tt_unit_id = "target"
jcallahan@1 742 end
jcallahan@1 743 local spell_flags = private.SPELL_FLAGS_BY_LABEL[spell_label]
jcallahan@1 744
jcallahan@16 745 if tt_unit_name and not tt_item_name then
jcallahan@16 746 if bit.band(spell_flags, AF.NPC) == AF.NPC then
jcallahan@16 747 if not tt_unit_id or tt_unit_name ~= target_name then
jcallahan@16 748 return
jcallahan@16 749 end
jcallahan@16 750 action_data.type = AF.NPC
jcallahan@16 751 action_data.loot_type = spell_label:lower()
jcallahan@16 752 end
jcallahan@16 753 elseif bit.band(spell_flags, AF.ITEM) == AF.ITEM then
jcallahan@16 754 action_data.type = AF.ITEM
jcallahan@16 755 action_data.loot_type = spell_label:lower()
jcallahan@16 756
jcallahan@16 757 if tt_item_name and tt_item_name == target_name then
jcallahan@16 758 action_data.item_id = ItemLinkToID(tt_item_link)
jcallahan@16 759 elseif target_name and target_name ~= "" then
jcallahan@16 760 local _, target_item_link = _G.GetItemInfo(target_name)
jcallahan@16 761 action_data.item_id = ItemLinkToID(target_item_link)
jcallahan@16 762 end
jcallahan@16 763 elseif not tt_item_name and not tt_unit_name then
jcallahan@8 764 local zone_name, x, y, map_level, instance_type = CurrentLocationData()
jcallahan@1 765
jcallahan@17 766 action_data.instance_type = instance_type
jcallahan@17 767 action_data.map_level = map_level
jcallahan@17 768 action_data.name = target_name
jcallahan@17 769 action_data.x = x
jcallahan@17 770 action_data.y = y
jcallahan@17 771 action_data.zone = zone_name
jcallahan@17 772
jcallahan@1 773 if bit.band(spell_flags, AF.OBJECT) == AF.OBJECT then
jcallahan@17 774 if target_name == "" then
jcallahan@17 775 return
jcallahan@17 776 end
jcallahan@11 777 local identifier = ("%s:%s"):format(spell_label, target_name)
jcallahan@11 778 UpdateObjectLocation(identifier)
jcallahan@11 779
jcallahan@1 780 action_data.type = AF.OBJECT
jcallahan@11 781 action_data.identifier = identifier
jcallahan@1 782 elseif bit.band(spell_flags, AF.ZONE) == AF.ZONE then
jcallahan@17 783 action_data.type = AF.ZONE
jcallahan@17 784 action_data.loot_type = spell_label:lower()
jcallahan@1 785 end
jcallahan@1 786 end
jcallahan@1 787
jcallahan@18 788 -- print(("%s: '%s', '%s', '%s', '%s', '%s'"):format(event_name, unit_id, spell_name, spell_rank, target_name, spell_line))
jcallahan@1 789 private.tracked_line = spell_line
jcallahan@0 790 end
jcallahan@0 791
jcallahan@0 792
jcallahan@1 793 function WDP:UNIT_SPELLCAST_SUCCEEDED(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id)
jcallahan@1 794 if unit_id ~= "player" then
jcallahan@1 795 return
jcallahan@1 796 end
jcallahan@1 797
jcallahan@18 798 -- if private.SPELL_LABELS_BY_NAME[spell_name] then
jcallahan@18 799 -- print(("%s: '%s', '%s', '%s', '%s', '%s'"):format(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id))
jcallahan@18 800 -- end
jcallahan@1 801 private.tracked_line = nil
jcallahan@0 802 end
jcallahan@0 803
jcallahan@1 804 function WDP:HandleSpellFailure(event_name, unit_id, spell_name, spell_rank, spell_line, spell_id)
jcallahan@1 805 if unit_id ~= "player" then
jcallahan@1 806 return
jcallahan@1 807 end
jcallahan@0 808
jcallahan@1 809 if private.tracked_line == spell_line then
jcallahan@1 810 private.tracked_line = nil
jcallahan@1 811 end
jcallahan@0 812 end