Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
651 lines (519 loc) · 19.3 KB
/
Copy pathmain.lua
File metadata and controls
651 lines (519 loc) · 19.3 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
local api = require("api")
local enhanced_x_up = {
name = "Enhanced X UP",
version = "1.2",
author = "Yuck",
desc = "Raid recruitment with player blacklist."
}
-- CONSTANTS
local MSG_PREFIX_RECRUIT = "[Recruit] "
local MSG_PREFIX_BLACKLIST = "[Blacklist] "
local FILTER_EQUALS = 1
local FILTER_CONTAINS = 2
local FILTER_STARTS_WITH = 3
local CHAT_ALL = 1
local CHAT_WHISPERS = 2
local CHAT_GUILD = 3
local CHANNEL_WHISPER = -3
local CHANNEL_GUILD = 7
local NAME_CACHE_MAX_SIZE = 1000
-- STATE VARIABLES
local state = {
blocklist = {}, -- Ordered list for saving/UI
blocklist_lookup = {}, -- [OPTIMIZATION] Hash map for O(1) instant checking
recruit_message = "",
is_recruiting = false,
canvas_width = 0
}
-- Widget references
local widgets = {
recruit_button = nil,
recruit_textfield = nil,
filter_dropdown = nil,
dms_only = nil,
blacklist_button = nil,
recruit_canvas = nil,
cancel_button = nil,
raid_manager = nil,
blacklist_window = nil,
blacklist_input = nil,
blacklist_listbox = nil
}
-- Cache for lowercase names to avoid repeated string.lower() calls
local name_cache = {}
local name_cache_size = 0
-- UTILITY FUNCTIONS
local function GetLowerName(name)
if not name then return "" end
local cached = name_cache[name]
if cached then
return cached
end
if name_cache_size >= NAME_CACHE_MAX_SIZE then
name_cache = {}
name_cache_size = 0
end
local lower = string.lower(name)
name_cache[name] = lower
name_cache_size = name_cache_size + 1
return lower
end
local function ClearNameCache()
name_cache = {}
name_cache_size = 0
end
local function LogInfo(message)
api.Log:Info(message)
end
local function LogError(message)
api.Log:Err(message)
end
-- [OPTIMIZATION] Rebuild the hash map from the array list
local function RebuildLookup()
state.blocklist_lookup = {}
for i = 1, #state.blocklist do
local name = state.blocklist[i]
state.blocklist_lookup[GetLowerName(name)] = true
end
end
-- BLACKLIST CORE FUNCTIONS
local function SaveBlocklist()
local settings = api.GetSettings("enhanced_x_up")
settings.blocklist = state.blocklist
api.SaveSettings()
if #state.blocklist > 0 then
api.File:Write("enhanced_x_up/blacklist.lua", state.blocklist)
end
end
local function IsBlacklisted(name)
-- [OPTIMIZATION] Instant lookup
return state.blocklist_lookup[GetLowerName(name)] == true
end
local function AddToBlocklist(name)
if IsBlacklisted(name) then
return false, name .. " is already blacklisted."
end
-- Add to Array
state.blocklist[#state.blocklist + 1] = name
-- Add to Lookup
state.blocklist_lookup[GetLowerName(name)] = true
SaveBlocklist()
return true, "Added player: " .. name
end
local function RemoveFromBlocklist(name)
local lower_name = GetLowerName(name)
local blocklist = state.blocklist
for i = #blocklist, 1, -1 do
if GetLowerName(blocklist[i]) == lower_name then
table.remove(blocklist, i)
-- Remove from Lookup
state.blocklist_lookup[lower_name] = nil
SaveBlocklist()
ClearNameCache()
return true, "Removed: " .. name
end
end
return false, "Entry not found: " .. name
end
local function ClearBlocklist()
if #state.blocklist == 0 then
return false, "List is already empty."
end
state.blocklist = {}
state.blocklist_lookup = {}
SaveBlocklist()
ClearNameCache()
return true, "All entries cleared."
end
local function DestroyWidgetList(widget_list)
if not widget_list then return end
for i = 1, #widget_list do
if widget_list[i].label then
widget_list[i].label:ReleaseHandler("all")
widget_list[i].label:Show(false)
end
if widget_list[i].button then
widget_list[i].button:ReleaseHandler("all")
widget_list[i].button:Show(false)
end
end
end
local function UpdateBlacklistDisplay()
local frame = widgets.blacklist_listbox
if not frame then return end
DestroyWidgetList(frame.player_widgets)
frame.player_widgets = {}
local blocklist = state.blocklist
local count = #blocklist
if count == 0 then
local empty_label = frame:CreateChildWidget("label", "empty_msg", 0, true)
empty_label:SetText("(No entries)")
empty_label:SetExtent(380, 20)
empty_label:AddAnchor("TOPLEFT", frame, 0, 0)
ApplyTextColor(empty_label, FONT_COLOR.MIDDLE_TITLE)
empty_label:Show(true)
frame.player_widgets[1] = {label = empty_label}
return
end
local visible_count = math.min(count, 5)
for i = 1, visible_count do
local y_offset = (i - 1) * 35
local name_label = frame:CreateChildWidget("label", "player_" .. i, 0, true)
name_label:SetText(blocklist[i])
name_label:SetExtent(240, 25)
name_label:AddAnchor("TOPLEFT", frame, 5, y_offset + 5)
ApplyTextColor(name_label, FONT_COLOR.MIDDLE_TITLE)
name_label:Show(true)
local remove_btn = frame:CreateChildWidget("button", "remove_" .. i, 0, true)
remove_btn:SetText("Remove")
remove_btn:SetExtent(80, 25)
remove_btn:AddAnchor("LEFT", name_label, "RIGHT", 10, 0)
api.Interface:ApplyButtonSkin(remove_btn, BUTTON_BASIC.DEFAULT)
remove_btn.playerName = blocklist[i]
remove_btn:SetHandler("OnClick", function(self)
local success, message = RemoveFromBlocklist(self.playerName)
if success then
UpdateBlacklistDisplay()
LogInfo(MSG_PREFIX_BLACKLIST .. message)
else
LogError(MSG_PREFIX_BLACKLIST .. message)
end
end)
remove_btn:Show(true)
frame.player_widgets[i] = {label = name_label, button = remove_btn}
end
if count > 5 then
local more_label = frame:CreateChildWidget("label", "more_msg", 0, true)
more_label:SetText("... and " .. (count - 5) .. " more (use Export/Import)")
more_label:SetExtent(380, 20)
more_label:AddAnchor("TOPLEFT", frame, 5, 175)
ApplyTextColor(more_label, FONT_COLOR.MIDDLE_TITLE)
more_label:Show(true)
frame.player_widgets[visible_count + 1] = {label = more_label}
end
end
-- IMPORT/EXPORT FUNCTIONS
local function ImportBlocklist()
local imported_list = api.File:Read("enhanced_x_up/blacklist.lua")
if not imported_list or type(imported_list) ~= "table" then
return false, "Import failed. File not found or invalid."
end
local added_count = 0
local blocklist = state.blocklist
for i = 1, #imported_list do
local entry = imported_list[i]
if not IsBlacklisted(entry) then
blocklist[#blocklist + 1] = entry
state.blocklist_lookup[GetLowerName(entry)] = true
added_count = added_count + 1
end
end
if added_count > 0 then
SaveBlocklist()
ClearNameCache()
end
return true, "Imported " .. added_count .. " new entries."
end
local function ExportBlocklist()
if #state.blocklist == 0 then
return false, "Nothing to export."
end
api.File:Write("enhanced_x_up/blacklist.lua", state.blocklist)
return true, "Exported " .. #state.blocklist .. " entries to enhanced_x_up/blacklist.lua"
end
-- RECRUITMENT CONTROL
local function StartRecruiting()
local msg = widgets.recruit_textfield:GetText()
if not msg or msg == "" then
LogError(MSG_PREFIX_RECRUIT .. "Please enter a recruit message.")
return false
end
state.is_recruiting = true
-- Store recruit message in lower case for insensitive matching
state.recruit_message = GetLowerName(msg)
widgets.recruit_button:SetText("Stop Recruiting")
widgets.recruit_textfield:Enable(false)
widgets.recruit_canvas:Show(true)
LogInfo(MSG_PREFIX_RECRUIT .. "Now recruiting for: " .. msg)
return true
end
local function StopRecruiting()
state.is_recruiting = false
state.recruit_message = ""
widgets.recruit_button:SetText("Start Recruiting")
widgets.recruit_textfield:Enable(true)
widgets.recruit_canvas:Show(false)
LogInfo(MSG_PREFIX_RECRUIT .. "Stopped recruiting.")
end
local function ToggleRecruiting()
if state.is_recruiting then
StopRecruiting()
else
StartRecruiting()
end
end
-- BLACKLIST WINDOW CREATION
local function CreateBlacklistWindow()
local window = api.Interface:CreateWindow("blacklistWindow", "Blacklist Manager", 0, 0)
window:SetExtent(420, 460)
window:AddAnchor("CENTER", "UIParent", 0, 0)
window:Show(false)
widgets.blacklist_window = window
local instructions = window:CreateChildWidget("label", "instructions", 0, true)
instructions:SetText("Add players to prevent raid invites")
instructions:SetExtent(380, 20)
instructions:AddAnchor("TOPLEFT", window, 20, 50)
ApplyTextColor(instructions, FONT_COLOR.MIDDLE_TITLE)
local player_label = window:CreateChildWidget("label", "player_label", 0, true)
player_label:SetText("Player Name:")
player_label:SetExtent(100, 20)
player_label:AddAnchor("TOP", window, 0, 80)
ApplyTextColor(player_label, FONT_COLOR.MIDDLE_TITLE)
local input = W_CTRL.CreateEdit("blacklist_input", window)
input:SetExtent(200, 30)
input:AddAnchor("CENTER", window, -55, -110)
input:SetMaxTextLength(32)
input:Show(true)
widgets.blacklist_input = input
local add_button = window:CreateChildWidget("button", "add_player_button", 0, true)
add_button:SetText("Add Player")
add_button:SetExtent(100, 30)
add_button:AddAnchor("LEFT", input, "RIGHT", 10, 0)
api.Interface:ApplyButtonSkin(add_button, BUTTON_BASIC.DEFAULT)
add_button:SetHandler("OnClick", function()
local name = input:GetText()
if not name or name == "" then
LogError(MSG_PREFIX_BLACKLIST .. "Please enter a player name.")
return
end
local success, message = AddToBlocklist(name)
if success then
UpdateBlacklistDisplay()
input:SetText("")
LogInfo(MSG_PREFIX_BLACKLIST .. message)
else
LogError(MSG_PREFIX_BLACKLIST .. message)
end
end)
local list_label = window:CreateChildWidget("label", "list_label", 0, true)
list_label:SetText("Current Blacklist:")
list_label:SetExtent(300, 20)
list_label:AddAnchor("TOPLEFT", window, 20, 150)
ApplyTextColor(list_label, FONT_COLOR.MIDDLE_TITLE)
local list_frame = window:CreateChildWidget("emptywidget", "list_frame", 0, true)
list_frame:SetExtent(380, 180)
list_frame:AddAnchor("TOPLEFT", window, 20, 175)
list_frame:Show(true)
widgets.blacklist_listbox = list_frame
local clear_button = window:CreateChildWidget("button", "clear_button", 0, true)
clear_button:SetText("Clear All")
clear_button:SetExtent(90, 30)
clear_button:AddAnchor("TOPLEFT", window, 65, 390)
api.Interface:ApplyButtonSkin(clear_button, BUTTON_BASIC.DEFAULT)
clear_button:SetHandler("OnClick", function()
local success, message = ClearBlocklist()
if success then
UpdateBlacklistDisplay()
end
if success then
LogInfo(MSG_PREFIX_BLACKLIST .. message)
else
LogError(MSG_PREFIX_BLACKLIST .. message)
end
end)
local export_button = window:CreateChildWidget("button", "export_button", 0, true)
export_button:SetText("Export")
export_button:SetExtent(90, 30)
export_button:AddAnchor("LEFT", clear_button, "RIGHT", 10, 0)
api.Interface:ApplyButtonSkin(export_button, BUTTON_BASIC.DEFAULT)
export_button:SetHandler("OnClick", function()
local success, message = ExportBlocklist()
if success then
LogInfo(MSG_PREFIX_BLACKLIST .. message)
else
LogError(MSG_PREFIX_BLACKLIST .. message)
end
end)
local import_button = window:CreateChildWidget("button", "import_button", 0, true)
import_button:SetText("Import")
import_button:SetExtent(90, 30)
import_button:AddAnchor("LEFT", export_button, "RIGHT", 10, 0)
api.Interface:ApplyButtonSkin(import_button, BUTTON_BASIC.DEFAULT)
import_button:SetHandler("OnClick", function()
local success, message = ImportBlocklist()
if success then
UpdateBlacklistDisplay()
end
if success then
LogInfo(MSG_PREFIX_BLACKLIST .. message)
else
LogError(MSG_PREFIX_BLACKLIST .. message)
end
end)
UpdateBlacklistDisplay()
end
local function ToggleBlacklistWindow()
if not widgets.blacklist_window then
CreateBlacklistWindow()
end
local window = widgets.blacklist_window
window:Show(not window:IsVisible())
end
-- CHAT MESSAGE HANDLING
local function OnChatMessage(channelId, speakerId, _, speakerName, message)
if not state.is_recruiting or not speakerName or state.recruit_message == "" then
return
end
-- Normalize message to lowercase for case-insensitive matching
local normalized_message = GetLowerName(message)
local recruit_message = state.recruit_message -- Already lowercase
-- Filter check
local filter_selection = widgets.filter_dropdown.selctedIndex
local message_matches = false
if filter_selection == FILTER_EQUALS then
message_matches = (normalized_message == recruit_message)
elseif filter_selection == FILTER_CONTAINS then
-- string.find with 'true' for 4th arg prevents regex magic characters, checks for literal string
-- Since both inputs are lowercase, this is case-insensitive
message_matches = (string.find(normalized_message, recruit_message, 1, true) ~= nil)
elseif filter_selection == FILTER_STARTS_WITH then
message_matches = (string.sub(normalized_message, 1, #recruit_message) == recruit_message)
end
if not message_matches then
return
end
-- Check Blacklist (Optimized O(1))
if IsBlacklisted(speakerName) then
return
end
-- Chat source check
local recruit_method = widgets.dms_only.selctedIndex
local should_invite = false
if recruit_method == CHAT_ALL then
should_invite = true
elseif recruit_method == CHAT_WHISPERS and channelId == CHANNEL_WHISPER then
should_invite = true
elseif recruit_method == CHAT_GUILD and channelId == CHANNEL_GUILD then
should_invite = true
end
if should_invite then
-- Check if already in team to avoid spam
local existingMemberIndex = api.Team:GetMemberIndexByName(speakerName)
if not existingMemberIndex then
LogInfo(MSG_PREFIX_RECRUIT .. "Inviting " .. speakerName)
api.Team:InviteToTeam(speakerName, false)
end
end
end
-- DRAG HANDLERS
local function OnCanvasDragStart(self)
if api.Input:IsShiftKeyDown() then
self:StartMoving()
api.Cursor:ClearCursor()
api.Cursor:SetCursorImage(CURSOR_PATH.MOVE, 0, 0)
end
end
local function OnCanvasDragStop(self)
self:StopMovingOrSizing()
api.Cursor:ClearCursor()
end
-- INITIALIZATION
local function OnLoad()
local settings = api.GetSettings("enhanced_x_up")
if not settings.blocklist then
settings.blocklist = {}
settings.hide_cancel = false
api.SaveSettings()
end
state.blocklist = settings.blocklist
RebuildLookup()
-- Auto-import
local success, message = ImportBlocklist()
if success and message ~= "Imported 0 new entries." then
LogInfo(MSG_PREFIX_BLACKLIST .. "Auto-imported entries from file.")
end
-- Recruit canvas
local canvas = api.Interface:CreateEmptyWindow("recruitWindow")
canvas:AddAnchor("CENTER", "UIParent", 0, 50)
canvas:SetExtent(200, 100)
canvas:Show(false)
widgets.recruit_canvas = canvas
local bg = canvas:CreateNinePartDrawable(TEXTURE_PATH.HUD, "background")
bg:SetTextureInfo("bg_quest")
bg:SetColor(0, 0, 0, 0.5)
bg:AddAnchor("TOPLEFT", canvas, 0, 0)
bg:AddAnchor("BOTTOMRIGHT", canvas, 0, 0)
local cancel_button = canvas:CreateChildWidget("button", "cancel_x", 0, true)
cancel_button:SetText("Stop Recruiting")
cancel_button:AddAnchor("TOPLEFT", canvas, "TOPLEFT", 37, 34)
api.Interface:ApplyButtonSkin(cancel_button, BUTTON_BASIC.DEFAULT)
cancel_button:SetHandler("OnClick", StopRecruiting)
widgets.cancel_button = cancel_button
canvas:SetHandler("OnDragStart", OnCanvasDragStart)
canvas:SetHandler("OnDragStop", OnCanvasDragStop)
canvas:EnableDrag(true)
-- Raid Manager UI Hook
local raid_manager = ADDON:GetContent(UIC.RAID_MANAGER)
state.canvas_width = raid_manager:GetWidth()
raid_manager:SetExtent(state.canvas_width, 510)
widgets.raid_manager = raid_manager
local recruit_button = raid_manager:CreateChildWidget("button", "raid_setup_button", 0, true)
recruit_button:SetExtent(105, 30)
recruit_button:AddAnchor("LEFT", raid_manager, 20, 140)
recruit_button:SetText("Start Recruiting")
api.Interface:ApplyButtonSkin(recruit_button, BUTTON_BASIC.DEFAULT)
recruit_button:SetHandler("OnClick", ToggleRecruiting)
widgets.recruit_button = recruit_button
local textfield = W_CTRL.CreateEdit("recruit_message", raid_manager)
textfield:AddAnchor("LEFT", raid_manager, 131, 140)
textfield:SetExtent(150, 30)
textfield:SetMaxTextLength(64)
textfield:CreateGuideText("X CR")
textfield:Show(true)
widgets.recruit_textfield = textfield
local filter = api.Interface:CreateComboBox(raid_manager)
filter:SetExtent(100, 30)
filter:AddAnchor("LEFT", raid_manager, 285, 140)
filter.dropdownItem = {"Equals", "Contains", "Starts With"}
-- Default selected index set to 2 (Contains) per request
filter:Select(2)
filter:Show(true)
widgets.filter_dropdown = filter
local dms = api.Interface:CreateComboBox(raid_manager)
dms:SetExtent(100, 30)
dms:AddAnchor("LEFT", raid_manager, 390, 140)
dms.dropdownItem = {"All Chats", "Whispers", "Guild"}
dms:Select(1)
dms:Show(true)
widgets.dms_only = dms
local blacklist_button = raid_manager:CreateChildWidget("button", "blacklist_button", 0, true)
blacklist_button:SetExtent(150, 30)
blacklist_button:AddAnchor("LEFT", raid_manager, 20, 180)
blacklist_button:SetText("Manage Blacklist")
api.Interface:ApplyButtonSkin(blacklist_button, BUTTON_BASIC.DEFAULT)
blacklist_button:SetHandler("OnClick", ToggleBlacklistWindow)
widgets.blacklist_button = blacklist_button
api.On("CHAT_MESSAGE", OnChatMessage)
end
local function OnUnload()
api.Off("CHAT_MESSAGE", OnChatMessage)
if widgets.recruit_button then
widgets.recruit_button:Show(false)
widgets.recruit_textfield:Show(false)
widgets.raid_manager:SetExtent(state.canvas_width, 395)
widgets.recruit_canvas:Show(false)
widgets.filter_dropdown:Show(false)
widgets.dms_only:Show(false)
widgets.blacklist_button:Show(false)
end
if widgets.blacklist_window then
if widgets.blacklist_listbox and widgets.blacklist_listbox.player_widgets then
DestroyWidgetList(widgets.blacklist_listbox.player_widgets)
end
widgets.blacklist_window:Show(false)
end
ClearNameCache()
end
enhanced_x_up.OnLoad = OnLoad
enhanced_x_up.OnUnload = OnUnload
return enhanced_x_up