View Single Post
01-06-19, 09:33 PM   #14
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
Back again with further optimization on the function previously discussed.

The old version, shown here:
Lua Code:
  1. local function modHexColorNames(chatMessage, modChatColor)
  2.     local coloredMessage = ''
  3.     local _, class
  4.     local words = {strsplit(' ', chatMessage)}
  5.     for i = 1, #words do
  6.         local w = words[i]
  7.         local s, p = false, false
  8.         local lowW, modW = w:lower(), w:gsub('%.', '')
  9.         if (lowW ~= 'player' and lowW ~= 'target' and lowW ~= 'focus' and UnitIsPlayer(modW)) then
  10.             _, class = UnitClass(modW)
  11.             s = true
  12.             if strfind(w, '-') then
  13.                 w = strsub(w, 1, strfind(w, '-') - 1)
  14.             end
  15.         elseif (w == 'You' or w == 'you') then
  16.             p = true
  17.         end
  18.         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
  19.         coloredMessage = coloredMessage == '' and w or coloredMessage..' '..w
  20.     end
  21.     return coloredMessage
  22. 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:
  1. local function modHexColorNames(chatMessage, modChatColor) --differs from hexColorNames because it class colors 'You' and 'you' and 'Your' and 'your' and removes realm names
  2.     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'
  3.     for i = 1, #words do --we parse through the table of words one at a time
  4.         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()
  5.         local lowW = w:lower()
  6.         if strfind(w, '%p') then --determine if our word even has punctuation
  7.             modW = w:gsub("%'s", '') --remove "'s" from potential player names in possessive form
  8.             modW = modW:gsub('%p', '') --remove any other punctuation from our modW
  9.             lowModW = modW:lower() --lower our modW to make it easier to see if it's one of the 'unit' names below
  10.             q = true
  11.         end
  12.         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
  13.             if q then
  14.                 _, class = UnitClass(modW) --use our punctuation-less word to determine class
  15.             else
  16.                 _, class = UnitClass(lowW) --just use our lowered word to determine class
  17.             end
  18.             s = true --let's us know we're dealing with a word that should be colored
  19.             if strfind(w, '-') then --removes the hyphen and realmname from words
  20.                 w = strsub(w, 1, strfind(w, '-') - 1)
  21.             end
  22.         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
  23.             p = true --let's us know we're dealing with you or your that should be player class colored
  24.         end
  25.         if (q and s) then --we have a word with punctuation that should be class colored
  26.             w = w:gsub(modW, '|c'..RAID_CLASS_COLORS[class].colorStr..modW..modChatColor)
  27.         elseif s then --we have a word without punctuation that should be class colored
  28.             w = '|c'..RAID_CLASS_COLORS[class].colorStr..w..modChatColor
  29.         elseif (q and p) then --we have you or your with punctuation that should be player class colored
  30.             w = w:gsub(modW, '|c'..playerClassColorTable.colorStr..modW..modChatColor)
  31.         elseif p then --we have you or your without punctuation that should be player class colored
  32.             w = '|c'..playerClassColorTable.colorStr..w..modChatColor
  33.         end
  34.         coloredMessage = coloredMessage == '' and w or coloredMessage..' '..w
  35.     end
  36.     return coloredMessage
  37. end

Any insight, as always, is greatly appreciated.
  Reply With Quote