PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/BattlePetsCollector.lua

https://bitbucket.org/alexandergraf/battle-pets-collector
Lua | 697 lines | 388 code | 164 blank | 145 comment | 63 complexity | b8c5528cedea53e67eee18c973f3a4ef MD5 | raw file
  1. -- Define version
  2. local addonVersion = "5.0.5.7"
  3. -------------------------------------------------------------------------------
  4. -- Localized Lua globals.
  5. -------------------------------------------------------------------------------
  6. local _G = getfenv(0)
  7. local ITEM_QUALITY_COLORS = ITEM_QUALITY_COLORS
  8. local HIGHLIGHT_FONT_COLOR = HIGHLIGHT_FONT_COLOR
  9. -- Functions
  10. local pairs = _G.pairs
  11. local type = _G.type
  12. -- Libraries
  13. local string = _G.string
  14. local table = _G.table
  15. -- LibPetJournal and locale
  16. local LibPetJournal = LibStub( "LibPetJournal-2.0" )
  17. local L = LibStub( "AceLocale-3.0" ):GetLocale( "BattlePetsCollector" )
  18. -- Addon Frame
  19. local BattlePetsCollector = CreateFrame( "Frame" )
  20. -- Make Event functions available via : interface
  21. BattlePetsCollector:SetScript( "OnEvent", function( self, event, ... ) return self[event] and self[event]( self, ... ) end )
  22. BattlePetsCollector:RegisterEvent( "ADDON_LOADED" )
  23. -- Defines the tooltip text, this gets updates every time the zone changes or pets are caught/removed
  24. local toolTipText = ""
  25. -- Defines the current player zone
  26. local currentZone = 0
  27. -- This keeps a list of all the pets the player owns
  28. local PlayerPets = {}
  29. -- This indicates the average level of the players top 3 pets
  30. local averageLevel = 0
  31. -- Retrieve the pet levels
  32. BattlePetsCollectorPetLevels = nil
  33. -- Retrieve extra pet locations
  34. BattlePetsCollectorExtraPetLocations = nil
  35. BattlePetsCollector.LastMMTooltipText = ""
  36. local g_currentZonePetsUncaught = 0
  37. local g_currentZonePetsCaught = 0
  38. -- Print a short message once the addon is loaded
  39. function BattlePetsCollector:ADDON_LOADED( addon )
  40. if addon ~= "BattlePetsCollector" then
  41. return
  42. end
  43. print ( format( '%s', L["AddonLoadedMsg"] ) )
  44. self:UnregisterEvent( "ADDON_LOADED" )
  45. self.ADDON_LOADED = nil
  46. -- If the player is already logged in (i.e. reloadui), run the PLAYER_LOGIN function, else register an event for it
  47. if IsLoggedIn() then
  48. self:PLAYER_LOGIN()
  49. else
  50. self:RegisterEvent( "PLAYER_LOGIN" )
  51. end
  52. end
  53. function BattlePetsCollector:CreatureIdFromName( lookupName )
  54. for i = 1, 1500 do
  55. local _,_,_,_,_,_,_,name,_,_,creatureID,_,_,isWildPet,canBattle,_,_ = C_PetJournal.GetPetInfoByIndex( i, false )
  56. if( isWildPet and canBattle and lookupName == name ) then
  57. return creatureID
  58. end
  59. end
  60. return false
  61. end
  62. function BattlePetsCollector:PLAYER_LOGIN()
  63. -- Unregister the PLAYER_LOGIN event
  64. self:UnregisterEvent( "PLAYER_LOGIN" )
  65. self.PLAYER_LOGIN = nil
  66. -- Register some events for zone changes so we can update the LDB text
  67. self:RegisterEvent( "ZONE_CHANGED_NEW_AREA" )
  68. self:RegisterEvent( "PLAYER_ENTERING_WORLD" )
  69. -- If the pet list gets updated, we also need to update our local table
  70. LibPetJournal.RegisterCallback( BattlePetsCollector, "PetListUpdated", "UpdatePetTable" )
  71. -- @todo: I still don't really know how to do this with object access
  72. GameTooltip:HookScript( "OnHide", function( self ) BattlePetsCollector:ResetMMTooltipText() end )
  73. GameTooltip:HookScript( "OnUpdate", function( self ) BattlePetsCollector:ShowMMTooltip() end )
  74. end
  75. function BattlePetsCollector:ShowMMTooltip()
  76. if( not MouseIsOver( MinimapCluster ) ) then
  77. return
  78. end
  79. local text = _G["GameTooltipTextLeft1"]:GetText()
  80. if( text == BattlePetsCollector.LastMMTooltipText ) then
  81. return
  82. end
  83. if( text == nil ) then
  84. return
  85. end
  86. BattlePetsCollector.LastMMTooltipText = string.trim( text )
  87. local trackingTable = {text}
  88. if( string.find( text, "\n" ) ) then
  89. trackingTable = {strsplit( "\n", text )}
  90. end
  91. local addToolTipText = ""
  92. for i = 1, #trackingTable do
  93. -- If Text contains a texture escape sequence (color or up/down icon)
  94. local escapeSequenceStart = string.find( trackingTable[i], "|t" )
  95. if( escapeSequenceStart ~= nil ) then
  96. trackingTable[i] = string.sub(trackingTable[i], escapeSequenceStart + 2)
  97. end
  98. local petName = trackingTable[i]
  99. addToolTipText = addToolTipText .. petName
  100. if ( petName ) then
  101. local creatureId = BattlePetsCollector:CreatureIdFromName( petName )
  102. addToolTipText = addToolTipText .. " => " .. tostring( creatureId ) .. "\n"
  103. if( creatureId ) then
  104. addToolTipText = addToolTipText .. BattlePetsCollector:TooltipAddOwnedInfo( creatureId, petName ) .. "\n"
  105. end
  106. end
  107. end
  108. if( addToolTipText ~= "" ) then
  109. GameTooltip:AddLine( " " )
  110. BattlePetsCollector:AddTooltipText( addToolTipText )
  111. GameTooltip:Show()
  112. end
  113. end
  114. function BattlePetsCollector:ResetMMTooltipText()
  115. if( MouseIsOver( Minimap ) ) then
  116. BattlePetsCollector.LastMMTooltipText = ""
  117. end
  118. end
  119. function BattlePetsCollector:TooltipAddOwnedInfo( creatureId, name )
  120. local petQualities = ""
  121. local petName = name
  122. if( petName == nil ) then
  123. petName = ""
  124. end
  125. -- if the player owns one of those creatures
  126. if( PlayerPets[creatureId] ~= nil ) then
  127. -- then looping over the pet IDs
  128. for k, petID in pairs( PlayerPets[creatureId] ) do
  129. -- Get all the details about this pet
  130. local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
  131. sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
  132. local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
  133. petQualities = petQualities .. BattlePetsCollector:GetColoredText( format( L["PetLevel"], level ), ITEM_QUALITY_COLORS[rarity-1] ) .. ", "
  134. petName = name
  135. end
  136. if( string.len( petQualities ) > 2 ) then
  137. petQualities = strsub( petQualities, 1, strlen( petQualities ) - 2 )
  138. end
  139. petQualities = format( "|cff82c5ff%s:|r\n%s", format( L["PetAlreadyCaptured"], petName ), petQualities )
  140. else
  141. petQualities = BattlePetsCollector:GetRedText( format( L["PetNotCapturedYet"], petName ) )
  142. end
  143. return petQualities
  144. end
  145. function BattlePetsCollector:ZONE_CHANGED_NEW_AREA()
  146. -- Set the map to the current zone and retrieve the map id
  147. SetMapToCurrentZone()
  148. currentZone = GetCurrentMapAreaID()
  149. -- Load pets
  150. LibPetJournal:LoadPets()
  151. end
  152. function BattlePetsCollector:PLAYER_ENTERING_WORLD()
  153. -- Set the map to the current zone and retrieve the map id
  154. SetMapToCurrentZone()
  155. currentZone = GetCurrentMapAreaID()
  156. LibPetJournal:LoadPets()
  157. end
  158. -- This function creates a local
  159. function BattlePetsCollector:UpdatePetTable()
  160. -- Loop over all pets the player owns and put them in our local table
  161. if( not LibPetJournal:IsLoaded() ) then
  162. return
  163. end
  164. PlayerPets = {}
  165. for i, petID in LibPetJournal:IteratePetIDs() do
  166. local _,_,level,_,_,_,_,_,_,creatureID = C_PetJournal.GetPetInfoByPetID( petID )
  167. -- If the table doesn't contain an entry for the creature id, create an empty one
  168. if( PlayerPets[creatureID] == nil ) then
  169. PlayerPets[creatureID] = {}
  170. end
  171. -- Insert the petID in the table for the creature id
  172. table.insert( PlayerPets[creatureID], petID )
  173. end
  174. -- Calculate our teams average level
  175. averageLevel = round( BattlePetsCollector:CalculateAverageLevel() )
  176. -- The pet table got updated so we also need to update the LDB Text and Tooltip
  177. BattlePetsCollector:UpdateLDB()
  178. BattlePetsCollector:UpdateTooltip()
  179. end
  180. function BattlePetsCollector:CalculateAverageLevel()
  181. -- Calculate our teams average level
  182. local averageLevel = 0
  183. for i = 1,3 do
  184. local petId = C_PetJournal.GetPetLoadOutInfo( i )
  185. if( petId ~= nil ) then
  186. local _,_,level = C_PetJournal.GetPetInfoByPetID( petId )
  187. if( level ~= nil and level > 0 ) then
  188. averageLevel = averageLevel + level
  189. end
  190. end
  191. end
  192. -- Devide by 3... team could theoretically be smaller, but that would reduce the chances of catching a rare pet anyway.
  193. averageLevel = averageLevel / 3
  194. return averageLevel
  195. end
  196. function BattlePetsCollector:UpdateLDB()
  197. -- "obj" is the local copy of the LDB object
  198. local obj = BattlePetsCollector.dataObject
  199. -- Prepare the counter variables
  200. local currentZonePetsCaught = {}
  201. local currentZonePetsUncaught = 0
  202. local currentZonePetsQuality = { [1]=0, [2]=0, [3]=0, [4]=0, [5]=0, [6]=0 }
  203. -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
  204. local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
  205. -- First, figure out which ones we have by looping over all our creatures ...
  206. for creatureID, petIDs in pairs( PlayerPets ) do
  207. -- ... and then looping over the pet IDs
  208. for k, petID in pairs( petIDs ) do
  209. -- Get all the details about this pet
  210. local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
  211. sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
  212. local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
  213. -- if it is a wild pet AND (
  214. -- ( there is a source text AND the current zone is found in the source text)
  215. -- OR
  216. -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
  217. -- )
  218. if( isWild and (
  219. ( sourceText and string.find( sourceText, zoneText ) )
  220. or
  221. ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
  222. ) ) then
  223. -- Increase the caught and rarity counters
  224. if( currentZonePetsCaught[creatureID] == nil ) then
  225. currentZonePetsCaught[creatureID] = 0
  226. end
  227. -- Increase the "caught" and "rarity" counters
  228. currentZonePetsCaught[creatureID] = currentZonePetsCaught[creatureID] + 1
  229. currentZonePetsQuality[rarity] = currentZonePetsQuality[rarity] + 1
  230. end
  231. end
  232. end
  233. -- Since currentZonePetsCaught is a dictionary table, getn/# will not work
  234. local numCurrentZonePetsCaught = 0 for _,_ in pairs( currentZonePetsCaught ) do numCurrentZonePetsCaught = numCurrentZonePetsCaught + 1 end
  235. -- Now we need to figure out which pets are missing. Unfortunately, that means looping over ALL pets that exist
  236. for i = 1, 1500 do
  237. local petID, speciesID, isOwned, customName, level, favorite, isRevoked, name, icon, petType,
  238. creatureID, sourceText, description, isWildPet, canBattle, tradable, unique = C_PetJournal.GetPetInfoByIndex( i, false )
  239. -- if it is a wild pet AND is not owned by the player AND (
  240. -- ( there is a source text AND the current zone is found in the source text)
  241. -- OR
  242. -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
  243. -- )
  244. if( isWildPet and not isOwned and (
  245. ( sourceText and string.find( sourceText, zoneText ) )
  246. or
  247. ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
  248. ) ) then
  249. currentZonePetsUncaught = currentZonePetsUncaught + 1
  250. end
  251. end
  252. 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 )
  253. g_currentZonePetsUncaught = currentZonePetsUncaught
  254. g_currentZonePetsCaught = numCurrentZonePetsCaught
  255. -- Hide the game tooltip because that should probably be updated as well
  256. GameTooltip:Hide()
  257. end
  258. function BattlePetsCollector:GetOwnedText()
  259. -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
  260. local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
  261. local ownedText = ""
  262. -- First, figure out which ones we have by looping over all our creatures ...
  263. for creatureID, petIDs in pairs( PlayerPets ) do
  264. local petQualities = ""
  265. local petName = nil
  266. -- ... and then looping over the pet IDs
  267. for k, petID in pairs( petIDs ) do
  268. -- Get all the details about this pet
  269. local speciesID, customName, level, xp, maxXp, displayID, name, icon, petType, creatureID,
  270. sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID( petID )
  271. local health, maxHealth, attack, speed, rarity = C_PetJournal.GetPetStats( petID )
  272. -- if it is a wild pet AND (
  273. -- ( there is a source text AND the current zone is found in the source text)
  274. -- OR
  275. -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
  276. -- )
  277. if( isWild and (
  278. ( sourceText and string.find( sourceText, zoneText ) )
  279. or
  280. ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
  281. ) ) then
  282. petQualities = petQualities .. BattlePetsCollector:GetColoredText( format( L["PetLevel"], level ), ITEM_QUALITY_COLORS[rarity-1] ) .. ", "
  283. petName = name
  284. end
  285. end
  286. -- If we have some pets of this type, add a tooltip line
  287. if( petName ~= nil ) then
  288. petQualities = strsub( petQualities, 1, strlen( petQualities ) - 2 )
  289. ownedText = ownedText .. petName .. "\t" .. petQualities .. "\n"
  290. end
  291. end
  292. return ownedText
  293. end
  294. function BattlePetsCollector:UpdateTooltip()
  295. local toolTip = ""
  296. -- Prepare the zone text... since a hyphen is a special sign (regex), escape it
  297. local zoneText = string.gsub( GetZoneText(), "%-", "%%-" )
  298. -- loop over 1500 pets (500 max pets, 3 of each pet) - probably less but better safe than sorry
  299. for i = 1, 1500 do
  300. local petID, speciesID, isOwned, customName, level, favorite, isRevoked, name, icon, petType,
  301. creatureID, sourceText, description, isWildPet, canBattle, tradable, unique = C_PetJournal.GetPetInfoByIndex( i, false )
  302. -- if it is a wild pet AND (
  303. -- ( there is a source text AND the current zone is found in the source text)
  304. -- OR
  305. -- ( there are some extra locations known AND the current zone has extra pets AND the current pet belongs to the current zone)
  306. -- )
  307. if( isWildPet and not isOwned and (
  308. ( sourceText and string.find( sourceText, zoneText ) )
  309. or
  310. ( BattlePetsCollectorExtraPetLocations ~= nil and BattlePetsCollectorExtraPetLocations[currentZone] ~= nil and table.contains( BattlePetsCollectorExtraPetLocations[currentZone], creatureID ) )
  311. ) ) then
  312. sourceTextOrig = sourceText
  313. local petBattleSearchString = CHAT_PET_BATTLE_COMBAT_LOG_GET
  314. petBattleSearchString = string.gsub( petBattleSearchString, "%|Hchannel%:PET_BATTLE_COMBAT_LOG%|h%[", "" )
  315. petBattleSearchString = string.gsub( petBattleSearchString, "%]%|h%:%\32", "" )
  316. sourceText = string.gsub( sourceText, petBattleSearchString .. ".*%|n", "" )
  317. sourceText = string.gsub( sourceText, petBattleSearchString .. ".*", "" )
  318. sourceText = string.gsub( sourceText, petBattleSearchString .. ".*%s.*%|n", "" )
  319. sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*%|n", "" )
  320. sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*", "" )
  321. sourceText = string.gsub( sourceText, BATTLE_PET_SOURCE_5 .. ".*%s.*%|n", "" )
  322. -- Remove new lines
  323. sourceText = string.gsub( sourceText, "%|n", "" )
  324. sourceText = string.gsub( sourceText, "%|r", "|r|cffffffff" )
  325. -- Trim
  326. sourceText = string.trim( sourceText )
  327. -- Get the pet level
  328. petlevel = L["LevelUnknown"]
  329. if( BattlePetsCollectorPetLevels ~= nil and BattlePetsCollectorPetLevels[creatureID] ~= nil and BattlePetsCollectorPetLevels[creatureID][currentZone] ~= nil ) then
  330. petlevel = BattlePetsCollector:GetEnemyColor( BattlePetsCollectorPetLevels[creatureID][currentZone] )
  331. end
  332. -- Add uncaught pet to tooltip
  333. toolTip = toolTip .. name .. "\t"
  334. if( sourceText ~= nil and string.len( sourceText ) > 10 ) then
  335. toolTip = toolTip .. sourceText .. "|r, "
  336. end
  337. toolTip = toolTip .. petlevel .. "\n"
  338. end
  339. end
  340. -- Calculate number of total unique pets
  341. totalUniquePets = g_currentZonePetsUncaught + g_currentZonePetsCaught
  342. toolTipText = format( L["LDBTooltipTextCurrentZone"], BattlePetsCollector:GetHighlightText( GetZoneText() ) )
  343. toolTipText = toolTipText .. format( L["LDBTooltipTextPetsInThisZone"], BattlePetsCollector:GetHighlightText( format( L["LDBTooltipTextUnique"], totalUniquePets ) ) )
  344. if( g_currentZonePetsCaught > 0 ) then
  345. toolTipText = toolTipText .. "\n" .. BattlePetsCollector:GetHighlightText( L["LDBTooltipTextAlreadyCaught"] ) .. "\n"
  346. toolTipText = toolTipText .. BattlePetsCollector:GetOwnedText()
  347. end
  348. if( g_currentZonePetsUncaught > 0 ) then
  349. toolTipText = toolTipText .. "\n" .. BattlePetsCollector:GetHighlightText( L["LDBTooltipTextNotYetCaught"] ) .. "\n"
  350. toolTipText = toolTipText .. toolTip
  351. end
  352. end
  353. BattlePetsCollector.dataObject = LibStub("LibDataBroker-1.1"):NewDataObject( "BattlePetsCollector", {
  354. type = "data source",
  355. label = L["LDBLabel"],
  356. icon = "Interface\\AddOns\\BattlePetsCollector\\BattlePetsCollector",
  357. name = "Battle Pets Collector",
  358. text = L["LDBUpdating"],
  359. version = addonVersion,
  360. iconWidth = 16,
  361. controlVariables = {
  362. ShowIcon = true,
  363. ShowLabelText = true,
  364. ShowRegularText = false,
  365. ShowColoredText = false,
  366. DisplayOnRightSide = false
  367. },
  368. -- When hovering over the button, show the LDB tooltip
  369. OnEnter = function( self )
  370. LibPetJournal:LoadPets()
  371. GameTooltip:SetOwner( self, "ANCHOR_NONE" )
  372. GameTooltip:ClearAllPoints()
  373. local cx, cy = self:GetCenter()
  374. if cy < GetScreenHeight() / 2 then
  375. GameTooltip:SetPoint( "BOTTOM", self, "TOP", dx, dy )
  376. else
  377. GameTooltip:SetPoint( "TOP", self, "BOTTOM", dx, dy )
  378. end
  379. GameTooltip:SetText( BattlePetsCollector:GetHighlightText( "Battle Pets Collector" ) .. " " .. BattlePetsCollector:GetColoredText( addonVersion, { r=0.5, g=0.3, b=0.8 } ) )
  380. BattlePetsCollector:AddTooltipText( toolTipText )
  381. GameTooltip:Show()
  382. end,
  383. -- Hide the tooltip when moving away from the button
  384. OnLeave = function( dataObject )
  385. GameTooltip:FadeOut()
  386. end,
  387. -- On click, open the pet journal
  388. OnClick = function( dataObject )
  389. GameTooltip:FadeOut()
  390. -- Show the pet journal
  391. TogglePetJournal( 2 )
  392. end,
  393. })
  394. --[[ API
  395. NAME: BattlePetsCollector:GetRedText
  396. DESC: Make the given text red.
  397. VAR: text - text to color
  398. OUT: string - Red string with proper start and end font encoding
  399. --]]
  400. function BattlePetsCollector:GetRedText( text )
  401. if( text ) then
  402. return _G["RED_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
  403. end
  404. end
  405. --[[ API
  406. NAME: BattlePetsCollector:GetGreenText
  407. DESC: Make the given text red.
  408. VAR: text - text to color
  409. OUT: string - Red string with proper start and end font encoding
  410. --]]
  411. function BattlePetsCollector:GetGreenText( text )
  412. if( text ) then
  413. return _G["GREEN_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
  414. end
  415. end
  416. --[[ API
  417. NAME: BattlePetsCollector:GetNormalText
  418. DESC: Make the given text normal (gray-white).
  419. VAR: text - text to color
  420. OUT: string - Normal string with proper start and end font encoding
  421. --]]
  422. function BattlePetsCollector:GetNormalText(text)
  423. if( text ) then
  424. return _G["NORMAL_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
  425. end
  426. end
  427. --[[ API
  428. NAME: BattlePetsCollector:GetHighlightText
  429. DESC: Make the given text highlight (brighter white).
  430. VAR: text - text to color
  431. OUT: string - Highlight string with proper start and end font encoding
  432. --]]
  433. function BattlePetsCollector:GetHighlightText( text )
  434. if( text ) then
  435. return _G["HIGHLIGHT_FONT_COLOR_CODE"] .. text .. _G["FONT_COLOR_CODE_CLOSE"]
  436. end
  437. end
  438. --[[ API
  439. NAME: BattlePetsCollector:GetColoredText
  440. DESC: Make the given text a custom color.
  441. VAR: text - text to color
  442. VAR: color - color is the color table with r, b, g values set.
  443. OUT: string - Custom color string with proper start and end font encoding
  444. --]]
  445. function BattlePetsCollector:GetColoredText( text, color )
  446. if( text and color ) then
  447. local redColorCode = format( "%02x", color.r * 255 )
  448. local greenColorCode = format( "%02x", color.g * 255 )
  449. local blueColorCode = format( "%02x", color.b * 255 )
  450. local colorCode = "|cff" .. redColorCode .. greenColorCode .. blueColorCode
  451. return colorCode .. text .. _G["FONT_COLOR_CODE_CLOSE"]
  452. end
  453. end
  454. --[[ local
  455. NAME: BattlePetsCollector:AddTooltipText
  456. DESC: Helper to add a line of tooltip text to the tooltip.
  457. VAR: text - string
  458. OUT: None
  459. NOTE:
  460. - Append a "\n" to the end if there is not one already there
  461. :NOTE
  462. --]]
  463. function BattlePetsCollector:AddTooltipText( text )
  464. if ( text ) then
  465. -- Append a "\n" to the end
  466. if ( string.sub(text, -1, -1) ~= "\n" ) then
  467. text = text.. "\n"
  468. end
  469. -- See if the string is intended for a double column
  470. for text1, text2 in string.gmatch( text, "([^\t\n]*)\t?([^\t\n]*)\n" ) do
  471. if ( text2 ~= "" ) then
  472. -- Add as double wide
  473. GameTooltip:AddDoubleLine( text1, text2 )
  474. elseif ( text1 ~= "" ) then
  475. -- Add single column line
  476. GameTooltip:AddLine( text1 )
  477. else
  478. -- Assume a blank line
  479. GameTooltip:AddLine( "\n" )
  480. end
  481. end
  482. end
  483. end
  484. function BattlePetsCollector:GetEnemyColor( enemyLevel )
  485. local origEnemyLevel = enemyLevel
  486. -- first, get the average enemy level (could be 22 but also 23-25 (=24))
  487. for level1, level2 in string.gmatch( enemyLevel, "([0-9]+)-?([0-9]*)" ) do
  488. if( level1 ~= nil and level2 ~= nil and level1 ~= "" and level2 ~= "" and level1 ~= level2 ) then
  489. enemyLevel = ( tonumber( level1 ) + tonumber( level2 ) ) / 2
  490. end
  491. end
  492. -- now, calculate the difference to the users average level
  493. local petDifference = enemyLevel - averageLevel
  494. -- define the colored pet level
  495. local color = "YELLOW_FONT_COLOR_CODE"
  496. if( petDifference > 2 ) then
  497. color = "RED_FONT_COLOR_CODE" -- red
  498. elseif( petDifference > 1 ) then
  499. color = "ORANGE_FONT_COLOR_CODE" -- orange
  500. elseif( petDifference <= 2 and petDifference >= -2 ) then
  501. color = "YELLOW_FONT_COLOR_CODE" -- yellow
  502. elseif( petDifference < -3 ) then
  503. color = "GRAY_FONT_COLOR_CODE" -- grey
  504. elseif( petDifference < -1 ) then
  505. color = "GREEN_FONT_COLOR_CODE" -- green
  506. end
  507. return _G[color] .. format( L["PetLevel"], origEnemyLevel ) .. _G["FONT_COLOR_CODE_CLOSE"]
  508. end
  509. --[[ API
  510. NAME: round
  511. DESC: Rounds a numeric value
  512. VAR: num - Value to round
  513. VAR: idp - Number of decimal points
  514. OUT: float - Rounded value with specified number of decimal points
  515. --]]
  516. function round( num, idp )
  517. return tonumber( string.format( "%." .. ( idp or 0 ) .. "f", num ) )
  518. end
  519. --[[ API
  520. NAME: table.contains
  521. DESC: Extends table to allow searching for a specified element
  522. VAR: table - The table to search in
  523. VAR: element - The element to find
  524. OUT: boolean - Whether the given table has the specified element
  525. --]]
  526. function table.contains( table, element )
  527. for _, value in pairs( table ) do
  528. if value == element then
  529. return true
  530. end
  531. end
  532. return false
  533. end