View Single Post
01-09-19, 09:09 PM   #17
jlam
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 29
I dry-coded this (didn't run, didn't check for syntax errors), but hopefully it gets the idea across. It looks like you're mainly concerned with replacing names with class-colored versions of those same names, and you have two functions for doing the name-replacement in slightly different ways. Since the two functions are so similar, I made them into a single function implementation.

Now that I see what you're trying to accomplish, I think there's something smarter that can be done to iterate through chatMessage and alternate grabbing whitespace and non-whitespace. I'll think about how to do that tomorrow when I have time.

Lua Code:
  1. -- Gets a name from the given string after stripping out punctuation.
  2. local function getName(s)
  3.     local stripped = s:gsub("'s$", ''):gsub('%p', '') -- strip possessive then punctuation
  4.     local name, realm = UnitName(stripped)
  5.     local fullName = (realm and realm ~= "") and name .. '-' .. realm or name
  6.     if fullName == stripped then
  7.         -- "stripped" isn't a unit ID or GUID.
  8.         return fullName, name, realm
  9.     end
  10.     return nil
  11. end
  12.  
  13. --[[
  14.     Extracts a full name (relative to the player's realm) from the given string
  15.     Returns:
  16.         unit ID: a unit ID that can be passed to unit functions, or nil
  17.         fullName: "name-realm", or "name" if the realm is the same as the player's realm, or nil.
  18. --]]
  19. local function extractFullName(s)
  20.     local fullName = getName(s)
  21.     return fullName, fullname
  22. end
  23.  
  24. --[[
  25.     Extracts a simple name (no realm), or "you" or "your", from the given string.
  26.     Returns:
  27.         unit ID: a unit ID that can be passed to unit functions, or nil
  28.         name: "name" with no realm information, or nil.
  29. --]]
  30. local playerName = UnitName("player")
  31.  
  32. local function extractShortName(s)
  33.     local fullName, name = getName(s)
  34.     if fullName then
  35.         return fullName, name
  36.     end
  37.     local stripped = s:gsub("'s$", ''):gsub('%p', '') -- strip possessive then punctuation
  38.     local lowered = stripped:lower()
  39.     if lowered == "you" or lowered == "your" then
  40.         return playerName, stripped
  41.     end
  42.     return nil
  43. end
  44.  
  45. -- Replaces name with class-colored names in chatMessage.
  46. local hexColorNames
  47. do
  48.     local coloredMessage = {}
  49.  
  50.     function hexColorNames(chatMessage, modChatColor)
  51.         extractNameFunc = extractNameFunc or extractFullName
  52.         wipe(coloredMessage)
  53.         for word in gmatch(chatMessage, "%S+") do
  54.             local unit, name = extractNameFunc(word)
  55.             if unit and name then
  56.                 local _, class = UnitClass(unit)
  57.                 if class then
  58.                     local coloredName = "|c" .. RAID_CLASS_COLORS[class].colorStr .. name .. modChatColor
  59.                     word = word:gsub(name, coloredName)
  60.                 end
  61.             end
  62.             table.insert(coloredMessage, word)
  63.         end
  64.         return table.concat(coloredMessage, ' ')
  65.     end
  66. end
  67.  
  68. local function modHexColorNames(chatMessage, modChatColor)
  69.     return hexColorNames(chatMessage, modChatColor, extractShortName)
  70. end
  Reply With Quote