Wrote LibGuildInfo and hooked it into the chat section

Wobin [05-20-14 - 14:41]
Wrote LibGuildInfo and hooked it into the chat section
Filename
GuildChat.lua
GuildChat.txt
LibGuildInfo.lua
Libs/libGuildInfo/LibGuildInfo.lua
diff --git a/GuildChat.lua b/GuildChat.lua
index d15f777..26738dc 100644
--- a/GuildChat.lua
+++ b/GuildChat.lua
@@ -3,69 +3,49 @@ GuildChat = {}
 local LC = LibStub('libChat-1.0')
 LC:registerName(function(...) return GuildChat:GetName(...) end)

-function GuildChat:GetLevel(name)
-	return "29"
+local LGI = LibStub("LibGuildInfo-1.0")
+
+local AllianceColours = { ["Aldmeri Dominion"] = "|cffff00", ["Daggerfall Convenant"]= "|c4169E1", ["Ebonhart Pact"] = "|c8B0000"}
+local ClassColours = {["Dragon Knight"] = "ff0000", ["Sorcerer"] = "00ffff", ["Nightblade"] = "FF8C00", ["Templar"] = "FF00FF"}
+
+function GuildChat:GetLevel(name)
+	return LGI:GetLevelByMemberName(name)
 end

 function GuildChat:GetClassColour(name)
-	return "ff1122"
+	return ClassColours[LGI:GetClassNameByMemberName(name)]
 end

 function GuildChat:GetLevelColour(name)
-	return "ff8100"
+	return "|9932CC"
 end

 function GuildChat:GetAtColour(name)
-	return "aa8100"
+	return AllianceColours[LGI:GetAllianceNameByMemberName(name)]
 end

-function GuildChat:GetName(channelId, fromName, text)
+local ChanInfoArray = ZO_ChatSystem_GetChannelInfo()
+local CategoryMapping = ZO_ChatSystem_GetEventCategoryMappings()

-	local ChanInfoArray = ZO_ChatSystem_GetChannelInfo()
+function GuildChat:GetName(channelId, fromName, text)
 	local info = ChanInfoArray[channelId]
+	local r,g,b = GetChatCategoryColor(CategoryMapping[EVENT_CHAT_MESSAGE_CHANNEL][channelId])
+	local baseColour = ZO_ColorDef:New(r, g, b, 1)
+
 	local nameLink = (info.playerLinkable and not fromName:find("%[")) and ZO_LinkHandler_CreatePlayerLink(fromName:gsub("%^.+", "")) or fromName

 	local formattedName = nil

 	local  colour, display, name = nameLink:match("^|H([^:]*):([^\[]*)%[@([^\]]*)%]|h")
-		if colour then
-			formattedName = "|H"..colour.. ":".. display.."|h" ..
-							"|H" .. colour ..":" .. display .. "[|h"..
-							"|H" .. GuildChat:GetLevelColour(name) ..":" .. display .. GuildChat:GetLevel(name) .. "|h" ..
-							"|H" .. colour ..":" .. display .. "][|h"..
-							"|H" .. GuildChat:GetAtColour(name) .. ":".. display .. "@|h" ..
-							"|H" .. GuildChat:GetClassColour(name) ..":" .. display .. name .."|h" ..
-							"|H" .. colour ..":" .. display .. "]|h"
+		if colour then
+			formattedName = "[" .. GuildChat:GetLevelColour(fromName) .. GuildChat:GetLevel(fromName) .. "|c" .. baseColour:ToHex() .. "]" ..
+							"[" .. GuildChat:GetAtColour(fromName) .. "@" ..
+							"|H" .. GuildChat:GetClassColour(fromName) .. ":" .. display .. name .. "|h" ..
+							"|c" .. baseColour:ToHex() .. "]"
 			end
 	return formattedName or nameLink
 end

-function GuildChat:HookChat()
-
-	if GuildChat.ChatFunction then return end
-
-	if not CHAT_SYSTEM["containers"] then d("calling again") return zo_callLater(GuildChat.HookChat, 3000) end
-
-	GuildChat.ChatFunction = CHAT_SYSTEM["containers"][1].AddMessageToWindow
-	CHAT_SYSTEM["containers"][1].AddMessageToWindow =
-		function(self, window, message, r, g, b, category)
-			-- |Hignore:channel:My Guild Channel|h[My Guild Channel]|h|Hignore:display:SnuuSnuu|h[@SnuuSnuu]|h
-			-- (|Hignore:channel:My Guild Channel|h[My Guild Channel]|h)|H(ignore):(display:SnuuSnuu|h)[@(SnuuSnuu)]|h
-			local channel, colour, display, name, chat = message:match("^(.*)|H([^:]*):([^\[]*)%[@([^\]]*)%]|h(.*)")
-			if channel then
-				message = channel .."|H"..colour.. ":".. display.."|h" ..
-							"|H" .. colour ..":" .. display .. "[|h"..
-							"|H" .. GuildChat:GetLevelColour(name) ..":" .. display .. GuildChat:GetLevel(name) .. "|h" ..
-							"|H" .. colour ..":" .. display .. "][|h"..
-							"|H" .. GuildChat:GetAtColour(name) .. ":".. display .. "@|h" ..
-							"|H" .. GuildChat:GetClassColour(name) ..":" .. display .. name .."|h" ..
-							"|H" .. colour ..":" .. display .. "]|h".. chat
-			end
-			GuildChat.ChatFunction(self, window, message, r,g,b, category)
-		end
-end
-
-
 function GuildChat:Loaded(...)
 	local eventId, addonName = ...
 	if addonName ~= "GuildChat" then return end
