Yeah, that works.
On second glance, though, you're calling GetWorldStateUIInfo() too many times, and select() should generally be avoided.
Code:
local matchStrings = {
-- BGs in which score format is ####/####
["Arathi Basin"] = "(%d+)/",
["Battle for Gilneas"] = "(%d+)/",
["Eye of the Storm"] = "(%d+)/",
-- BGs in which score format is ##
-- Realistically you don't need these entries, since
-- their value is the default.
["Alterac Valley"] = "(%d+)",
["Isle of Conquest"] = "(%d+)",
["Strand of the Ancients"] = "(%d+)",
["Twin Peaks"] = "(%d+)",
["Warsong Gulch"] = "(%d+)",
}
local hasGlobalInfo = {
-- BGs in which the first info line is global (not faction score)
["Eye of the Storm"] = true,
["Twin Peaks"] = true,
["Warsong Gulch"] = true,
}
function MyAddon:GetBGScore(locationName)
local uiType, state, hidden, text, icon, dynamicIcon, tooltip,
dynamicTooltip, extendedUI, extendedUIState1, extendedUIState2,
extendedUIState3 = GetWorldStateUIInfo()
local zoneName = GetRealZoneText()
local matchString = matchStrings[zoneName] or "(%d+)"
local n1, n2
if hasGlobalInfo[zoneName] then
n1, n2 = 2, 3
else
n1, n2 = 1, 2
end
local _, _, _, text1, icon1 = GetWorldStateUIInfo(n1)
local _, _, _, text2, icon2 = GetWorldStateUIInfo(n2)
local allianceScore = tonumber((text1 or ""):match(matchString)) or 0
local hordeScore = tonumber((text2 or ""):match(matchString)) or 0
if not icon1:lower():match("alliance") then
-- Would be more efficient to check for whatever is in the
-- is in the Horde icon path... probably "horde" ?
-- Anyway, if needed, just swap the values; you don't need
-- extra variables to do this:
allianceScore, hordeScore = hordeScore, allianceScore
end
if zoneName == "Strand of the Ancients" then
-- No score in Strand of the Ancients, report instead end of round timer.
local _, _, _, endTime = GetWorldStateUIInfo(8)
endTime = endTime and endTime:match("%d+.*") or L["<no data>"]
return L["End of round in "] .. endTime .. L[" min"]
end
return L["Score: "] .. allianceScore .. "(A) - " .. hordeScore .. "(H), "
end
Also, rather than concatenating strings for the output, which requires translators to translate random pieces of sentences that may not end up in the correct order for their language, you should take advantage of string substitutions.
So, instead of this:
Code:
L["End of round in "] .. end .. L[" min"]
L["Score: "] .. allianceScore .. "(A) - " .. hordeScore .. "(H), "
Do this:
Code:
format(L["End of round in %d min"], endTime)
format(L["Score: %d (A) - %d (H), ", allianceScore, hordeScore)
This way, translators can place the substition tokens where it is more appropriate for their language. It may not be particularly relevant for these specific examples, but in many cases it will be.