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
-- or path. Else, set to indices[name] or index of current map
if param then
for i, map in pairs(ctf_map.available_maps) do
if map.name:find(param) or map.path:find(param) then
idx = i
break
end
end
idx = ctf_map.get_idx_and_map(param)
else
idx = indices[name] or ctf_map.map and ctf_map.map.idx or 1
end

View file

@ -55,19 +55,27 @@ function minetest.get_server_status(name, joined)
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
minetest.register_chatcommand("set_next", {
privs = { ctf_admin = true },
func = function(name, param)
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
next_idx = i
return true, "Selected " .. map.name
end
local idx, map = ctf_map.get_idx_and_map(param)
if idx then
next_idx = idx
return true, "Selected " .. map.name
else
return false, "Couldn't find any matches"
end
return false, "Couldn't find any matches"
end,
})