-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuildNotificator.lua
383 lines (314 loc) · 11.4 KB
/
GuildNotificator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
GuildNotificator = GuildNotificator or {}
local ADDON_NAME = "GuildNotificator"
local ADDON_VERSION = "1.0.1"
local ADDON_AUTHOR = "@Llether"
local gn = GuildNotificator
local lcm = LibChatMessage('GNotif', "gn")
local lam2 = LibAddonMenu2
-- isOnlineManager table
local isOnlineManager = {}
local db
-- Defaults SV values
local defaults = {
friendorguild = 2,
notifintab = 2,
}
-- Display Message in system tab or where chat category is displayed
local function DisplayInTab(guildId, displayedMessage)
local category
-- to match categories
if guildId >= 1 and guildId <= 5 then
category = guildId + 9
-- can be > 5 if reorganized, so use CHAT_CATEGORY_SYSTEM
else
category = CHAT_CATEGORY_SYSTEM
end
-- Can occur if event is before EVENT_PLAYER_ACTIVATED
if CHAT_SYSTEM and CHAT_SYSTEM.primaryContainer then
-- TODO Should display in the right tab
lcm:Print(displayedMessage)
end
end
-- Will notify a friend login/logout if needed
local function NotifyFriend(displayName, message, newStatus)
-- Per default, showMessage is displayed
local showMessage = true
-- if friendorguild = 1, friend is always displayed
if db.friendorguild == 2 then
-- Do we need to display message?
for guild = 1, GetNumGuilds() do
-- Avoid some loops
if showMessage then
-- Guildname
local guildId = GetGuildId(guild)
local guildName = GetGuildName(guildId)
-- Occurs sometimes
if(not guildName or (guildName):len() < 1) then
guildName = "Guild " .. guildId
end
-- Only if notification activated for guild X
if db[guildName].notify then
-- Get account name and character name
local memberIndex = GetGuildMemberIndexFromDisplayName(guildId, displayName)
if memberIndex then
showMessage = false
end
end
end
end
end
end
-- Executed when EVENT_FRIEND_PLAYER_STATUS_CHANGED triggers
local function FormatFriendPlayerStatus(displayName, characterName, oldStatus, newStatus)
local wasOnline = oldStatus ~= PLAYER_STATUS_OFFLINE
local isOnline = newStatus ~= PLAYER_STATUS_OFFLINE
-- DisplayName is linkable
local displayNameLink = ZO_LinkHandler_CreateDisplayNameLink(displayName)
-- CharacterName is linkable
local characterNameLink = ZO_LinkHandler_CreateCharacterLink(characterName)
-- Not connected before and Connected now (no messages for Away/Busy)
if not wasOnline and isOnline then
NotifyFriend(displayName, zo_strformat(SI_FRIENDS_LIST_FRIEND_CHARACTER_LOGGED_ON, displayNameLink, characterNameLink), true)
-- Connected before and Offline now
elseif wasOnline and not isOnline then
NotifyFriend(displayName, zo_strformat(SI_FRIENDS_LIST_FRIEND_CHARACTER_LOGGED_OFF, displayNameLink, characterNameLink), false)
end
end
-- Format Message in tabs
local function DisplayNotif(message, guildId, guildName, account, displayNameLink, characterNameLink, newStatus)
local displayedMessage = db[guildName].customcolor .. zo_strformat(message, displayNameLink, characterNameLink) .. "|r"
if db.notifintab == 1 then
lcm:Print(displayedMessage)
elseif db.notifintab == 2 then
DisplayInTab(guildId, displayedMessage)
else
DisplayInTab(guildId, displayedMessage)
end
end
-- Runs whenever a guild member changes status
-- Display guild notification if needed
-- Strip my notification and multiGuilds too
local function OnGuildMemberPlayerStatusChanged(_, guildId, account, prevStatus, currStatus)
-- Not for me
if account ~= GetDisplayName() then
-- Get GuildName
local guildName = GetGuildName(guildId)
-- Occurs sometimes (still exists in One Tamriel)
if guildName and (guildName):len() > 0 then
-- If IsFriend and friendorguild, do not show
if IsFriend(account) and db.friendorguild == 1 then
return
elseif db[guildName].notify then
local wasOnline = prevStatus ~= PLAYER_STATUS_OFFLINE
local isOnline = currStatus ~= PLAYER_STATUS_OFFLINE
local memberIndex = GetGuildMemberIndexFromDisplayName(guildId, account)
local _, characterName = GetGuildMemberCharacterInfo(guildId, memberIndex)
-- DisplayName is linkable
local displayNameLink = ZO_LinkHandler_CreateDisplayNameLink(account)
-- CharacterName is linkable
local characterNameLink = ZO_LinkHandler_CreateCharacterLink(characterName)
-- Not connected before and Connected now (no messages for Away/Busy)
if(not wasOnline and isOnline and (isOnlineManager[account] == nil or isOnlineManager[account] == false)) then
DisplayNotif(SI_FRIENDS_LIST_FRIEND_CHARACTER_LOGGED_ON, guildId, guildName, account, displayNameLink, characterNameLink, true)
elseif(wasOnline and not isOnline and (isOnlineManager[account] == nil or isOnlineManager[account])) then
DisplayNotif(SI_FRIENDS_LIST_FRIEND_CHARACTER_LOGGED_OFF, guildId, guildName, account, displayNameLink, characterNameLink, false)
end
-- Update isOnlineManager to avoid multiguild message.
isOnlineManager[account] = isOnline
end
end
end
end
local function OnPlayerActivated()
-- TODO maybe rework all this thing
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_FRIEND_PLAYER_STATUS_CHANGED, FormatFriendPlayerStatus)
-- Unregisters
EVENT_MANAGER:UnregisterForEvent(ADDON_NAME, EVENT_PLAYER_ACTIVATED)
end
-- Build LAM Menu
local function BuildMenu()
-- Convert a colour from "|cABCDEF" form to [0,1] RGB form.
local function getColour(colourString)
local r=tonumber(string.sub(colourString, 3, 4), 16)
local g=tonumber(string.sub(colourString, 5, 6), 16)
local b=tonumber(string.sub(colourString, 7, 8), 16)
return r/255, g/255, b/255, 1
end
-- Used to reset colors to default value, lam need a formatted array
local function defaultColour(chanCode)
local r, g, b = GetChatCategoryColor(chanCode)
return {["r"] = r, ["g"] = g, ["b"] = b, ["a"] = 1}
end
-- Convert a decimal number in [0,255] to a hexadecimal number.
local function d2h(n)
local str = "0123456789abcdef"
local l = string.sub(str, math.floor(n/16)+1, math.floor(n/16)+1)
local r = string.sub(str, n%16 + 1, n%16 + 1)
return l..r
end
-- Turn a ([0,1])^3 RGB colour to "|cABCDEF" form.
local function makeColour(r, g, b)
r = math.floor(255*r)
g = math.floor(255*g)
b = math.floor(255*b)
return "|c"..d2h(r)..d2h(g)..d2h(b)
end
-- Start adding elements to control panel
local optionsTable = {}
-- Config per guild now
local guildindex = 0
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "header",
name = GetString(GNOTIF_OPTIONS_H),
width = "full",
}
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "dropdown",
name = GetString(GNOTIF_NOTIFINTAB),
tooltip = GetString(GNOTIF_NOTIFINTAB_TT),
choices = {GetString(GNOTIF_NOTIFINTAB1), GetString(GNOTIF_NOTIFINTAB2)},
getFunc = function()
if db.notifintab == 1 then
return GetString(GNOTIF_NOTIFINTAB1)
elseif db.notifintab == 2 then
return GetString(GNOTIF_NOTIFINTAB2)
else
return GetString(GNOTIF_NOTIFINTAB2)
end
end,
setFunc = function(choice)
if choice == GetString(GNOTIF_NOTIFINTAB1) then
db.notifintab = 1
elseif choice == GetString(GNOTIF_NOTIFINTAB2) then
db.notifintab = 2
else
db.notifintab = 2
end
end,
width = "full",
default = defaults.notifintab,
}
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "dropdown",
name = GetString(GNOTIF_FRIENDORGUILD),
tooltip = GetString(GNOTIF_FRIENDORGUILD_TT),
choices = {GetString(GNOTIF_FRIENDORGUILD1), GetString(GNOTIF_FRIENDORGUILD2)},
getFunc = function()
if db.friendorguild == 1 then
return GetString(GNOTIF_FRIENDORGUILD1)
elseif db.friendorguild == 2 then
return GetString(GNOTIF_FRIENDORGUILD2)
else
return GetString(GNOTIF_FRIENDORGUILD2)
end
end,
setFunc = function(choice)
if choice == GetString(GNOTIF_FRIENDORGUILD1) then
db.friendorguild = 1
elseif choice == GetString(GNOTIF_FRIENDORGUILD2) then
db.friendorguild = 2
else
db.friendorguild = 2
end
end,
width = "full",
default = defaults.friendorguild,
}
for guild = 1, GetNumGuilds() do
-- Guildname
local guildId = GetGuildId(guild)
local guildName = GetGuildName(guildId)
-- Occurs sometimes
if(not guildName or (guildName):len() < 1) then
guildName = "Guild " .. guild
end
local defaultcolor = defaultColour(guild+9)
-- 1st launch & New Guild
if not db[guildName] then
db[guildName] = {}
db[guildName].notify = true
local r, g, b = GetChatCategoryColor(guild+9)
db[guildName].customcolor = makeColour(r, g, b)
end
-- One submenu / guild
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "header",
name = guildName,
width = "full",
}
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "checkbox",
name = GetString(GNOTIF_NOTIFY),
tooltip = GetString(GNOTIF_NOTIFY_TT),
getFunc = function() return db[guildName].notify end,
setFunc = function(newValue) db[guildName].notify = newValue end,
width = "full",
default = true,
}
guildindex = guildindex + 1
optionsTable[guildindex] = {
type = "colorpicker",
name = GetString(GNOTIF_CUSTOMCOLOR),
tooltip = GetString(GNOTIF_CUSTOMCOLOR_TT),
getFunc = function() return getColour(db[guildName].customcolor) end,
setFunc = function(r, g, b) db[guildName].customcolor = makeColour(r, g, b) end,
default = defaultcolor,
disabled = function()
if not db[guildName].notify then
return true
else
return false
end
end,
}
end
lam2:RegisterOptionControls("GuildNotificatorNewOptions", optionsTable)
end
-- Runs whenever "me" join a new guild
local function NewGuild()
-- It will rebuild optionsTable and recreate tables
BuildMenu()
end
function gn.Initialize()
-- Create control panel
local panelData = {
type = "panel",
name = ADDON_NAME,
displayName = ZO_HIGHLIGHT_TEXT:Colorize(ADDON_NAME),
author = ADDON_AUTHOR,
version = ADDON_VERSION,
registerForRefresh = true,
registerForDefaults = true,
}
lam2:RegisterAddonPanel("GuildNotificatorNewOptions", panelData)
-- Build Menu, if reorganization trigger, it can display corrects values if LAM wasn't loaded before
BuildMenu()
-- TODO make this work again
-- Because ChatSystem is loaded after EVENT_ADD_ON_LOADED, we use EVENT_PLAYER_ACTIVATED
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_PLAYER_ACTIVATED, OnPlayerActivated)
-- Register OnGuildMemberPlayerStatusChanged with EVENT_GUILD_MEMBER_PLAYER_STATUS_CHANGED
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_GUILD_MEMBER_PLAYER_STATUS_CHANGED, OnGuildMemberPlayerStatusChanged)
-- To Rebuild LAM
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_GUILD_SELF_JOINED_GUILD, NewGuild)
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_GUILD_SELF_LEFT_GUILD, NewGuild)
end
-------------------------------------------------------------------------------------------------
-- Initialize --
-------------------------------------------------------------------------------------------------
function gn.OnAddOnLoaded(event, addonName)
-- Fetch the saved variables
db = ZO_SavedVars:NewAccountWide('GNOTIFICATOR', 1, nil, defaults)
if addonName == ADDON_NAME then
gn.Initialize()
EVENT_MANAGER:UnregisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED)
end
end
-------------------------------------------------------------------------------------------------
-- Register Events --
-------------------------------------------------------------------------------------------------
EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, gn.OnAddOnLoaded)