2020-07-01 09:51:37 +00:00
|
|
|
use common::msg::{ClientState, RequestStateError, ServerMsg};
|
2019-10-15 04:06:14 +00:00
|
|
|
use hashbrown::HashSet;
|
2020-07-05 23:29:28 +00:00
|
|
|
use network::{Participant, Stream};
|
2019-10-06 17:35:47 +00:00
|
|
|
use specs::{Component, FlaggedStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-07-05 23:29:28 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-10-06 17:35:47 +00:00
|
|
|
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,
|
2020-07-05 23:29:28 +00:00
|
|
|
pub participant: Mutex<Option<Arc<Participant>>>,
|
2020-07-04 09:52:21 +00:00
|
|
|
pub singleton_stream: Stream,
|
2019-03-03 22:02:38 +00:00
|
|
|
pub last_ping: f64,
|
2019-12-31 08:10:51 +00:00
|
|
|
pub login_msg_sent: bool,
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
impl Component for Client {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
impl Client {
|
2020-07-04 09:52:21 +00:00
|
|
|
pub fn notify(&mut self, msg: ServerMsg) { let _ = self.singleton_stream.send(msg); }
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
pub fn is_registered(&self) -> bool {
|
|
|
|
match self.client_state {
|
2019-12-31 08:10:51 +00:00
|
|
|
ClientState::Registered | ClientState::Spectator | ClientState::Character => true,
|
2019-10-15 04:06:14 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
pub fn is_ingame(&self) -> bool {
|
|
|
|
match self.client_state {
|
2019-12-31 08:10:51 +00:00
|
|
|
ClientState::Spectator | ClientState::Character => true,
|
2019-10-15 04:06:14 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-20 17:54:37 +00:00
|
|
|
pub fn allow_state(&mut self, new_state: ClientState) {
|
|
|
|
self.client_state = new_state;
|
2020-07-01 09:51:37 +00:00
|
|
|
let _ = self
|
|
|
|
.singleton_stream
|
|
|
|
.send(ServerMsg::StateAnswer(Ok(new_state)));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-20 17:54:37 +00:00
|
|
|
pub fn error_state(&mut self, error: RequestStateError) {
|
2020-07-01 09:51:37 +00:00
|
|
|
let _ = self
|
|
|
|
.singleton_stream
|
|
|
|
.send(ServerMsg::StateAnswer(Err((error, self.client_state))));
|
2019-04-20 17:54:37 +00:00
|
|
|
}
|
2019-04-10 17:23:27 +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 {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
2019-10-06 17:35:47 +00:00
|
|
|
}
|