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-10-06 17:35:47 +00:00
|
|
|
use hashbrown::{hash_map::DefaultHashBuilder, HashSet};
|
|
|
|
use indexmap::IndexMap;
|
2019-04-23 09:53:45 +00:00
|
|
|
use specs::Entity as EcsEntity;
|
2019-10-06 17:35:47 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
|
|
|
use specs_idvs::IDVStorage;
|
|
|
|
use vek::*;
|
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-10-06 17:35:47 +00:00
|
|
|
clients: IndexMap<EcsEntity, Client, DefaultHashBuilder>,
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clients {
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
Self {
|
2019-10-06 17:35:47 +00:00
|
|
|
clients: IndexMap::default(),
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 11:19:26 +00:00
|
|
|
pub fn len(&mut self) -> usize {
|
|
|
|
self.clients.len()
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-10-06 17:35:47 +00:00
|
|
|
self.clients.get_mut(entit:y)
|
2019-05-19 22:20:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 04:30:34 +00:00
|
|
|
pub fn remove<'a>(&'a mut self, entity: &EcsEntity) -> Option<Client> {
|
|
|
|
self.clients.remove(entity)
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
pub fn get_client_index_ingame<'a>(&'a mut self, entity: &EcsEntity) -> Option<usize> {
|
|
|
|
self.clients.get_full(entity).and_then(|(i, _, c)| {
|
|
|
|
if c.client_state == ClientState::Spectator
|
|
|
|
|| c.client_state == ClientState::Character
|
|
|
|
|| c.client_state == ClientState::Dead
|
|
|
|
{
|
|
|
|
Some(i)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
pub fn notify_index(&mut self, index: usize, msg: ServerMsg) {
|
|
|
|
if let Some((_, client)) = self.clients.get_index_mut(index) {
|
|
|
|
client.notify(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:16:29 +00:00
|
|
|
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-06-30 20:25:37 +00:00
|
|
|
|| client.client_state == ClientState::Dead
|
2019-04-23 09:53:45 +00:00
|
|
|
{
|
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
|
2019-06-30 20:25:37 +00:00
|
|
|
|| client.client_state == ClientState::Dead
|
2019-05-27 18:07:35 +00:00
|
|
|
{
|
|
|
|
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
|
2019-06-30 20:25:37 +00:00
|
|
|
|| client.client_state == ClientState::Character
|
|
|
|
|| client.client_state == ClientState::Dead)
|
2019-04-23 09:53:45 +00:00
|
|
|
&& *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
|
2019-06-30 20:25:37 +00:00
|
|
|
|| client.client_state == ClientState::Character
|
|
|
|
|| client.client_state == ClientState::Dead)
|
2019-05-27 18:07:35 +00:00
|
|
|
&& *entity != except_entity
|
|
|
|
{
|
|
|
|
client.notify(msg.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
|
|
|
|
// Distance from fuzzy_chunk before snapping to current chunk
|
|
|
|
pub const CHUNK_FUZZ: u32 = 2;
|
|
|
|
// Distance out of the range of a region before removing it from subscriptions
|
|
|
|
pub const REGION_FUZZ: u32 = 16;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RegionSubscription {
|
|
|
|
pub fuzzy_chunk: Vec2<i32>,
|
|
|
|
pub regions: HashSet<Vec2<i32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for RegionSubscription {
|
|
|
|
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
|
|
|
|
}
|