-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRaidCalendar.lua
More file actions
273 lines (224 loc) · 7.54 KB
/
RaidCalendar.lua
File metadata and controls
273 lines (224 loc) · 7.54 KB
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
---@class RaidCalendar
RaidCalendar = RaidCalendar or {}
---@class RaidCalendar
local m = RaidCalendar
local lib_stub = LibStub
RaidCalendar.name = "RaidCalendar"
RaidCalendar.prefix = "RaidCal"
RaidCalendar.tagcolor = "FF7b1fa2"
RaidCalendar.events = {}
RaidCalendar.debug_enabled = false
RaidCalendar.api = getfenv()
---@alias NotAceTimer any
---@alias TimerId number
---@class AceTimer
---@field ScheduleTimer fun( self: NotAceTimer, callback: function, delay: number, ... ): TimerId
---@field ScheduleRepeatingTimer fun( self: NotAceTimer, callback: function, delay: number, arg: any ): TimerId
---@field CancelTimer fun( self: NotAceTimer, timer_id: number )
---@field TimeLeft fun( self: NotAceTimer, timer_id: number )
function RaidCalendar:init()
self.frame = CreateFrame( "Frame" )
self.frame:SetScript( "OnEvent", function()
if self.events[ event ] then
self.events[ event ]( self )
end
end )
for k, _ in pairs( m.events ) do
self.frame:RegisterEvent( k )
end
end
function RaidCalendar.events:ADDON_LOADED()
if arg1 ~= self.name then return end
---@type AceTimer
m.ace_timer = lib_stub( "AceTimer-3.0" )
m.player = UnitName( "player" )
m.player_class = UnitClass( "player" )
m.bot_online = false
RaidCalendarDB = RaidCalendarDB or {}
m.db = RaidCalendarDB
m.db.events = m.db.events or {}
m.db.user_settings = m.db.user_settings or {}
if m.db.user_settings.show_welcome_popup == nil then m.db.user_settings.show_welcome_popup = true end
m.db.user_settings.time_format = m.db.user_settings.time_format or "24"
m.db.user_settings.channel_access = m.db.user_settings.channel_access or {}
m.db.bots = m.db.bots or {}
m.db.popup_sr = m.db.popup_sr or {}
m.db.popup_event = m.db.popup_event or {}
m.db.popup_calendar = m.db.popup_calendar or {}
m.db.minimap_icon = m.db.minimap_icon or {}
m.time_format = m.db.user_settings.time_format == "24" and "%H:%M" or "%I:%M %p"
---@type MessageHandler
m.msg = m.MessageHandler.new()
---@type ChannelHandler
m.msg_channel = m.ChannelHandler.new()
---@type EventPopup
m.event_popup = m.EventPopup.new()
---@type CalendarPopup
m.calendar_popup = m.CalendarPopup.new()
---@type SRPopup
m.sr_popup = m.SRPopup.new()
---@type WelcomePopup
m.welcome_popup = m.WelcomePopup.new()
---@type MinimapIcon
m.minimap_icon = m.MinimapIcon.new()
if m.db.user_settings.sr_admins == nil then
m.debug("Get bot status")
m.msg.bot_status()
end
if m.api.IsAddOnLoaded( "pfUI" ) and m.api.pfUI and m.api.pfUI.api and m.api.pfUI.env and m.api.pfUI.env.C then
m.pfui_skin_enabled = true
m.api.pfUI:RegisterSkin( "RaidCalendar", "vanilla", function()
if m.api.pfUI.env.C.disabled and m.api.pfUI.env.C.disabled[ "skin_RaidCalendar" ] == "1" then
m.pfui_skin_enabled = false
end
end )
end
local orig_SetItemRef = SetItemRef
function SetItemRef( link, text, button, chatFrame )
local linkType, data = string.match( link, "^([^:]+):(.+)" )
if linkType == "raidcal" then
local type, id = string.match( data, "^(%w+):(.+)" )
if type == "event" then
m.event_popup.toggle( id )
elseif type == "sr" then
if tonumber( id ) == nil then
_, id = m.find( id, m.db.events, "srId" )
end
if tonumber( id ) then
m.sr_popup.toggle( id )
end
end
return
end
return orig_SetItemRef( link, text, button, chatFrame )
end
for i = 1, NUM_CHAT_WINDOWS do
local frame = self.api[ "ChatFrame" .. i ]
if frame then self.wrap_chat_frame( frame ) end
end
m.api[ "SLASH_RaidCalendar1" ] = "/rc"
m.api[ "SLASH_RaidCalendar2" ] = "/RaidCalendar"
SlashCmdList[ "RaidCalendar" ] = function( args )
if args == "debug" then
m.debug_enabled = not m.debug_enabled
m.info( "Debug is " .. (m.debug_enabled and "enabled" or "disabled") )
return
end
if args == "clear" then
m.info( "All events have been removed" )
m.db.events = {}
return
end
if args == "reset" then
m.info( "All settings have been reset" )
m.info( "Run \"/rc welcome\" to set up your bot again" )
m.db.events = {}
m.db.user_settings = {}
m.db.user_settings.show_welcome_popup = true
m.db.user_settings.time_format = "24"
m.db.user_settings.channel_access = {}
m.db.bots = m.db.bots or {}
m.db.popup_sr = m.db.popup_sr or {}
m.db.popup_event = m.db.popup_event or {}
m.db.popup_calendar = m.db.popup_calendar or {}
m.db.minimap_icon = m.db.minimap_icon or {}
m.msg.bot_status()
return
end
if args == "welcome" then
m.welcome_popup.show()
return
end
if args == "vc" then
m.msg.version_check( true )
return
end
if args == "refresh" then
m.msg.request_events()
return
end
m.calendar_popup.show()
end
m.msg_channel.join_channel()
m.version = GetAddOnMetadata( m.name, "Version" )
self.info( string.format( "(v%s) Loaded", m.version ) )
if m.db.user_settings.bot_name and m.db.user_settings.bot_name ~= "" or m.table_size( m.db.bots ) > 0 then
-- Refresh events if last update is older then 6h
if not m.db.user_settings.last_updated or time() - m.db.user_settings.last_updated > 3600 * 6 then
m.debug( "Fetching events..." )
m.get_events()
end
elseif m.db.user_settings.show_welcome_popup == true then
local delay = 4
m.debug( "Showing welcome popup in " .. delay .. " seconds..." )
self.frame:SetScript( "OnUpdate", function()
delay = delay - arg1
if delay > 0 then return end
m.welcome_popup.show()
self.frame:SetScript( "OnUpdate", nil )
end )
end
self.check_new_version()
end
function RaidCalendar.events:PLAYER_GUILD_UPDATE()
if not m.player_guild then
m.player_guild = GetGuildInfo( "player" )
end
end
function RaidCalendar.events.CHAT_MSG_CHANNEL()
m.msg_channel.on_msg( arg1, arg2, arg4 )
end
---@param frame Frame
function RaidCalendar.wrap_chat_frame( frame )
local original_add_message = frame[ "AddMessage" ]
local pattern = "(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):(%d%d)Z"
local bot_pattern = "player:" .. (m.db.user_settings.bot_name or "") .. ".*:%s(.*)"
frame[ "AddMessage" ] = function( self, msg, ... )
if msg then
-- Check for timestamp pattern
if string.find( msg, pattern ) then
local year, month, day, hour, minute, second = string.match( msg, pattern )
local timestamp = time( { year = year, month = month, day = day, hour = hour, min = minute, sec = second } )
local date_formatted = date( "%A", timestamp ) .. " " .. tonumber( date( "%d", timestamp ) ) .. ". " .. date( "%B", timestamp )
local time_formatted = date( m.time_format, timestamp )
msg = string.gsub( msg, pattern, date_formatted )
end
-- Check for bot commands
if string.find( msg, bot_pattern ) then
local command = string.match( msg, bot_pattern )
if command == "inv" then
return
end
end
end
return original_add_message( self, msg, unpack( arg ) )
end
end
function RaidCalendar.get_raid_members()
local members = {}
local num = GetNumRaidMembers()
for i = 1, num do
local name = GetRaidRosterInfo( i )
if name then
table.insert( members, string.upper( name ) )
end
end
return members
end
function RaidCalendar.get_events()
if m.db.user_settings.bot_name and m.db.user_settings.bot_name ~= "" then
m.debug( "Requesting events from guild bot: " .. m.db.user_settings.bot_name )
m.msg.request_events()
end
if m.table_size( m.db.bots ) > 0 then
m.debug( "Requesting events from channel bots" )
m.msg_channel.request_events( m.db.bots )
end
end
function RaidCalendar.check_new_version()
if not m.db.user_settings.last_versioncheck or time() - m.db.user_settings.last_versioncheck > 3600 * 24 then
m.msg.bot_status()
m.msg.version_check()
end
end
RaidCalendar:init()