diff --git a/GuildChat.txt b/GuildChat.txt
index 3c77df0..f91dd4d 100644
--- a/GuildChat.txt
+++ b/GuildChat.txt
@@ -8,5 +8,5 @@ Libs/LibStub/LibStub.lua
 Libs/libChat/libChat.lua
 Libs/LibAddonMenu-1.0/LibAddonMenu-1.0.lua

-
+LibGuildInfo.lua
 GuildChat.lua
\ No newline at end of file
diff --git a/LibGuildInfo.lua b/LibGuildInfo.lua
deleted file mode 100644
index 8304f92..0000000
--- a/LibGuildInfo.lua
+++ /dev/null
@@ -1,28 +0,0 @@
-local MAJOR, MINOR = "LibGuildInfo-1.0", 1
-local LibGuildInfo, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
-if not LibGuildInfo then return end	--the same or newer version of this lib is already loaded into memory
-
-function LibGuildInfo:DataLoaded()
-end
-
-function LibGuildInfo:OnGuildMemberAdded(guildId, displayName)
-end
-
-function LibGuildInfo:OnGuildMemberRemoved(guildId, displayName)
-end
-
-function LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName)
-end
-
-function LibGuildInfo:OnGuildMemberCharacterLevelChanged(guildId, displayName, level)
-end
-
-function LibGuildInfo:OnGuildMemberCharacterVeteranRankChanged(guildId, displayName, level)
-end
-
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_DATA_LOADED", "EVENT_GUILD_DATA_LOADED", function() LibGuildInfo:DataLoaded() end)
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_ADDED", "EVENT_GUILD_MEMBER_ADDED", function(_, guildId, displayName) LibGuildInfo:OnGuildMemberAdded(guildId, displayName) end)
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_REMOVED", "EVENT_GUILD_MEMBER_REMOVED", function(_, guildId, displayName) LibGuildInfo:OnGuildMemberRemoved(guildId, displayName) end)
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_UPDATED", "EVENT_GUILD_MEMBER_CHARACTER_UPDATED",  function(_, guildId, displayName) LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName) end)
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_LEVEL_CHANGED", "EVENT_GUILD_MEMBER_CHARACTER_LEVEL_CHANGED",	function(_, guildId, displayName, characterName, level) LibGuildInfo:OnGuildMemberCharacterLevelChanged(guildId, displayName, level) end)
-EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_VETERAN_RANK_CHANGED", "EVENT_GUILD_MEMBER_CHARACTER_VETERAN_RANK_CHANGED", function(_, guildId, displayName, characterName, veteranRank) LibGuildInfo:OnGuildMemberCharacterVeteranRankChanged(guildId, displayName, veteranRank) end)
\ No newline at end of file
diff --git a/Libs/libGuildInfo/LibGuildInfo.lua b/Libs/libGuildInfo/LibGuildInfo.lua
new file mode 100644
index 0000000..968c5bc
--- /dev/null
+++ b/Libs/libGuildInfo/LibGuildInfo.lua
@@ -0,0 +1,144 @@
+local MAJOR, MINOR = "LibGuildInfo-1.0", 1
+local LibGuildInfo, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
+if not LibGuildInfo then return end	--the same or newer version of this lib is already loaded into memory
+
+local Classes = {[1] = "Dragon Knight", [2]="Sorcerer", [3]="Nightblade", [6]="Templar"}
+local Alliances = {[1] = "Aldmeri Dominion", [2] = "Ebonhart Pact", [3] = "Daggerfall Convenant"}
+-- API --
+function LibGuildInfo:GetGuildMemberByMemberName(name)
+	local member = name:find("@") and self.DisplayNames[name:lower()] or self.CharacterNames[name:lower()]
+	if member and next(member) then return member end
+end
+
+function LibGuildInfo:GetClassNumByMemberName(name)
+	local member = self:GetGuildMemberByMemberName(name)
+	if member then return member.class end
+end
+
+function LibGuildInfo:GetClassNameByMemberName(name)
+	local classNum = self:GetClassNumByMemberName(name)
+	if classNum and Classes[classNum] then return Classes[classNum] end
+end
+
+function LibGuildInfo:GetLevelByMemberName(name)
+	local member = self:GetGuildMemberByMemberName(name)
+	if member then
+		if member.level < 50 then return member.level end
+		return "v" .. member.veteranRank
+	end
+end
+
+function LibGuildInfo:GetAllianceNumByMemberName(name)
+	local member = self:GetGuildMemberByMemberName(name)
+	if member then return member.alliance end
+end
+
+function LibGuildInfo:GetAllianceNameByMemberName(name)
+	local allianceNum = self:GetAllianceNumByMemberName(name)
+	if allianceNum and Alliances[allianceNum] then return Alliances[allianceNum] end
+end
+
+-- Setup functions --
+local visitedTables = {}
+
+function LibGuildInfo:DeepTableCopy(source, subCall)
+    local dest =  {}
+
+    for k, v in pairs(source) do
+        if type(v) == "table" and not visitedTables[v] then
+        	visitedTables[v] = true
+            dest[k] = self:DeepTableCopy(v, true)
+        else
+            dest[k] = v
+        end
+    end
+
+    if not subCall then visitedTables = {} end
+
+    return dest
+end
+
+
+function LibGuildInfo:DataLoaded()
+	if self.GuildDataLoaded then return end
+
+	self.GuildDataLoaded = true
+
+	local currentGuildId = GUILD_ROSTER.guildId
+
+	self.GuildRoster = {}
+
+	for i=1, GetNumGuilds() do
+		GUILD_ROSTER:SetGuildId(i)
+		self.GuildRoster[i] = self:DeepTableCopy(GUILD_ROSTER["masterList"])
+	end
+
+	GUILD_ROSTER:SetGuildId(currentGuildId)
+	self:ProcessData()
+end
+
+function LibGuildInfo:ProcessData()
+	self.DisplayNames = self.DisplayNames or {}
+
+	ZO_ClearTable(self.DisplayNames)
+
+	for i,roster in pairs(self.GuildRoster) do
+		for i,v in pairs(roster) do
+			self.DisplayNames[v.displayName:lower()] = v
+		end
+	end
+
+	self.CharacterNames = self.CharacterNames or {}
+
+	ZO_ClearTable(self.CharacterNames)
+
+	for i, roster in pairs(self.GuildRoster) do
+		for i,v in pairs(roster) do
+			self.CharacterNames[v.characterName:lower()] = v
+		end
+	end
+end
+
+function LibGuildInfo:FindInCurrentRoster(name)
+	for i,v in pairs(GUILD_ROSTER["masterList"]) do
+		if name:find("@") and v.displayName:lower() == name:lower() or v.characterName == name:lower() then
+			return v
+		end
+	end
+end
+
+function LibGuildInfo:OnGuildMemberAdded(guildId, displayName)
+	if not self.GuildDataLoaded then return end
+	local currentGuildId = GUILD_ROSTER.guildId
+
+	GUILD_ROSTER:SetGuildId(guildId)
+	local v = self:DeepTableCopy(self:FindInCurrentRoster(displayName))
+	table.insert(self.GuildRoster[guildId], v)
+	self.DisplayNames[v.displayName:lower()] = v
+	self.CharacterNames[v.characterName:lower()] = v
+
+	GUILD_ROSTER:SetGuildId(currentGuildId)
+end
+
+function LibGuildInfo:OnGuildMemberRemoved(guildId, displayName)
+	if not self.GuildDataLoaded then return end
+	local v = self.DisplayNames[displayName:lower()]
+	ZO_ClearTable(v)
+end
+
+function LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName)
+	if not self.GuildDataLoaded then return end
+	local currentGuildId = GUILD_ROSTER.guildId
+
+	GUILD_ROSTER:SetGuildId(guildId)
+	local v = self:FindInCurrentRoster(displayName)
+	ZO_ShallowTableCopy(v,self.DisplayNames[displayName:lower()])
+	GUILD_ROSTER:SetGuildId(currentGuildId)
+end
+
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_PLAYER_ACTIVATED", EVENT_PLAYER_ACTIVATED, function() LibGuildInfo:DataLoaded() end)
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_ADDED", EVENT_GUILD_MEMBER_ADDED, function(_, guildId, displayName) LibGuildInfo:OnGuildMemberAdded(guildId, displayName) end)
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_REMOVED", EVENT_GUILD_MEMBER_REMOVED, function(_, guildId, displayName) LibGuildInfo:OnGuildMemberRemoved(guildId, displayName) end)
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_UPDATED", EVENT_GUILD_MEMBER_CHARACTER_UPDATED,  function(_, guildId, displayName) LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName) end)
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_LEVEL_CHANGED", EVENT_GUILD_MEMBER_CHARACTER_LEVEL_CHANGED,	function(_, guildId, displayName) LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName) end)
+EVENT_MANAGER:RegisterForEvent("LGI_EVENT_GUILD_MEMBER_CHARACTER_VETERAN_RANK_CHANGED", EVENT_GUILD_MEMBER_CHARACTER_VETERAN_RANK_CHANGED, function(_, guildId, displayName) LibGuildInfo:OnGuildMemberCharacterUpdated(guildId, displayName) end)
\ No newline at end of file