View Single Post
11-06-18, 12:02 AM   #11
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
I'm nearing completion on this addon, but there's a function that I feel is just not very optimized.

Lua Code:
  1. local function hexColorNames(chatMessage, modChatColor) --this will churn through a message and find names that match players and color it by their class color
  2.     local words = {strsplit(' ', chatMessage)}
  3.     local name = UnitName('player')
  4.     local combinedName = (name..'-'..GetRealmName())
  5.     for i = 1, #words do
  6.         local w = words[i]
  7.         if (w and not (w == 'player' or w == 'target') and UnitName(w) and UnitIsPlayer(w)) then
  8.             local _, class = UnitClass(w)
  9.             local colors = RAID_CLASS_COLORS[class] --color the words that match player names with class colors, return back to the channel color for the next word(s)
  10.             if strfind(w, '-') then --this weird thing is because - is found in hyphenated names, - is also a magic character, if we simply use gsub, it never finds the full name because it gets stuck on '-'
  11.                 local hyphenatedName = w --hold the old hyphenated name so we can replace it
  12.                 local a, b = strsplit('-', w) --split the hyphenated name into two strings
  13.                 w = a..'%-'..b --rejoin the strings with a % before the - so our gsub can actually look for it
  14.                 chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..hyphenatedName..modChatColor)
  15.             else
  16.                 chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..w..modChatColor)
  17.             end
  18.         elseif combinedName:lower() == w:lower() then --if for whatever reason someone types your name and realm unitname(w) AND unitisplayer(w) would be false for the whole name and realm
  19.             local _, class = UnitClass('player')
  20.             local colors = RAID_CLASS_COLORS[class]
  21.             local hyphenatedName = w --hold the old hyphenated name so we can replace it
  22.             local a, b = strsplit('-', w) --split the hyphenated name into two strings
  23.             w = a..'%-'..b --rejoin the strings with a % before the - so our gsub can actually look for it
  24.             chatMessage = chatMessage:gsub(w, '|c'..colors.colorStr..hyphenatedName..modChatColor)
  25.         end
  26.     end
  27.     return chatMessage
  28. end

The goal of this function is to split any message you send to it by words. Since these chatMessage strings can have non-ascii characters via player names and the like, I just use the blizzard strsplit function to break the statement up into individual words. Each word is checked to see if it is a player's name, and if it is, it's colored by their class. I ran into particular issues with hyphenated names for off-realm players, since '-' is a magic character, typical gsub functions would get caught up with that. So I have to do a lot of string manipulation.

Furthering this issue, if you type in your full charactername-realmname, this returns false on your server, but true on a client off-server, so I had to write a second part to the if statement to get around this weird issue (if you don't understand what I'm saying, type /script print(UnitName('yourCharacterName-yourRealmName')) on your realm and on another realm. It will return false and true, respectively.

If you can see someway to better optimize this function, I'd greatly appreciate it. The function works through every scenario currently, I just don't know (but likely assume) if it can be written better. Thank you!

Last edited by Terenna : 11-06-18 at 12:25 AM.
  Reply With Quote