From c975f811e71593b32a57a8ca8b37f7f606883b8c Mon Sep 17 00:00:00 2001 From: ubruntu Date: Fri, 4 Dec 2020 02:18:42 +0000 Subject: [PATCH] Chat commands for group manipulation --- CHANGELOG.md | 2 + common/src/cmd.rs | 28 +++++++++ server/src/cmd.rs | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5723f93aa..54c3145669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added chat commands for inviting, kicking, leaving, and promoting in groups + ### Changed - Doubled range of ScaleMode slider when set to Custom diff --git a/common/src/cmd.rs b/common/src/cmd.rs index adf77aa169..8ab1e6266b 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -49,6 +49,10 @@ pub enum ChatCommand { GiveItem, Goto, Group, + GroupInvite, + GroupKick, + GroupLeave, + GroupPromote, Health, Help, Home, @@ -97,6 +101,10 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[ ChatCommand::GiveItem, ChatCommand::Goto, ChatCommand::Group, + ChatCommand::GroupInvite, + ChatCommand::GroupKick, + ChatCommand::GroupLeave, + ChatCommand::GroupPromote, ChatCommand::Health, ChatCommand::Help, ChatCommand::Home, @@ -259,6 +267,22 @@ impl ChatCommand { "Send messages to your group", NoAdmin, ), + ChatCommand::GroupInvite => cmd( + vec![PlayerName(Required)], + "Invite a player to join a group", + NoAdmin, + ), + ChatCommand::GroupKick => cmd( + vec![PlayerName(Required)], + "Remove a player from a group", + NoAdmin, + ), + ChatCommand::GroupLeave => cmd(vec![], "Leave the current group", NoAdmin), + ChatCommand::GroupPromote => cmd( + vec![PlayerName(Required)], + "Promote a player to group leader", + NoAdmin, + ), ChatCommand::Health => cmd( vec![Integer("hp", 100, Required)], "Set your current health", @@ -427,6 +451,10 @@ impl ChatCommand { ChatCommand::GiveItem => "give_item", ChatCommand::Goto => "goto", ChatCommand::Group => "group", + ChatCommand::GroupInvite => "group_invite", + ChatCommand::GroupKick => "group_kick", + ChatCommand::GroupPromote => "group_promote", + ChatCommand::GroupLeave => "group_leave", ChatCommand::Health => "health", ChatCommand::JoinFaction => "join_faction", ChatCommand::Help => "help", diff --git a/server/src/cmd.rs b/server/src/cmd.rs index b9dc5fa2df..cb93fc4b95 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -81,6 +81,10 @@ fn get_handler(cmd: &ChatCommand) -> CommandHandler { ChatCommand::GiveItem => handle_give_item, ChatCommand::Goto => handle_goto, ChatCommand::Group => handle_group, + ChatCommand::GroupInvite => handle_group_invite, + ChatCommand::GroupKick => handle_group_kick, + ChatCommand::GroupLeave => handle_group_leave, + ChatCommand::GroupPromote => handle_group_promote, ChatCommand::Health => handle_health, ChatCommand::Help => handle_help, ChatCommand::Home => handle_home, @@ -1451,6 +1455,144 @@ fn handle_group( } } +fn handle_group_invite( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + args: String, + action: &ChatCommand, +) { + if let Some(target_alias) = scan_fmt_some!(&args, &action.arg_fmt(), String) { + let ecs = server.state.ecs(); + let target_player_opt = (&ecs.entities(), &ecs.read_storage::()) + .join() + .find(|(_, player)| player.alias == target_alias) + .map(|(entity, _)| entity); + + if let Some(target_player) = target_player_opt { + let uid = *ecs + .read_storage::() + .get(target_player) + .expect("Failed to get uid for player"); + + ecs.read_resource::>() + .emit_now(ServerEvent::GroupManip( + client, + comp::GroupManip::Invite(uid), + )); + + server.notify_client( + client, + ChatType::CommandInfo.server_msg(format!("Invited {} to the group.", target_alias)), + ); + } else { + server.notify_client( + client, + ChatType::CommandError + .server_msg(format!("Player with alias {} not found", target_alias)), + ) + } + } else { + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + } +} + +fn handle_group_kick( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + args: String, + action: &ChatCommand, +) { + // Checking if leader is already done in group_manip + if let Some(target_alias) = scan_fmt_some!(&args, &action.arg_fmt(), String) { + let ecs = server.state.ecs(); + let target_player_opt = (&ecs.entities(), &ecs.read_storage::()) + .join() + .find(|(_, player)| player.alias == target_alias) + .map(|(entity, _)| entity); + + if let Some(target_player) = target_player_opt { + let uid = *ecs + .read_storage::() + .get(target_player) + .expect("Failed to get uid for player"); + + ecs.read_resource::>() + .emit_now(ServerEvent::GroupManip(client, comp::GroupManip::Kick(uid))); + } else { + server.notify_client( + client, + ChatType::CommandError + .server_msg(format!("Player with alias {} not found", target_alias)), + ) + } + } else { + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + } +} + +fn handle_group_leave( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + _args: String, + _action: &ChatCommand, +) { + server + .state + .ecs() + .read_resource::>() + .emit_now(ServerEvent::GroupManip(client, comp::GroupManip::Leave)); +} + +fn handle_group_promote( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + args: String, + action: &ChatCommand, +) { + // Checking if leader is already done in group_manip + if let Some(target_alias) = scan_fmt_some!(&args, &action.arg_fmt(), String) { + let ecs = server.state.ecs(); + let target_player_opt = (&ecs.entities(), &ecs.read_storage::()) + .join() + .find(|(_, player)| player.alias == target_alias) + .map(|(entity, _)| entity); + + if let Some(target_player) = target_player_opt { + let uid = *ecs + .read_storage::() + .get(target_player) + .expect("Failed to get uid for player"); + + ecs.read_resource::>() + .emit_now(ServerEvent::GroupManip( + client, + comp::GroupManip::AssignLeader(uid), + )); + } else { + server.notify_client( + client, + ChatType::CommandError + .server_msg(format!("Player with alias {} not found", target_alias)), + ) + } + } else { + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + } +} + fn handle_region( server: &mut Server, client: EcsEntity,