-
Notifications
You must be signed in to change notification settings - Fork 1
/
FADEWorldTimers.lua
296 lines (249 loc) · 9.04 KB
/
FADEWorldTimers.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
local AddonName, FADEWT = ...
local HBDP = LibStub("HereBeDragons-Pins-2.0")
local HBD = LibStub("HereBeDragons-2.0")
local AceTimer = LibStub("AceTimer-3.0")
local Serializer = LibStub("AceSerializer-3.0")
local Comm = LibStub("AceComm-3.0")
------ INITIALIZE ADDON CORE
-----------------------
FADEWT.WorldTimers = {}
FADEWT.MessageCallbacks = {}
FADEWT.COMMKEY = "FADEWT-5" -- CHANGE BACK TO 4
FADEWT.LastEventAt = GetServerTime() - 60
FADEWT.LastYellAt = GetServerTime() - 380
FADEWT.InitTime = GetTime()
FADEWT.RealmName = GetRealmName()
FADEWT.Faction, _ = UnitFactionGroup("player")
FADEWT.VERSION = 130
FADEWT.VERSIONCHECK = 0
-- Initializes our addon
function FADEWT:Init()
-- Create an empty DB if need be
FADEWT:SetupDB()
local Frame = CreateFrame("Frame", nil, UIParent)
-- Update the active timers every second
AceTimer:ScheduleRepeatingTimer(FADEWT.Tick, 1)
-- Every 300 seconds try and send message
-- Should keep timers fresh
AceTimer:ScheduleRepeatingTimer(FADEWT.SendMessage, 300)
-- Register event on UNIT_AURA so we can check if player got timer
Frame:RegisterEvent("UNIT_AURA")
Frame:RegisterEvent("CHAT_MSG_LOOT")
Frame:RegisterEvent("CHAT_MSG_MONSTER_YELL")
Frame:RegisterEvent("PLAYER_ENTERING_WORLD")
Frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
Frame:SetScript("OnEvent", FADEWT.HandleEvent)
Comm:RegisterComm(FADEWT.COMMKEY, FADEWT.HandleMessage)
for _,Timer in ipairs(FADEWT.WorldTimers) do
if Timer.Init ~= nil then
Timer:Init()
end
end
-- Save current version so we can use it to clear timers from old versions when you update
FADEWTConfig.Version = FADEWT.VERSION
end
SLASH_FADEWTCMD1 = '/fade';
function SlashCmdList.FADEWTCMD(cmd, editBox)
if cmd == "print" then
FADEWT.SendReport()
else
print("Welcome to FADE World Timers")
print("Currently we have timers for:")
print("Onyxia, Nefarian, Songflower, WCB, Whipper Root Tubers")
print("----------")
print("Available Commands:")
print("print - Prints a list of all active timers and the time left")
end
end
-- Fires on every event
-- Todo: Add every needed event here
function FADEWT:HandleEvent(event, ...)
if event == "UNIT_AURA" then
local unit = ...
FADEWT:OnUnitAura(self, unit)
end
if event == "CHAT_MSG_LOOT" then
FADEWT:OnChatMsgLoot(self, ...)
end
if event == "CHAT_MSG_MONSTER_YELL" then
FADEWT:OnChatMsgMonsterYell(self, ...)
end
-- Broadcast message when entering world
if event == "PLAYER_ENTERING_WORLD" then
FADEWT:SendMessage()
end
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
FADEWT:OnUnfilteredCombatLogEvent(self, ...)
end
end
function FADEWT:OnChatMsgMonsterYell(self, ...)
local msg, npc = ...
for _, Timer in ipairs(FADEWT.WorldTimers) do
if Timer.OnMsgMonsterYell ~= nil then
Timer:OnMsgMonsterYell(npc)
end
end
end
-- Called when loot is received
function FADEWT:OnChatMsgLoot(self, ...)
for _,Timer in ipairs(FADEWT.WorldTimers) do
if Timer.OnChatMsgLoot ~= nil then
Timer.OnChatMsgLoot(...)
end
end
end
-- Runs our init function when ready
function FADEWT:Initialize()
local f = CreateFrame("FRAME")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("onEvent", function(self, event, addon)
if event == "ADDON_LOADED" and addon == AddonName then
FADEWT:Init()
end
end)
end
-- This fires every second
-- Is meant to update the timers
-- No need to do it more often, would only cause lag issues
function FADEWT:Tick()
for _, Timer in ipairs(FADEWT.WorldTimers) do
if Timer.Tick ~= nil then
Timer:Tick()
end
end
end
-- Can check when certain combat log events happen
function FADEWT:OnUnfilteredCombatLogEvent(event)
for _, Timer in ipairs(FADEWT.WorldTimers) do
if Timer.OnUnfilteredCombatLogEvent ~= nil then
Timer.OnUnfilteredCombatLogEvent(event, CombatLogGetCurrentEventInfo())
end
end
end
-- Function to print a report of all timers currectly active
function FADEWT.SendReport()
for _, Timer in ipairs(FADEWT.WorldTimers) do
if Timer.SendReport ~= nil then
Timer.SendReport()
end
end
end
-- Fires the OnUnitAura on every WorldTimer that has the function when UNIT_AURA event is fired
function FADEWT:OnUnitAura(self, unit)
for _,Timer in ipairs(FADEWT.WorldTimers) do
if Timer.OnUnitAura ~= nil then
Timer:OnUnitAura(unit)
end
end
end
-- Function for timers to use to register their message handler
function FADEWT:RegisterMessageHandler(key, fn)
FADEWT.MessageCallbacks[key] = fn
end
-- Handle COMM Messages, send it to the correct message handler, handler needs to be registered to be sent
function FADEWT:HandleMessage(message, distribution, sender)
local ok, decodedMessage = Serializer:Deserialize(message);
if not ok or not decodedMessage then return false end
if sender == UnitName("player") then return false end
-- Validate if sender is from the same realm as us. Don't continue if not.
local charName, realm = strsplit("-", sender, 2)
if realm ~= nil then
FADEWT.Debug("Message from: ", charName, realm)
return false
end
if decodedMessage["version"] ~= nil then
local version = decodedMessage["version"]
if (version > FADEWT.VERSION) and (GetServerTime() > (FADEWT.VERSIONCHECK + 12000)) then
print("|cFFD13300[FADE World Timers] Your version is out of date - Please download the newest version on Curseforge or through the Twitch Client")
print("|cFFD13300Using an older version, you will not be able to share your timers with newer versions of the addon.")
FADEWT.VERSIONCHECK = GetServerTime()
end
-- Don't receive updated timers from people with older versions of the addon.
if (version < FADEWT.VERSION) then
FADEWT.Debug("Stopped a message from ", sender, " with version: ", version)
return false
end
end
for key,timers in pairs(decodedMessage) do
if FADEWT.MessageCallbacks[key] ~= nil then
FADEWT.MessageCallbacks[key](timers, distribution, sender)
end
end
end
function FADEWT:ConvertTimestampToHumanReadable()
return nil
end
-- Debug message
function FADEWT.Debug(...)
if FADEWTConfig.Debug == true then
print("FADEWT", ...)
end
end
-- Sends data from each timer in an aggregated manner, not more than once every 10 seconds
-- Avoids spamming the chat and getting errors because of it
-- Timer needs function GetMessageData before the object is sent
function FADEWT:SendMessage(force)
if ((GetServerTime() - FADEWT.LastEventAt) <= 60) and (force ~= true) then return end
FADEWT.LastEventAt = GetServerTime()
local messageData = {}
messageData["version"] = FADEWT.VERSION
-- Loop through every timer and get the data they want to send
for _,Timer in ipairs(FADEWT.WorldTimers) do
if Timer.GetMessageData ~= nil then
local key, data = Timer:GetMessageData()
messageData[key] = data
end
end
local serializedMessageData = Serializer:Serialize(messageData)
FADEWT.Debug("Broadcasting timers")
-- TODO: Enable when I'm sure what causes the bugs
if FADEWTConfig.YellDisabled ~= true and (GetServerTime() - FADEWT.LastYellAt) >= 380 then
Comm:SendCommMessage(FADEWT.COMMKEY , serializedMessageData, "YELL", nil, "BULK");
FADEWT.LastYellAt = GetServerTime()
end
if (IsInRaid() and not IsInGroup(LE_PARTY_CATEGORY_INSTANCE)) then
-- Comm:SendCommMessage(FADEWT.COMMKEY , serializedMessageData, "RAID");
end
if (GetGuildInfo("player") ~= nil) then
Comm:SendCommMessage(FADEWT.COMMKEY , serializedMessageData, "GUILD", nil, "BULK");
end
end
-- Returns active timers for a given timer
function FADEWT:GetTimers()
local timers = {}
for _,Timer in ipairs(FADEWT.WorldTimers) do
if Timer.GetTimers ~= nil then
local key, cds = Timer.GetTimers()
timers[key] = cds
end
end
--print(dump(timers))
return timers
end
-- Sets up SavedVariabled for all registered timers
-- Also sets up our config object
function FADEWT:SetupDB()
if FADEWTConfig == nil then
FADEWTConfig = {}
FADEWTConfig.OnyxiaHidden = false
FADEWTConfig.WCBHidden = false
FADEWTConfig.SongflowerHidden = false
FADEWTConfig.YellDisabled = false
FADEWTConfig.Debug = false
FADEWTConfig.Version = FADEWT.VERSION
end
if FADEWTConfig.Version == nil then
FADEWTConfig.Version = FADEWT.VERSION - 1
end
for _, Timer in ipairs(FADEWT.WorldTimers) do
if Timer.SetupDB ~= nil then
Timer:SetupDB()
end
end
end
-- Run our initialize script
FADEWT:Initialize()
-- Set our addon object as global
_G["FADEWT"] = FADEWT
------ END INITIALIZE ADDON CORE
---------------------------