-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandler.lua
More file actions
236 lines (198 loc) · 6.02 KB
/
MessageHandler.lua
File metadata and controls
236 lines (198 loc) · 6.02 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
GuildAlts = GuildAlts or {}
---@class GuildAlts
local m = GuildAlts
if m.MessageHandler then return end
---@type MessageCommand
local MessageCommand = {
SendCharacter = "SCHAR",
SendAlts = "SALTS",
RequestAlts = "RALTS",
Ping = "PING",
Pong = "PONG",
VersionCheck = "VERC",
Version = "VER"
}
---@alias MessageCommand
---| "SCHAR"
---| "SALTS"
---| "RALTS"
---| "PING"
---| "PONG"
---| "VERC"
---| "VER"
---@alias CharacterName string -- Max 12 characters (WoW limitation)
---@class MainCharacterEntry
---@field locked integer -- 1 if locked, 0 or nil if not
---@field alts CharacterName[] -- List of alt names
---@class AltMap
---@field [CharacterName] MainCharacterEntry
---@class AceSerializer
---@field Serialize fun( self: any, ... ): string
---@field Deserialize fun( self: any, str: string ): any
---@class AceComm
---@field RegisterComm fun( self: any, prefix: string, method: function? )
---@field SendCommMessage fun( self: any, prefix: string, text: string, distribution: string, target: string?, prio: "BULK"|"NORMAL"|"ALERT"?, callbackFn: function?, callbackArg: any? )
---@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 )
---@class MessageHandler
---@field send_character fun( character: AltMap )
---@field send_alts fun()
---@field request_alts fun()
---@field version_check fun()
local M = {}
function M.new()
---@diagnostic disable-next-line: undefined-global
local lib_stub = LibStub
---@type AceSerializer
local ace_serializer = lib_stub( "AceSerializer-3.0" )
---@type AceComm
local ace_comm = lib_stub( "AceComm-3.0" )
---@type AceTimer
local ace_timer = lib_stub( "AceTimer-3.0" )
local pinging = false
local best_ping = nil
local alts_sent = false
local var_names = {
lu = "last_update",
c = "count",
}
setmetatable( var_names, { __index = function( _, key ) return key end } );
---@param t table
local function decode( t )
local l = {}
for key, value in pairs( t ) do
if type( value ) == "table" then
value = decode( value )
end
l[ var_names[ key ] ] = value
end
return l
end
---@param command MessageCommand
---@param data table?
local function broadcast( command, data )
m.debug( string.format( "Broadcasting %s", command ) )
ace_comm:SendCommMessage( m.prefix, command .. "::" .. ace_serializer.Serialize( M, data ), "GUILD", nil, "NORMAL" )
end
---@param character AltMap
local function send_character( character )
broadcast( MessageCommand.SendCharacter, character )
end
local function send_alts()
broadcast( MessageCommand.SendAlts, m.db.characters )
end
local function request_alts()
pinging = true
alts_sent = false
best_ping = nil
broadcast( MessageCommand.Ping, {
c = m.count( m.db.characters )
} )
end
local function version_check()
broadcast( MessageCommand.VersionCheck )
end
---@param command string
---@param data table
---@param sender string
local function on_command( command, data, sender )
if command == MessageCommand.SendCharacter then
--
-- Receive single character
--
m.update_character( data )
elseif command == MessageCommand.RequestAlts and data.player == m.player then
--
-- Receive request for all alts
--
send_alts()
elseif command == MessageCommand.SendAlts then
--
-- Receive alts
--
for main, cdata in pairs( data ) do
m.db.characters[ main ] = cdata
end
m.db.last_update = m.get_server_timestamp()
m.build_alt_map()
elseif command == MessageCommand.Ping then
--
-- Recive ping
--
broadcast( MessageCommand.Pong, {
lu = m.db.last_update,
c = m.count( m.db.characters )
} )
elseif command == MessageCommand.Pong and pinging then
--
-- Receive pong
--
if not best_ping or (data and ((data.last_update and data.last_update > best_ping.last_update) or (data.count and data.count > best_ping.count))) then
best_ping = {
player = sender,
count = data.count or 0,
last_update = data.last_update or 0
}
end
if data and data.count < m.count( m.db.characters ) and not alts_sent then
alts_sent = true
send_alts()
end
if ace_timer:TimeLeft( M[ "ping_timer" ] ) == 0 then
M[ "ping_timer" ] = ace_timer.ScheduleTimer( M, function()
if pinging then
pinging = false
m.debug( "Fetching from " .. m.dump( best_ping ) )
if best_ping.count and best_ping.count ~= m.count( m.db.characters ) then
broadcast( MessageCommand.RequestAlts, { player = best_ping.player } )
else
m.debug( "Alt list is already up to date." )
end
end
end, 2 )
end
elseif command == MessageCommand.VersionCheck then
--
-- Receive version request
--
broadcast( MessageCommand.Version, { requester = sender, version = m.version, class = m.player_class } )
elseif command == MessageCommand.Version then
--
-- Receive version
--
if data.requester == m.player then
m.info( string.format( "%s [v%s]", m.colorize_player_by_class( sender, data.class ), data.version ), true )
end
end
end
local function on_comm_received( prefix, data_str, _, sender )
if prefix ~= m.prefix or sender == m.player then return end
local command = string.match( data_str, "^(.-)::" )
data_str = string.gsub( data_str, "^.-::", "" )
m.debug( "Received " .. command )
local success, data = ace_serializer.Deserialize( M, data_str )
if success then
if data then
data = decode( data )
end
on_command( command, data, sender )
else
m.error( "Corrupt data in addon message!" )
end
end
ace_comm.RegisterComm( M, m.prefix, on_comm_received )
---@type MessageHandler
return {
send_character = send_character,
send_alts = send_alts,
request_alts = request_alts,
version_check = version_check
}
end
m.MessageHandler = M
return M