annotate Core.lua @ 127:65c285394049 v59

Fixed an issue with reading bank data.
author yellowfive
date Tue, 17 Jul 2018 16:32:12 -0700
parents e31b02b24488
children d9a059484b22
rev   line source
yellowfive@57 1 AskMrRobot = LibStub("AceAddon-3.0"):NewAddon("AskMrRobot", "AceEvent-3.0", "AceComm-3.0", "AceConsole-3.0", "AceSerializer-3.0")
yellowfive@57 2 local Amr = AskMrRobot
yellowfive@57 3 Amr.Serializer = LibStub("AskMrRobot-Serializer")
yellowfive@57 4
yellowfive@57 5 Amr.ADDON_NAME = "AskMrRobot"
yellowfive@57 6
yellowfive@127 7 local VAR_VERSION = 1
yellowfive@127 8
yellowfive@57 9 -- types of inter-addon messages that we receive, used to parcel them out to the proper handlers
yellowfive@57 10 Amr.MessageTypes = {
yellowfive@57 11 Version = "_V",
yellowfive@124 12 VersionRequest = "_VR"
yellowfive@57 13 }
yellowfive@57 14
yellowfive@57 15 local L = LibStub("AceLocale-3.0"):GetLocale("AskMrRobot", true)
yellowfive@57 16 local AceGUI = LibStub("AceGUI-3.0")
yellowfive@57 17
yellowfive@57 18 -- minimap icon and LDB support
yellowfive@57 19 local _amrLDB = LibStub("LibDataBroker-1.1"):NewDataObject(Amr.ADDON_NAME, {
yellowfive@57 20 type = "launcher",
yellowfive@57 21 text = "Ask Mr. Robot",
yellowfive@57 22 icon = "Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\icon",
yellowfive@57 23 OnClick = function(self, button, down)
yellowfive@57 24 if button == "LeftButton" then
yellowfive@124 25 --if IsControlKeyDown() then
yellowfive@124 26 -- Amr:Wipe()
yellowfive@124 27 --else
yellowfive@57 28 Amr:Toggle()
yellowfive@124 29 --end
yellowfive@57 30 elseif button == "RightButton" then
yellowfive@57 31 Amr:EquipGearSet()
yellowfive@57 32 end
yellowfive@57 33 end,
yellowfive@57 34 OnTooltipShow = function(tt)
yellowfive@57 35 tt:AddLine("Ask Mr. Robot", 1, 1, 1);
yellowfive@57 36 tt:AddLine(" ");
yellowfive@57 37 tt:AddLine(L.MinimapTooltip)
yellowfive@57 38 end
yellowfive@57 39 })
yellowfive@57 40 local _icon = LibStub("LibDBIcon-1.0")
yellowfive@57 41
yellowfive@57 42
yellowfive@57 43 -- initialize the database
yellowfive@57 44 local function initializeDb()
yellowfive@57 45
yellowfive@127 46 local charDefaults = {
yellowfive@127 47 FirstUse = true, -- true if this is first time use, gets cleared after seeing the export help splash window
yellowfive@127 48 Talents = {}, -- for each spec, selected talents
yellowfive@127 49 Equipped = {}, -- for each spec, slot id to item info
yellowfive@127 50 BagItems = {}, -- list of item info for bags
yellowfive@127 51 BankItems = {}, -- list of item info for bank
yellowfive@127 52 BagItemsAndCounts = {}, -- used mainly for the shopping list
yellowfive@127 53 BankItemsAndCounts = {}, -- used mainly for the shopping list
yellowfive@127 54 GearSets = {}, -- imported gear sets
yellowfive@127 55 ExtraEnchantData = {}, -- enchant id to enchant display information and material information
yellowfive@127 56 Logging = { -- character logging settings
yellowfive@127 57 Enabled = false, -- whether logging is currently on or not
yellowfive@127 58 LastZone = nil, -- last zone the player was in
yellowfive@127 59 LastDiff = nil, -- last difficulty for the last zone the player was in
yellowfive@127 60 LastWipe = nil -- last time a wipe was called by this player
yellowfive@127 61 }
yellowfive@127 62 }
yellowfive@127 63
yellowfive@57 64 local defaults = {
yellowfive@127 65 char = charDefaults,
yellowfive@57 66 profile = {
yellowfive@57 67 minimap = { -- minimap hide/show and position settings
yellowfive@57 68 hide = false
yellowfive@57 69 },
yellowfive@57 70 window = {}, -- main window position settings
yellowfive@57 71 shopWindow = {}, -- shopping list window position settings
yellowfive@57 72 options = {
yellowfive@57 73 autoGear = false, -- auto-equip saved gear sets when changing specs
yellowfive@61 74 shopAh = false, -- auto-show shopping list at AH
yellowfive@91 75 disableEm = false, -- disable auto-creation of equipment manager sets
yellowfive@61 76 uiScale = 1 -- custom scale for AMR UI
yellowfive@57 77 },
yellowfive@57 78 Logging = { -- global logging settings
yellowfive@57 79 Auto = {} -- for each instanceId, for each difficultyId, true if auto-logging enabled
yellowfive@57 80 }
yellowfive@57 81 },
yellowfive@57 82 global = {
yellowfive@57 83 Region = nil, -- region that this user is in, all characters on the same account should be the same region
yellowfive@57 84 Shopping = {}, -- shopping list data stored globally for access on any character
yellowfive@57 85 Logging = { -- a lot of log data is stored globally for simplicity, can only be raiding with one character at a time
yellowfive@57 86 Wipes = {}, -- times that a wipe was called
yellowfive@57 87 PlayerData = {}, -- player data gathered at fight start
yellowfive@57 88 PlayerExtras = {} -- player extra data like auras, gathered at fight start
yellowfive@57 89 }
yellowfive@57 90 }
yellowfive@57 91 }
yellowfive@57 92
yellowfive@124 93 Amr.db = LibStub("AceDB-3.0"):New("AskMrRobotDb4", defaults)
yellowfive@110 94
yellowfive@127 95 -- make sure character data is on current version
yellowfive@127 96 if not Amr.db.char.VarVersion or Amr.db.char.VarVersion ~= VAR_VERSION then
yellowfive@127 97 Amr.db.char = charDefaults
yellowfive@127 98 end
yellowfive@127 99
yellowfive@110 100 -- set defaults for auto logging; if a new zone is added and some other stuff was turned on, turn on the new zone too
yellowfive@110 101 local hasSomeLogging = false
yellowfive@110 102 local addedLogging = {}
yellowfive@57 103 for i, instanceId in ipairs(Amr.InstanceIdsOrdered) do
yellowfive@110 104 local byDiff = Amr.db.profile.Logging.Auto[instanceId]
yellowfive@57 105 if not byDiff then
yellowfive@57 106 byDiff = {}
yellowfive@110 107 Amr.db.profile.Logging.Auto[instanceId] = byDiff
yellowfive@110 108 addedLogging[instanceId] = byDiff
yellowfive@57 109 end
yellowfive@57 110
yellowfive@57 111 for k, difficultyId in pairs(Amr.Difficulties) do
yellowfive@110 112 if not byDiff[difficultyId] then
yellowfive@57 113 byDiff[difficultyId] = false
yellowfive@110 114 else
yellowfive@110 115 hasSomeLogging = true
yellowfive@57 116 end
yellowfive@57 117 end
yellowfive@57 118 end
yellowfive@57 119
yellowfive@110 120 if hasSomeLogging then
yellowfive@110 121 for instanceId, byDiff in pairs(addedLogging) do
yellowfive@110 122 for k, difficultyId in pairs(Amr.Difficulties) do
yellowfive@110 123 byDiff[difficultyId] = true
yellowfive@110 124 end
yellowfive@110 125 end
yellowfive@110 126 end
yellowfive@110 127
yellowfive@57 128
yellowfive@57 129 Amr.db.RegisterCallback(Amr, "OnProfileChanged", "RefreshConfig")
yellowfive@57 130 Amr.db.RegisterCallback(Amr, "OnProfileCopied", "RefreshConfig")
yellowfive@57 131 Amr.db.RegisterCallback(Amr, "OnProfileReset", "RefreshConfig")
yellowfive@57 132 end
yellowfive@57 133
yellowfive@57 134 function Amr:OnInitialize()
yellowfive@57 135
yellowfive@57 136 initializeDb()
yellowfive@57 137
yellowfive@57 138 Amr:RegisterChatCommand("amr", "SlashCommand")
yellowfive@57 139
yellowfive@57 140 _icon:Register(Amr.ADDON_NAME, _amrLDB, self.db.profile.minimap)
yellowfive@57 141
yellowfive@57 142 -- listen for inter-addon communication
yellowfive@57 143 self:RegisterComm(Amr.ChatPrefix, "OnCommReceived")
yellowfive@57 144 end
yellowfive@57 145
yellowfive@57 146 local _enteredWorld = false
yellowfive@57 147 local _pendingInit = false
yellowfive@57 148
yellowfive@57 149 function finishInitialize()
yellowfive@57 150
yellowfive@57 151 -- record region, the only thing that we still can't get from the log file
yellowfive@57 152 Amr.db.global.Region = Amr.RegionNames[GetCurrentRegion()]
yellowfive@57 153
yellowfive@57 154 -- make sure that some initialization is deferred until after PLAYER_ENTERING_WORLD event so that data we need is available;
yellowfive@57 155 -- also delay this initialization for a few extra seconds to deal with some event spam that is otherwise hard to identify and ignore when a player logs in
yellowfive@57 156 Amr.Wait(5, function()
yellowfive@57 157 Amr:InitializeVersions()
yellowfive@57 158 Amr:InitializeGear()
yellowfive@57 159 Amr:InitializeExport()
yellowfive@57 160 Amr:InitializeCombatLog()
yellowfive@57 161 end)
yellowfive@57 162 end
yellowfive@57 163
yellowfive@57 164 function onPlayerEnteringWorld()
yellowfive@57 165
yellowfive@57 166 _enteredWorld = true
yellowfive@57 167
yellowfive@57 168 if _pendingInit then
yellowfive@57 169 finishInitialize()
yellowfive@57 170 _pendingInit = false
yellowfive@57 171 end
yellowfive@57 172 end
yellowfive@57 173
yellowfive@57 174 function Amr:OnEnable()
yellowfive@57 175
yellowfive@81 176 --[[
yellowfive@57 177 -- listen for changes to the snapshot enable state, and always make sure it is enabled if using the core AskMrRobot addon
yellowfive@57 178 self:RegisterMessage("AMR_SNAPSHOT_STATE_CHANGED", function(eventName, isEnabled)
yellowfive@57 179 if not isEnabled then
yellowfive@57 180 -- immediately re-enable on any attempt to disable
yellowfive@57 181 Amr.Serializer:EnableSnapshots()
yellowfive@57 182 end
yellowfive@57 183 end)
yellowfive@57 184 self.Serializer:EnableSnapshots()
yellowfive@81 185 ]]
yellowfive@57 186
yellowfive@57 187 -- update based on current configuration whenever enabled
yellowfive@57 188 self:RefreshConfig()
yellowfive@57 189
yellowfive@57 190 -- if we have fully entered the world, do initialization; otherwise wait for PLAYER_ENTERING_WORLD to continue
yellowfive@57 191 if not _enteredWorld then
yellowfive@57 192 _pendingInit = true
yellowfive@57 193 else
yellowfive@57 194 _pendingInit = false
yellowfive@57 195 finishInitialize()
yellowfive@57 196 end
yellowfive@57 197 end
yellowfive@57 198
yellowfive@57 199 function Amr:OnDisable()
yellowfive@57 200 -- disabling is not supported
yellowfive@57 201 end
yellowfive@57 202
yellowfive@57 203
yellowfive@57 204 ----------------------------------------------------------------------------------------
yellowfive@57 205 -- Slash Commands
yellowfive@57 206 ----------------------------------------------------------------------------------------
yellowfive@57 207 local _slashMethods = {
yellowfive@57 208 hide = "Hide",
yellowfive@57 209 show = "Show",
yellowfive@57 210 toggle = "Toggle",
yellowfive@124 211 equip = "EquipGearSet",
yellowfive@57 212 version = "PrintVersions",
yellowfive@124 213 --wipe = "Wipe",
yellowfive@124 214 --undowipe = "UndoWipe",
yellowfive@61 215 reset = "Reset",
yellowfive@57 216 test = "Test"
yellowfive@57 217 }
yellowfive@57 218
yellowfive@57 219 function Amr:SlashCommand(input)
yellowfive@57 220 input = string.lower(input)
yellowfive@57 221 local parts = {}
yellowfive@57 222 for w in input:gmatch("%S+") do
yellowfive@57 223 table.insert(parts, w)
yellowfive@57 224 end
yellowfive@57 225
yellowfive@57 226 if #parts == 0 then return end
yellowfive@57 227
yellowfive@57 228 local func = _slashMethods[parts[1]]
yellowfive@57 229 if not func then return end
yellowfive@57 230
yellowfive@57 231 local funcArgs = {}
yellowfive@57 232 for i = 2, #parts do
yellowfive@57 233 table.insert(funcArgs, parts[i])
yellowfive@57 234 end
yellowfive@57 235
yellowfive@57 236 Amr[func](Amr, unpack(funcArgs))
yellowfive@57 237 end
yellowfive@57 238
yellowfive@57 239
yellowfive@57 240 ----------------------------------------------------------------------------------------
yellowfive@57 241 -- Configuration
yellowfive@57 242 ----------------------------------------------------------------------------------------
yellowfive@57 243
yellowfive@57 244 -- refresh all state based on the current values of configuration options
yellowfive@57 245 function Amr:RefreshConfig()
yellowfive@57 246
yellowfive@57 247 self:UpdateMinimap()
yellowfive@57 248 self:RefreshOptionsUi()
yellowfive@57 249 self:RefreshLogUi()
yellowfive@57 250 end
yellowfive@57 251
yellowfive@57 252 function Amr:UpdateMinimap()
yellowfive@57 253
yellowfive@57 254 if self.db.profile.minimap.hide or not Amr:IsEnabled() then
yellowfive@57 255 _icon:Hide(Amr.ADDON_NAME)
yellowfive@57 256 else
yellowfive@57 257 -- change icon color if logging
yellowfive@57 258 if Amr:IsLogging() then
yellowfive@57 259 _amrLDB.icon = 'Interface\\AddOns\\AskMrRobot\\Media\\icon_green'
yellowfive@57 260 else
yellowfive@57 261 _amrLDB.icon = 'Interface\\AddOns\\AskMrRobot\\Media\\icon'
yellowfive@57 262 end
yellowfive@57 263
yellowfive@57 264 _icon:Show(Amr.ADDON_NAME)
yellowfive@57 265 end
yellowfive@57 266 end
yellowfive@57 267
yellowfive@57 268
yellowfive@57 269 ----------------------------------------------------------------------------------------
yellowfive@57 270 -- Version Checking
yellowfive@57 271 ----------------------------------------------------------------------------------------
yellowfive@57 272
yellowfive@57 273 -- version of addon being run by each person in the player's raid or group
yellowfive@57 274 Amr.GroupVersions = {}
yellowfive@57 275
yellowfive@57 276 local function toGroupVersionKey(realm, name)
yellowfive@57 277 realm = string.gsub(realm, "%s+", "")
yellowfive@57 278 return name .. "-" .. realm
yellowfive@57 279 end
yellowfive@57 280
yellowfive@57 281 -- prune out version information for players no longer in the current raid group
yellowfive@57 282 local function pruneVersionInfo()
yellowfive@57 283
yellowfive@57 284 local newVersions = {}
yellowfive@57 285 local units = Amr:GetGroupUnitIdentifiers()
yellowfive@57 286
yellowfive@57 287 for i, unitId in ipairs(units) do
yellowfive@57 288 local realm, name = Amr:GetRealmAndName(unitId)
yellowfive@57 289 if realm then
yellowfive@57 290 local key = toGroupVersionKey(realm, name)
yellowfive@57 291 newVersions[key] = Amr.GroupVersions[key]
yellowfive@57 292 end
yellowfive@57 293 end
yellowfive@57 294
yellowfive@57 295 Amr.GroupVersions = newVersions
yellowfive@57 296 end
yellowfive@57 297
yellowfive@57 298 -- send version information to other people in the same raid group
yellowfive@57 299 local function sendVersionInfo()
yellowfive@57 300
yellowfive@57 301 local realm = GetRealmName()
yellowfive@57 302 local name = UnitName("player")
yellowfive@57 303 local ver = GetAddOnMetadata(Amr.ADDON_NAME, "Version")
yellowfive@57 304
yellowfive@57 305 local msg = string.format("%s\n%s\n%s\n%s", Amr.MessageTypes.Version, realm, name, ver)
yellowfive@57 306 Amr:SendAmrCommMessage(msg)
yellowfive@57 307 end
yellowfive@57 308
yellowfive@57 309 local function onVersionInfoReceived(message)
yellowfive@57 310
yellowfive@57 311 -- message will be of format: realm\nname\nversion
yellowfive@57 312 local parts = {}
yellowfive@57 313 for part in string.gmatch(message, "([^\n]+)") do
yellowfive@57 314 table.insert(parts, part)
yellowfive@57 315 end
yellowfive@57 316
yellowfive@57 317 local key = toGroupVersionKey(parts[2], parts[3])
yellowfive@57 318 local ver = parts[4]
yellowfive@57 319
yellowfive@57 320 Amr.GroupVersions[key] = tonumber(ver)
yellowfive@57 321
yellowfive@57 322 -- make sure that versions are properly pruned in case this message arrived late and the player has since been removed from the group
yellowfive@57 323 pruneVersionInfo()
yellowfive@57 324 end
yellowfive@57 325
yellowfive@57 326 -- get the addon version another person in the player's raid/group is running, or 0 if they are not running the addon
yellowfive@57 327 function Amr:GetAddonVersion(realm, name)
yellowfive@57 328 local ver = Amr.GroupVersions[toGroupVersionKey(realm, name)]
yellowfive@57 329 return ver or 0
yellowfive@57 330 end
yellowfive@57 331
yellowfive@57 332 function Amr:PrintVersions()
yellowfive@57 333
yellowfive@57 334 if not IsInGroup() and not IsInRaid() then
yellowfive@57 335 self:Print(L.VersionChatNotGrouped)
yellowfive@57 336 return
yellowfive@57 337 end
yellowfive@57 338
yellowfive@57 339 local units = self:GetGroupUnitIdentifiers()
yellowfive@57 340
yellowfive@57 341 local msg = {}
yellowfive@57 342 table.insert(msg, L.VersionChatTitle)
yellowfive@57 343
yellowfive@57 344 for i, unitId in ipairs(units) do
yellowfive@57 345 local realm, name = self:GetRealmAndName(unitId)
yellowfive@57 346 if realm then
yellowfive@57 347 local key = toGroupVersionKey(realm, name)
yellowfive@57 348 local ver = Amr.GroupVersions[key]
yellowfive@57 349 if not ver then
yellowfive@57 350 table.insert(msg, key .. " |cFFFF0000" .. L.VersionChatNotInstalled .. "|r")
yellowfive@57 351 else
yellowfive@57 352 table.insert(msg, key .. " v" .. ver)
yellowfive@57 353 end
yellowfive@57 354 end
yellowfive@57 355 end
yellowfive@57 356
yellowfive@57 357 msg = table.concat(msg, "\n")
yellowfive@57 358 print(msg)
yellowfive@57 359 end
yellowfive@57 360
yellowfive@57 361 function Amr:InitializeVersions()
yellowfive@57 362 Amr:AddEventHandler("GROUP_ROSTER_UPDATE", pruneVersionInfo)
yellowfive@57 363 Amr:AddEventHandler("GROUP_ROSTER_UPDATE", sendVersionInfo)
yellowfive@57 364
yellowfive@57 365 -- request version information from anyone in my group upon initialization
yellowfive@57 366 if IsInGroup() or IsInRaid() then
yellowfive@57 367 Amr:SendAmrCommMessage(Amr.MessageTypes.VersionRequest)
yellowfive@57 368 end
yellowfive@57 369 end
yellowfive@57 370
yellowfive@57 371
yellowfive@57 372 ----------------------------------------------------------------------------------------
yellowfive@57 373 -- Generic Helpers
yellowfive@57 374 ----------------------------------------------------------------------------------------
yellowfive@57 375
yellowfive@57 376 local _waitTable = {}
yellowfive@57 377 local _waitFrame = nil
yellowfive@57 378
yellowfive@57 379 -- execute the specified function after the specified delay (in seconds)
yellowfive@57 380 function Amr.Wait(delay, func, ...)
yellowfive@57 381 if not _waitFrame then
yellowfive@57 382 _waitFrame = CreateFrame("Frame", "AmrWaitFrame", UIParent)
yellowfive@57 383 _waitFrame:SetScript("OnUpdate", function (self, elapse)
yellowfive@57 384 local count = #_waitTable
yellowfive@57 385 local i = 1
yellowfive@57 386 while(i <= count) do
yellowfive@57 387 local waitRecord = table.remove(_waitTable, i)
yellowfive@57 388 local d = table.remove(waitRecord, 1)
yellowfive@57 389 local f = table.remove(waitRecord, 1)
yellowfive@57 390 local p = table.remove(waitRecord, 1)
yellowfive@57 391 if d > elapse then
yellowfive@57 392 table.insert(_waitTable, i, { d-elapse, f, p })
yellowfive@57 393 i = i + 1
yellowfive@57 394 else
yellowfive@57 395 count = count - 1
yellowfive@57 396 f(unpack(p))
yellowfive@57 397 end
yellowfive@57 398 end
yellowfive@57 399 end)
yellowfive@57 400 end
yellowfive@57 401 table.insert(_waitTable, { delay, func, {...} })
yellowfive@57 402 return true
yellowfive@57 403 end
yellowfive@57 404
yellowfive@57 405 -- helper to iterate over a table in order by its keys
yellowfive@57 406 function Amr.spairs(t, order)
yellowfive@57 407 -- collect the keys
yellowfive@57 408 local keys = {}
yellowfive@57 409 for k in pairs(t) do keys[#keys+1] = k end
yellowfive@57 410
yellowfive@57 411 -- if order function given, sort by it by passing the table and keys a, b,
yellowfive@57 412 -- otherwise just sort the keys
yellowfive@57 413 if order then
yellowfive@57 414 table.sort(keys, function(a,b) return order(t, a, b) end)
yellowfive@57 415 else
yellowfive@57 416 table.sort(keys)
yellowfive@57 417 end
yellowfive@57 418
yellowfive@57 419 -- return the iterator function
yellowfive@57 420 local i = 0
yellowfive@57 421 return function()
yellowfive@57 422 i = i + 1
yellowfive@57 423 if keys[i] then
yellowfive@57 424 return keys[i], t[keys[i]]
yellowfive@57 425 end
yellowfive@57 426 end
yellowfive@57 427 end
yellowfive@57 428
yellowfive@57 429 function Amr.StartsWith(str, prefix)
yellowfive@57 430 if string.len(str) < string.len(prefix) then return false end
yellowfive@57 431 return string.sub(str, 1, string.len(prefix)) == prefix
yellowfive@57 432 end
yellowfive@57 433
yellowfive@124 434 function Amr.IsEmpty(table)
yellowfive@124 435 return next(table) == nil
yellowfive@124 436 end
yellowfive@124 437
yellowfive@124 438 function Amr.Contains(table, value)
yellowfive@124 439 if not table then return false end
yellowfive@124 440 for k,v in pairs(table) do
yellowfive@124 441 if v == value then
yellowfive@124 442 return true
yellowfive@124 443 end
yellowfive@124 444 end
yellowfive@124 445 return false
yellowfive@124 446 end
yellowfive@124 447
yellowfive@57 448 -- helper to get the unit identifiers (e.g. to pass to GetUnitName) for all members of the player's current group/raid
yellowfive@57 449 function Amr:GetGroupUnitIdentifiers()
yellowfive@57 450
yellowfive@57 451 local units = {}
yellowfive@57 452 if IsInRaid() then
yellowfive@57 453 for i = 1,40 do
yellowfive@57 454 table.insert(units, "raid" .. i)
yellowfive@57 455 end
yellowfive@57 456 elseif IsInGroup() then
yellowfive@57 457 table.insert(units, "player")
yellowfive@57 458 for i = 1,4 do
yellowfive@57 459 table.insert(units, "party" .. i)
yellowfive@57 460 end
yellowfive@57 461 else
yellowfive@57 462 table.insert(units, "player")
yellowfive@57 463 end
yellowfive@57 464
yellowfive@57 465 return units
yellowfive@57 466 end
yellowfive@57 467
yellowfive@57 468 -- helper to get the realm and name from a unitId (e.g. "player" or "raid1")
yellowfive@57 469 function Amr:GetRealmAndName(unitId)
yellowfive@57 470
yellowfive@57 471 local name = GetUnitName(unitId, true)
yellowfive@57 472 if not name then return end
yellowfive@57 473
yellowfive@57 474 local realm = GetRealmName()
yellowfive@57 475 local splitPos = string.find(name, "-")
yellowfive@57 476 if splitPos ~= nil then
yellowfive@57 477 realm = string.sub(name, splitPos + 1)
yellowfive@57 478 name = string.sub(name, 1, splitPos - 1)
yellowfive@57 479 end
yellowfive@57 480
yellowfive@57 481 return realm, name
yellowfive@57 482 end
yellowfive@57 483
yellowfive@57 484 -- find the unitid of a player given the name and realm... this comes from the server so the realm will be in english...
yellowfive@57 485 -- TODO: more robust handling of players with same name but different realms in the same group on non-english clients
yellowfive@57 486 function Amr:GetUnitId(unitRealm, unitName)
yellowfive@57 487
yellowfive@57 488 local nameMatches = {}
yellowfive@57 489
yellowfive@57 490 local units = Amr:GetGroupUnitIdentifiers()
yellowfive@57 491 for i, unitId in ipairs(units) do
yellowfive@57 492 local realm, name = Amr:GetRealmAndName(unitId)
yellowfive@57 493 if realm then
yellowfive@57 494 -- remove spaces to ensure proper matches
yellowfive@57 495 realm = string.gsub(realm, "%s+", "")
yellowfive@57 496 unitRealm = string.gsub(unitRealm, "%s+", "")
yellowfive@57 497
yellowfive@57 498 if unitRealm == realm and unitName == name then return unitId end
yellowfive@57 499 if unitName == name then
yellowfive@57 500 table.insert(nameMatches, unitId)
yellowfive@57 501 end
yellowfive@57 502 end
yellowfive@57 503 end
yellowfive@57 504
yellowfive@57 505 -- only one player with same name, must be the player of interest
yellowfive@57 506 if #nameMatches == 1 then return nameMatches[1] end
yellowfive@57 507
yellowfive@57 508 -- could not find or ambiguous
yellowfive@57 509 return nil
yellowfive@57 510 end
yellowfive@57 511
yellowfive@57 512
yellowfive@57 513 -- search the tooltip for txt, returns true if it is encountered on any line
yellowfive@57 514 function Amr:IsTextInTooltip(tt, txt)
yellowfive@57 515 local regions = { tt:GetRegions() }
yellowfive@57 516 for i, region in ipairs(regions) do
yellowfive@57 517 if region and region:GetObjectType() == "FontString" then
yellowfive@57 518 if region:GetText() == txt then
yellowfive@57 519 return true
yellowfive@57 520 end
yellowfive@57 521 end
yellowfive@57 522 end
yellowfive@57 523 return false
yellowfive@57 524 end
yellowfive@57 525
yellowfive@124 526 -- helper to determine if we can equip an item (it is soulbound)
yellowfive@59 527 function Amr:CanEquip(bagId, slotId)
yellowfive@124 528 local item = Item:CreateFromBagAndSlot(bagId, slotId)
yellowfive@124 529 if item then
yellowfive@124 530 local loc = item:GetItemLocation()
yellowfive@124 531 return C_Item.IsBound(loc)
yellowfive@124 532 else
yellowfive@124 533 -- for now just return true if we can't find the item... will get an error trying to equip if it isn't bound
yellowfive@124 534 return true
yellowfive@124 535 end
yellowfive@124 536
yellowfive@124 537 --local tt = Amr.GetItemTooltip(bagId, slotId)
yellowfive@124 538 --if self:IsTextInTooltip(tt, ITEM_SOULBOUND) then return true end
yellowfive@124 539 --if self:IsTextInTooltip(tt, ITEM_BNETACCOUNTBOUND) then return true end
yellowfive@124 540 --if self:IsTextInTooltip(tt, ITEM_ACCOUNTBOUND) then return true end
yellowfive@57 541 end
yellowfive@57 542
yellowfive@57 543 -- helper to determine if an item has a unique constraint
yellowfive@57 544 function Amr:IsUnique(bagId, slotId)
yellowfive@81 545 local tt = Amr.GetItemTooltip(bagId, slotId)
yellowfive@57 546 if self:IsTextInTooltip(tt, ITEM_UNIQUE_EQUIPPABLE) then return true end
yellowfive@57 547 if self:IsTextInTooltip(tt, ITEM_UNIQUE) then return true end
yellowfive@57 548 return false
yellowfive@57 549 end
yellowfive@57 550
yellowfive@57 551
yellowfive@57 552 ----------------------------------------------------------------------------------------
yellowfive@57 553 -- Inter-Addon Communication
yellowfive@57 554 ----------------------------------------------------------------------------------------
yellowfive@57 555 function Amr:SendAmrCommMessage(message, channel)
yellowfive@57 556 -- prepend version to all messages
yellowfive@57 557 local v = GetAddOnMetadata(Amr.ADDON_NAME, "Version")
yellowfive@57 558 message = v .. "\r" .. message
yellowfive@57 559
yellowfive@79 560 Amr:SendCommMessage(Amr.ChatPrefix, message, channel or (IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or "RAID"))
yellowfive@57 561 end
yellowfive@57 562
yellowfive@57 563 function Amr:OnCommReceived(prefix, message, distribution, sender)
yellowfive@57 564
yellowfive@57 565 local parts = {}
yellowfive@57 566 for part in string.gmatch(message, "([^\r]+)") do
yellowfive@57 567 table.insert(parts, part)
yellowfive@57 568 end
yellowfive@57 569
yellowfive@57 570 local ver = parts[1]
yellowfive@57 571 if ver then ver = tonumber(ver) end
yellowfive@57 572 if ver then
yellowfive@57 573 -- newest versions of the addon start all messages with a version number
yellowfive@57 574 message = parts[2]
yellowfive@57 575 end
yellowfive@57 576
yellowfive@57 577 -- we always allow version checks, even from old versions of the addon that aren't otherwise compatible
yellowfive@57 578 if Amr.StartsWith(message, Amr.MessageTypes.Version) then
yellowfive@57 579 -- version checking between group members
yellowfive@57 580 if Amr.StartsWith(message, Amr.MessageTypes.VersionRequest) then
yellowfive@57 581 sendVersionInfo()
yellowfive@57 582 else
yellowfive@57 583 onVersionInfoReceived(message)
yellowfive@57 584 end
yellowfive@57 585
yellowfive@57 586 return
yellowfive@57 587 end
yellowfive@57 588
yellowfive@57 589 -- any other kind of message is ignored if the version is too old
yellowfive@57 590 if not ver or ver < Amr.MIN_ADDON_VERSION then return end
yellowfive@57 591
yellowfive@124 592 --[[
yellowfive@57 593 if Amr.StartsWith(message, Amr.MessageTypes.Team) then
yellowfive@57 594 -- if fully initialized, process team optimizer messages
yellowfive@57 595 if Amr["ProcessTeamMessage"] then
yellowfive@57 596 Amr:ProcessTeamMessage(message)
yellowfive@57 597 end
yellowfive@57 598 else
yellowfive@57 599 -- if we are fully loaded, process a player snapshot when it is received (combat logging)
yellowfive@57 600 if Amr["ProcessPlayerSnapshot"] then
yellowfive@57 601 self:ProcessPlayerSnapshot(message)
yellowfive@57 602 end
yellowfive@57 603 end
yellowfive@124 604 ]]
yellowfive@57 605 end
yellowfive@57 606
yellowfive@57 607
yellowfive@57 608 ----------------------------------------------------------------------------------------
yellowfive@57 609 -- Events
yellowfive@57 610 ----------------------------------------------------------------------------------------
yellowfive@57 611 local _eventHandlers = {}
yellowfive@57 612
yellowfive@57 613 local function handleEvent(eventName, ...)
yellowfive@57 614 local list = _eventHandlers[eventName]
yellowfive@57 615 if list then
yellowfive@57 616 --print(eventName .. " handled")
yellowfive@57 617 for i, handler in ipairs(list) do
yellowfive@57 618 if type(handler) == "function" then
yellowfive@57 619 handler(select(1, ...))
yellowfive@57 620 else
yellowfive@57 621 Amr[handler](Amr, select(1, ...))
yellowfive@57 622 end
yellowfive@57 623 end
yellowfive@57 624 end
yellowfive@57 625 end
yellowfive@57 626
yellowfive@57 627 -- WoW and Ace seem to work on a "one handler" kind of approach to events (as far as I can tell from the sparse documentation of both).
yellowfive@57 628 -- This is a simple wrapper to allow adding multiple handlers to the same event, thus allowing better encapsulation of code from file to file.
yellowfive@57 629 function Amr:AddEventHandler(eventName, methodOrName)
yellowfive@57 630 local list = _eventHandlers[eventName]
yellowfive@57 631 if not list then
yellowfive@57 632 list = {}
yellowfive@57 633 _eventHandlers[eventName] = list
yellowfive@57 634 Amr:RegisterEvent(eventName, handleEvent)
yellowfive@57 635 end
yellowfive@57 636 table.insert(list, methodOrName)
yellowfive@57 637 end
yellowfive@57 638
yellowfive@57 639 Amr:AddEventHandler("PLAYER_ENTERING_WORLD", onPlayerEnteringWorld)
yellowfive@57 640
yellowfive@57 641
yellowfive@57 642 ----------------------------------------------------------------------------------------
yellowfive@57 643 -- Debugging
yellowfive@57 644 ----------------------------------------------------------------------------------------
yellowfive@124 645 function Amr:dump(o)
yellowfive@124 646 if type(o) == 'table' then
yellowfive@124 647 local s = '{ '
yellowfive@124 648 for k,v in pairs(o) do
yellowfive@124 649 if type(k) ~= 'number' then k = '"'..k..'"' end
yellowfive@124 650 s = s .. '['..k..'] = ' .. Amr:dump(v) .. ','
yellowfive@124 651 end
yellowfive@124 652 return s .. '} '
yellowfive@124 653 else
yellowfive@124 654 return tostring(o)
yellowfive@124 655 end
yellowfive@124 656 end
yellowfive@124 657
yellowfive@69 658 function Amr:Test()
yellowfive@69 659
yellowfive@124 660 --local itemLink = GetInventoryItemLink("player", 17)
yellowfive@124 661
yellowfive@124 662 --print(itemLink)
yellowfive@124 663
yellowfive@124 664 --local blah = Amr.ParseItemLink(itemLink)
yellowfive@124 665
yellowfive@124 666 --print(dump(blah.relicBonusIds))
yellowfive@124 667
yellowfive@124 668 --[[
yellowfive@124 669 --print(NUM_BANKBAGSLOTS)
yellowfive@124 670
yellowfive@124 671 local bagId = NUM_BAG_SLOTS + 1
yellowfive@124 672
yellowfive@124 673 local item = Item:CreateFromBagAndSlot(bagId, 2)
yellowfive@124 674 if item then
yellowfive@124 675 print(item:GetItemName())
yellowfive@124 676 else
yellowfive@124 677 print("no item")
yellowfive@124 678 end
yellowfive@124 679
yellowfive@124 680 local numSlots = GetContainerNumSlots(bagId)
yellowfive@124 681 print(numSlots .. " bag slots")
yellowfive@124 682 ]]
yellowfive@124 683
yellowfive@124 684 -- EquipItemByName
yellowfive@124 685
yellowfive@124 686 --[[
yellowfive@124 687 for slotId = 1, numSlots do
yellowfive@124 688 local _, itemCount, _, _, _, _, itemLink = GetContainerItemInfo(bagId, slotId)
yellowfive@124 689 if itemLink ~= nil then
yellowfive@124 690 print(slotId .. " " .. itemLink)
yellowfive@124 691 end
yellowfive@124 692 end
yellowfive@124 693 ]]
yellowfive@124 694
yellowfive@124 695 --[[
yellowfive@69 696 local s = "|cff0070dd|Hitem:127224:5337:0:0:0:0:0:0:100:105:512:22:2:615:656:100|h[Staff of Polarities]|h|r"
yellowfive@69 697 Amr.GetItemInfo(s, function(obj, name, link, quality, iLevel)
yellowfive@69 698 print(iLevel)
yellowfive@69 699 end)
yellowfive@124 700 ]]
yellowfive@69 701 end