Rebased on master and moved logout message to events

This commit is contained in:
Capucho 2020-03-10 21:04:13 +00:00
parent 16e4214a52
commit a9580a81bc
2 changed files with 10 additions and 20 deletions

View File

@ -7,7 +7,7 @@ use common::{
sync::{Uid, UidAllocator}, sync::{Uid, UidAllocator},
}; };
use log::error; use log::error;
use specs::{saveload::MarkerAllocator, Builder, Entity as EcsEntity, WorldExt}; use specs::{saveload::MarkerAllocator, Builder, Entity as EcsEntity, Join, WorldExt};
pub fn handle_exit_ingame(server: &mut Server, entity: EcsEntity) { pub fn handle_exit_ingame(server: &mut Server, entity: EcsEntity) {
let state = server.state_mut(); let state = server.state_mut();
@ -53,12 +53,19 @@ pub fn handle_client_disconnect(server: &mut Server, entity: EcsEntity) -> Event
} }
// Make sure to remove the player from the logged in list. (See AuthProvider) // Make sure to remove the player from the logged in list. (See AuthProvider)
// And send a disconnected message
{ {
let players = state.ecs().read_storage::<Player>(); let players = state.ecs().read_storage::<Player>();
let mut accounts = state.ecs().write_resource::<AuthProvider>(); let mut accounts = state.ecs().write_resource::<AuthProvider>();
let mut clients = state.ecs().write_storage::<Client>();
if let Some(player) = players.get(entity) { if let Some(player) = players.get(entity) {
accounts.logout(player.uuid()); accounts.logout(player.uuid());
let msg = ServerMsg::broadcast(format!("{} went offline.", &player.alias));
for client in (&mut clients).join().filter(|c| c.is_registered()) {
client.notify(msg.clone());
}
} }
} }
// Delete client entity // Delete client entity

View File

@ -1,7 +1,7 @@
use super::SysTimer; use super::SysTimer;
use crate::{auth_provider::AuthProvider, client::Client, CLIENT_TIMEOUT}; use crate::{auth_provider::AuthProvider, client::Client, CLIENT_TIMEOUT};
use common::{ use common::{
comp::{Admin, Body, CanBuild, Controller, ForceUpdate, Ori, Player, Pos, Stats, Vel}, comp::{Admin, CanBuild, Controller, ForceUpdate, Ori, Player, Pos, Stats, Vel},
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
msg::{ msg::{
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerListUpdate, validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, PlayerListUpdate,
@ -27,7 +27,6 @@ impl<'a> System<'a> for Sys {
ReadExpect<'a, TerrainGrid>, ReadExpect<'a, TerrainGrid>,
Write<'a, SysTimer<Self>>, Write<'a, SysTimer<Self>>,
ReadStorage<'a, Uid>, ReadStorage<'a, Uid>,
ReadStorage<'a, Body>,
ReadStorage<'a, CanBuild>, ReadStorage<'a, CanBuild>,
ReadStorage<'a, Admin>, ReadStorage<'a, Admin>,
ReadStorage<'a, ForceUpdate>, ReadStorage<'a, ForceUpdate>,
@ -51,7 +50,6 @@ impl<'a> System<'a> for Sys {
terrain, terrain,
mut timer, mut timer,
uids, uids,
bodies,
can_build, can_build,
admins, admins,
force_updates, force_updates,
@ -81,7 +79,6 @@ impl<'a> System<'a> for Sys {
let mut new_players = Vec::new(); let mut new_players = Vec::new();
for (entity, client) in (&entities, &mut clients).join() { for (entity, client) in (&entities, &mut clients).join() {
let mut disconnect = false;
let new_msgs = client.postbox.new_messages(); let new_msgs = client.postbox.new_messages();
// Update client ping. // Update client ping.
@ -285,27 +282,13 @@ impl<'a> System<'a> for Sys {
ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong), ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong),
ClientMsg::Pong => {}, ClientMsg::Pong => {},
ClientMsg::Disconnect => { ClientMsg::Disconnect => {
disconnect = true; client.postbox.send_message(ServerMsg::Disconnect);
}, },
ClientMsg::Terminate => { ClientMsg::Terminate => {
server_emitter.emit(ServerEvent::ClientDisconnect(entity)); server_emitter.emit(ServerEvent::ClientDisconnect(entity));
}, },
} }
} }
if disconnect {
if let (Some(player), Some(_)) = (
players.get(entity),
// It only shows a message if you had a body (not in char selection)
bodies.get(entity),
) {
new_chat_msgs.push((
None,
ServerMsg::broadcast(format!("{} went offline.", &player.alias)),
));
}
client.postbox.send_message(ServerMsg::Disconnect);
}
} }
// Handle new players. // Handle new players.