Make /tournament parameter parsing cleaner

master
upsilon 2017-04-17 14:45:08 +02:00
parent 2d98ac9a33
commit d4f60fdcc3
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C
1 changed files with 103 additions and 65 deletions

View File

@ -87,71 +87,6 @@ minetest.register_chatcommand("engage", {
end
})
minetest.register_chatcommand("tournament", {
params = "[noteleport] [seconds]",
description = "Creates a new tournament, optionally teleporting players to your current position 10 seconds before the tournament starts.",
privs = {interact = true},
func = function(name, param)
local params = param:split(" ")
if #params > 2 then
return false, "Invalid usage. See /help tournament."
end
local starting_time = tournament_starting_time
local teleport = true
if tonumber(params[1]) then
starting_time = tonumber(params[1])
elseif params[1] == "noteleport" then
teleport = false
if tonumber(params[2]) then
starting_time = tonumber(params[2])
elseif params[2] ~= "" and params[2] ~= nil then
return false, "Invalid usage. See /help tournament."
end
end
if starting_time < 10 or starting_time > 600 then
return false, "Please set a starting time between 10s and 600s."
end
-- Fill start infos
tournament.starting_infos.starter = name
tournament.starting_infos.open_time = os.time()
tournament.starting_infos.start_time = os.time() + starting_time
-- Allow engaging
local e, m = pvpplus.allow_engaging(name, teleport)
if e == false then
return false, m
end
-- Engage starter
pvpplus.engage_player(name)
-- Chat messages
minetest.chat_send_all("The tournament will begin in " .. tostring(starting_time).."s.")
minetest.after(starting_time - 10, function()
minetest.chat_send_all("The tournament will begin in 10s! Engage yourself by typing /engage!")
pvpplus.teleport_engaged_players()
end)
minetest.after(starting_time - 5, function()
minetest.chat_send_all("The tournament will begin in 5s!")
end)
for i = 1, 4 do
minetest.after(starting_time - i, function()
minetest.chat_send_all(tostring(i).."!")
end)
end
-- Start tournament
minetest.after(starting_time, function(name)
local ok, e = pvpplus.start_tournament()
if ok == false and e then
minetest.chat_send_player(name, e)
end
end, name)
end
})
minetest.register_chatcommand("tournament_info", {
params = "",
description = "Prints tournament informations",
@ -192,3 +127,106 @@ minetest.register_chatcommand("tournament_info", {
end
end
})
--- /tournament command ---
-- value: {[bool: whether the parameter requires the tournament_mod privilege], [string: type of the wanted value (number or string), nil if no value]}
local param_list = {
noteleport = {false, nil},
start_delay = {true, "number"}
}
local function parse_params(param)
local params = param:split(" ")
local param_table = {}
for _, param in ipairs(params) do
local opt, val = param:match("(.+)=(.*)")
if opt then
param = opt
end
-- Validity checks...
if not param_list[param] then
return false, "Invalid option: " .. param
end
if val and not param_list[param][1] then
return false, "Option " .. param .. " should not be used with a value."
end
if not val and param_list[param][1] then
return false, "Option " .. param .. " expects a value (e.g. option=value)."
end
if param_list[param][2] == "number" and tonumber(val) == nil then
return false, "Option " .. param .. " expects a number as value."
end
if val then
if param_list[param][2] == "number" then
param_table[param] = tonumber(val)
else -- string
param_table[param] = val
end
else
param_table[param] = true
end
end
return true, param_table
end
minetest.register_chatcommand("tournament", {
params = "[noteleport] [start_delay=<seconds>]",
description = "Creates a new tournament, optionally teleporting players to your current position 10 seconds before the tournament starts.",
privs = {interact = true},
func = function(name, param)
local params
do
local ok, res = parse_params(param)
if ok == false then
return false, res
end
params = res
end
local starting_time = params.start_delay or tournament_starting_time
if starting_time < 10 or starting_time > 600 then
return false, "Please set a start delay between 10s and 600s."
end
local teleport = params.noteleport ~= true
-- Fill start infos
tournament.starting_infos.starter = name
tournament.starting_infos.open_time = os.time()
tournament.starting_infos.start_time = os.time() + starting_time
-- Allow engaging
local e, m = pvpplus.allow_engaging(name, teleport)
if e == false then
return false, m
end
-- Engage starter
pvpplus.engage_player(name)
-- Chat messages
minetest.chat_send_all("The tournament will begin in " .. tostring(starting_time).."s.")
minetest.after(starting_time - 10, function()
minetest.chat_send_all("The tournament will begin in 10s! Engage yourself by typing /engage!")
pvpplus.teleport_engaged_players()
end)
minetest.after(starting_time - 5, function()
minetest.chat_send_all("The tournament will begin in 5s!")
end)
for i = 1, 4 do
minetest.after(starting_time - i, function()
minetest.chat_send_all(tostring(i).."!")
end)
end
-- Start tournament
minetest.after(starting_time, function(name)
local ok, e = pvpplus.start_tournament()
if ok == false and e then
minetest.chat_send_player(name, e)
end
end, name)
end
})