-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatInteractor.lua
150 lines (137 loc) · 5.83 KB
/
ChatInteractor.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
-- namespace
local _, ns = ...;
-- imports
local Items = ns.Items
local ITEM_LIST = ns.ITEM_LIST
local Players = ns.Players
local RoleSelectionEvent = ns.RoleSelectionEvent
local PrioSelectionEvent = ns.PrioSelectionEvent
local utils = ns.utils
local commandHandlers = {}
commandHandlers["role"] = function(msg, player)
local possibleRoles = ns.CLASS_ROLES[player.class]
local roles = {}
-- roles may be comma separated
for _, split in ipairs({ strsplit(",", msg) }) do
split = strtrim(split)
for roleId, role in pairs(possibleRoles) do
if (split == roleId or split == role.key) then
roles[roleId] = true
break
end
end
end
if (not utils.tblempty(roles)) then
local rolesText = utils.toCSV(roles, tostring)
if (not utils.tblequals(player.roles, roles)) then
-- override the players roles
player.roles = roles
print("> Player '"..player.name.."' changed roles to '"..rolesText.."'.")
end
-- inform the player about its current roles
SendChatMessage("Your current roles are: "..rolesText..".", "WHISPER", nil, player.name)
-- create a RoleSelectionEvent
local event = RoleSelectionEvent.new(nil, roles)
Players.selectRole(player.name, event)
-- if the instance is prio, respond with a description
if (ns.DB.activeInstance) then
local instance = ns.DB.INSTANCE_LIST[ns.DB.activeInstance]
if (instance and instance.prio > 0) then
local response = "To specify the prio items, use !prl prio "
for i=1,instance.prio do
response = response.."[ItemLink"..tostring(i).."]"
end
SendChatMessage(response, "WHISPER", nil, player.name)
end
end
else
local response = "Invalid roles. Possible roles: "..utils.toCSV(possibleRoles, function(roleId, role)
return role.key.." "..roleId
end, ", ")
SendChatMessage(response, "WHISPER", nil, player.name)
end
end
commandHandlers["prio"] = function(msg, player)
local prioItems = { Items.getItemIdsFromChat(msg) }
-- quick item validation
for index, itemId in ipairs(prioItems) do
local item = ITEM_LIST[itemId]
if (item and not player:needsItem(item)) then
local response = "The item '"..item:getName().."' is not in your need-list."
SendChatMessage(response, "WHISPER", nil, player.name)
-- abort selection
return
end
end
local event = PrioSelectionEvent.new(nil, prioItems)
Players.selectPrioItems(player.name, event)
SendChatMessage("Prio items successfully specified.", "WHISPER", nil, player.name)
end
commandHandlers["list"] = function(msg, player)
local slot = tonumber(msg)
if (slot) then
if (ns.DB.activeInstance) then
local instance = ns.DB.INSTANCE_LIST[ns.DB.activeInstance]
if (instance) then
local itemcount = 0
local msg = ""
for itemId in pairs(player.needlist) do
local item = ITEM_LIST[itemId]
if (item
and (slot == item.slot or slot == 0 and not item.slot)
and item:dropsIn(instance.raids)
and player:needsItem(item)
) then
local link = item:getLink()
if (link) then
msg = msg..link.." "
itemcount = itemcount + 1
if (itemcount >= 3) then
SendChatMessage(msg, "WHISPER", nil, player.name)
itemcount = 0
msg = ""
end
else
SendChatMessage("Not all items loaded yet. Please try again...", "WHISPER", nil, player.name)
break
end
end
end
if (itemcount > 0) then
SendChatMessage(msg, "WHISPER", nil, player.name)
end
end
end
else
SendChatMessage("Invalid slot number. 0:Misc, 1:Head, 2:Neck, 3:Shoulder, 5:Chest, 6:Belt, 7:Legs, 8:Feet, 9:Wrist, 10:Hands, 11:Ring, 13:Trinket, 15:Back, 16:Mainhand, 17:Offhand, 18:Ranged.", "WHISPER", nil, player.name)
end
end
-- parse the whisper chat
local whisperEventFrame = CreateFrame("Frame")
whisperEventFrame:RegisterEvent("CHAT_MSG_WHISPER")
whisperEventFrame:SetScript("OnEvent", function(frame, event, arg1, arg2)
local msg = arg1
-- detect a prl command
if (utils.strstarts(msg, "!prl ")) then
-- remove the realm part from the author
local author = strsplit("-", arg2, 2)
if (ns.DB.options.chatInteraction) then
local player = ns.DB.PLAYER_LIST[author]
if (player) then
msg = strsub(msg, 6)
local cmd, rest = strsplit(" ", msg, 2)
local handler = commandHandlers[cmd]
if (handler) then
handler(rest, player)
else
SendChatMessage("Unknown command.", "WHISPER", nil, author)
end
else
print("> Player '"..author.."' is not registered for Personal Roll Loot.")
SendChatMessage("You are not registered for Personal Roll Loot.", "WHISPER", nil, author)
end
else
SendChatMessage("Chat interaction disabled.", "WHISPER", nil, author)
end
end
end)