-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommands.lua
230 lines (195 loc) · 5.44 KB
/
Commands.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
--[[
Shame (C) Kruithne <[email protected]>
Licensed under GNU General Public Licence version 3.
https://github.com/Kruithne/Shame
Commands.lua - Registers and handles chat commands.
]]--
do
-- [[ Local Optimization ]] --
local Shame = Shame;
local pairs = pairs;
local unpack = unpack;
local table_sort = table.sort;
local string_len = string.len;
local table_remove = table.remove;
local string_gmatch = string.gmatch;
local table_concat = table.concat;
local string_sub = string.sub;
local BreakUpLargeNumbers = BreakUpLargeNumbers;
--[[
Shame.OnCommand
Invoked when a command is executed.
text - The input of the user.
editbox - Which region the command was executed from.
]]--
Shame.OnCommand = function(text, editbox)
local args, self = {}, Shame;
for arg in string_gmatch(text, "%S+") do
args[#args + 1] = arg:lower();
end
if #args > 0 then
-- Command entered, process it.
local command = table_remove(args, 1);
local commandNode = self.commandList[command];
if not commandNode then
-- No command found by index match, explore for partial.
for cmd, cmdData in pairs(self.commandList) do
if string_sub(cmd, 1, string_len(command)) == command then
if not commandNode then
-- First hit, store for possible use.
commandNode = cmdData;
else
-- Multiple hits, abandon partial search.
commandNode = nil;
break;
end
end
end
end
if commandNode then
if not commandNode.func(self, args) then
self:Message(self.L_COMMAND_SYNTAX, nil, command, commandNode.usage);
end
else
self:Message(self.L_UNKNOWN_COMMAND);
end
else
-- No command entered, display available commands.
self:ListCommands();
end
end
--[[
Shame.ListCommands
List all available commands.
self - Reference to the addon container.
]]--
Shame.ListCommands = function(self)
self:Message(self.L_AVAILABLE_COMMANDS);
for cmd, cmdData in pairs(self.commandList) do
if not cmdData.hidden then
self:Message(self.FORMAT_COMMAND_FULL, nil, cmd, cmdData.usage or "", cmdData.desc);
end
end
return true;
end
--[[
Shame.GetCommandFormat
Format the syntax of a command.
self - Reference to the addon container.
id - ID of the command.
]]--
Shame.GetCommandFormat = function(self, id)
id = id:lower();
local command = COMMANDS[id];
if command then
return string_format(self.FORMAT_COMMAND, id, command.usage or "");
end
return self.L_INVALID_COMMAND;
end
--[[
Shame.GetFormattedList
Get a formatted list of table values.
self - Reference to the addon container.
pool - Values to format.
]]--
Shame.GetFormattedList = function(self, pool)
local items = {};
for item, _ in pairs(pool) do
items[#items + 1] = item;
end
return self.LIST_FORMAT:format(table_concat(items, ", "));
end
--[[
Shame.RosterSort
Sorting function for roster ordering.
]]--
Shame.RosterSort = function(a, b)
return a[2] > b[2];
end
--[[
Shame.Command_Print
Handler for the 'print' command.
self - Reference to the addon container.
args - Command arguments.
]]--
Shame.Command_Print = function(self, args)
local channel = self.Validate(args[1], self.validChannels);
if channel then
self:Message(self.L_CURRENT_SESSION, channel);
local rosterIndex = {};
for actorName, actorData in pairs(self.boardGroup) do
rosterIndex[#rosterIndex + 1] = actorData;
end
table_sort(rosterIndex, self.RosterSort);
local done = false;
for index, actor in pairs(rosterIndex) do
local suffix = actor.mistakes > 1 and self.L_MISTAKE_MULTI or self.L_MISTAKE_SINGLE;
local damageTaken = BreakUpLargeNumbers(actor.damage or 0);
self:Message(self.L_BOARD_FORMAT, channel, index, actor.name, damageTaken, suffix:format(actor.mistakes));
done = true;
end
if not done then
self.Message(self.L_NO_SHAME, channel);
end
else
self:Message(self.L_INVALID_CHANNEL, nil, self:GetFormattedList(self.validChannels));
end
return true;
end
--[[
Shame.Command_Enable
Handler for the 'enable' command.
self - Reference to the addon container.
]]--
Shame.Command_Enable = function(self)
if not self.tracking then
self:Enable();
self:Message(self.L_NEW_SESSION);
self:PrintCurrentMode();
else
self:Message(self.L_ALREADY_RUNNING .. self.GetCommandFormat(self.L_CMD_STOP));
end
return true;
end
--[[
Shame.Command_Disable
Handler for the 'disable' command.
self - Reference to the addon container.
]]--
Shame.Command_Disable = function(self)
if self.tracking then
self:Disable();
self:Message(self.L_STOPPED);
else
self:Message(self.L_NOT_STARTED .. self:GetCommandFormat(self.L_CMD_START));
end
return true;
end
--[[
Shame.Command_SetMode
Handler for the 'mode' command.
self - Reference to the addon container.
args - Command arguments.
]]--
Shame.Command_SetMode = function(self, args)
local mode = self.Validate(args[1], self.validModes);
if mode then
if mode == self.L_MODE_ALL then
local channel = self.Validate(args[2], self.validChannels);
if channel then
self.modeChannel = channel;
else
self:Message(self.L_VALID_CHANNELS, nil, self:GetFormattedList(self.validChannels));
return false;
end
end
self.currentMode = mode;
self:PrintCurrentMode();
return true;
else
self:Message(self.L_VALID_MODES, nil, self:GetFormattedList(self.validModes));
return false;
end
return false;
end
end