command to show the online players

This commit is contained in:
Thomas JULLIEN 2019-06-11 12:24:35 +08:00 committed by timokoesters
parent 22af8cb9d5
commit 7182257214
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097

View File

@ -98,6 +98,12 @@ lazy_static! {
"/spawn <alignment> <entity> [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::<comp::Player>();
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