mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-21 17:04:00 +00:00
Address review on the role api
- Roles are objects and everything done to or with a role is a method on it: assign, unassign, has_player, has_permission, is_higher_than, is_lower_than, get_players, get_player_names, print. Assignment has one entry point, role:assign(player, options), with local_only as an option rather than a second function. - Roles are looked up by clusterio id with get_role; get_role_by_name searches the list for the few places, such as configs, which only know a name. The name map and the ordered list are gone, get_roles sorts on demand and the index field is replaced by the comparison methods. - Players are LuaPlayer objects only, with nil or index 0 for the server. - get_higher_roles and get_lower_roles replace print_to_roles_higher and print_to_roles_lower, call sites loop over them with role:print. - Permission groups are removed from roles again, exp_groups owns the mapping from roles to groups. - Seeding is a SeedRolesRequest behind a button on the roles page rather than running on first start, and creates only the roles; the player assignments are dropped. The seed lists each permission once at the lowest role which has it and lets the parent chain carry it up. - System commands unlock for core.admin rather than a permission of their own. - Role metatables are registered with Storage.register_metatable so the methods survive save and load, which the role records in storage needed. - The player list auth uses Roles.player_outranks directly, and the event carries role ids rather than names. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
78a533a6f6
commit
bea8d46c82
@@ -8,7 +8,7 @@ return {
|
||||
trust_time = 3600 * 60 * 10, --- @setting trust_time The time in ticks that a player must be online for to count as trusted
|
||||
update_time = 3600 * 30, --- @setting update_time How often in ticks the script checks for active players
|
||||
custom_active_check = function(player)
|
||||
local veteran = Roles.get_role("Veteran")
|
||||
return veteran ~= nil and Roles.get_player_highest_role(player).index <= veteran.index
|
||||
local veteran = Roles.get_role_by_name("Veteran")
|
||||
return veteran ~= nil and not Roles.get_player_highest_role(player):is_lower_than(veteran)
|
||||
end,
|
||||
}
|
||||
|
||||
@@ -21,11 +21,6 @@ local function set_accessors(player_getter, action_setter)
|
||||
get_selected_player, set_selected_action = player_getter, action_setter
|
||||
end
|
||||
|
||||
-- auth that will only allow when on player's of lower roles
|
||||
local function auth_lower_role(player, selected_player_name)
|
||||
return Roles.player_outranks(player, selected_player_name)
|
||||
end
|
||||
|
||||
-- gets the action player and a coloured name for the action to be used on
|
||||
local function get_action_player(player)
|
||||
local selected_player = get_selected_player(player) --[[@as LuaPlayer]]
|
||||
@@ -98,7 +93,10 @@ local function report_player_callback(player, reason)
|
||||
local selected_player, selected_player_color = get_action_player(player)
|
||||
local by_player_name_color = format_player_name(player)
|
||||
game.print{ "exp-commands_reports.response", selected_player_color, reason }
|
||||
Roles.print_to_roles_higher("Trainee", { "exp-commands_reports.response-admin", selected_player_color, by_player_name_color, reason })
|
||||
local trainee = Roles.get_role_by_name("Trainee")
|
||||
for _, role in ipairs(trainee and Roles.get_higher_roles(trainee) or {}) do
|
||||
role:print{ "exp-commands_reports.response-admin", selected_player_color, by_player_name_color, reason }
|
||||
end
|
||||
Reports.report_player(selected_player.name, player.name, reason)
|
||||
end
|
||||
|
||||
@@ -180,22 +178,22 @@ return {
|
||||
report_player,
|
||||
},
|
||||
["exp_scenario.command.create_warning"] = {
|
||||
auth = auth_lower_role, -- warn a lower user, replaces report
|
||||
auth = Roles.player_outranks, -- warn a lower user, replaces report
|
||||
reason_callback = warn_player_callback,
|
||||
warn_player,
|
||||
},
|
||||
["exp_scenario.command.jail"] = {
|
||||
auth = auth_lower_role,
|
||||
auth = Roles.player_outranks,
|
||||
reason_callback = jail_player_callback,
|
||||
jail_player,
|
||||
},
|
||||
["exp_scenario.gui.player_list.kick"] = {
|
||||
auth = auth_lower_role,
|
||||
auth = Roles.player_outranks,
|
||||
reason_callback = kick_player_callback,
|
||||
kick_player,
|
||||
},
|
||||
["exp_scenario.gui.player_list.ban"] = {
|
||||
auth = auth_lower_role,
|
||||
auth = Roles.player_outranks,
|
||||
reason_callback = ban_player_callback,
|
||||
ban_player,
|
||||
},
|
||||
|
||||
@@ -20,8 +20,10 @@ local Roles = require("modules/exp_roles")
|
||||
|
||||
local valid_player = function(p) return type(p) == "userdata" and p or game.get_player(p) end
|
||||
|
||||
--- The role which is given to jailed players, it has a higher priority than every other role
|
||||
local jail_role = "Jail"
|
||||
--- The role given to jailed players, it has a higher priority than every other role
|
||||
local function jail_role()
|
||||
return assert(Roles.get_role_by_name("Jail"), "The Jail role does not exist")
|
||||
end
|
||||
|
||||
local Jail = {
|
||||
events = {
|
||||
@@ -62,7 +64,8 @@ end
|
||||
-- @tparam LuaPlayer player the player to check if they are in jail
|
||||
-- @treturn boolean whether the player is currently in jail
|
||||
function Jail.is_jailed(player)
|
||||
return Roles.player_has_role(valid_player(player), jail_role)
|
||||
local valid = valid_player(player)
|
||||
return valid ~= nil and jail_role():has_player(valid)
|
||||
end
|
||||
|
||||
--- Moves a player to jail, which suppresses all of their other roles
|
||||
@@ -77,7 +80,8 @@ function Jail.jail_player(player, by_player_name, reason)
|
||||
|
||||
reason = reason or "Non given."
|
||||
|
||||
if Roles.player_has_role(player, jail_role) then return end
|
||||
local role = jail_role()
|
||||
if role:has_player(player) then return end
|
||||
|
||||
player.walking_state = { walking = false, direction = player.walking_state.direction }
|
||||
player.riding_state = { acceleration = defines.riding.acceleration.nothing, direction = player.riding_state.direction }
|
||||
@@ -86,7 +90,7 @@ function Jail.jail_player(player, by_player_name, reason)
|
||||
player.picking_state = false
|
||||
player.repair_state = { repairing = false, position = player.repair_state.position }
|
||||
|
||||
Roles.assign_player(player, jail_role, by_player_name, true)
|
||||
role:assign(player, { by_player_name = by_player_name, silent = true })
|
||||
|
||||
event_emit(Jail.events.on_player_jailed, player, by_player_name, reason)
|
||||
|
||||
@@ -102,9 +106,10 @@ function Jail.unjail_player(player, by_player_name)
|
||||
if not player then return end
|
||||
if not by_player_name then return end
|
||||
|
||||
if not Roles.player_has_role(player, jail_role) then return end
|
||||
local role = jail_role()
|
||||
if not role:has_player(player) then return end
|
||||
|
||||
Roles.unassign_player(player, jail_role, by_player_name, true)
|
||||
role:unassign(player, { by_player_name = by_player_name, silent = true })
|
||||
|
||||
event_emit(Jail.events.on_player_unjailed, player, by_player_name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user