Back again with further optimization on the function previously discussed.
The old version, shown here:
Lua Code:
local function modHexColorNames(chatMessage, modChatColor)
local coloredMessage = ''
local _, class
local words = {strsplit(' ', chatMessage)}
for i = 1, #words do
local w = words[i]
local s, p = false, false
local lowW, modW = w:lower(), w:gsub('%.', '')
if (lowW ~= 'player' and lowW ~= 'target' and lowW ~= 'focus' and UnitIsPlayer(modW)) then
_, class = UnitClass(modW)
s = true
if strfind(w, '-') then
w = strsub(w, 1, strfind(w, '-') - 1)
end
elseif (w == 'You' or w == 'you') then
p = true
end
w = ((s or p) and '|c'..RAID_CLASS_COLORS[(p and playerClass) or class].colorStr..w..modChatColor) or w --(w = w, unless s or p is true, then color w by RAID_CLASS_COLORS
coloredMessage = coloredMessage == '' and w or coloredMessage..' '..w
end
return coloredMessage
end
would not properly color instances where people typed "," "." "!" attached to a player's name. I rewrote it (probably poorly) to account for possessive form of names (Terenna's), and also instances where there was punctuation in the word (Terenna!).
The current version works flawlessly, under any weird scenario I can throw at it; I'm once again looking for optimization as I feel it has a lot of if then elseif pipeline stalls. The code is heavily commented so you can see (hopefully) my logic throughout writing it.
Lua Code:
local function modHexColorNames(chatMessage, modChatColor) --differs from hexColorNames because it class colors 'You' and 'you' and 'Your' and 'your' and removes realm names
local words, coloredMessage = {strsplit(' ', chatMessage)}, '' --we use the blizzard strsplit function to split our string into words (we separate the words by spaces) and put them in a table called 'words'
for i = 1, #words do --we parse through the table of words one at a time
local w, s, p, q, modW, lowModW, _, class = words[i], false, false, false --we set w to our 'words' table value we are parsing through, s, p, and q will be used to determine our conditionals, modW is our word with "'s" for possessives or other punctuation removed, lowModW, is just modW:lower()
local lowW = w:lower()
if strfind(w, '%p') then --determine if our word even has punctuation
modW = w:gsub("%'s", '') --remove "'s" from potential player names in possessive form
modW = modW:gsub('%p', '') --remove any other punctuation from our modW
lowModW = modW:lower() --lower our modW to make it easier to see if it's one of the 'unit' names below
q = true
end
if (q and (lowModW ~= 'player' and lowModW ~= 'target' and lowModW ~= 'focus' and UnitIsPlayer(modW))) or (lowW ~= 'player' and lowW ~= 'target' and lowW ~= 'focus' and UnitIsPlayer(lowW)) then --our word had a punctuation in it, see if the word without punctuation is a player
if q then
_, class = UnitClass(modW) --use our punctuation-less word to determine class
else
_, class = UnitClass(lowW) --just use our lowered word to determine class
end
s = true --let's us know we're dealing with a word that should be colored
if strfind(w, '-') then --removes the hyphen and realmname from words
w = strsub(w, 1, strfind(w, '-') - 1)
end
elseif (q and (lowModW == 'you' or lowModW == 'your')) or lowW == 'you' or lowW == 'your' then --our lowered word may be you or your, but also may contain a punctuation, see if it does
p = true --let's us know we're dealing with you or your that should be player class colored
end
if (q and s) then --we have a word with punctuation that should be class colored
w = w:gsub(modW, '|c'..RAID_CLASS_COLORS[class].colorStr..modW..modChatColor)
elseif s then --we have a word without punctuation that should be class colored
w = '|c'..RAID_CLASS_COLORS[class].colorStr..w..modChatColor
elseif (q and p) then --we have you or your with punctuation that should be player class colored
w = w:gsub(modW, '|c'..playerClassColorTable.colorStr..modW..modChatColor)
elseif p then --we have you or your without punctuation that should be player class colored
w = '|c'..playerClassColorTable.colorStr..w..modChatColor
end
coloredMessage = coloredMessage == '' and w or coloredMessage..' '..w
end
return coloredMessage
end
Any insight, as always, is greatly appreciated.