-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuild_Recruitment_Helper.lua
1282 lines (1078 loc) · 31.4 KB
/
Guild_Recruitment_Helper.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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Guild Recruitment Helper
-- Help advertise recruiting information to trade and lfg channels
--
-- Author: janike
-- Version: 3.4
--
-- Thanks to
-- Isilorn for guild cooperation code and concept, and for frFR localization
--
--
-- debug mode variable
local debug = false
--@alpha@
debug = true
--@end-alpha@
-- use addon revision for non alpha builds (for update notification feature)
local addon_revision = 0
--@non-alpha@
addon_revision = @project-revision@
--@end-non-alpha@
GRH = LibStub("AceAddon-3.0"):NewAddon("GRH", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "AceComm-3.0", "AceSerializer-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("GRH", true)
local LibC = LibStub:GetLibrary("LibCompress")
local LibCE = LibC:GetAddonEncodeTable()
--
-- Library functions
--
-- safe cut utf8 string
local function utf8cut(value, len)
if type(value) == "string" and #value > 0 and len >= 1 then
if #value > len then
if value:byte(len) >= 192 then -- UTF-8 multibyte first byte
value = value:sub(1, len - 1)
elseif value:byte(len - 1) >= 224 then -- UTF-8 triplebyte first byte
value = value:sub(1, len - 2)
else
value = value:sub(1, len)
end
end
end
return value
end
-- get server time in unix timestamp format
local function GetServerTime()
return C_DateAndTime.GetServerTimeLocal()
end
--
-- Addon configuration
--
-- update config revision for guild sync
local function UpdateConfRevision(changed)
local gametime = GetServerTime()
if changed and GRH.db.global.sync_revision ~= gametime then
if debug then
GRH:Printf("DEBUG: config revision changed from '%s' to '%s'", tostring(GRH.db.global.sync_revision), tostring(gametime))
end
GRH.db.global.sync_revision = gametime
end
end
-- get option value
local function GetGlobalOptionLocal(info)
return GRH.db.global[info[#info]]
end
-- set option value
local function SetGlobalOptionLocal(info, value)
if debug and GRH.db.global[info[#info]] ~= value then
GRH:Printf("DEBUG: global option %s changed from '%s' to '%s'", info[#info], tostring(GRH.db.global[info[#info]]), tostring(value))
end
GRH.db.global[info[#info]] = value
end
-- set versioned option value (for guild sync)
local function SetGlobalOptionVersioned(info, value)
-- update revision if needed
UpdateConfRevision(GRH.db.global[info[#info]] ~= value)
-- set option
SetGlobalOptionLocal(info, value)
-- reschedule timer on versioned option change to sync successfully
GRH:SetTimer()
end
-- format message (substitude some tags in text like $glevel and $gname)
local function FormatMessage(text)
local msg = text
-- replace $gname with guild name
if IsInGuild() then
local gname = GetGuildInfo("player")
if gname then
msg = string.gsub(msg, "$gname", gname)
end
end
return msg
end
-- update message
local function SetMessage(info, value)
if not value then
self.db.global.message = ""
return
end
-- split message
local msg = { strsplit("\n", strtrim(value) .. "\n") }
-- TODO: ugly code, split to functions
-- only 2 lines allowed of 255 bytes each
-- prepare to cut those lines
local l1 = strtrim(msg[1])
local l2 = strtrim(msg[2])
-- compare line size before/afrer formatting
local l1_before = #l1
local l1_after = #FormatMessage(l1)
local l1_diff = max(0, l1_after - l1_before)
local l2_before = #l2
local l2_after = #FormatMessage(l2)
local l2_diff = max(0, l2_after - l2_before)
-- cut lines to fit formatting
l1 = utf8cut(strtrim(l1), 255 - l1_diff)
l2 = utf8cut(strtrim(l2), 255 - l2_diff)
-- put lines back together
msg = strjoin("\n", l1, l2)
if debug and GRH.db.global[info[#info]] ~= msg then
GRH:Printf("DEBUG: global option %s changed. L1_diff: %d L2_diff: %d", info[#info], l1_diff, l2_diff)
end
-- update config
SetGlobalOptionVersioned(info, msg)
end
-- declare defaults to be used in the DB
local defaults = {
realm = {
enabled = false,
},
global = {
test_mode = false,
message = "",
timer = 5,
min_lines = 100,
announce_general = false,
announce_trade = false,
announce_lfg = true,
guild_coop = true,
guild_sync = true,
sync_revision = 0,
sync_key = "",
set_dnd = false,
dnd_message = "",
}
}
-- declare config interface
local options = {
name = "Guild Recruitment",
handler = GRH,
type = "group",
childGroups = "tab",
args = {
general_tab = {
name = "General",
type = "group",
order = 10,
args = {
enabled = {
type = "toggle",
order = 11,
name = L["Enable Addon"],
desc = L["Enable or disable addon functionality."],
width = "full",
get = function ()
return GRH.db.realm.enabled
end,
set = function (info, value)
GRH.db.realm.enabled = value
GRH:SetTimer()
end,
},
description = {
type = "description",
order = 12,
fontSize = "medium",
name = L["addon_description"],
},
debug_options = {
type = "header",
order = 13,
name = L["Debug options"],
},
test_mode = {
type = "toggle",
order = 14,
name = L["Test mode"],
desc = L["Doesn't send anything to channels, simulating only."],
descStyle = "inline",
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
},
},
message_tab = {
name = "Message",
type = "group",
order = 20,
args = {
message = {
type = "input",
order = 21,
name = L["Message to announce, 2 lines max"],
desc = L["Type the message to announce, every line will be announced. Message should not be longer than 2 lines."],
multiline = 7,
width = "full",
get = GetGlobalOptionLocal,
set = SetMessage,
},
message_help = {
type = "description",
order = 22,
name = L["You can use $gname tag in text. This will be replaced by your guild name."],
},
settings = {
type = "header",
order = 23,
name = L["Announce settings"],
},
timer = {
type = "range",
order = 24,
name = L["Announce interval, minutes"],
desc = L["How often announce your message to channel."],
min = 3,
max = 30,
step = 1,
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
min_lines = {
type = "range",
order = 25,
name = L["SPAM protection, lines"],
desc = L["This add-on will try to prevent spamming to idle channels. If there is no activity in a channel, then it waits a little longer. Specify how many lines should pass before add-on is allowed to announce message."],
min = 0,
max = 100,
step = 10,
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
channels = {
type = "header",
order = 26,
name = L["Select channels"],
},
announce_general = {
type = "toggle",
order = 27,
name = L["Announce to General"],
desc = L["Announce your message to General channel when out of a city."],
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
announce_trade = {
type = "toggle",
order = 28,
name = L["Announce to Trade"],
desc = L["Announce your message to city trade channel."],
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
announce_lfg = {
type = "toggle",
order = 29,
name = L["Announce to LFG"],
desc = L["Announce your message to LFG channel."],
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionVersioned,
},
},
},
guild_tab = {
name = "Guild Mode",
type = "group",
order = 30,
args = {
guild_coop = {
type = "toggle",
order = 31,
name = L["Guild cooperation mode"],
desc = L["Doesn't allow multiple announces from your guild members at the same time."],
descStyle = "inline",
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionLocal,
},
guild_sync = {
type = "toggle",
order = 32,
name = L["Allow guild synchronization"],
desc = L["This will synchronize message and settings between guild members. Latter version of a message will be used by all guild members with synchronization enabled."],
descStyle = "inline",
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionLocal,
},
sync_key_help = {
type = "description",
order = 33,
name = L["SYNC_KEY_HELP"],
},
sync_key = {
type = "input",
order = 34,
name = L["Synchronization key"],
desc = L["Use this key to separate sync settings for different groups of peoples. Leave empty for default."],
width = "full",
get = GetGlobalOptionLocal,
set = SetGlobalOptionLocal,
},
},
},
status_tab = {
name = "Status",
type = "group",
order = 50,
args = {
set_dnd = {
type = "toggle",
order = 51,
name = L["Set DND"],
desc = L["Always set your DND status while announcing to channels"],
descStyle = "inline",
width = "full",
get = GetGlobalOptionLocal,
set = function (info, value)
SetGlobalOptionLocal(info, value)
if not value then GRH:SetDND(true) end -- clear dnd
end,
},
dnd_msg = {
type = "input",
order = 52,
name = L["Set DND message"],
desc = L["Type DND message there."],
width = "full",
get = GetGlobalOptionLocal,
set = function (info, value)
SetGlobalOptionLocal(info, strtrim(value))
GRH:SetDND(true) -- clear dnd
end,
},
},
},
},
}
--
-- Addon initialization
--
-- Code that you want to run when the addon is first loaded goes here.
function GRH:OnInitialize()
-- initialize saved variables
self.db = LibStub("AceDB-3.0"):New("GRHDB", defaults, true)
-- setup variables
self:InitVars()
-- initialize configuration options
LibStub("AceConfig-3.0"):RegisterOptionsTable("GRH", options)
self.configFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("GRH", "Guild Recruitment");
-- create LibDataBroker
self.ldb = LibStub("LibDataBroker-1.1"):NewDataObject("Guild Recruitment", {
type = "data source",
text = "",
label = "",
icon = "Interface\\Icons\\INV_Shirt_GuildTabard_01",
OnClick = function (frame, button)
if button == "LeftButton" then
-- update config
GRH.db.realm.enabled = not GRH.db.realm.enabled
LibStub("AceConfigRegistry-3.0"):NotifyChange("GRH")
-- reschedule timer
GRH:SetTimer()
else
-- RightButton: options menu
InterfaceOptionsFrame_OpenToCategory(self.configFrame)
end
end,
OnTooltipShow = function(tooltip)
tooltip:AddLine("Guild Recruitment")
tooltip:AddLine(" ")
tooltip:AddLine(L["Left-click to toggle ON/OFF"], 0, 1, 0)
tooltip:AddLine(L["Right-click to open the configuration"], 0, 1, 0)
end,
})
end
-- Called when the addon is enabled
function GRH:OnEnable()
-- register events
self:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE", "EventChannelNotice")
self:RegisterEvent("CHAT_MSG_CHANNEL", "EventChannelMsg")
-- register comm events
self:RegisterComm("GRH", "EventComm")
-- register console commands
self:RegisterChatCommand("grh", "ConsoleCommand")
self:RegisterChatCommand("grec", "ConsoleCommand")
self:RegisterChatCommand("gr", "ConsoleCommand")
-- schedule timer
self:SetTimer()
-- init profile variables
self:InitProfileVars()
end
-- Called when the addon is disabled
function GRH:OnDisable()
-- unregister events
self:UnregisterAllEvents()
-- unregister comm events
self:UnregisterAllComm()
-- unregister console commands
self:UnregisterChatCommand("GRH")
self:UnregisterChatCommand("grec")
self:UnregisterChatCommand("gr")
-- cancel timers
self:CancelAllTimers()
end
-- Initalize local variables
function GRH:InitVars()
-- general, trade and lfg channels id
self.general_id = nil
self.trade_id = nil
self.lfg_id = nil
-- channel msg counters
self.trade_cnt = 0
self.lfg_cnt = 0
-- skip counter
self.skip_cnt = 0
-- guild coop mode, last advertised player name
self.guild_coop_by = nil
-- guild sync, last advertised config revision
self.sync_revision_sent = 0
-- dnd set trigger (fix reload)
self.dnd_set = self.db.global.set_dnd
-- upgrade version notification
self.version_notice = false
-- timer handle
self.timer = nil
self.timer_ldb = nil
end
-- Initalize profile variables
function GRH:InitProfileVars()
-- sanitize sync data
if not self.db.global.sync_revision or self.db.global.sync_revision > GetServerTime() then
self.db.global.sync_revision = 0
end
if not self.db.global.sync_key then
self.db.global.sync_key = ""
end
end
-- Setup announce timer
function GRH:SetTimer(drift)
if debug then
self:Printf("DEBUG: SetTimer called with %d drift", drift)
end
-- set ldb text
self.ldb.text = L["Off"]
self.ldb.label = "GRH"
-- cancel running timer if any
if self.timer then
self:CancelTimer(self.timer, false)
self.timer = nil
end
if self.timer_ldb then
self:CancelTimer(self.timer_ldb, false)
self.timer_ldb = nil
end
-- schedule new timer
if self.db.realm.enabled then
-- should put clients out of synchronization
local timer_drift = 0
if drift then
timer_drift = random(drift)
end
self.timer = self:ScheduleTimer("TimerAnnounce", self.db.global.timer * 60 + timer_drift)
-- periodic ldb text updates
self.timer_ldb = self:ScheduleRepeatingTimer("LDBTimer", 1)
end
end
-- LDB timer handler
function GRH:LDBTimer()
-- update ldb text
if not self.timer then
self.ldb.text = L["Off"]
return
end
-- calculate time (seconds) difference between now and target time
local time_left = self:TimeLeft(self.timer)
if time_left == nil then
self.ldb.text = L["Off"]
return
end
-- err what?
if time_left < 0 then
if debug then
self:Print("DEBUG: LDB timer difference is negative!")
end
self.ldb.text = L["Off"]
return
end
local time_min = time_left / 60
local time_sec = time_left % 60
-- handle
if not time_min or not time_sec then
self.ldb.text = L["On"]
return
end
-- more than a minute
if time_min >= 1 then
self.ldb.text = format(L["On (%d min)"], ceil(time_min))
return
else
self.ldb.text = format(L["On (%d sec)"], time_sec)
return
end
end
-- set DND status if requested
function GRH:SetDND(clear)
-- clear DND status
if clear and self.dnd_set and UnitIsDND("player") then
if debug then
self:Print("DEBUG: DND mode cleared")
end
self.dnd_set = false
SendChatMessage("", "DND")
return
end
-- set DND if required
if clear or not self.db.global.set_dnd or not self.db.global.dnd_msg then
return
end
if not UnitIsDND("player") then
if debug then
self:Print("DEBUG: DND mode set")
end
self.dnd_set = true
SendChatMessage(self.db.global.dnd_msg, "DND")
end
end
-- check for data available and addon running
function GRH:IsDataReady()
return self.db.realm.enabled and self.db.global.message and strtrim(self.db.global.message) and (self.db.global.announce_general or self.db.global.announce_trade or self.db.global.announce_lfg)
end
-- get configuration revision number and a key as a string
function GRH:GetConfRevStr()
local revision = tostring(self.db.global.guild_sync and self.db.global.sync_revision or "sync_disabled")
local key = self.db.global.sync_key
if #key > 0 then
return format("%s|%s", revision, key)
else
return revision
end
end
-- offer configuration to guild or player
function GRH:OfferConf(dest)
if not dest or not self:IsDataReady() then
if debug then
self:Print("DEBUG: Offering configuration cancelled. Addon disabled or no data ready.")
end
return
end
-- select destination
local dist, target
if dest == "GUILD" then
dist, target = "GUILD", nil
else
dist, target = "WHISPER", dest
end
if debug then
self:Printf("DEBUG: Offering configuration to: %s (%s)", tostring(dist), tostring(target))
end
-- prepare an array of configs
-- it is possible to define several versions of the config with different abi's
-- and app should decide which version is suitable.
-- abi is data structure specification if you like. as we can't handle data we arn't aware of
-- we could add more data to the end, but we should not alter existing data specification
-- on which previous versions of application relies. this is basic principe of backward compatibility.
local conf = {
-- define abi in descending order (later version on the top). app will use first compatible version
-- only one version so far
{
abi = 1, -- abi version (integer)
sync_revision = self.db.global.sync_revision, -- data revision
test_mode = self.db.global.test_mode,
message = self.db.global.message,
timer = self.db.global.timer,
min_lines = self.db.global.min_lines,
announce_general = self.db.global.announce_general,
announce_trade = self.db.global.announce_trade,
announce_lfg = self.db.global.announce_lfg,
},
}
-- compress and encode data
conf = self:Serialize(conf)
conf = LibC:Compress(conf)
conf = LibCE:Encode(conf)
-- send config
local msg = format("ConfigOffer %s %d %s", self:GetConfRevStr(), addon_revision, conf, "BULK")
self:SendCommMessage("GRH", msg, dist, target)
end
-- request configuration from guild or player
function GRH:RequestConf(dest)
if not dest then
return
end
-- select destination
local dist, target
if dest == "GUILD" then
dist, target = "GUILD", nil
else
dist, target = "WHISPER", dest
end
if debug then
self:Printf("DEBUG: Requesting configuration from: %s (%s)", tostring(dist), tostring(target))
end
-- send request
local msg = format("ConfigRequest %s", self:GetConfRevStr())
self:SendCommMessage("GRH", msg, dist, target)
end
-- validate and accept configuration
function GRH:AcceptConf(text)
if debug then
self:Print("DEBUG: Accepting configuration...")
end
if type(text) ~= "string" then
if debug then
self:Print("DEBUG: Serialized data expected.")
end
return false
end
-- decode and decompress
local data, err_msg
data, err_msg = LibC:Decompress(LibCE:Decode(text))
if not data then
if debug then
self:Print("DEBUG: Decompression failed! " .. err_msg)
end
return false
end
if debug then
self:Printf("DEBUG: Data decompressed. Compressed size: %d. Decompressed size: %d.", #text, #data)
end
-- deserialize
local success, conf_list = self:Deserialize(data)
if not success or type(conf_list) ~= "table" then
if debug then
self:Print("DEBUG: Deserialization failed! table expected.")
end
return false
end
-- search for proper abi
local abi = 1
local config
for i, v in ipairs(conf_list) do
if debug then
self:Printf("DEBUG: Considering data at index:%d abi:%s", i, tostring(type(v) == "table" and v.abi or nil))
end
if type(v) == "table" and v.abi == abi then
config = v
break
end
end
-- validate basic minimum for abi
if type(config.sync_revision) ~= "number" or type(config.message) ~= "string" then
if debug then
self:Print("DEBUG: data validation failed")
end
return false
end
-- and once again, should we update?
if config.sync_revision <= self.db.global.sync_revision then
if debug then
self:Print("DEBUG: Err. config revision is older than ours!")
end
return false
end
-- accepting base configuration
SetGlobalOptionLocal({ "message" }, config.message)
-- store new revision
SetGlobalOptionLocal({ "sync_revision" }, config.sync_revision)
self.sync_revision_sent = config.sync_revision
-- optional abi configuration
if type(config.test_mode) == "boolean" then
SetGlobalOptionLocal({ "test_mode" }, config.test_mode)
end
if type(config.timer) == "number" then
SetGlobalOptionLocal({ "timer" }, config.timer)
end
if type(config.min_lines) == "number" then
SetGlobalOptionLocal({ "min_lines" }, config.min_lines)
end
if type(config.announce_general) == "boolean" then
SetGlobalOptionLocal({ "announce_general" }, config.announce_general)
end
if type(config.announce_trade) == "boolean" then
SetGlobalOptionLocal({ "announce_trade" }, config.announce_trade)
end
if type(config.announce_lfg) == "boolean" then
SetGlobalOptionLocal({ "announce_lfg" }, config.announce_lfg)
end
-- update config interface
LibStub("AceConfigRegistry-3.0"):NotifyChange("GRH")
-- reschedule timer with drift
GRH:SetTimer(30)
if debug then
self:Printf("DEBUG: Configuration accepted! Revision: %d", config.sync_revision)
end
return true
end
--
-- Event Handlers
--
-- Fired when you enter or leave a chat channel
function GRH:EventChannelNotice(event, arg1, _, _, _, _, _, arg7, arg8)
-- arg7 channel type (1 - General, 2 - Trade, 26 - LFG)
-- arg8 channel number
if debug then
self:Printf("DEBUG: EventChannelNotice - Event: '%s' Type: '%s' Channel: '%s'", tostring(arg1), tostring(arg7), tostring(arg8))
end
-- sanity check
if not arg1 or not arg7 or not arg8 then
if debug then
self:Print("DEBUG: EventChannelNotice - argument missing")
end
return
end
-- General channel
if arg7 == 1 then
if arg1 == "YOU_JOINED" or arg1 == "YOU_CHANGED" then
if debug then
GRH:Print("DEBUG: joined General channel.")
end
self.general_id = arg8
else
if debug then
GRH:Print("DEBUG: left General channel.")
end
self.general_id = nil
end
end
-- Trade channel
if arg7 == 2 then
if arg1 == "YOU_JOINED" then
if debug then
GRH:Print("DEBUG: joined Trade channel.")
end
self.trade_id = arg8
else
if debug then
GRH:Print("DEBUG: left Trade channel.")
end
self.trade_id = nil
end
end
-- LFG channel
if arg7 == 26 then
if arg1 == "YOU_JOINED" then
if debug then
GRH:Print("DEBUG: joined LFG channel.")
end
self.lfg_id = arg8
else
if debug then
GRH:Print("DEBUG: left LFG channel.")
end
self.lfg_id = nil
end
end
-- clear DND out of city
if arg1 == "SUSPENDED" and (arg7 == 2 or arg7 == 26) and self.db.global.set_dnd then
self:SetDND(true)
end
end
-- Fired when the client receives a channel message.
function GRH:EventChannelMsg(event, _, _, _, _, _, _, arg7, arg8)
-- sanity check
if not arg7 or not arg8 then
if debug then
self:Printf("DEBUG: EventChannelMsg - argument missing. Type: '%s' Channel: '%s'", tostring(arg7), tostring(arg8))
end
return
end
-- fix reload: rejoin channels if message received on that channel
if arg7 == 1 then
if debug and not self.general_id then
GRH:Print("DEBUG: joined General channel.")
end
self.general_id = arg8
end
if arg7 == 2 then
if debug and not self.trade_id then
GRH:Print("DEBUG: joined Trade channel.")
end
self.trade_id = arg8
end
if arg7 == 26 then
if debug and not self.lfg_id then
GRH:Print("DEBUG: joined LFG channel.")
end
self.lfg_id = arg8
end
-- count trade messages
if self.trade_id and arg8 == self.trade_id then
self.trade_cnt = self.trade_cnt + 1
end
-- count lfg messages
if self.lfg_id and arg8 == self.lfg_id then
self.lfg_cnt = self.lfg_cnt + 1
end
end
-- Announce timer handler
function GRH:TimerAnnounce()
if debug then
self:Print("DEBUG: Time to announce something!")
end
-- enabled but no data/channels?!
if not self:IsDataReady() then
self:Print(L["Addon enabled, but no message or channels selected."])
return
end
-- check channel availability and possibility to announce
local can_trade = self.trade_id and self.db.global.announce_trade and (self.trade_cnt >= self.db.global.min_lines or self.skip_cnt >= 2)
local can_lfg = self.lfg_id and self.db.global.announce_lfg and (self.lfg_cnt >= self.db.global.min_lines or self.skip_cnt >= 2)
local can_general = self.general_id and self.db.global.announce_general and (not (self.trade_id and self.db.global.announce_trade) and not (self.lfg_id and self.db.global.announce_lfg)) -- use general only outside of a city
-- guild sync check
if IsInGuild() and self.db.global.guild_sync and self.sync_revision_sent < self.db.global.sync_revision then
self:OfferConf("GUILD")
self.sync_revision_sent = self.db.global.sync_revision
end
-- guild coop check
if self.guild_coop_by then
self:Printf(L["Skipping. Message already advertised by %s"], self.guild_coop_by)
self.guild_coop_by = nil
self.trade_cnt = 0
self.lfg_cnt = 0
self.skip_cnt = 0
return
end
-- determine most active channel if both available
if can_trade and can_lfg then
if self.trade_cnt > self.lfg_cnt then
can_lfg = false
else
can_trade = false
end
end
-- announce to trade channel