forked from sica42/GuildRecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandler.lua
More file actions
414 lines (375 loc) · 11.7 KB
/
MessageHandler.lua
File metadata and controls
414 lines (375 loc) · 11.7 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
GuildRecipes = GuildRecipes or {}
---@class GuildRecipes
local m = GuildRecipes
if m.MessageHandler then return end
---@type MessageCommand
local MessageCommand = {
RequestTradeskill = "RTS",
Tradeskill = "TS",
Ping = "PING",
Pong = "PONG",
VersionCheck = "VERSIONCHECK",
Version = "VERSION",
}
---@alias MessageCommand
---| "RTS"
---| "TS"
---| "PING"
---| "PONG"
---| "VERSIONCHECK"
---| "VERSION"
---@class MessageHandler
---@field send_tradeskill fun( tradeskill: string )
---@field request_tradeskill fun( tradeskill: string )
---@field request_tradeskills fun()
---@field version_check fun()
local M = {}
---@param ace_timer AceTimer
---@param ace_serializer AceSerializer
---@param ace_comm AceComm
function M.new( ace_timer, ace_serializer, ace_comm )
local pinging = false
local best_ping = {}
local refresh_summary_logged = false
local var_names = {
n = "name",
q = "quality",
c = "count",
p = "players",
d = "data",
i = "items",
ts = "tradeskill",
h = "hashes",
lu = "last_update",
--tlu = "tradeskills_last_update",
Alc = "Alchemy",
Bla = "Blacksmithing",
Eng = "Engineering",
Enc = "Enchanting",
Lea = "Leatherworking",
Tai = "Tailoring",
Jew = "Jewelcrafting",
}
setmetatable( var_names, { __index = function( _, key ) return key end } );
M.refresh_sources = {}
local function is_guild_member(name)
name = string.lower(string.gsub(name, "-.*", "")) -- normalize sender name
for i = 1, GetNumGuildMembers() do
local guild_name = GetGuildRosterInfo(i)
if guild_name then
guild_name = string.lower(string.gsub(guild_name, "-.*", ""))
if guild_name == name then
return true
end
end
end
return false
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
local function send_tradeskill( tradeskill )
m.debug( string.format( "Sending %s", tradeskill ) )
local data = {
tradeskill = tradeskill,
recipes = {}
}
for id, item in m.db.tradeskills[ tradeskill ] do
local players = {}
for _, p in pairs( m.comma_separated_to_table( item.p ) ) do
local name = m.db.players[ tonumber( p ) ]
if name and is_guild_member(name) then
table.insert( players, name )
end
end
if getn(players) > 0 then
table.insert( data.recipes, {
id = id,
p = players
} )
end
end
broadcast( MessageCommand.Tradeskill, data )
end
local function send_tradeskills()
for tradeskill in m.db.tradeskills do
send_tradeskill( tradeskill )
end
end
local function request_tradeskill( tradeskill )
pinging = true
best_ping = {}
broadcast( MessageCommand.Ping, {
[string.sub( tradeskill, 1, 3 )] = m.tradeskill_hash( tradeskill )
} )
end
local function request_tradeskills()
refresh_summary_logged = false
pinging = true
best_ping = {}
local hashes = {}
for tradeskill in pairs(m.TRADE_SKILL_LOCALIZATION) do
hashes[ string.sub( tradeskill, 1, 3 ) ] = m.tradeskill_hash( tradeskill )
end
if type(m.info) == "function" then
m.info("Refreshing tradeskill data from guild...", true)
end
broadcast( MessageCommand.Ping, hashes )
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.Tradeskill then
--
-- Receive tradeskill
--
local valid_recipes = {}
m.debug( string.format( "Receiving %s from %s.", data.tradeskill, sender ) )
if is_guild_member(sender) then
M.refresh_sources[sender] = true
M.updated_tradeskills = M.updated_tradeskills or {}
M.updated_tradeskills[sender] = M.updated_tradeskills[sender] or {}
table.insert(M.updated_tradeskills[sender], data.tradeskill)
for _, recipe in ipairs(data.recipes or {}) do
local has_valid_player = false
for _, name in ipairs(recipe.p or {}) do
if is_guild_member(name) then
has_valid_player = true
break
end
end
if has_valid_player then
table.insert(valid_recipes, recipe)
end
end
M.recipe_counts = M.recipe_counts or {}
M.recipe_counts[sender] = M.recipe_counts[sender] or {}
M.recipe_counts[sender][data.tradeskill] = getn(valid_recipes)
if M.expected_tradeskill_responses then
M.expected_tradeskill_responses[sender] = nil
end
end
if M.expected_tradeskill_responses and not next(M.expected_tradeskill_responses) and not refresh_summary_logged then
refresh_summary_logged = true
m.debug("All tradeskill responses received. Building summary...")
-- summary
local summary = {}
local has_valid_data = false
for sender, skills in pairs(M.updated_tradeskills or {}) do
local skill_summaries = {}
for _, skill in pairs(skills) do
local count = M.recipe_counts[sender] and M.recipe_counts[sender][skill] or 0
if count > 0 then
table.insert(skill_summaries, skill .. ": " .. count)
end
end
if getn(skill_summaries) > 0 then
has_valid_data = true
table.insert(summary, sender .. " (" .. table.concat(skill_summaries, ", ") .. ")")
end
end
if has_valid_data then
table.sort(summary)
m.info("Tradeskill refresh complete. Received updates from: " .. table.concat(summary, "; "), true)
else
m.info("Tradeskill refresh complete. No updates received — guild members responded but had no recipes to share.", true)
end
-- cleanup
M.refresh_sources = {}
M.updated_tradeskills = {}
M.expected_tradeskill_responses = nil
M.recipe_counts = {}
-- ✅ prune stale data even if no tradeskills came in
GuildRecipes.cleanup_orphaned_recipes()
end
local tradeskill = data.tradeskill
m.db.tradeskills[ tradeskill ] = m.db.tradeskills[ tradeskill ] or {}
if getn(valid_recipes) > 0 then
for _, v in valid_recipes do
if v.id then
---@type Item
local item = nil
if m.db.tradeskills[ tradeskill ][ v.id ] then
item = {
id = v.id,
name = m.db.tradeskills[ tradeskill ][ v.id ].n,
quality = m.db.tradeskills[ tradeskill ][ v.id ].q
}
else
if tradeskill == "Enchanting" then
if v and v.id then
local name = m.Enchants[ v.id ] and m.Enchants[ v.id ].name
if not name then
m.error( string.format( "Unknown enchantment received (%d)", v.id ) )
else
item = {
id = v.id,
name = name,
}
end
else
m.debug( "empty enchant data??" )
end
else
m.debug( "Fetching item info for " .. tostring( v.id ) )
m.get_item_info( v.id, function( item_info, players )
if item_info then
local i = {
id = item_info.id,
name = item_info.name,
quality = item_info.quality
}
local filtered_players = {}
for _, name in ipairs(players) do
if is_guild_member(name) then
table.insert(filtered_players, name)
end
end
if getn(filtered_players) > 0 then
m.update_tradeskill_item(tradeskill, i, filtered_players)
else
m.debug("Skipped recipe " .. tostring(i.id) .. " — no known guild members")
end
else
m.debug( "No item_info for " .. tostring( v.id ) )
end
end, v.players )
end
end
if item then
--m.debug( "got item" )
local filtered_players = {}
for _, name in ipairs(v.players) do
if is_guild_member(name) then
table.insert(filtered_players, name)
end
end
if getn(filtered_players) > 0 then
m.update_tradeskill_item(tradeskill, item, filtered_players)
else
m.debug("Skipped recipe " .. tostring(item.id) .. " — no known guild members")
end
end
end
end
else
m.info("No recipes received for tradeskill: " .. tradeskill, true)
end
m.db.tradeskills_last_update[ tradeskill ] = m.get_server_timestamp()
GuildRecipes.cleanup_orphaned_recipes()
elseif command == MessageCommand.RequestTradeskill and data.player == m.player then
--
-- Request for tradeskill
--
m.debug(m.dump(data))
send_tradeskill( data.tradeskill )
elseif command == MessageCommand.Ping and sender ~= m.player then
--
-- Recive ping
--
for tradeskill, hash in (data or {}) do
local local_hash = m.tradeskill_hash( tradeskill )
if local_hash == hash then
m.info(string.format("Player %s has same data for %s — skipping", sender, tradeskill))
data[ tradeskill ] = nil
else
data[ tradeskill ] = m.db.tradeskills_last_update[ tradeskill ] or 0
end
end
m.debug( m.dump( data ) )
broadcast( MessageCommand.Pong, data )
elseif command == MessageCommand.Pong and pinging then
--
-- Receive pong
--
m.debug( m.dump( data ) )
for tradeskill, last_update in pairs( data or {} ) do
if not best_ping[ tradeskill ] or (last_update > best_ping[ tradeskill ].last_update) then
best_ping[ tradeskill ] = {
player = sender,
last_update = last_update
}
end
end
if ace_timer:TimeLeft( M[ "ping_timer" ] ) == 0 then
M[ "ping_timer" ] = ace_timer.ScheduleTimer( M, function()
if pinging then
m.debug( "Ping timeout, requesting tradeskills from best pings." )
pinging = false
local requested_any = false
for tradeskill, ping_info in pairs(best_ping) do
m.debug(string.format("Requesting %s from %s (last update: %d)", tradeskill, ping_info.player, ping_info.last_update))
broadcast(MessageCommand.RequestTradeskill, {
player = ping_info.player,
ts = tradeskill
})
M.expected_tradeskill_responses = M.expected_tradeskill_responses or {}
M.expected_tradeskill_responses[ping_info.player] = true
requested_any = true
end
if not requested_any then
m.info("Tradeskill refresh complete. No guild members were able to respond at this time.", true)
m.info("Pruning stale recipe info", true)
GuildRecipes.cleanup_orphaned_recipes()
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_tradeskill = send_tradeskill,
request_tradeskill = request_tradeskill,
request_tradeskills = request_tradeskills,
version_check = version_check
}
end
m.MessageHandler = M
return M