prevent players from setting ranks of higher level than their own

This commit is contained in:
octacian 2016-08-25 11:49:18 -07:00
parent 58f609cec3
commit bf82cb6ec9
2 changed files with 24 additions and 8 deletions

View File

@ -10,15 +10,17 @@
-- with /rank <player> <rank> Put a list of the privileges for the rank in the privileges table.
-- Put each privilege on a new line, each line seperated by a comma. You must be a owner or operator
-- to manage rankings. The owner will automatically be set to the value of the "admin" minetest.conf
-- variable.
-- variable. The level value is used to make sure that (for example) a operator could not set their
-- rank to owner. Multiple ranks can have the same level, but if the rank has the "setrank" capability
-- they could set their rank to any of that level, or a lower level.
-- ENABLED RANKS
st.ranks = {
default = "player",
-- player
{ name = "player", prefix = "", privs = { interact = true, shout = true, fast = true }, },
{ name = "player", level = 1, prefix = "", privs = { interact = true, shout = true, fast = true }, },
-- moderator
{ name = "moderator", prefix = "[moderator]", colour = "#f1c40f", cmd = "mod", privs = {
{ name = "moderator", level = 2, prefix = "[moderator]", colour = "#f1c40f", cmd = "mod", privs = {
interact = true,
shout = true,
fast = true,
@ -32,7 +34,7 @@ st.ranks = {
"getrank",
}, },
-- operator
{ name = "operator", prefix = "[operator]", colour = "#d35400", cmd = "op", privs = {
{ name = "operator", level = 3, prefix = "[operator]", colour = "#d35400", cmd = "op", privs = {
interact = true,
shout = true,
fast = true,
@ -51,7 +53,7 @@ st.ranks = {
"getrank",
}, },
-- owner
{ name = "owner", prefix = "[owner]", colour = "#e74c3c", privs = {
{ name = "owner", level = 10, prefix = "[owner]", colour = "#e74c3c", privs = {
interact = true,
shout = true,
fast = true,

View File

@ -26,6 +26,15 @@ function servertools.get_rank_privs(rank)
end
end
-- get rank level
function servertools.get_rank_level(rank)
for _,i in ipairs(st.ranks) do -- find rank
if i.name == rank then
return i.level -- return level
end
end
end
-- get rank value
function servertools.get_rank_value(rank, value)
for _,i in ipairs(st.ranks) do -- find rank
@ -52,7 +61,7 @@ function servertools.player_can(player, capability)
end
-- set rank
function servertools.set_player_rank(name, newrank)
function servertools.set_player_rank(from, name, newrank)
-- check params
if not servertools.get_player_rank(name) then
return "Invalid player ("..name..")"
@ -66,6 +75,11 @@ function servertools.set_player_rank(name, newrank)
return "Rank nonexistent."
end
-- check level
if servertools.get_rank_level(servertools.get_player_rank(from)) < servertools.get_rank_level(newrank) then
return "You cannot set the rank of "..name.." to a rank of higher level than your own."
end
-- update player privileges
if minetest.set_player_privs(name, rank_privs) then return "Failed to set privileges." end -- unsuccessful
-- TODO: fix setting table value
@ -173,7 +187,7 @@ minetest.register_chatcommand("rank", {
return true, "Please enter a valid target username and new rank."
elseif newrank and target then -- if fields available, continue operation
-- if new rank does not exist, return error
local set_rank = servertools.set_player_rank(target, newrank)
local set_rank = servertools.set_player_rank(name, target, newrank)
if set_rank then -- if unsuccessful, return error
return false, set_rank
else -- else, return success message
@ -214,7 +228,7 @@ for _,i in ipairs(st.ranks) do
-- if not param, return usage
if not param then return "Usage: /"..i.cmd.." <player>" end
-- if error while setting rank, return message
local set_rank = servertools.set_player_rank(param, i.name)
local set_rank = servertools.set_player_rank(name, param, i.name)
if set_rank then
return false, set_rank
else -- else, return success message