diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 1b6cef0047..b8bc2416c5 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -98,6 +98,12 @@ lazy_static! { "/spawn [amount] : Spawn a test entity", handle_spawn, ), + ChatCommand::new( + "who", + "{}", + "/who : Show the online players list", + handle_who + ), ChatCommand::new( "help", "", "/help: Display this message", handle_help) ]; @@ -285,6 +291,31 @@ fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &C } } +fn handle_who(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { + let ecs = server.state.ecs(); + let players = ecs.read_storage::(); + let count = players.join().count(); + let mut str: String = format!("Online players ({})", count); + if count > 0 { + str += ": "; + let mut player_list: String = players + .join() + .fold(String::new(), |mut s, player| { + s += &player.alias; + s += ","; + s + }); + player_list.pop(); + server + .clients + .notify(entity, ServerMsg::Chat(str + &player_list)); + } else { + server + .clients + .notify(entity, ServerMsg::Chat(str)); + } +} + fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { for cmd in CHAT_COMMANDS.iter() { server