zz_Friends/core.lua

136 lines
3.8 KiB
Lua

local name, addon = ...
local parentName = addon['parentName'] or "BrokerPack"
local childName = "zz_Friends"
local crealm = GetRealmName()
local defaults = {
}
local options = {
}
local roster,friendroster,bnetroster = {}, {}, {}
local GuildRoster = GuildRoster
if(C_GuildInfo) then
if(C_GuildInfo.GuildRoster) then
GuildRoster = C_GuildInfo.GuildRoster
end
end
local updateDelay = 15
local fmt = "|c0000ff00%s|r |c00ffffff%s|r"
local db = {}
local ldbicon = LibStub:GetLibrary("LibDBIcon-1.0")
local function anymod()
return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown()
end
local function ldbOnClick(self, button)
if(IsInGuild() and not anymod() and button == "LeftButton") then
ToggleGuildFrame()
elseif(not anymod() and (button == "LeftButton" or button == "RightButton")) then
ToggleFriendsFrame()
elseif(IsShiftKeyDown() and button == "LeftButton") then
addon['db']['global']['ldbicons'][childName]['hide'] = not addon['db']['global']['ldbicons'][childName]['hide']
if(ldbicon) then
if(addon['db']['global']['ldbicons'][childName]['hide']) then
ldbicon:Hide(childName)
else
ldbicon:Show(childName)
end
end
end
end
local function OnTooltipShow(tip)
tip:AddLine(childName)
tip:AddLine("")
tip:AddLine(format(fmt,"Leftclick","Toggles Guild or Friendframe"))
tip:AddLine(format(fmt,"Rightclick","Toggles Friendframe"))
tip:AddLine(format(fmt,"Shift+Leftclick","Toggles minimap icon"))
end
local function OnText(message)
addon:OnText(childName, message)
end
local function count(v)
local c = 0
for _ in pairs(v) do
c = c + 1
end
return c
end
local function checkRoster(self, event, ...)
local newroster = {}
local rostername, online, _
local dontShow = false
if(event == "GUILD_ROSTER_UPDATE") then
for i=1,GetNumGuildMembers() do
rostername, _, _, _, _, _, _, _, online = GetGuildRosterInfo(i)
if(online and rostername) then
newroster[rostername]=true
end
end
roster = newroster
elseif(event == "FRIENDLIST_UPDATE") then
for i = 1, C_FriendList.GetNumFriends() do
local res = C_FriendList.GetFriendInfoByIndex(i)
if(res) then
if(res['connected'] and res['name']) then
newroster[res['name']] = true
end
end
end
friendroster = newroster
elseif(event == "BN_FRIEND_INFO_CHANGED") then
local _, numOnline = BNGetNumFriends()
local presenceID, isFriend,client,accid
for i = 1, numOnline do
local res = C_BattleNet.GetFriendAccountInfo(i)
if(res) then
local aci = res['gameAccountInfo']
dontShow = false
if(aci) then
if(aci['clientProgram'] == BNET_CLIENT_WOW) then
if(aci['realmName'] == crealm and friendroster[aci['characterName']]) then
dontShow = true
end
end
if(aci['isOnline'] and aci['characterName'] and not dontShow) then
newroster[aci['characterName']] = true
end
end
end
end
bnetroster = newroster
end
local afkmsg = ""
if(UnitIsAFK("player")) then
afkmsg = " AFK"
elseif(UnitIsDND("player")) then
afkmsg = " DND"
end
OnText(format("|c0000ff00%i|r/|c0000ffff%i|r|c00ff0000%s|r", count(roster),count(friendroster)+count(bnetroster),afkmsg))
end
local function OnUpdate()
GuildRoster()
checkRoster(nil, "FRIENDLIST_UPDATE")
checkRoster(nil, "BN_FRIEND_INFO_CHANGED")
C_Timer.After(updateDelay, OnUpdate)
end
local function init()
options = addon:InitConfig(childName, true, {
['name'] = childName,
['type'] = "data source",
['OnTooltipShow'] = OnTooltipShow,
['OnClick'] = ldbOnClick,
})
db = addon['db']['profile'][childName]
crealm = GetRealmName()
addon:RegisterFunc({"GUILD_ROSTER_UPDATE","FRIENDLIST_UPDATE","CHAT_MSG_CHANNEL_JOIN","CHAT_MSG_CHANNEL_LEAVE","PLAYER_FLAGS_CHANGED","UNIT_FLAGS","BN_FRIEND_INFO_CHANGED"},"OnEvent", checkRoster)
C_Timer.After(updateDelay, OnUpdate)
C_FriendList.ShowFriends()
end
addon:startup(name, childName, init, true, defaults)