/BattlePetsCollector.lua
Lua | 697 lines | 388 code | 164 blank | 145 comment | 63 complexity | b8c5528cedea53e67eee18c973f3a4ef MD5 | raw file
- -- Define version
- local addonVersion = "5.0.5.7"
-
- -------------------------------------------------------------------------------
- -- Localized Lua globals.
- -------------------------------------------------------------------------------
- local _G = getfenv(0)
-
- local ITEM_QUALITY_COLORS = ITEM_QUALITY_COLORS
- local HIGHLIGHT_FONT_COLOR = HIGHLIGHT_FONT_COLOR
-
- -- Functions
- local pairs = _G.pairs
- local type = _G.type
-
- -- Libraries
- local string = _G.string
- local table = _G.table
-
- -- LibPetJournal and locale
- local LibPetJournal = LibStub( "LibPetJournal-2.0" )
- local L = LibStub( "AceLocale-3.0" ):GetLocale( "BattlePetsCollector" )
-
- -- Addon Frame
- local BattlePetsCollector = CreateFrame( "Frame" )
-
- -- Make Event functions available via : interface
- BattlePetsCollector:SetScript( "OnEvent", function( self, event, ... ) return self[event] and self[event]( self, ... ) end )
- BattlePetsCollector:RegisterEvent( "ADDON_LOADED" )
-
- -- Defines the tooltip text, this gets updates every time the zone changes or pets are caught/removed
- local toolTipText = ""
-
- -- Defines the current player zone
- local currentZone = 0
-
- -- This keeps a list of all the pets the player owns
- local PlayerPets = {}
-
- -- This indicates the average level of the players top 3 pets
- local averageLevel = 0
-
- -- Retrieve the pet levels
- BattlePetsCollectorPetLevels = nil
-
- -- Retrieve extra pet locations
- BattlePetsCollectorExtraPetLocations = nil
-
- BattlePetsCollector.LastMMTooltipText = ""
-
- local g_currentZonePetsUncaught = 0
- local g_currentZonePetsCaught = 0
-
-
- -- Print a short message once the addon is loaded
- function BattlePetsCollector:ADDON_LOADED( addon )
- if addon ~= "BattlePetsCollector" then
- return
- end
-
- print ( format( '%s', L["AddonLoadedMsg"] ) )
-
- self:UnregisterEvent( "ADDON_LOADED" )
- self.ADDON_LOADED = nil
-
- -- If the player is already logged in (i.e. reloadui), run the PLAYER_LOGIN function, else register an event for it
- if IsLoggedIn() then
- self:PLAYER_LOGIN()
- else
- self:RegisterEvent( "PLAYER_LOGIN" )
- end
- end
-
-
- function BattlePetsCollector:CreatureIdFromName( lookupName )
- for i = 1, 1500 do
- local _,_,_,_,_,_,_,name,_,_,creatureID,_,_,isWildPet,canBattle,_,_ = C_PetJournal.GetPetInfoByIndex( i, false )
-
- if( isWildPet and canBattle and lookupName == name ) then
- return creatureID
- end
- end
-
- return false
- end
-
-
- function BattlePetsCollector:PLAYER_LOGIN()
- -- Unregister the PLAYER_LOGIN event
- self:UnregisterEvent( "PLAYER_LOGIN" )
- self.PLAYER_LOGIN = nil
-
- -- Register some events for zone changes so we can update the LDB text
- self:RegisterEvent( "ZONE_CHANGED_NEW_AREA" )
- self:RegisterEvent( "PLAYER_ENTERING_WORLD" )
-
- -- If the pet list gets updated, we also need to update our local table
- LibPetJournal.RegisterCallback( BattlePetsCollector, "PetListUpdated", "UpdatePetTable" )
-
- -- @todo: I still don't really know how to do this with object access
- GameTooltip:HookScript( "OnHide", function( self ) BattlePetsCollector:ResetMMTooltipText() end )
- GameTooltip:HookScript( "OnUpdate", function( self ) BattlePetsCollector:ShowMMTooltip() end )
- end
-
-
- function BattlePetsCollector:ShowMMTooltip()
- if( not MouseIsOver( MinimapCluster ) ) then
- return
- end
-
- local text = _G["GameTooltipTextLeft1"]:GetText()
-
- if( text == BattlePetsCollector.LastMMTooltipText ) then
- return
- end
-
- if( text == nil ) then
- return
- end
-
- BattlePetsCollector.LastMMTooltipText = string.trim( text )
-
- local trackingTable = {text}
- if( string.find( text, "\n" ) ) then
- trackingTable = {strsplit( "\n", text )}
- end
-
- local addToolTipText = ""
-
- for i = 1, #trackingTable do
-
- -- If Text contains a texture escape sequence (color or up/down icon)
- local escapeSequenceStart = string.find( trackingTable[i], "|t" )
- if( escapeSequenceStart ~= nil ) then
- trackingTable[i] = string.sub(trackingTable[i], escapeSequenceStart + 2)
- end
-
- local petName = trackingTable[i]
-
- addToolTipText = addToolTipText .. petName
-
- if ( petName ) then
- local creatureId = BattlePetsCollector:CreatureIdFromName( petName )
-
- addToolTipText = addToolTipText .. " => " .. tostring( creatureId ) .. "\n"
-
- if( creatureId ) then
- addToolTipText = addToolTipText .. BattlePetsCollector:TooltipAddOwnedInfo( creatureId, petName ) .. "\n"
- end
- end
- end
-
- if( addToolTipText ~= "" ) then
- GameTooltip:AddLine( " " )
- BattlePetsCollector:AddTooltipText( addToolTipText )
- GameTooltip:Show()
- end
- end
-
-
- function BattlePetsCollector:ResetMMTooltipText()
- if( MouseIsOver( Minimap ) ) then
- BattlePetsCollector.LastMMTooltipText = ""
- end
- end
-
-
- function BattlePetsCollector:TooltipAddOwnedInfo( creatureId, name )
- local petQualities = ""
- local petName = name
-
- if( petName == nil ) then
- petName = ""
- end
-
- -- if the player owns one of those creatures
- if( PlayerPets[creatureId] ~= nil ) then
-
- -- then looping over the pet IDs
- for k, petID in pairs( PlayerPets[creatureId] ) do
- -- Get all the details about this pet
- local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
- sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
- local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
-
-
- petQualities = petQualities .. BattlePetsCollector:GetColoredText( format( L["PetLevel"], level ), ITEM_QUALITY_COLORS[rarity-1] ) .. ", "
- petName = name
- end
-
- if( string.len( petQualities ) > 2 ) then
- petQualities = strsub( petQualities, 1, strlen( petQualities ) - 2 )
- end
-
-
- petQualities = format( "|cff82c5ff%s:|r\n%s", format( L["PetAlreadyCaptured"], petName ), petQualities )
- else
- petQualities = BattlePetsCollector:GetRedText( format( L["PetNotCapturedYet"], petName ) )
- end
-
- return petQualities
- end
-
-
- function BattlePetsCollector:ZONE_CHANGED_NEW_AREA()
- -- Set the map to the current zone and retrieve the map id
- SetMapToCurrentZone()
- currentZone = GetCurrentMapAreaID()
-
- -- Load pets
- LibPetJournal:LoadPets()
- end
-
-
- function BattlePetsCollector:PLAYER_ENTERING_WORLD()
- -- Set the map to the current zone and retrieve the map id
- SetMapToCurrentZone()
- currentZone = GetCurrentMapAreaID()
-
- LibPetJournal:LoadPets()
- end
-
-
- -- This function creates a local
- function BattlePetsCollector:UpdatePetTable()
- -- Loop over all pets the player owns and put them in our local table
-
- if( not LibPetJournal:IsLoaded() ) then
- return
- end
-
- PlayerPets = {}
-
- for i, petID in LibPetJournal:IteratePetIDs() do
- local _,_,level,_,_,_,_,_,_,creatureID = C_PetJournal.GetPetInfoByPetID( petID )
-
- -- If the table doesn't contain an entry for the creature id, create an empty one
- if( PlayerPets[creatureID] == nil ) then
- PlayerPets[creatureID] = {}
- end
-
- -- Insert the petID in the table for the creature id
- table.insert( PlayerPets[creatureID], petID )
- end
-
- -- Calculate our teams average level
- averageLevel = round( BattlePetsCollector:CalculateAverageLevel() )
-
- -- The pet table got updated so we also need to update the LDB Text and Tooltip
- BattlePetsCollector:UpdateLDB()
- BattlePetsCollector:UpdateTooltip()
- end
-
-
- function BattlePetsCollector:CalculateAverageLevel()
- -- Calculate our teams average level
- local averageLevel = 0
-
- for i = 1,3 do
- local petId = C_PetJournal.GetPetLoadOutInfo( i )
- if( petId ~= nil ) then
- local _,_,level = C_PetJournal.GetPetInfoByPetID( petId )
- if( level ~= nil and level > 0 ) then
- averageLevel = averageLevel + level
- end
- end
- end
-
- -- Devide by 3... team could theoretically be smaller, but that would reduce the chances of catching a rare pet anyway.
- averageLevel = averageLevel / 3
-
- return averageLevel
- end
-
-
- function BattlePetsCollector:UpdateLDB()
- -- "obj" is the local copy of the LDB object
- local obj = BattlePetsCollector.dataObject
-
- -- Prepare the counter variables
- local currentZonePetsCaught = {}
- local currentZonePetsUncaught = 0
- local currentZonePetsQuality = { [1]=0, [2]=0, [3]=0, [4]=0, [5]=0, [6]=0 }
-
- -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
- local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
-
- -- First, figure out which ones we have by looping over all our creatures ...
- for creatureID, petIDs in pairs( PlayerPets ) do
- -- ... and then looping over the pet IDs
- for k, petID in pairs( petIDs ) do
-
- -- Get all the details about this pet
- local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
- sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
- local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
-
- -- if it is a wild pet AND (
- -- ( there is a source text AND the current zone is found in the source text)
- -- OR
- -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
- -- )
- if( isWild and (
- ( sourceText and string.find( sourceText, zoneText ) )
- or
- ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
- ) ) then
-
- -- Increase the caught and rarity counters
- if( currentZonePetsCaught[creatureID] == nil ) then
- currentZonePetsCaught[creatureID] = 0
- end
-
- -- Increase the "caught" and "rarity" counters
- currentZonePetsCaught[creatureID] = currentZonePetsCaught[creatureID] + 1
- currentZonePetsQuality[rarity] = currentZonePetsQuality[rarity] + 1
- end
- end
- end
-
- -- Since currentZonePetsCaught is a dictionary table, getn/# will not work
- local numCurrentZonePetsCaught = 0 for _,_ in pairs( currentZonePetsCaught ) do numCurrentZonePetsCaught = numCurrentZonePetsCaught + 1 end
-
- -- Now we need to figure out which pets are missing. Unfortunately, that means looping over ALL pets that exist
- for i = 1, 1500 do
- local petID, speciesID, isOwned, customName, level, favorite, isRevoked, name, icon, petType,
- creatureID, sourceText, description, isWildPet, canBattle, tradable, unique = C_PetJournal.GetPetInfoByIndex( i, false )
-
- -- if it is a wild pet AND is not owned by the player AND (
- -- ( there is a source text AND the current zone is found in the source text)
- -- OR
- -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
- -- )
- if( isWildPet and not isOwned and (
- ( sourceText and string.find( sourceText, zoneText ) )
- or
- ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
- ) ) then
- currentZonePetsUncaught = currentZonePetsUncaught + 1
- end
- end
-
- obj.text = format( L["LDBText"], numCurrentZonePetsCaught, ITEM_QUALITY_COLORS[0].hex, currentZonePetsQuality[1], ITEM_QUALITY_COLORS[1].hex, currentZonePetsQuality[2], ITEM_QUALITY_COLORS[2].hex, currentZonePetsQuality[3], ITEM_QUALITY_COLORS[3].hex, currentZonePetsQuality[4], currentZonePetsUncaught )
-
- g_currentZonePetsUncaught = currentZonePetsUncaught
- g_currentZonePetsCaught = numCurrentZonePetsCaught
-
- -- Hide the game tooltip because that should probably be updated as well
- GameTooltip:Hide()
- end
-
-
- function BattlePetsCollector:GetOwnedText()
-
- -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
- local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
-
- local ownedText = ""
-
- -- First, figure out which ones we have by looping over all our creatures ...
- for creatureID, petIDs in pairs( PlayerPets ) do
-
- local petQualities = ""
- local petName = nil
-
- -- ... and then looping over the pet IDs
- for k, petID in pairs( petIDs ) do
-
- -- Get all the details about this pet
- local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
- sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
-
- local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
-
- -- if it is a wild pet AND (
- -- ( there is a source text AND the current zone is found in the source text)
- -- OR
- -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
- -- )
- if( isWild and (
- ( sourceText and string.find( sourceText, zoneText ) )
- or
- ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
- ) ) then
-
- petQualities = petQualities .. BattlePetsCollector:GetColoredText( format( L["PetLevel"], level ), ITEM_QUALITY_COLORS[rarity-1] ) .. ", "
- petName = name
- end
- end
-
- -- If we have some pets of this type, add a tooltip line
- if( petName ~= nil ) then
- petQualities = strsub( petQualities, 1, strlen( petQualities ) - 2 )
- ownedText = ownedText .. petName .. "\t" .. petQualities .. "\n"
- end
- end
-
- return ownedText
- end
-
-
- function BattlePetsCollector:UpdateTooltip()
- local toolTip = ""
-
- -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
- local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
-
- -- loop over 1500 pets (500 max pets, 3 of each pet) - probably less but better safe than sorry
- for i = 1, 1500 do
- local petID, speciesID, isOwned, customName, level, favorite, isRevoked, name, icon, petType,
- creatureID, sourceText, description, isWildPet, canBattle, tradable, unique = C_PetJournal.GetPetInfoByIndex( i, false )
-
- -- if it is a wild pet AND (
- -- ( there is a source text AND the current zone is found in the source text)
- -- OR
- -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
- -- )
- if( isWildPet and not isOwned and (
- ( sourceText and string.find( sourceText, zoneText ) )
- or
- ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
- ) ) then
-
- sourceTextOrig = sourceText
-
- local petBattleSearchString = CHAT_PET_BATTLE_COMBAT_LOG_GET
- petBattleSearchString = string.gsub( petBattleSearchString, "%|Hchannel%:PET_BATTLE_COMBAT_LOG%|h%[", "" )
- petBattleSearchString = string.gsub( petBattleSearchString, "%]%|h%:%\32", "" )
-
- sourceText = string.gsub( sourceText, petBattleSearchString .. ".*%|n", "" )
- sourceText = string.gsub( sourceText, petBattleSearchString .. ".*", "" )
- sourceText = string.gsub( sourceText, petBattleSearchString .. ".*%s.*%|n", "" )
-
- sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*%|n", "" )
- sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*", "" )
- sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*%s.*%|n", "" )
-
- -- Remove new lines
- sourceText = string.gsub( sourceText, "%|n", "" )
- sourceText = string.gsub( sourceText, "%|r", "|r|cffffffff" )
-
- -- Trim
- sourceText = string.trim( sourceText )
-
- -- Get the pet level
- petlevel = L["LevelUnknown"]
-
- if( BattlePetsCollectorPetLevels ~= nil and BattlePetsCollectorPetLevels[creatureID] ~= nil and BattlePetsCollectorPetLevels[creatureID][currentZone] ~= nil ) then
- petlevel = BattlePetsCollector:GetEnemyColor( BattlePetsCollectorPetLevels[creatureID][currentZone] )
- end
-
-
- -- Add uncaught pet to tooltip
- toolTip = toolTip .. name .. "\t"
-
- if( sourceText ~= nil and string.len( sourceText ) > 10 ) then
- toolTip = toolTip .. sourceText .. "|r, "
- end
-
- toolTip = toolTip .. petlevel .. "\n"
- end
- end
-
- -- Calculate number of total unique pets
- totalUniquePets = g_currentZonePetsUncaught + g_currentZonePetsCaught
-
- toolTipText = format( L["LDBTooltipTextCurrentZone"], BattlePetsCollector:GetHighlightText( GetZoneText() ) )
- toolTipText = toolTipText .. format( L["LDBTooltipTextPetsInThisZone"], BattlePetsCollector:GetHighlightText( format( L["LDBTooltipTextUnique"], totalUniquePets ) ) )
-
- if( g_currentZonePetsCaught > 0 ) then
- toolTipText = toolTipText .. "\n" .. BattlePetsCollector:GetHighlightText( L["LDBTooltipTextAlreadyCaught"] ) .. "\n"
- toolTipText = toolTipText .. BattlePetsCollector:GetOwnedText()
- end
-
- if( g_currentZonePetsUncaught > 0 ) then
- toolTipText = toolTipText .. "\n" .. BattlePetsCollector:GetHighlightText( L["LDBTooltipTextNotYetCaught"] ) .. "\n"
- toolTipText = toolTipText .. toolTip
- end
- end
-
-
- BattlePetsCollector.dataObject = LibStub("LibDataBroker-1.1"):NewDataObject( "BattlePetsCollector", {
- type = "data source",
- label = L["LDBLabel"],
- icon = "Interface\\AddOns\\BattlePetsCollector\\BattlePetsCollector",
- name = "Battle Pets Collector",
- text = L["LDBUpdating"],
- version = addonVersion,
- iconWidth = 16,
- controlVariables = {
- ShowIcon = true,
- ShowLabelText = true,
- ShowRegularText = false,
- ShowColoredText = false,
- DisplayOnRightSide = false
- },
-
-
- -- When hovering over the button, show the LDB tooltip
- OnEnter = function( self )
- LibPetJournal:LoadPets()
-
- GameTooltip:SetOwner( self, "ANCHOR_NONE" )
- GameTooltip:ClearAllPoints()
-
- local cx, cy = self:GetCenter()
- if cy < GetScreenHeight() / 2 then
- GameTooltip:SetPoint( "BOTTOM", self, "TOP", dx, dy )
- else
- GameTooltip:SetPoint( "TOP", self, "BOTTOM", dx, dy )
- end
-
- GameTooltip:SetText( BattlePetsCollector:GetHighlightText( "Battle Pets Collector" ) .. " " .. BattlePetsCollector:GetColoredText( addonVersion, { r=0.5, g=0.3, b=0.8 } ) )
-
- BattlePetsCollector:AddTooltipText( toolTipText )
-
- GameTooltip:Show()
- end,
-
- -- Hide the tooltip when moving away from the button
- OnLeave = function( dataObject )
- GameTooltip:FadeOut()
- end,
-
- -- On click, open the pet journal
- OnClick = function( dataObject )
- GameTooltip:FadeOut()
-
- -- Show the pet journal
- TogglePetJournal( 2 )
- end,
- })
-
-
- --[[ API
- NAME: BattlePetsCollector:GetRedText
- DESC: Make the given text red.
- VAR: text - text to color
- OUT: string - Red string with proper start and end font encoding
- --]]
- function BattlePetsCollector:GetRedText( text )
- if( text ) then
- return _G["RED_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
- end
- end
-
-
- --[[ API
- NAME: BattlePetsCollector:GetGreenText
- DESC: Make the given text red.
- VAR: text - text to color
- OUT: string - Red string with proper start and end font encoding
- --]]
- function BattlePetsCollector:GetGreenText( text )
- if( text ) then
- return _G["GREEN_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
- end
- end
-
-
- --[[ API
- NAME: BattlePetsCollector:GetNormalText
- DESC: Make the given text normal (gray-white).
- VAR: text - text to color
- OUT: string - Normal string with proper start and end font encoding
- --]]
- function BattlePetsCollector:GetNormalText(text)
- if( text ) then
- return _G["NORMAL_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
- end
- end
-
-
- --[[ API
- NAME: BattlePetsCollector:GetHighlightText
- DESC: Make the given text highlight (brighter white).
- VAR: text - text to color
- OUT: string - Highlight string with proper start and end font encoding
- --]]
- function BattlePetsCollector:GetHighlightText( text )
- if( text ) then
- return _G["HIGHLIGHT_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
- end
- end
-
-
- --[[ API
- NAME: BattlePetsCollector:GetColoredText
- DESC: Make the given text a custom color.
- VAR: text - text to color
- VAR: color - color is the color table with r, b, g values set.
- OUT: string - Custom color string with proper start and end font encoding
- --]]
- function BattlePetsCollector:GetColoredText( text, color )
- if( text and color ) then
- local redColorCode = format( "%02x", color.r * 255 )
- local greenColorCode = format( "%02x", color.g * 255 )
- local blueColorCode = format( "%02x", color.b * 255 )
- local colorCode = "|cff" .. redColorCode .. greenColorCode .. blueColorCode
- return colorCode .. text .. _G["FONT_COLOR_CODE_CLOSE"]
- end
- end
-
-
- --[[ local
- NAME: BattlePetsCollector:AddTooltipText
- DESC: Helper to add a line of tooltip text to the tooltip.
- VAR: text - string
- OUT: None
- NOTE:
- - Append a "\n" to the end if there is not one already there
- :NOTE
- --]]
- function BattlePetsCollector:AddTooltipText( text )
- if ( text ) then
- -- Append a "\n" to the end
- if ( string.sub(text, -1, -1) ~= "\n" ) then
- text = text.. "\n"
- end
-
- -- See if the string is intended for a double column
- for text1, text2 in string.gmatch( text, "([^\t\n]*)\t?([^\t\n]*)\n" ) do
- if ( text2 ~= "" ) then
- -- Add as double wide
- GameTooltip:AddDoubleLine( text1, text2 )
- elseif ( text1 ~= "" ) then
- -- Add single column line
- GameTooltip:AddLine( text1 )
- else
- -- Assume a blank line
- GameTooltip:AddLine( "\n" )
- end
- end
- end
- end
-
-
- function BattlePetsCollector:GetEnemyColor( enemyLevel )
- local origEnemyLevel = enemyLevel
-
- -- first, get the average enemy level (could be 22 but also 23-25 (=24))
- for level1, level2 in string.gmatch( enemyLevel, "([0-9]+)-?([0-9]*)" ) do
- if( level1 ~= nil and level2 ~= nil and level1 ~= "" and level2 ~= "" and level1 ~= level2 ) then
- enemyLevel = ( tonumber( level1 ) + tonumber( level2 ) ) / 2
- end
- end
-
- -- now, calculate the difference to the users average level
- local petDifference = enemyLevel - averageLevel
-
- -- define the colored pet level
- local color = "YELLOW_FONT_COLOR_CODE"
-
- if( petDifference > 2 ) then
- color = "RED_FONT_COLOR_CODE" -- red
- elseif( petDifference > 1 ) then
- color = "ORANGE_FONT_COLOR_CODE" -- orange
- elseif( petDifference <= 2 and petDifference >= -2 ) then
- color = "YELLOW_FONT_COLOR_CODE" -- yellow
- elseif( petDifference < -3 ) then
- color = "GRAY_FONT_COLOR_CODE" -- grey
- elseif( petDifference < -1 ) then
- color = "GREEN_FONT_COLOR_CODE" -- green
- end
-
- return _G[color] .. format( L["PetLevel"], origEnemyLevel ) .. _G["FONT_COLOR_CODE_CLOSE"]
- end
-
-
-
- --[[ API
- NAME: round
- DESC: Rounds a numeric value
- VAR: num - Value to round
- VAR: idp - Number of decimal points
- OUT: float - Rounded value with specified number of decimal points
- --]]
- function round( num, idp )
- return tonumber( string.format( "%." .. ( idp or 0 ) .. "f", num ) )
- end
-
-
- --[[ API
- NAME: table.contains
- DESC: Extends table to allow searching for a specified element
- VAR: table - The table to search in
- VAR: element - The element to find
- OUT: boolean - Whether the given table has the specified element
- --]]
- function table.contains( table, element )
- for _, value in pairs( table ) do
- if value == element then
- return true
- end
- end
- return false
- end