Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
823 lines (718 loc) · 27.7 KB
/
Copy pathCore.lua
File metadata and controls
823 lines (718 loc) · 27.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
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
-- UnBot Core - Vanilla 1.12
-- 1.12 rules that shaped this file:
-- * script handlers receive no arguments; use globals `this`, `event`, `arg1`
-- * no `#` (use table.getn), no `%` (use math.mod), no `...` varargs
-- The addon is called Retinue.
Retinue = Retinue or {}
retinue = Retinue
UnBot = Retinue
Retinue.addonName = Retinue.addonName or "Retinue"
Retinue.version = Retinue.version or "1.0"
Retinue.groupButtons = Retinue.groupButtons or {}
Retinue.subButtons = Retinue.subButtons or {}
Retinue.handlers = Retinue.handlers or {}
-- Backwards compatibility aliases
UnBot = Retinue
retinue = Retinue
function UnBot:Message(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff00cccc"..msg.."|r")
end
-- Validates whether a character name is legitimate, filtering out "Unknown", "-", etc.
local function IsValidCharacterName(name)
if not name or type(name) ~= "string" then return false end
name = string.gsub(name, "^%s+", "")
name = string.gsub(name, "%s+$", "")
if name == "" or name == "-" or name == "nil" then return false end
local lower = string.lower(name)
if lower == "unknown" or lower == "unknown entity" or lower == "unknown being" then return false end
if UNKNOWNOBJECT and string.lower(UNKNOWNOBJECT) == lower then return false end
if UNKNOWNBEING and string.lower(UNKNOWNBEING) == lower then return false end
if string.len(name) < 2 then return false end
if string.find(name, "[^%a%d_]") then return false end
return true
end
UnBot.IsValidCharacterName = IsValidCharacterName
setglobal("IsValidCharacterName", IsValidCharacterName)
IsValidCharacterName = IsValidCharacterName
function UnBot:Init()
if self.inited then return end
self.inited = true
if RetinueConfig == nil then
RetinueConfig = UnBotConfig or {}
end
self.config = RetinueConfig
UnBotConfig = RetinueConfig
self.capturing = self.config.capturing
self:RegisterSlashCommands()
-- Data.lua and IconsFilename.lua carry every command table. If a file was
-- added to the .toc but the client has not been restarted, they are absent.
if UnBotGroupDefault == nil or UnBotExecuteCommand == nil
or UnBotCommandIcon == nil then
DEFAULT_CHAT_FRAME:AddMessage(
"|cffff0000Retinue: command data missing. Restart the game client "..
"completely - /reload does not pick up newly added files.|r")
return
end
if self.InitRoster then self:InitRoster() end
if self.InitItemEvents then self:InitItemEvents() end
if self.InitSpellEvents then self:InitSpellEvents() end
if self.InitUI then self:InitUI() end
self:Message(self.addonName.." "..self.version.." loaded. Use /retinue")
end
function UnBot:RegisterSlashCommands()
SLASH_RETINUE1 = "/retinue"
SLASH_RETINUE2 = "/ret"
SLASH_RETINUE3 = "/rtn"
SLASH_RETINUE4 = "/warband"
SLASH_RETINUE5 = "/wb"
SLASH_RETINUE6 = "/unbot"
SlashCmdList["RETINUE"] = function(msg)
msg = string.lower(msg or "")
-- some clients hand over a leading space, which would make every
-- comparison below miss
msg = string.gsub(msg, "^%s+", "")
msg = string.gsub(msg, "%s+$", "")
-- split off a first word so "ask actions" works
local _, _, verb, rest = string.find(msg, "^(%S*)%s*(.*)$")
if verb == nil then verb = msg end
if rest == nil then rest = "" end
if verb == "ask" then
UnBot:AskBotHelp(rest)
return
elseif verb == "invite" then
UnBot:TestInvite(rest)
return
elseif verb == "uninvite" or verb == "kick" then
if rest ~= "" and UnBot.UninviteBot then
UnBot:UninviteBot(rest)
elseif UnBot.RemoveSelectedFromGroup then
UnBot:RemoveSelectedFromGroup()
end
return
elseif verb == "remove" or verb == "delete" then
if rest ~= "" and UnBot.RemoveRosterCharacter then
UnBot:RemoveRosterCharacter(rest)
elseif UnBot.DeleteSelectedRoster then
UnBot:DeleteSelectedRoster()
end
return
elseif verb == "checkapi" then
UnBot:CheckAPI()
return
elseif verb == "iteminfo" then
UnBot:DumpItemInfo(rest)
return
elseif verb == "fill" or verb == "fillraid" then
if type(UnBot.FillWith) ~= "function" then
UnBot:Message("FillWith is missing - Roster.lua did not load. "..
"Restart the client completely.")
return
end
if verb == "fill" then UnBot:FillWith(5) else UnBot:FillWith(40) end
return
end
if msg == "roster" or msg == "bots" then
UnBot:ToggleRoster()
elseif msg == "sync" then
UnBot:RequestServerRoster(true)
elseif msg == "strategy" or msg == "strat" then
UnBot:ShowStrategyWindow()
elseif msg == "behavior" or msg == "behaviour" then
UnBot:ShowStrategyWindow("behavior")
elseif msg == "spells" then
UnBot:ShowSpellWindow()
elseif msg == "bags" then
UnBot:ShowItemWindow("view")
elseif msg == "catalog" or msg == "itemus" or msg == "spawn" then
UnBot:ToggleCatalogWindow()
elseif msg == "capture" then
UnBot:ToggleCapture()
elseif msg == "capture clear" then
UnBot.config.capture = {}
UnBot:Message("Capture log cleared")
elseif msg == "debug" then
UnBot.debug = not UnBot.debug
if UnBot.debug then
UnBot:Message("Debug on - chat messages will be echoed with their event name")
else
UnBot:Message("Debug off")
end
elseif msg == "help" then
UnBot:Message("/retinue - show or hide the command bar")
UnBot:Message("Everything else has a button on the bar; these are")
UnBot:Message("just shortcuts for the same windows:")
UnBot:Message(" roster, sync, strategy, spells, bags, catalog, fill, fillraid")
UnBot:Message("Diagnostics: capture, debug, checkapi, ask all")
else
UnBot:ToggleUI()
end
end
SlashCmdList["UNBOT"] = SlashCmdList["RETINUE"]
SLASH_RETINUECOM1 = "/retcom"
SLASH_RETINUECOM2 = "/rc"
SLASH_RETINUECOM3 = "/unbotcom"
SLASH_RETINUECOM4 = "/ubc"
SlashCmdList["RETINUECOM"] = function(msg)
local index = tonumber(msg)
if index then UnBot:RunCommand(index) end
end
SlashCmdList["UNBOTCOM"] = SlashCmdList["RETINUECOM"]
end
function UnBot:ToggleUI()
if not UnBotFrame then return end
if UnBotFrame:IsShown() then
self:CloseAll()
else
UnBotFrame:Show()
end
end
function UnBot:CloseAll()
if self.HideAllSubFrames then self:HideAllSubFrames() end
if self.HideActions then
self:HideActions()
elseif UnBotActionsFrame then
UnBotActionsFrame:Hide()
end
if UnBotCatalogFrame then UnBotCatalogFrame:Hide() end
if UnBotFrame then UnBotFrame:Hide() end
self:Message(UnBotL.MSG.CLOSE_BAR)
end
--=========================================================================--
-- Window positions
--
-- Every window is draggable, so remember where it was put. Frames anchor to
-- UIParent on restore, which is what StartMoving leaves them attached to.
--=========================================================================--
function UnBot:SaveFramePosition(frame)
if not frame or not frame:GetName() then return end
local point, _, relPoint, x, y = frame:GetPoint()
if not point then return end
if self.config.positions == nil then self.config.positions = {} end
self.config.positions[frame:GetName()] = {
point = point, relPoint = relPoint, x = x, y = y,
}
end
function UnBot:RestoreFramePosition(frame)
if not frame or not self.config.positions then return false end
local pos = self.config.positions[frame:GetName()]
if not pos then return false end
frame:ClearAllPoints()
frame:SetPoint(pos.point, UIParent, pos.relPoint, pos.x, pos.y)
return true
end
-- Wire dragging plus position saving onto a frame in one go.
function UnBot:MakeDraggable(frame)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", function() this:StartMoving() end)
frame:SetScript("OnDragStop", function()
this:StopMovingOrSizing()
UnBot:SaveFramePosition(this)
end)
frame:SetScript("OnHide", function()
if type(UnBot.ClearMainButtonHighlights) == "function" then
UnBot:ClearMainButtonHighlights()
end
end)
end
--=========================================================================--
-- Command lookup helpers
--=========================================================================--
function UnBot:GetIconPath(commandId)
local rti = self:GetRaidTargetIndex(commandId)
if rti then
return "Interface\\TargetingFrame\\UI-RaidTargetingIcons"
end
return UnBotCommandIcon[commandId] or "Interface\\Icons\\INV_Misc_QuestionMark"
end
function UnBot:GetRaidTargetIndex(commandId)
if UnBotCommandRaidTarget then
return UnBotCommandRaidTarget[commandId]
end
return nil
end
function UnBot:ApplyButtonIcon(buttonOrTexture, commandId)
if not buttonOrTexture then return end
local tex = buttonOrTexture.icon or buttonOrTexture
if not tex or not tex.SetTexture then return end
local rtiIndex = self:GetRaidTargetIndex(commandId)
if rtiIndex and rtiIndex >= 1 and rtiIndex <= 8 then
tex:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
local left = math.mod(rtiIndex - 1, 4) * 0.25
local right = left + 0.25
local top = math.floor((rtiIndex - 1) / 4) * 0.25
local bottom = top + 0.25
tex:SetTexCoord(left, right, top, bottom)
else
tex:SetTexture(self:GetIconPath(commandId))
tex:SetTexCoord(0, 1, 0, 1)
end
end
function UnBot:GetTitle(commandId)
return UnBotL.TITLE[commandId] or ("Command "..tostring(commandId))
end
-- A command is usable if it either has chat text or a special handler.
function UnBot:IsCommandDefined(commandId)
if self.handlers[commandId] then return true end
local text = UnBotExecuteCommand[commandId]
return text ~= nil and text ~= ""
end
-- Register a special handler that replaces plain chat sending.
function UnBot:RegisterHandler(commandId, func)
self.handlers[commandId] = func
end
--=========================================================================--
-- Target validation
--=========================================================================--
function UnBot:GetGroupedTarget()
local name = UnitName("target")
if not name or name == "" or not IsValidCharacterName(name) then
self:Message(UnBotL.MSG.NO_TARGET)
return nil
end
if not UnitInParty("target") and not UnitInRaid("target") then
self:Message(UnBotL.MSG.NOT_IN_PARTY)
return nil
end
return name
end
function UnBot:GetBotTarget()
local name = self:GetGroupedTarget()
if not name then return nil end
if not UnitIsPlayer("target") then
self:Message(UnBotL.MSG.NEED_PLAYER_BOT)
return nil
end
return name
end
--=========================================================================--
-- Execution
--=========================================================================--
function UnBot:RunCommand(commandId)
if commandId == nil then return end
local handler = self.handlers[commandId]
if handler then
handler(commandId)
return
end
local text = UnBotExecuteCommand[commandId]
if text == nil or text == "" then
self:Message(string.format(UnBotL.MSG.CMD_EMPTY, tostring(commandId)))
return
end
local cmdType = UnBotCommandType[commandId]
if cmdType == nil then
self:Message(string.format(UnBotL.MSG.CMD_NOT_FOUND, tostring(commandId)))
return
end
-- Route .bot commands through the quiet TBM transport when available
if string.find(text, "^%.bot%s+") then
local sub = string.gsub(text, "^%.bot%s+", "")
self:SendBotCommand(sub)
return
end
if cmdType == 1 then
SendChatMessage(text, "SAY")
elseif cmdType == 2 then
local target = self:GetGroupedTarget()
if not target then return end
SendChatMessage(text, "WHISPER", nil, target)
elseif cmdType == 3 then
local name = UnitName("target")
if not name or name == "" then
self:Message(UnBotL.MSG.NO_TARGET)
return
end
SendChatMessage(text, "PARTY")
elseif cmdType == 4 then
SendChatMessage(text, "PARTY")
else
self:Message(string.format(UnBotL.MSG.CMD_NOT_FOUND, tostring(commandId)))
end
end
--=========================================================================--
-- Quiet TBM Transport & Protocol Dispatcher
--=========================================================================--
UnBot.addonTransport = "none"
UnBot.serverCaps = {}
function UnBot:AddonCommandChannel()
if self.addonTransport == "party" and (GetNumPartyMembers() > 0 or GetNumRaidMembers() > 0) then
return (self.C and self.C.ADDON_TRANSPORT) or "PARTY"
end
return nil
end
function UnBot:SetAddonTransport(transport)
transport = string.lower(transport or "")
if transport == "party" then
self.addonTransport = "party"
else
self.addonTransport = "none"
end
end
function UnBot:HasServerCapability(cap)
if not cap or not self.serverCaps then return false end
return self.serverCaps[string.lower(cap)] and true or false
end
function UnBot:SendBotCommand(cmd, opts)
if not cmd or cmd == "" then return false end
cmd = string.gsub(cmd, "^%s+", "")
cmd = string.gsub(cmd, "%s+$", "")
if string.find(cmd, "^%.bot%s+") then
cmd = string.gsub(cmd, "^%.bot%s+", "")
end
if cmd == "" then return false end
local channel = self:AddonCommandChannel()
if channel then
SendAddonMessage((self.C and self.C.ADDON_PREFIX) or "TBM", cmd, channel)
else
SendChatMessage(".bot " .. cmd)
end
self.lastCommand = cmd
self.lastCommandAt = (GetTime and GetTime()) or 0
return true
end
function UnBot:OnTBMSystemMessage(msg)
if not msg or msg == "" then return false end
local _, _, caps = string.find(msg, "^TBM:CAPS|(.*)$")
if caps then
self.serverCaps = {}
local iter = string.gmatch or string.gfind
for item in iter(caps, "[^,%s|]+") do
self.serverCaps[string.lower(item)] = true
end
return true
end
local _, _, transport = string.find(msg, "^TBM:TRANSPORT|(%S+)")
if transport then
self:SetAddonTransport(transport)
return true
end
local _, _, version = string.find(msg, "^TBM:VERSION|(%S+)")
if version then
self.serverVersion = version
return true
end
if self.ParseServerRoster and self:ParseServerRoster(msg) then
return true
end
if self.ParseBotInspectorMessage and self:ParseBotInspectorMessage(msg) then
return true
end
if self.ParseBotStateMessage and self:ParseBotStateMessage(msg) then
return true
end
return false
end
--=========================================================================--
-- Diagnostics
--
-- Vanilla, Turtle and later clients disagree about which group functions
-- exist, so report what this client actually has rather than assuming.
--=========================================================================--
function UnBot:CheckAPI()
local names = {
"InviteByName", "InviteUnit", "UninviteByName", "UninviteUnit",
"ConvertToRaid", "AddFriend", "DoReadyCheck", "GetNumPartyMembers",
"GetNumRaidMembers",
}
self:Message("Group API on this client:")
for i = 1, table.getn(names) do
local n = names[i]
if type(getglobal(n)) == "function" then
DEFAULT_CHAT_FRAME:AddMessage("|cff44ff44 "..n.." yes|r")
else
DEFAULT_CHAT_FRAME:AddMessage("|cffff4444 "..n.." MISSING|r")
end
end
end
-- Show everything GetItemInfo hands back on this client. Stock 1.12 returns
-- eight values and no texture, but modified clients sometimes add fields.
function UnBot:DumpItemInfo(arg)
local id = tonumber(arg) or 3777 -- Lesser Healing Potion
self:Message("GetItemInfo("..id..") returns:")
local v = { GetItemInfo(id) }
local n = table.getn(v)
if n == 0 then
self:Message(" nothing - the client has not cached this item")
return
end
for i = 1, n do
DEFAULT_CHAT_FRAME:AddMessage("|cff888888 ["..i.."] "..type(v[i])
.." = "..tostring(v[i]).."|r")
end
end
-- Invite someone straight away, no bot login and no delay, to separate an
-- invite problem from a timing problem.
function UnBot:TestInvite(name)
if name == nil or name == "" then
name = UnitName("target")
end
if not name or name == "" or not IsValidCharacterName(name) then
self:Message("Usage: /unbot invite <name>, or target someone")
return
end
-- restore the capitalisation the server expects
name = string.upper(string.sub(name, 1, 1))..string.sub(name, 2)
if type(InviteByName) == "function" then
self:Message("InviteByName(\""..name.."\")")
InviteByName(name)
elseif type(InviteUnit) == "function" then
self:Message("InviteUnit(\""..name.."\")")
InviteUnit(name)
else
self:Message("No invite function exists on this client")
end
end
--=========================================================================--
-- Whisper capture
--
-- The bots document themselves: whisper "help", then "actions", "strategies"
-- and so on. Those replies are long, so record them into SavedVariables and
-- read the file afterwards instead of squinting at the chat frame.
--=========================================================================--
function UnBot:ToggleCapture()
-- kept in SavedVariables so a /reload does not silently switch it off
self.capturing = not self.capturing
self.config.capturing = self.capturing
if self.capturing then
if self.config.capture == nil then self.config.capture = {} end
self:Message("Capture ON. Whisper a bot: help, actions, strategies, "..
"triggers, values, chatfilters")
self:Message("Then /reload and the log is written to SavedVariables")
else
self:Message("Capture OFF - "..table.getn(self.config.capture or {})
.." line(s) recorded")
end
end
-- The help system answers only to its own hyperlinks, not to typed words:
-- |c0000FFFF|Hvalue:help:object:action|h[actions]|h|r
-- so build the link and whisper it for them.
local HELP_TOPICS = {
strategies = "strategy",
actions = "action",
triggers = "trigger",
values = "value",
chatfilters = "chatfilter",
}
local HELP_CLASSES = {
"warrior", "paladin", "hunter", "rogue", "priest",
"shaman", "mage", "warlock", "druid",
}
-- Verified against the bot: a hand-built |Hvalue:...|h link does go through and
-- is answered, while the same path sent as plain text is ignored. So the full
-- link markup is required, not optional.
local function HelpLink(path, label)
return "|c0000FFFF|H"..path.."|h["..label.."]|h|r"
end
function UnBot:AskBotHelp(topic)
local target = UnitName("target")
if not target or target == "" or not IsValidCharacterName(target) then
self:Message(UnBotL.MSG.NO_TARGET)
return
end
if topic == nil or topic == "" then
SendChatMessage("help", "WHISPER", nil, target)
return
end
-- second level: "ask list generic strategy" -> value:help:list:<name>
local _, _, listName = string.find(topic, "^list%s+(.+)$")
if listName then
SendChatMessage(HelpLink("value:help:list:"..listName, listName),
"WHISPER", nil, target)
self:Message("Asked "..target.." for the "..listName.." list")
return
end
-- Everything at once. Each list is a dozen long whispers, so leave a wide
-- gap - asking back to back gets most of the replies dropped.
if topic == "all" then
self:QueueChat(HelpLink("value:help:list:generic strategy", "general"),
"WHISPER", target)
for i = 1, table.getn(HELP_CLASSES) do
local c = HELP_CLASSES[i]
self:QueueWait(3)
self:QueueChat(HelpLink("value:help:list:"..c.." strategy", c),
"WHISPER", target)
end
self:Message("Asking "..target.." for all strategy lists - takes about "
..(table.getn(HELP_CLASSES) * 3).."s, then /reload")
return
end
-- first level: the five object categories
local object = HELP_TOPICS[topic]
if not object then
self:Message("Try: all | actions | strategies | list generic strategy")
return
end
SendChatMessage(HelpLink("value:help:object:"..object, topic),
"WHISPER", nil, target)
self:Message("Asked "..target.." about "..topic)
end
-- Help replies list strategies as value:help:strategy:<name> links. Harvest
-- them so the strategy picker works off the server's real names instead of a
-- table baked into the addon. Class strategies are prefixed, e.g.
-- "warrior aoe fury pve".
function UnBot:ParseStrategyList(msg)
if not msg then return end
if not string.find(msg, "Hvalue:help:strategy:") then return end
if self.config.strategies == nil then self.config.strategies = {} end
local iter = string.gmatch or string.gfind
local found = 0
for name in iter(msg, "Hvalue:help:strategy:([^|]+)|h") do
local class, short = "generic", name
for i = 1, table.getn(HELP_CLASSES) do
local c = HELP_CLASSES[i]
local _, _, rest = string.find(name, "^"..c.."%s+(.+)$")
if rest then
class, short = c, rest
break
end
end
if self.config.strategies[class] == nil then
self.config.strategies[class] = {}
end
if not self.config.strategies[class][short] then
self.config.strategies[class][short] = name -- full name to send
found = found + 1
end
end
if found > 0 then
if self.debug then
self:Message("Learned "..found.." strategy name(s)")
end
-- an open strategy window should show them straight away
if self.RebuildStrategyTree then self:RebuildStrategyTree() end
end
end
function UnBot:CaptureLine(sender, msg)
if not self.capturing or not msg then return end
if self.config.capture == nil then self.config.capture = {} end
table.insert(self.config.capture, tostring(sender)..": "..msg)
end
local capture = CreateFrame("Frame", "UnBotCaptureFrame")
capture:RegisterEvent("CHAT_MSG_WHISPER")
capture:RegisterEvent("CHAT_MSG_SYSTEM")
capture:RegisterEvent("CHAT_MSG_ADDON")
capture:RegisterEvent("CHAT_MSG_WHISPER_INFORM") -- what we sent, to verify it left
capture:RegisterEvent("CHAT_MSG_PARTY")
capture:SetScript("OnEvent", function()
if event == "CHAT_MSG_ADDON" then
if arg1 == "TBM" and arg2 then
UnBot:OnTBMSystemMessage(arg2)
end
return
elseif event == "CHAT_MSG_SYSTEM" and arg1 then
if string.find(arg1, "^TBM:") then
UnBot:OnTBMSystemMessage(arg1)
elseif string.find(arg1, "^Strategy change '") then
-- Server confirmed a strategy change via .bot strategy: refresh active view
if UnBotStrategyFrame and UnBotStrategyFrame:IsShown() and type(UnBot.RefreshStrategies) == "function" then
UnBot:RefreshStrategies()
end
end
end
local who = arg2 or "system"
if event == "CHAT_MSG_WHISPER_INFORM" then who = "-> "..who end
UnBot:CaptureLine(who, arg1)
-- learn strategy names whenever they appear, capture on or off
if event == "CHAT_MSG_WHISPER" or event == "CHAT_MSG_PARTY" then
if event == "CHAT_MSG_WHISPER" and string.find(arg1, "^TBM:") and type(UnBot.OnTBMSystemMessage) == "function" then
UnBot:OnTBMSystemMessage(arg1)
end
UnBot:ParseStrategyList(arg1)
if type(UnBot.ParseCombatStrategies) == "function" then
UnBot:ParseCombatStrategies(arg2, arg1)
end
if type(UnBot.ParseNonCombatStrategies) == "function" then
UnBot:ParseNonCombatStrategies(arg2, arg1)
end
end
end)
--=========================================================================--
-- Throttled chat queue
--
-- Firing twenty ".bot add" lines in one frame gets most of them dropped by
-- the client's chat throttle, so anything bulk goes through here instead.
--=========================================================================--
UnBot.queue = {}
UnBot.queueTimer = 0
UnBot.queueWait = 0
local QUEUE_DELAY = 0.4
function UnBot:QueueChat(text, channel, target)
table.insert(self.queue, {
text = text,
channel = channel or "SAY",
target = target,
})
end
-- Run something later, in order, e.g. inviting a bot once it has logged in.
function UnBot:QueueAction(func)
table.insert(self.queue, { action = func })
end
-- Hold the queue for a while before continuing.
function UnBot:QueueWait(seconds)
table.insert(self.queue, { wait = seconds })
end
function UnBot:QueueSize()
return table.getn(self.queue)
end
local pump = CreateFrame("Frame", "UnBotQueuePump")
pump:SetScript("OnUpdate", function()
local now = GetTime()
local elapsed = (type(arg1) == "number" and arg1 > 0 and arg1 < 1) and arg1 or (UnBot.lastPumpTime and (now - UnBot.lastPumpTime) or 0)
UnBot.lastPumpTime = now
if UnBot.CheckPendingInvites then
UnBot:CheckPendingInvites()
end
if UnBot.queueWait > 0 then
UnBot.queueWait = UnBot.queueWait - elapsed
return
end
if table.getn(UnBot.queue) == 0 then return end
UnBot.queueTimer = UnBot.queueTimer + elapsed
if UnBot.queueTimer < QUEUE_DELAY then return end
UnBot.queueTimer = 0
local item = table.remove(UnBot.queue, 1)
if item.wait then
if UnBot.debug then UnBot:Message("queue: wait "..item.wait.."s") end
UnBot.queueWait = item.wait
elseif item.action then
if UnBot.debug then UnBot:Message("queue: action") end
item.action()
else
if UnBot.debug then UnBot:Message("queue: say "..item.text) end
if string.find(item.text, "^%.bot%s+") then
local sub = string.gsub(item.text, "^%.bot%s+", "")
UnBot:SendBotCommand(sub)
elseif string.find(item.text, "^[%.%/]") then
SendChatMessage(item.text)
else
SendChatMessage(item.text, item.channel, nil, item.target)
end
end
end)
--=========================================================================--
-- Load
--=========================================================================--
local loader = CreateFrame("Frame", "UnBotLoader")
loader:RegisterEvent("VARIABLES_LOADED")
loader:RegisterEvent("PLAYER_LOGIN")
loader:RegisterEvent("PLAYER_ENTERING_WORLD")
loader:SetScript("OnEvent", function()
if event == "VARIABLES_LOADED" or event == "PLAYER_LOGIN" then
UnBot:Init()
elseif event == "PLAYER_ENTERING_WORLD" then
if not UnBot.inited then
UnBot:Init()
end
if UnBot.RecordCurrentCharacter then
UnBot:RecordCurrentCharacter()
end
if UnBot.ScanGroupBots then
UnBot:ScanGroupBots()
end
if UnBot.RefreshRoster then
UnBot:RefreshRoster()
end
end
end)