-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.lua
376 lines (324 loc) · 9.5 KB
/
command.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
--- Server Shops Chat Commands
--
-- @topic commands
local ss = server_shop
local S = core.get_translator(ss.modname)
local commands = {
{
cmd = "help",
params = "[" .. S("command") .. "]",
desc = S("Shows usage info."),
},
{
cmd = "list",
desc = S("Lists all registered shop IDs."),
},
{
cmd = "info",
params = "<" .. S("ID") .. ">",
desc = S("Lists information about a shop."),
},
{
cmd = "register",
params = "<" .. S("ID") .. ">" .. " <sell/buy> "
.. " [" .. S("product1=value,product2=value,...") .. "]",
desc = S("Registers a new shop."),
persists = true,
},
{
cmd = "unregister",
params = "<" .. S("ID") .. ">",
desc = S("Unregisters a shop."),
persists = true,
},
{
cmd = "add",
params = "<" .. S("ID") .. "> <" .. S("product1=value,product2=value,...") .. ">",
desc = S("Adds one or more items to a shop's product list."),
persists = true,
},
{
cmd = "remove",
params = "<" .. S("ID") .. "> <" .. S("product") .. ">",
desc = S("Removes first instance of an item from a shop's product list."),
persists = true,
},
{
cmd = "removeall",
params = "<" .. S("ID") .. "> <" .. S("product") .. ">",
desc = S("Removes all instances of an item from a shop's product list."),
persists = true,
},
{
cmd = "reload",
desc = S("Reloads shops configuration."),
},
}
local format_usage = function(cmd)
local usage = S("Usage:")
if cmd then
local desc, params, persists
for _, c in ipairs(commands) do
if c.cmd == cmd then
desc = c.desc
params = c.params
persists = c.persists
break
end
end
usage = usage .. "\n /" .. ss.modname .. " " .. cmd
if params then
usage = usage .. " " .. params
end
if desc then
if persists then
desc = desc .. " " .. S("(changes are written to config)")
end
usage = desc .. "\n\n" .. usage
end
else
for _, c in ipairs(commands) do
usage = usage .. "\n /" .. ss.modname .. " " .. c.cmd
if c.params then
usage = usage .. " " .. c.params
end
end
end
return usage
end
--- Manages shops & config.
--
-- @chatcmd server_shop
-- @param command Command to execute.
-- @param[opt] params Parameters associated with command.
-- @usage
-- /server_shop <command> [<params>]
--
-- Commands:
-- - help
-- - Shows usage info.
-- - parameters: [command]
-- - list
-- - Lists all registered shop IDs.
-- - info
-- - Lists information about a shop.
-- - parameters: <id>
-- - register
-- - Registers new shop & updates configuration.
-- - parameters: <id> <sell/buy> [product1=value,product2=value,...]
-- - unregister
-- - Unregisters shop & updates configuration.
-- - parameters: <id>
-- - add
-- - Adds 1 or more items to a shop's product list.
-- - parameters: <id> <product1=value,product2=value,...>
-- - remove
-- - Removes the first instance of an item from a shop's product list.
-- - parameters: <id> <product>
-- - removeall
-- - Removes all instances of an item from a shop's product list.
-- - parameters: <id> <product>
-- - reload
-- - Reloads shops configuration.
core.register_chatcommand(ss.modname, {
description = S("Manage shops configuration.") .. "\n\n"
.. format_usage(),
privs = {server=true},
params = "<" .. S("command") .. "> [" .. S("params") .. "]",
func = function(name, param)
local params = param:split(" ")
local cmd = params[1]
table.remove(params, 1)
if not cmd then
return false, S("Must provide a command:") .. "\n\n" .. format_usage()
end
local shop_id = params[1]
if cmd == "help" then
if #params > 1 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
if params[1] then
local sub_cmd
for _, c in ipairs(commands) do
if params[1] == c.cmd then
sub_cmd = c.cmd
break
end
end
if sub_cmd then
return true, format_usage(sub_cmd)
else
return false, S("Unknown command: @1", sub_cmd)
end
end
return true, S("Manage shops configuration.") .. "\n\n" .. format_usage()
elseif cmd == "reload" then
if #params > 0 then
return false, S('"@1" command takes no parameters.', cmd) .. "\n\n"
.. format_usage(cmd)
end
ss.file_load()
ss.prune_shops()
return true, S("Shops configuration loaded.")
elseif cmd == "register" then
if #params > 3 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
local shop_type = params[2]
local shop_products = params[3]
if not shop_id then
return false, S("Must provide ID.") .. "\n\n" .. format_usage(cmd)
elseif not shop_type then
return false, S("Must provide type.") .. "\n\n" .. format_usage(cmd)
end
if shop_type ~= "sell" and shop_type ~= "buy" then
return false, S('Shop type must be "@1" or "@2".', "sell", "buy")
.. "\n\n" .. format_usage(cmd)
end
local products = {}
if shop_products then
shop_products = shop_products:split(",")
for _, p in ipairs(shop_products) do
local item = p:split("=")
local item_name = item[1]
local item_value = tonumber(item[2])
if not core.registered_items[item_name] then
return false, S('"@1" is not a recognized item.', item_name)
.. "\n\n" .. format_usage(cmd)
elseif not item_value then
return false, S("Item value must be a number.")
.. "\n\n" .. format_usage(cmd)
end
table.insert(products, {item_name, item_value})
end
end
ss.register_persist(shop_id, products, shop_type == "buy")
return true, S("Registered shop with ID: @1", shop_id)
elseif cmd == "unregister" then
if #params > 1 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
if not shop_id then
return false, S("Must provide ID.") .. "\n\n" .. format_usage(cmd)
end
if not ss.unregister_persist(shop_id) then
return false, S("Cannot unregister shop with ID: @1", shop_id)
end
return true, S("Unregistered shop with ID: @1", shop_id)
elseif cmd == "add" then
if #params > 2 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
if not shop_id then
return false, S("Must provide ID.") .. "\n\n" .. format_usage(cmd)
end
if not ss.is_registered(shop_id) then
return false, S("Shop ID @1 is not registered.", shop_id)
end
local shop_products = params[2]
if not shop_products then
return false, S("Must provide product.") .. "\n\n" .. format_usage(cmd)
end
local products = {}
shop_products = shop_products:split(",")
for _, p in ipairs(shop_products) do
local item = p:split("=")
local item_name = item[1]
local item_value = tonumber(item[2])
if not core.registered_items[item_name] then
return false, S('"@1" is not a recognized item.', item_name)
.. "\n\n" .. format_usage(cmd)
elseif not item_value then
return false, S("Item value must be a number.")
.. "\n\n" .. format_usage(cmd)
end
table.insert(products, {item_name, item_value})
end
ss.add_product_persist(shop_id, products)
if #products == 1 then
return true, S("Added 1 item to shop ID @1.", shop_id)
else
return true, S("Added @1 items to shop ID @2.", #products, shop_id)
end
elseif cmd == "remove" or cmd == "removeall" then
if #params > 2 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
if not shop_id then
return false, S("Must provide ID.").. "\n\n" .. format_usage(cmd)
end
local product = params[2]
if not product then
return false, S("Must provide product.") .. "\n\n" .. format_usage(cmd)
end
local count = 0
if cmd == "remove" then
count = ss.remove_product_persist(shop_id, product, false)
else
count = ss.remove_product_persist(shop_id, product, true)
end
if count then
if count == 1 then
return true, S("Removed 1 item from shop ID @1.", shop_id)
elseif count > 1 then
return true, S("Removed @1 items from shop ID @2.", count, shop_id)
end
end
return false, S("Shop ID @1 does not contain @2 in its product list.", shop_id, product)
elseif cmd == "list" then
if #params > 0 then
return false, S('"@1" command takes no parameters.', cmd) .. "\n\n"
end
local shops_list = {}
for id in pairs(ss.get_shops()) do
table.insert(shops_list, id)
end
local msg
local id_count = #shops_list
if id_count > 0 then
if id_count == 1 then
msg = S("There is 1 shop registered: @1", table.concat(shops_list, ", "))
else
msg = S("There are @1 shops registered: @2", id_count, table.concat(shops_list, ", "))
end
else
msg = S("There are no shops registered.")
end
return true, msg
elseif cmd == "info" then
if #params > 1 then
return false, S("Too many parameters.") .. "\n\n"
.. format_usage(cmd)
end
if not shop_id then
return false, S("Must provide ID.").. "\n\n" .. format_usage(cmd)
end
local shop = ss.get_shop(shop_id)
if not shop then
return false, S("Shop ID @1 is not registered.", shop_id)
end
local s_type = S("seller")
if shop.buyer then
s_type = S("buyer")
end
local product_list = {}
for _, p in ipairs(shop.products) do
p = p[1] .. " (" .. p[2]
if ss.currency_suffix then
p = p .. " " .. ss.currency_suffix
end
p = p .. ")"
table.insert(product_list, p)
end
return true, S("Information about shop ID: @1", shop_id)
.. "\n" .. S("Type: @1", s_type)
.. "\n" .. S("Products: @1", table.concat(product_list, ", "))
end
return false, S("Unknown command: @1", cmd)
end,
})