2019-03-03 22:02:38 +00:00
|
|
|
use common::{
|
2019-04-23 09:53:45 +00:00
|
|
|
msg::{ClientMsg, ClientState, RequestStateError, ServerMsg},
|
2019-03-03 22:02:38 +00:00
|
|
|
net::PostBox,
|
|
|
|
};
|
2019-04-23 09:53:45 +00:00
|
|
|
use specs::Entity as EcsEntity;
|
|
|
|
use std::collections::HashMap;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
pub struct Client {
|
2019-04-19 19:32:47 +00:00
|
|
|
pub client_state: ClientState,
|
2019-03-03 22:02:38 +00:00
|
|
|
pub postbox: PostBox<ServerMsg, ClientMsg>,
|
|
|
|
pub last_ping: f64,
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
impl Client {
|
|
|
|
pub fn notify(&mut self, msg: ServerMsg) {
|
2019-04-11 22:26:43 +00:00
|
|
|
self.postbox.send_message(msg);
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
2019-04-20 17:54:37 +00:00
|
|
|
pub fn allow_state(&mut self, new_state: ClientState) {
|
|
|
|
self.client_state = new_state;
|
2019-04-23 09:53:45 +00:00
|
|
|
self.postbox
|
|
|
|
.send_message(ServerMsg::StateAnswer(Ok(new_state)));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
|
|
|
pub fn error_state(&mut self, error: RequestStateError) {
|
2019-04-23 09:53:45 +00:00
|
|
|
self.postbox
|
|
|
|
.send_message(ServerMsg::StateAnswer(Err((error, self.client_state))));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
|
|
|
pub fn force_state(&mut self, new_state: ClientState) {
|
|
|
|
self.client_state = new_state;
|
2019-04-23 09:53:45 +00:00
|
|
|
self.postbox.send_message(ServerMsg::ForceState(new_state));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
pub struct Clients {
|
2019-04-10 23:16:29 +00:00
|
|
|
clients: HashMap<EcsEntity, Client>,
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clients {
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self {
|
2019-04-10 23:16:29 +00:00
|
|
|
clients: HashMap::new(),
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:16:29 +00:00
|
|
|
pub fn add(&mut self, entity: EcsEntity, client: Client) {
|
|
|
|
self.clients.insert(entity, client);
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:20:25 +00:00
|
|
|
pub fn get<'a>(&'a self, entity: &EcsEntity) -> Option<&'a Client> {
|
|
|
|
self.clients.get(entity)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut<'a>(&'a mut self, entity: &EcsEntity) -> Option<&'a mut Client> {
|
|
|
|
self.clients.get_mut(entity)
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:16:29 +00:00
|
|
|
pub fn remove_if<F: FnMut(EcsEntity, &mut Client) -> bool>(&mut self, mut f: F) {
|
|
|
|
self.clients.retain(|entity, client| !f(*entity, client));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify(&mut self, entity: EcsEntity, msg: ServerMsg) {
|
|
|
|
if let Some(client) = self.clients.get_mut(&entity) {
|
|
|
|
client.notify(msg);
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 00:38:29 +00:00
|
|
|
pub fn notify_registered(&mut self, msg: ServerMsg) {
|
2019-04-10 23:16:29 +00:00
|
|
|
for client in self.clients.values_mut() {
|
2019-04-21 15:52:15 +00:00
|
|
|
if client.client_state != ClientState::Connected {
|
2019-04-10 23:16:29 +00:00
|
|
|
client.notify(msg.clone());
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 22:05:37 +00:00
|
|
|
|
2019-04-22 00:38:29 +00:00
|
|
|
pub fn notify_ingame(&mut self, msg: ServerMsg) {
|
|
|
|
for client in self.clients.values_mut() {
|
2019-04-23 09:53:45 +00:00
|
|
|
if client.client_state == ClientState::Spectator
|
|
|
|
|| client.client_state == ClientState::Character
|
|
|
|
{
|
2019-04-22 00:38:29 +00:00
|
|
|
client.notify(msg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 18:07:35 +00:00
|
|
|
pub fn notify_ingame_if<F: FnMut(EcsEntity) -> bool>(&mut self, msg: ServerMsg, mut f: F) {
|
2019-06-06 14:48:41 +00:00
|
|
|
for (_entity, client) in self.clients.iter_mut().filter(|(e, _)| f(**e)) {
|
2019-05-27 18:07:35 +00:00
|
|
|
if client.client_state == ClientState::Spectator
|
|
|
|
|| client.client_state == ClientState::Character
|
|
|
|
{
|
|
|
|
client.notify(msg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 00:38:29 +00:00
|
|
|
pub fn notify_registered_except(&mut self, except_entity: EcsEntity, msg: ServerMsg) {
|
2019-04-10 23:16:29 +00:00
|
|
|
for (entity, client) in self.clients.iter_mut() {
|
2019-04-21 15:52:15 +00:00
|
|
|
if client.client_state != ClientState::Connected && *entity != except_entity {
|
2019-04-10 23:16:29 +00:00
|
|
|
client.notify(msg.clone());
|
2019-03-04 22:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-22 00:38:29 +00:00
|
|
|
|
|
|
|
pub fn notify_ingame_except(&mut self, except_entity: EcsEntity, msg: ServerMsg) {
|
|
|
|
for (entity, client) in self.clients.iter_mut() {
|
2019-04-23 09:53:45 +00:00
|
|
|
if (client.client_state == ClientState::Spectator
|
|
|
|
|| client.client_state == ClientState::Character)
|
|
|
|
&& *entity != except_entity
|
|
|
|
{
|
2019-04-22 00:38:29 +00:00
|
|
|
client.notify(msg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 18:07:35 +00:00
|
|
|
|
|
|
|
pub fn notify_ingame_if_except<F: FnMut(EcsEntity) -> bool>(
|
|
|
|
&mut self,
|
|
|
|
except_entity: EcsEntity,
|
|
|
|
msg: ServerMsg,
|
|
|
|
mut f: F,
|
|
|
|
) {
|
|
|
|
for (entity, client) in self.clients.iter_mut().filter(|(e, _)| f(**e)) {
|
|
|
|
if (client.client_state == ClientState::Spectator
|
|
|
|
|| client.client_state == ClientState::Character)
|
|
|
|
&& *entity != except_entity
|
|
|
|
{
|
|
|
|
client.notify(msg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|