From 586bf5faffdb2e6d655c93872dcd03fc1ae12365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Korg=C3=B3l?= Date: Sat, 17 Aug 2019 20:16:58 +0200 Subject: [PATCH] Add adminify command to temporarily give admin perms --- common/src/comp/admin.rs | 2 +- server/src/cmd.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/common/src/comp/admin.rs b/common/src/comp/admin.rs index 025fcc9bf2..5ce3c59327 100644 --- a/common/src/comp/admin.rs +++ b/common/src/comp/admin.rs @@ -1,6 +1,6 @@ use specs::{Component, NullStorage}; -#[derive(Default)] +#[derive(Clone, Copy, Default)] pub struct Admin; impl Component for Admin { diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 3853b0c48e..df5b63775d 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -190,6 +190,13 @@ lazy_static! { false, handle_explosion, ), + ChatCommand::new( + "adminify", + "{}", + "/adminify : Temporarily gives a player admin permissions or removes them", + true, + handle_adminify, + ) ]; } @@ -732,6 +739,39 @@ fn handle_explosion(server: &mut Server, entity: EcsEntity, args: String, action } } +fn handle_adminify(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { + if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) { + let ecs = server.state.ecs(); + let opt_player = (&ecs.entities(), &ecs.read_storage::()) + .join() + .find(|(_, player)| player.alias == alias) + .map(|(entity, _)| entity); + match opt_player { + Some(player) => match server.state.read_component_cloned::(player) { + Some(_admin) => { + ecs.write_storage::().remove(player); + } + None => { + server.state.write_component(player, comp::Admin); + } + }, + None => { + server.clients.notify( + entity, + ServerMsg::private(format!("Player '{}' not found!", alias)), + ); + server + .clients + .notify(entity, ServerMsg::private(String::from(action.help_string))); + } + } + } else { + server + .clients + .notify(entity, ServerMsg::private(String::from(action.help_string))); + } +} + fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) { if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) { let ecs = server.state.ecs();