Advertisement
Guest User

prat_update_playernames

a guest
Dec 14th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.41 KB | None | 0 0
  1. --[[
  2. Name: Prat_PlayerNames
  3. Revision: $Revision: 16689 $
  4. Author(s): Curney (asml8ed@gmail.com)
  5.            Krtek (krtek4@gmail.com)
  6. Inspired by: idChat2_PlayerNames by Industrial
  7. Website: http://www.wowace.com/files/index.php?path=Prat/
  8. Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#PlayerNames
  9. SVN: http://svn.wowace.com/wowace/trunk/Prat/
  10. Description: Module for Prat that adds player name options.
  11. Dependencies: Prat
  12. ]]
  13.  
  14. local L = AceLibrary("AceLocale-2.2"):new("PratPlayerNames")
  15.  
  16. L:RegisterTranslations("enUS", function() return {
  17.     ["PlayerNames"] = true,
  18.     ["Player name formating options."] = true,
  19.     ["Brackets"] = true,
  20.     ["Square"] = true,
  21.     ["Angled"] = true,
  22.     ["None"] = true,
  23.     ["Class"] = true,
  24.     ["Random"] = true,
  25.     ["Sets style of brackets to use around player names."] = true,
  26.     ["Use common color"] = true,
  27.     ["Toggle useing common color for unknown player names."] = true,
  28.     ["Set common color"] = true,
  29.     ["Change the common color of PlayerNames"] = true,
  30.     ["Use TabComplete"] = true,
  31.     ["Toggle tab completion of player names."] = true,
  32.     ["Show Level"] = true,
  33.     ["Toggle level showing."] = true,
  34.     ["Show Group"] = true,
  35.     ["Toggle raid group showing."] = true,
  36.     ["Keep Info"] = true,
  37.     ["Keep player information between session."] = true,
  38.     ["Color Mode"] = true,
  39.     ["How to color Players name."] = true,
  40.     ["Toggle"] = true,
  41.     ["Toggle the module on and off."] = true,
  42. } end)
  43.  
  44. L:RegisterTranslations("zhCN", function() return {
  45.     ["PlayerNames"] = "玩家姓名",
  46.     ["Player name formating options."] = "玩家姓名格式选项。",
  47.     ["Brackets"] = "圆弧",
  48.     ["Square"] = "方形",
  49.     ["Angled"] = "折角",
  50.     ["None"] = "无",
  51.     ["Class"] = "职业",
  52.     ["Random"] = "随机",
  53.     ["Sets style of brackets to use around player names."] = "设置玩家名字的括号。",
  54.     ["Use TabComplete"] = "标签",
  55.     ["Toggle tab completion of player names."] = "为玩家姓名作标签。",
  56.     ["Show Level"] = "显示等级",
  57.     ["Toggle level showing."] = "人物等级显示。",
  58.     ["Show Group"] = "显示小组",
  59.     ["Toggle raid group showing."] = "团队小组显示。",
  60.     ["Keep Info"] = "持续",
  61.     ["Keep player information between session."] = "在进程间保持信息。",
  62.     ["Color Mode"] = "颜色模块",
  63.     ["How to color Players name."] = "玩家名字颜色。",
  64.     ["Toggle"] = "切换",
  65.     ["Toggle the module on and off."] = "切换此模块的打开与关闭。",
  66. } end)
  67.  
  68. L:RegisterTranslations("koKR", function() return {
  69.     ["PlayerNames"] = "플레이어명",
  70.     ["Player name formating options."] = "플레이어명 형식을 설정합니다.",
  71.     ["Brackets"] = "괄호",
  72.     ["Square"] = "사각형",
  73.     ["Angled"] = "각진형",
  74.     ["None"] = "없음",
  75.     ["Class"] = "직업",
  76.     ["Random"] = "무작위",
  77.     ["Sets style of brackets to use around player names."] = "플레이어명 주위 괄호의 스타일 설정",
  78.     ["Use common color"] = "공통 색상 사용",
  79.     ["Toggle useing common color for unknown player names."] = "알려지지 않은 플레이어명에 대한 공통 색상 사용을 전환합니다.",
  80.     ["Set common color"] = "공통 색상 설정",
  81.     ["Change the common color of PlayerNames"] = "플레이어명의 공통 색상을 설정합니다.",
  82.     ["Use TabComplete"] = "탭완성 사용",
  83.     ["Toggle tab completion of player names."] = "플레이어명의 탭 완성기능을 전환합니다.",
  84.     ["Show Level"] = "레벨 표시",
  85.     ["Toggle level showing."] = "레벨 표시를 전환합니다.",
  86.     ["Show Group"] = "파티 표시",
  87.     ["Toggle raid group showing."] = "공격대 파티 표시를 전환합니다.",
  88.     ["Keep Info"] = "정보 유지",
  89.     ["Keep player information between session."] = "연결 간 플레이어 정보를 유지합니다.",
  90.     ["Color Mode"] = "색상 모드",
  91.     ["How to color Players name."] = "플레이어명 색상을 변경합니다.",
  92.     ["Toggle"] = "전환",
  93.     ["Toggle the module on and off."] = "모듈 켜고 끄기를 전환합니다.",
  94. } end)
  95.  
  96. local BC = AceLibrary("Babble-Class-2.2")
  97.  
  98. Prat_PlayerNames = Prat:NewModule("playernames")
  99.  
  100. Prat_PlayerNames.Classes = {}
  101. Prat_PlayerNames.Levels = {}
  102. Prat_PlayerNames.Subgroups = {}
  103.  
  104. function Prat_PlayerNames:OnInitialize()
  105.     Prat_PlayerNames.db = Prat:AcquireDBNamespace("PlayerNames")
  106.     Prat:RegisterDefaults("PlayerNames", "profile", {
  107.         on = true,
  108.         brackets = "Angled",
  109.         tabcomplete = true,
  110.         level = true,
  111.         classes = {},
  112.         levels = {},
  113.         subgroup = true,
  114.         keep = true,
  115.         colormode = "CLASS",
  116.         usecommoncolor = true,
  117.         color = {
  118.             r = 1,
  119.             g = 0,
  120.             b = 1,
  121.         },
  122.     })
  123.     Prat.Options.args.playernames = {
  124.         name = L["PlayerNames"],
  125.         desc = L["Player name formating options."],
  126.         type = "group",
  127.         args = {
  128.             brackets = {
  129.                 name = L["Brackets"],
  130.                 desc = L["Sets style of brackets to use around player names."],
  131.                 type = "text",
  132.                 order = 140,
  133.                 get = function() return Prat_PlayerNames.db.profile.brackets end,
  134.                 set = function(v) Prat_PlayerNames.db.profile.brackets = v end,
  135.                 validate = {["Square"] = L["Square"], ["Angled"] = L["Angled"], ["None"] = L["None"]}
  136.             },
  137.             colormode = {
  138.                 name = L["Color Mode"],
  139.                 desc = L["How to color Players name."],
  140.                 type = "text",
  141.                 order = 150,
  142.                 get = function() return Prat_PlayerNames.db.profile.colormode end,
  143.                 set = function(v) Prat_PlayerNames.db.profile.colormode = v end,
  144.                 validate = {["RANDOM"] = L["Random"], ["CLASS"] = L["Class"], ["NONE"] = L["None"]}
  145.             },
  146.             level = {
  147.                 name = L["Show Level"],
  148.                 desc = L["Toggle level showing."],
  149.                 type = "toggle",
  150.                 order = 160,
  151.                 get = function() return Prat_PlayerNames.db.profile.level end,
  152.                 set = function(v) Prat_PlayerNames.db.profile.level = v end
  153.             },
  154.             subgroup = {
  155.                 name = L["Show Group"],
  156.                 desc = L["Toggle raid group showing."],
  157.                 type = "toggle",
  158.                 order = 170,
  159.                 get = function() return Prat_PlayerNames.db.profile.subgroup end,
  160.                 set = function(v) Prat_PlayerNames.db.profile.subgroup = v end
  161.             },
  162.             keep = {
  163.                 name = L["Keep Info"],
  164.                 desc = L["Keep player information between session."],
  165.                 type = "toggle",
  166.                 order = 180,
  167.                 get = function() return Prat_PlayerNames.db.profile.keep end,
  168.                 set = function(v) Prat_PlayerNames.db.profile.keep = v end
  169.             },
  170.             tabcomplete = {
  171.                 name = L["Use TabComplete"],
  172.                 desc = L["Toggle tab completion of player names."],
  173.                 type = "toggle",
  174.                 order = 190,
  175.                 get = function() return Prat_PlayerNames.db.profile.tabcomplete end,
  176.                 set = function(v) Prat_PlayerNames.db.profile.tabcomplete = v; self:TabComplete(v) end
  177.             },
  178.             usecommoncolor = {
  179.                 name = L["Use common color"],
  180.                 desc = L["Toggle useing common color for unknown player names."],
  181.                 type = "toggle",
  182.                 order = 200,
  183.                 get = function() return Prat_PlayerNames.db.profile.usecommoncolor end,
  184.                 set = function(v) Prat_PlayerNames.db.profile.usecommoncolor = v end
  185.             },
  186.             setcolor = {
  187.                 name = L["Set common color"],
  188.                 desc = L["Change the common color of PlayerNames"],
  189.                 type = "color",
  190.                 order = 210,
  191.                 get = function() return Prat_PlayerNames.db.profile.color.r, Prat_PlayerNames.db.profile.color.g, Prat_PlayerNames.db.profile.color.b end,
  192.                 set = function(r, g, b, a) Prat_PlayerNames.db.profile.color.r, Prat_PlayerNames.db.profile.color.g, Prat_PlayerNames.db.profile.color.b = r, g, b end,
  193.                 disabled = function() if not Prat_PlayerNames.db.profile.usecommoncolor then return true else return false end end,
  194.             },
  195.             toggle = {
  196.                 name = L["Toggle"],
  197.                 desc = L["Toggle the module on and off."],
  198.                 type = "toggle",
  199.                 order = 220,
  200.                 get = function() return Prat_PlayerNames.db.profile.on end,
  201.                 set = function() Prat_PlayerNames.db.profile.on = Prat:ToggleModuleActive("playernames") end
  202.             }
  203.         }
  204.     }
  205. end
  206.  
  207. function Prat_PlayerNames:OnEnable()
  208.     for i = 1, NUM_CHAT_WINDOWS do
  209.         self:Hook(getglobal("ChatFrame"..i), "AddMessage")
  210.     end
  211.  
  212.     self:RegisterEvent("FRIENDLIST_UPDATE", "updateFriends")
  213.     self:RegisterEvent("GUILD_ROSTER_UPDATE", "updateGuild")
  214.     self:RegisterEvent("RAID_ROSTER_UPDATE", "updateRaid")
  215.     self:RegisterEvent("PARTY_MEMBERS_CHANGED", "updateParty")
  216.     self:RegisterEvent("PLAYER_TARGET_CHANGED", "updateTarget")
  217.     self:RegisterEvent("WHO_LIST_UPDATE", "updateWho")
  218.     self:RegisterEvent("CHAT_MSG_SYSTEM", "updateWho") -- for short /who command
  219.  
  220.     for name, color in pairs(Prat_PlayerNames.db.profile.classes) do Prat_PlayerNames.Classes[name] = color end
  221.     for name, level in pairs(Prat_PlayerNames.db.profile.levels) do Prat_PlayerNames.Levels[name] = level end
  222.  
  223.     local PlayerClass
  224.     _, PlayerClass = UnitClass("player")
  225.     self:addName(UnitName("player"), Playerclass, UnitLevel("player"))
  226.  
  227.     self:updateParty()
  228.     self:updateRaid()
  229.     self:updateFriends()
  230.     self:updateGuild()
  231.        
  232.     self:TabComplete(Prat_PlayerNames.db.profile.tabcomplete)
  233. end
  234.  
  235. function Prat_PlayerNames:OnDisable()
  236.     self:TabComplete(false)
  237. end
  238.  
  239. function Prat_PlayerNames:updateFriends()
  240.     local Name, Class, Level
  241.     for i = 1, GetNumFriends() do
  242.         Name, Level, Class = GetFriendInfo(i)
  243.         self:addName(Name, Class, Level)
  244.     end
  245. end
  246.  
  247. function Prat_PlayerNames:updateGuild()
  248.     local Name, Class, Level
  249.     for i = 1, GetNumGuildMembers() do
  250.         Name, _, _, Level, Class = GetGuildRosterInfo(i)
  251.         self:addName(Name, Class, Level)
  252.     end
  253. end
  254.  
  255. function Prat_PlayerNames:updateRaid()
  256.     local Name, Class, SubGroup, Level
  257.     Prat_PlayerNames.Subgroups = {}
  258.     for i = 1, GetNumRaidMembers() do
  259.         Name, _, SubGroup, Level, Class = GetRaidRosterInfo(i)
  260.         self:addName(Name, Class, Level, SubGroup)
  261.     end
  262. end
  263.  
  264. function Prat_PlayerNames:updateParty()
  265.     local Class, Unit
  266.     for i = 1, GetNumPartyMembers() do
  267.         Unit = "party" .. i
  268.         _, Class = UnitClass(Unit)
  269.         self:addName(UnitName(Unit), Class, UnitLevel(Unit))
  270.     end
  271. end
  272.  
  273. function Prat_PlayerNames:updateTarget()
  274.     local Class
  275.     if not UnitIsPlayer("target") or not UnitIsFriend("player", "target") then
  276.         return
  277.     end
  278.     _, Class = UnitClass("target")
  279.     self:addName(UnitName("target"), Class, UnitLevel("target"))
  280. end
  281.  
  282. function Prat_PlayerNames:updateWho()
  283.     local Name, Class, Level
  284.     for i = 1, GetNumWhoResults() do
  285.         Name, _, Level, _, Class = GetWhoInfo(i)
  286.         self:addName(Name, Class, Level)
  287.     end
  288. end
  289.  
  290. function Prat_PlayerNames:addName(Name, Class, Level, SubGroup)
  291.   if Name then
  292.     if Level and Level ~= 0 and ( not ( Prat_PlayerNames.Levels[Name] and Prat_PlayerNames.Levels[Name] > Level ) ) then
  293.         Prat_PlayerNames.Levels[Name] = Level
  294.         if ( Prat_PlayerNames.db.profile.keep) then Prat_PlayerNames.db.profile.levels[Name] = Level end
  295.     end
  296.     if Class then
  297.         Prat_PlayerNames.Classes[Name] = Class
  298.         if ( Prat_PlayerNames.db.profile.keep ) then Prat_PlayerNames.db.profile.classes[Name] = Class end
  299.     end
  300.     if SubGroup then
  301.         Prat_PlayerNames.Subgroups[Name] = SubGroup
  302.     end
  303.   end
  304. end
  305.  
  306. function Prat_PlayerNames:randomColor(Name)
  307.     local hash = 17
  308.     for i=1,string.len(Name) do
  309.         hash = hash * 37 * string.byte(Name, i);
  310.     end
  311.    
  312.     local r = math.floor(math.mod(hash / 97, 255));
  313.     local g = math.floor(math.mod(hash / 17, 255));
  314.     local b = math.floor(math.mod(hash / 227, 255));
  315.        
  316.     if ((r * 299 + g * 587 + b * 114) / 1000) < 105 then
  317.         r = math.abs(r - 255);
  318.         g = math.abs(g - 255);
  319.         b = math.abs(b - 255);
  320.     end
  321.  
  322.     return string.format("%02x%02x%02x", r, g, b)
  323. end
  324.  
  325. function Prat_PlayerNames:addInfo(Name)
  326.     if Name then
  327.         local returnName = Name
  328.  
  329.         if Prat_PlayerNames.Levels[Name] and Prat_PlayerNames.db.profile.level then
  330.             returnName = string.format("%s:%s", Prat_PlayerNames.Levels[Name], returnName)
  331.         end
  332.         if Prat_PlayerNames.Subgroups[Name] and Prat_PlayerNames.db.profile.subgroup then
  333.             returnName = string.format("%s:%s", returnName, Prat_PlayerNames.Subgroups[Name])
  334.         end
  335.         if Prat_PlayerNames.Classes[Name] then
  336.                 if Prat_PlayerNames.db.profile.colormode == "CLASS" then
  337.                 returnName =  string.format("|cff%s%s|r", BC:GetHexColor(Prat_PlayerNames.Classes[Name]), returnName)
  338.             elseif Prat_PlayerNames.db.profile.colormode == "RANDOM" then
  339.                 returnName =  string.format("|cff%s%s|r", self:randomColor(Name), returnName)          
  340.                 end
  341.         else
  342.             if Prat_PlayerNames.db.profile.usecommoncolor then
  343.                 local color = string.format("%02x%02x%02x", Prat_PlayerNames.db.profile.color.r*255, Prat_PlayerNames.db.profile.color.g*255, Prat_PlayerNames.db.profile.color.b*255)
  344.                 returnName = string.format("|cff%s%s|r", color, returnName)    
  345.             end
  346.         end
  347.        
  348.         return returnName
  349.     else
  350.         return ""
  351.     end
  352. end
  353.  
  354. function Prat_PlayerNames:AddMessage(frame, text, r, g, b, id)
  355.     if text then
  356.         local Name = string.gsub(text, ".*|Hplayer:(.-)|h.*", "%1")
  357.         local Brackets
  358.  
  359.         Name = self:addInfo(Name)
  360.  
  361.         if Prat_PlayerNames.db.profile.brackets == "Angled" then
  362.             Brackets = "<|Hplayer:%1|h" .. Name .. "|h>"
  363.         elseif Prat_PlayerNames.db.profile.brackets == "None" then
  364.             Brackets = "|Hplayer:%1|h" .. Name .. "|h"
  365.         else
  366.             Brackets = "[|Hplayer:%1|h" .. Name .. "|h]"
  367.         end
  368.        
  369.         text = string.gsub(text, "|Hplayer:(.-)|h%[.-%]|h(.-:-)", Brackets .. "%2")
  370.     end
  371.     Prat_PlayerNames.hooks[frame].AddMessage(frame, text, r, g, b, id)
  372. end
  373.  
  374. function Prat_PlayerNames:TabComplete(enabled)
  375.     if enabled then
  376.         if not AceLibrary("AceTab-2.0"):IsTabCompletionRegistered("Prat_PlayerNames") then
  377.             AceLibrary("AceTab-2.0"):RegisterTabCompletion("Prat_PlayerNames", "",
  378.                 function(t, text, pos)
  379.                     for name in pairs(Prat_PlayerNames.Classes) do
  380.                         table.insert(t, pos == 0 and name..":" or name)
  381.                     end
  382.                 end,
  383.                 function(u, cands)
  384.                     local text = ""
  385.                     for _, cand in ipairs(cands) do
  386.                         cand = string.gsub(cand, ":", "")
  387.                         cand = self:addInfo(cand)
  388.                         text = text .. " " .. cand
  389.                     end
  390.                     Prat:Print("Tab completion : " .. text)
  391.                 end
  392.             )
  393.         end
  394.     else
  395.         if AceLibrary("AceTab-2.0"):IsTabCompletionRegistered("Prat_PlayerNames") then
  396.             AceLibrary("AceTab-2.0"):UnregisterTabCompletion("Prat_PlayerNames")
  397.         end
  398.     end
  399. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement