ctf_map: Move duplicated code to helper function (#410)
This commit is contained in:
parent
9956606899
commit
4d6b3512ab
2 changed files with 17 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
local idx, map = ctf_map.get_idx_and_map(param)
|
||||
if idx then
|
||||
next_idx = idx
|
||||
return true, "Selected " .. map.name
|
||||
end
|
||||
end
|
||||
|
||||
else
|
||||
return false, "Couldn't find any matches"
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue