Chat commands for group manipulation

This commit is contained in:
ubruntu 2020-12-04 02:18:42 +00:00 committed by Imbris
parent f8d48ad4a0
commit c975f811e7
3 changed files with 172 additions and 0 deletions

View File

@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added chat commands for inviting, kicking, leaving, and promoting in groups
### Changed ### Changed
- Doubled range of ScaleMode slider when set to Custom - Doubled range of ScaleMode slider when set to Custom

View File

@ -49,6 +49,10 @@ pub enum ChatCommand {
GiveItem, GiveItem,
Goto, Goto,
Group, Group,
GroupInvite,
GroupKick,
GroupLeave,
GroupPromote,
Health, Health,
Help, Help,
Home, Home,
@ -97,6 +101,10 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[
ChatCommand::GiveItem, ChatCommand::GiveItem,
ChatCommand::Goto, ChatCommand::Goto,
ChatCommand::Group, ChatCommand::Group,
ChatCommand::GroupInvite,
ChatCommand::GroupKick,
ChatCommand::GroupLeave,
ChatCommand::GroupPromote,
ChatCommand::Health, ChatCommand::Health,
ChatCommand::Help, ChatCommand::Help,
ChatCommand::Home, ChatCommand::Home,
@ -259,6 +267,22 @@ impl ChatCommand {
"Send messages to your group", "Send messages to your group",
NoAdmin, 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( ChatCommand::Health => cmd(
vec![Integer("hp", 100, Required)], vec![Integer("hp", 100, Required)],
"Set your current health", "Set your current health",
@ -427,6 +451,10 @@ impl ChatCommand {
ChatCommand::GiveItem => "give_item", ChatCommand::GiveItem => "give_item",
ChatCommand::Goto => "goto", ChatCommand::Goto => "goto",
ChatCommand::Group => "group", ChatCommand::Group => "group",
ChatCommand::GroupInvite => "group_invite",
ChatCommand::GroupKick => "group_kick",
ChatCommand::GroupPromote => "group_promote",
ChatCommand::GroupLeave => "group_leave",
ChatCommand::Health => "health", ChatCommand::Health => "health",
ChatCommand::JoinFaction => "join_faction", ChatCommand::JoinFaction => "join_faction",
ChatCommand::Help => "help", ChatCommand::Help => "help",

View File

@ -81,6 +81,10 @@ fn get_handler(cmd: &ChatCommand) -> CommandHandler {
ChatCommand::GiveItem => handle_give_item, ChatCommand::GiveItem => handle_give_item,
ChatCommand::Goto => handle_goto, ChatCommand::Goto => handle_goto,
ChatCommand::Group => handle_group, 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::Health => handle_health,
ChatCommand::Help => handle_help, ChatCommand::Help => handle_help,
ChatCommand::Home => handle_home, 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::<comp::Player>())
.join()
.find(|(_, player)| player.alias == target_alias)
.map(|(entity, _)| entity);
if let Some(target_player) = target_player_opt {
let uid = *ecs
.read_storage::<Uid>()
.get(target_player)
.expect("Failed to get uid for player");
ecs.read_resource::<EventBus<ServerEvent>>()
.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::<comp::Player>())
.join()
.find(|(_, player)| player.alias == target_alias)
.map(|(entity, _)| entity);
if let Some(target_player) = target_player_opt {
let uid = *ecs
.read_storage::<Uid>()
.get(target_player)
.expect("Failed to get uid for player");
ecs.read_resource::<EventBus<ServerEvent>>()
.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::<EventBus<ServerEvent>>()
.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::<comp::Player>())
.join()
.find(|(_, player)| player.alias == target_alias)
.map(|(entity, _)| entity);
if let Some(target_player) = target_player_opt {
let uid = *ecs
.read_storage::<Uid>()
.get(target_player)
.expect("Failed to get uid for player");
ecs.read_resource::<EventBus<ServerEvent>>()
.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( fn handle_region(
server: &mut Server, server: &mut Server,
client: EcsEntity, client: EcsEntity,