1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-20 03:29:26 +02:00

Check regular/probation against know players. (#1316)

- Also allow force execute if command is rerun.
- Fix error in banning players that are not in game.
This commit is contained in:
grilledham 2022-05-01 15:51:17 +01:00 committed by GitHub
parent 1e3a26deef
commit ad16370301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 30 deletions

View File

@ -10,13 +10,16 @@ local Ranks = require 'resources.ranks'
--- A table of players with tpmode turned on
local tp_players = {}
local players_last_command = {}
Global.register(
{
tp_players = tp_players
tp_players = tp_players,
players_last_command = players_last_command
},
function(tbl)
tp_players = tbl.tp_players
players_last_command = tbl.players_last_command
end
)
@ -25,6 +28,27 @@ local function print_no_target(target_name)
Game.player_print({'common.fail_no_target', target_name}, Color.fail)
end
local function know_player_or_rerun(player_name, actor, command_name)
if Rank.know_player(player_name) then
return true
end
local actor_last_command = players_last_command[actor]
if actor_last_command and actor_last_command.command == command_name and actor_last_command.parameters == player_name then
return true;
end
Game.player_print({'common.rerun_no_target', player_name}, Color.fail)
return false
end
local function console_command(event)
local actor = Utils.get_admin_or_server_actor(event.player_index)
if actor then
players_last_command[actor] = {command = event.command, parameters = event.parameters}
end
end
--- Sends a message to all online admins
local function admin_chat(args, player)
Utils.print_admins(args.msg, player)
@ -38,11 +62,11 @@ end
--- Promote someone to regular
local function add_regular(args)
local target_ident = args.player
local target, target_name = Utils.validate_player(target_ident)
local target_name = args.player
local maybe_target_player = game.get_player(target_name)
local actor = Utils.get_actor()
if not target then
print_no_target(target_ident)
if not maybe_target_player and not know_player_or_rerun(target_name, actor, 'regular') then
return
end
@ -53,8 +77,10 @@ local function add_regular(args)
local success = Rank.increase_player_rank_to(target_name, Ranks.regular)
if success then
game.print({'admin_commands.regular_add_success', Utils.get_actor(), target_name}, Color.info)
target.print({'admin_commands.regular_add_notify_target'}, Color.warning)
game.print({'admin_commands.regular_add_success', actor, target_name}, Color.info)
if maybe_target_player then
maybe_target_player.print({'admin_commands.regular_add_notify_target'}, Color.warning)
end
else
Game.player_print({'admin_commands.regular_add_fail', target_name, Rank.get_player_rank_name(target_name)}, Color.fail)
end
@ -62,19 +88,19 @@ end
--- Demote someone from regular
local function remove_regular(args)
local target_ident = args.player
local target, target_name = Utils.validate_player(target_ident)
local target_name = args.player
local maybe_target_player = game.get_player(target_name)
local actor = Utils.get_actor()
if not target then
print_no_target(target_ident)
if not maybe_target_player and not know_player_or_rerun(target_name, actor, 'regular-remove') then
return
end
if Rank.equal(target_name, Ranks.regular) then
local _, new_rank = Rank.reset_player_rank(target_name)
game.print({'admin_commands.regular_remove_success', Utils.get_actor(), target_name, new_rank}, Color.info)
if target then
target.print({'admin_commands.regular_remove_notify_target'}, Color.warning)
game.print({'admin_commands.regular_remove_success', actor, target_name, new_rank}, Color.info)
if maybe_target_player then
maybe_target_player.print({'admin_commands.regular_remove_notify_target'}, Color.warning)
end
else
local rank_name = Rank.get_player_rank_name(target_name)
@ -84,27 +110,27 @@ end
--- Put someone on probation
local function probation_add(args)
local target_ident = args.player
local target, target_name = Utils.validate_player(target_ident)
local target_name = args.player
local maybe_target_player = game.get_player(target_name)
local actor = Utils.get_actor()
if not target then
print_no_target(target_ident)
if not maybe_target_player and not know_player_or_rerun(target_name, actor, 'probation') then
return
end
if Rank.equal(target_name, Ranks.admin) then
Game.player_print({'admin_commands.probation_add_fail_admin'}, Color.fail)
if target then
target.print({'admin_commands.probation_warn_admin', Utils.get_actor()}, Color.warning)
if maybe_target_player then
maybe_target_player.print({'admin_commands.probation_warn_admin', actor}, Color.warning)
end
return
end
local success = Rank.decrease_player_rank_to(target_name, Ranks.probation)
if success then
game.print({'admin_commands.probation_add_success', Utils.get_actor(), target_name}, Color.info)
if target then
target.print({'admin_commands.probation_add_notify_target'}, Color.warning)
game.print({'admin_commands.probation_add_success', actor, target_name}, Color.info)
if maybe_target_player then
maybe_target_player.print({'admin_commands.probation_add_notify_target'}, Color.warning)
end
else
Game.player_print({'admin_commands.probation_add_fail', target_name}, Color.fail)
@ -113,19 +139,19 @@ end
--- Remove someone from probation
local function probation_remove(args)
local target_ident = args.player
local target, target_name = Utils.validate_player(target_ident)
local target_name = args.player
local maybe_target_player = game.get_player(target_name)
local actor = Utils.get_actor()
if not target then
print_no_target(target_ident)
if not maybe_target_player and not know_player_or_rerun(target_name, actor, 'probation-remove') then
return
end
if Rank.equal(target_name, Ranks.probation) then
Rank.reset_player_rank(target_name)
game.print({'admin_commands.probation_remove_success', Utils.get_actor(), target_name}, Color.info)
if target then
target.print({'admin_commands.probation_remove_notify_target'}, Color.warning)
game.print({'admin_commands.probation_remove_success', actor, target_name}, Color.info)
if maybe_target_player then
maybe_target_player.print({'admin_commands.probation_remove_notify_target'}, Color.warning)
end
else
Game.player_print({'admin_commands.probation_remove_fail', target_name}, Color.fail)
@ -296,6 +322,7 @@ end
-- Event registrations
Event.add(defines.events.on_built_entity, built_entity)
Event.add(defines.events.on_console_command, console_command)
-- Command registrations

View File

@ -148,6 +148,11 @@ Event.add(defines.events.on_pre_player_left_game, function(event)
end)
Event.add(defines.events.on_player_banned, function(event)
local player_index = event.player_index
if not player_index then
return
end
local player = game.get_player(event.player_index)
if not player or not player.valid then
return

View File

@ -144,6 +144,10 @@ end
-- Exposed functions
function Public.know_player(player_name)
return player_ranks[player_name] ~= nil
end
--- Gets a player's rank. In cases of comparison, the appropriate functions should be used.
-- This function is exposed for the purpose of returning a numerical value for players for the
-- purposes of sorting.

View File

@ -3,6 +3,7 @@
[common]
fail_no_target=No player found with name: __1__
warn_no_target=Warning: player __1__ is not found, but the command will still be executed.
rerun_no_target=No player found with name: __1__. Run the command again to force execution.
close_button=Close
server_unavailable=The server is currently unavailable.
player_name=Player name

View File

@ -82,6 +82,20 @@ function Module.get_actor()
return '<server>'
end
--- Returns a valid string with the name of the actor for a player_index if they are an admin or the server.
function Module.get_admin_or_server_actor(player_index)
if not player_index then
return '<server>'
end
local player = game.get_player(player_index)
if player.valid and player.admin then
return player.name
end
return nil
end
function Module.cast_bool(var)
if var then
return true