ctf_map: Move duplicated code to helper function (#410)

This commit is contained in:
Thomas--S 2019-05-12 19:33:32 +02:00 committed by ANAND ︻气デ═一
parent 9956606899
commit 4d6b3512ab
2 changed files with 17 additions and 14 deletions

View file

@ -100,12 +100,7 @@ minetest.register_chatcommand("maps", {
-- If arg. supplied, set idx to index of the matching map name -- If arg. supplied, set idx to index of the matching map name
-- or path. Else, set to indices[name] or index of current map -- or path. Else, set to indices[name] or index of current map
if param then if param then
for i, map in pairs(ctf_map.available_maps) do idx = ctf_map.get_idx_and_map(param)
if map.name:find(param) or map.path:find(param) then
idx = i
break
end
end
else else
idx = indices[name] or ctf_map.map and ctf_map.map.idx or 1 idx = indices[name] or ctf_map.map and ctf_map.map.idx or 1
end end

View file

@ -55,19 +55,27 @@ function minetest.get_server_status(name, joined)
end end
function ctf_map.get_idx_and_map(param)
param = param:lower():trim()
for i, map in pairs(ctf_map.available_maps) do
if map.name:lower():find(param, 1, true) or
map.path:lower():find(param, 1, true) then
return i, map
end
end
end
local next_idx local next_idx
minetest.register_chatcommand("set_next", { minetest.register_chatcommand("set_next", {
privs = { ctf_admin = true }, privs = { ctf_admin = true },
func = function(name, param) func = function(name, param)
for i, map in pairs(ctf_map.available_maps) do local idx, map = ctf_map.get_idx_and_map(param)
if map.name:lower():find(param, 1, true) or if idx then
map.path:lower():find(param, 1, true) then next_idx = idx
next_idx = i return true, "Selected " .. map.name
return true, "Selected " .. map.name else
end return false, "Couldn't find any matches"
end end
return false, "Couldn't find any matches"
end, end,
}) })