Advertisement
Arborus

nicktag

Sep 1st, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. local have_repeated = false
  2. local count_spaces = 0
  3. local check_repeated = function(char)
  4. if (char == " ") then
  5. have_repeated = false
  6. elseif (string.len (char) > 2) then
  7. have_repeated = false
  8. elseif (char == " ") then
  9. count_spaces = count_spaces + 1
  10. end
  11. end
  12.  
  13. --we need to keep game smooth checking and formating nicknames.
  14. --SetNickname and names comming from other player need to be check.
  15. function NickTag:CheckName (name)
  16. --as nicktag only work internally in the guild, we think that is not necessary a work filter to avoid people using bad language.
  17.  
  18. if (type(name) ~= "string") then
  19. return false, LibStub ("AceLocale-3.0"):GetLocale ("NickTag-1.0")["STRING_ERROR_4"] --error 4 = name isn't a valid string
  20. end
  21.  
  22. name = trim (name)
  23.  
  24. --which alphabet to use
  25. local alphabetToUse = "latin"
  26. local firstLetter = name:match("^.")
  27. local maxLength = 999
  28.  
  29. if (alphabet["cyrillic"][firstLetter]) then
  30. --reserve cyrillic only to clients running ruRU
  31. if (GetLocale() == "ruRU") then
  32. alphabetToUse = "cyrillic"
  33. maxLength = 28 --cyrillic uses two bytes
  34. else
  35. alphabetToUse = "latin"
  36. end
  37.  
  38. elseif (alphabet["chinese"][firstLetter]) then
  39. if (GetLocale() == "zhCN" or GetLocale() == "zhTW") then
  40. alphabetToUse = "chinese"
  41. maxLength = 56 --chinese uses 4 bytes
  42. else
  43. alphabetToUse = "latin"
  44. end
  45. end
  46.  
  47. --limit nickname to 12 characters, same as wow.
  48. --cyrillic seems to double the len using 2 bytes
  49. local len = string.len (name)
  50. if (len > maxLength) then
  51. return false, LibStub ("AceLocale-3.0"):GetLocale ("NickTag-1.0")["STRING_ERROR_1"] --error 1 = nickname is too long, max of 12 characters.
  52. end
  53.  
  54. --check if contain any non allowed characters, by now only accpet letters, numbers and spaces.
  55. --by default wow do not accetp spaces, but here will allow.
  56. --tested over lua 5.2 and this capture was okey with accents, not sure why inside wow this doesn't work.
  57.  
  58. -- local notallow = string.find (name, "[^a-zA-Z�������%s]")
  59. -- if (notallow) then
  60. -- return false, LibStub ("AceLocale-3.0"):GetLocale ("NickTag-1.0")["STRING_ERROR_2"] --error 2 = nickname only support letters, numbers and spaces.
  61. -- end
  62.  
  63. --[=[
  64. for letter in name:gmatch(".") do
  65. if (not allowedLetters[letter]) then
  66. return false, LibStub ("AceLocale-3.0"):GetLocale ("NickTag-1.0")["STRING_ERROR_2"] --error 2 = nickname only support letters, numbers and spaces.
  67. end
  68. end
  69. --]=]
  70.  
  71. --check if there is sequencial repeated characters, like "Jasooon" were repeats 3 times the "o" character.
  72. --got this from http://stackoverflow.com/questions/15608299/lua-pattern-matching-repeating-character
  73. have_repeated = false
  74. count_spaces = 0
  75. string.gsub (name, '.', '\0%0%0'):gsub ('(.)%z%1','%1'):gsub ('%z.([^%z]+)', check_repeated)
  76. if (count_spaces > 3) then
  77. have_repeated = false
  78. end
  79. if (have_repeated) then
  80. return false, LibStub ("AceLocale-3.0"):GetLocale ("NickTag-1.0")["STRING_ERROR_3"] --error 3 = cant use the same letter three times consecutively, 2 spaces consecutively or 3 or more spaces.
  81. end
  82.  
  83. return true
  84. end
  85.  
  86. --set the "player" nickname and schedule for send updated persona
  87. function NickTag:SetNickname (name)
  88.  
  89. --check data before
  90. assert (type(name) == "string", "NickTag 'SetNickname' expects a string on #1 argument.")
  91.  
  92. --check if the nickname is okey to allowed to use.
  93. local okey, errortype = NickTag:CheckName (name)
  94. if (not okey) then
  95. NickTag:Msg ("SetNickname() invalid name ", name)
  96. return false, errortype
  97. end
  98.  
  99. --here we format the text to match titles, e.g converts name like "JASON NICKSHOW" into "Jason Nickshow".
  100.  
  101. local playerName = UnitName ("player")
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement