2019-10-20 07:20:21 +00:00
|
|
|
use super::SysTimer;
|
2020-05-12 23:58:15 +00:00
|
|
|
use crate::{
|
2020-07-16 14:05:35 +00:00
|
|
|
alias_validator::AliasValidator, auth_provider::AuthProvider, client::Client,
|
|
|
|
persistence::character::CharacterLoader, ServerSettings, CLIENT_TIMEOUT,
|
2020-05-12 23:58:15 +00:00
|
|
|
};
|
2019-10-15 04:06:14 +00:00
|
|
|
use common::{
|
2020-05-24 22:18:41 +00:00
|
|
|
comp::{
|
2020-06-24 03:29:46 +00:00
|
|
|
Admin, AdminList, CanBuild, ChatMode, ChatMsg, ChatType, ControlEvent, Controller,
|
|
|
|
ForceUpdate, Ori, Player, Pos, Stats, Vel,
|
2020-05-24 22:18:41 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2019-12-23 06:02:00 +00:00
|
|
|
msg::{
|
2020-05-24 09:20:54 +00:00
|
|
|
validate_chat_msg, CharacterInfo, ChatMsgValidationError, ClientMsg, ClientState,
|
|
|
|
PlayerInfo, PlayerListUpdate, RequestStateError, ServerMsg, MAX_BYTES_CHAT_MSG,
|
2019-12-23 06:02:00 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
state::{BlockChange, Time},
|
2019-12-23 06:02:00 +00:00
|
|
|
sync::Uid,
|
2020-06-25 12:07:01 +00:00
|
|
|
terrain::{Block, TerrainChunkSize, TerrainGrid},
|
|
|
|
vol::{RectVolSize, Vox},
|
2019-10-15 04:06:14 +00:00
|
|
|
};
|
2020-07-01 09:51:37 +00:00
|
|
|
use futures_executor::block_on;
|
|
|
|
use futures_timer::Delay;
|
|
|
|
use futures_util::{select, FutureExt};
|
2019-12-23 06:02:00 +00:00
|
|
|
use hashbrown::HashMap;
|
2019-10-15 04:06:14 +00:00
|
|
|
use specs::{
|
|
|
|
Entities, Join, Read, ReadExpect, ReadStorage, System, Write, WriteExpect, WriteStorage,
|
|
|
|
};
|
2020-07-01 09:45:39 +00:00
|
|
|
|
|
|
|
impl Sys {
|
2020-07-11 14:08:25 +00:00
|
|
|
///We needed to move this to a async fn, if we would use a async closures
|
|
|
|
/// the compiler generates to much recursion and fails to compile this
|
2020-07-01 09:45:39 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
async fn handle_client_msg(
|
|
|
|
server_emitter: &mut common::event::Emitter<'_, ServerEvent>,
|
|
|
|
new_chat_msgs: &mut Vec<(Option<specs::Entity>, ChatMsg)>,
|
|
|
|
player_list: &HashMap<Uid, PlayerInfo>,
|
|
|
|
new_players: &mut Vec<specs::Entity>,
|
|
|
|
entity: specs::Entity,
|
|
|
|
client: &mut Client,
|
|
|
|
cnt: &mut u64,
|
|
|
|
character_loader: &ReadExpect<'_, CharacterLoader>,
|
|
|
|
terrain: &ReadExpect<'_, TerrainGrid>,
|
|
|
|
uids: &ReadStorage<'_, Uid>,
|
|
|
|
can_build: &ReadStorage<'_, CanBuild>,
|
|
|
|
force_updates: &ReadStorage<'_, ForceUpdate>,
|
2020-07-03 19:40:37 +00:00
|
|
|
stats: &mut WriteStorage<'_, Stats>,
|
2020-07-01 09:45:39 +00:00
|
|
|
chat_modes: &ReadStorage<'_, ChatMode>,
|
|
|
|
accounts: &mut WriteExpect<'_, AuthProvider>,
|
|
|
|
block_changes: &mut Write<'_, BlockChange>,
|
|
|
|
admin_list: &ReadExpect<'_, AdminList>,
|
|
|
|
admins: &mut WriteStorage<'_, Admin>,
|
|
|
|
positions: &mut WriteStorage<'_, Pos>,
|
|
|
|
velocities: &mut WriteStorage<'_, Vel>,
|
|
|
|
orientations: &mut WriteStorage<'_, Ori>,
|
|
|
|
players: &mut WriteStorage<'_, Player>,
|
|
|
|
controllers: &mut WriteStorage<'_, Controller>,
|
|
|
|
settings: &Read<'_, ServerSettings>,
|
2020-07-16 14:05:35 +00:00
|
|
|
alias_validator: &ReadExpect<'_, AliasValidator>,
|
2020-07-01 09:45:39 +00:00
|
|
|
) -> Result<(), crate::error::Error> {
|
|
|
|
loop {
|
2020-07-11 14:08:25 +00:00
|
|
|
let msg = client.recv().await?;
|
2020-07-01 09:45:39 +00:00
|
|
|
*cnt += 1;
|
|
|
|
match msg {
|
|
|
|
// Go back to registered state (char selection screen)
|
|
|
|
ClientMsg::ExitIngame => match client.client_state {
|
|
|
|
// Use ClientMsg::Register instead.
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientState::Connected => client.error_state(RequestStateError::WrongMessage),
|
2020-07-01 09:45:39 +00:00
|
|
|
ClientState::Registered => client.error_state(RequestStateError::Already),
|
|
|
|
ClientState::Spectator | ClientState::Character => {
|
|
|
|
server_emitter.emit(ServerEvent::ExitIngame { entity });
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
// Request spectator state
|
|
|
|
ClientMsg::Spectate => match client.client_state {
|
|
|
|
// Become Registered first.
|
|
|
|
ClientState::Connected => client.error_state(RequestStateError::Impossible),
|
|
|
|
ClientState::Spectator => client.error_state(RequestStateError::Already),
|
|
|
|
ClientState::Registered | ClientState::Character => {
|
|
|
|
client.allow_state(ClientState::Spectator)
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
// Request registered state (login)
|
|
|
|
ClientMsg::Register {
|
|
|
|
view_distance,
|
|
|
|
token_or_username,
|
|
|
|
} => {
|
|
|
|
let (username, uuid) = match accounts.query(token_or_username.clone()) {
|
|
|
|
Err(err) => {
|
|
|
|
client.error_state(RequestStateError::RegisterDenied(err));
|
|
|
|
break Ok(());
|
|
|
|
},
|
|
|
|
Ok((username, uuid)) => (username, uuid),
|
|
|
|
};
|
|
|
|
|
2020-07-01 09:51:37 +00:00
|
|
|
let vd =
|
|
|
|
view_distance.map(|vd| vd.min(settings.max_view_distance.unwrap_or(vd)));
|
2020-07-01 09:45:39 +00:00
|
|
|
let player = Player::new(username.clone(), None, vd, uuid);
|
|
|
|
let is_admin = admin_list.contains(&username);
|
|
|
|
|
|
|
|
if !player.is_valid() {
|
|
|
|
// Invalid player
|
|
|
|
client.error_state(RequestStateError::Impossible);
|
|
|
|
break Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
match client.client_state {
|
|
|
|
ClientState::Connected => {
|
|
|
|
// Add Player component to this client
|
|
|
|
let _ = players.insert(entity, player);
|
|
|
|
|
|
|
|
// Give the Admin component to the player if their name exists in
|
|
|
|
// admin list
|
|
|
|
if is_admin {
|
|
|
|
let _ = admins.insert(entity, Admin);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the client its request was successful.
|
|
|
|
client.allow_state(ClientState::Registered);
|
|
|
|
|
|
|
|
// Send initial player list
|
|
|
|
client.notify(ServerMsg::PlayerListUpdate(PlayerListUpdate::Init(
|
|
|
|
player_list.clone(),
|
|
|
|
)));
|
|
|
|
|
|
|
|
// Add to list to notify all clients of the new player
|
|
|
|
new_players.push(entity);
|
|
|
|
},
|
|
|
|
// Use RequestState instead (No need to send `player` again).
|
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
|
|
|
}
|
|
|
|
//client.allow_state(ClientState::Registered);
|
|
|
|
|
|
|
|
// Limit view distance if it's too high
|
|
|
|
// This comes after state registration so that the client actually hears it
|
|
|
|
if settings
|
|
|
|
.max_view_distance
|
|
|
|
.zip(view_distance)
|
2020-07-06 15:36:13 +00:00
|
|
|
.map(|(max, vd)| vd > max)
|
2020-07-01 09:45:39 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
client.notify(ServerMsg::SetViewDistance(
|
|
|
|
settings.max_view_distance.unwrap_or(0),
|
|
|
|
));
|
|
|
|
};
|
|
|
|
},
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientMsg::SetViewDistance(view_distance) => {
|
|
|
|
if let ClientState::Character { .. } = client.client_state {
|
2020-07-07 11:42:05 +00:00
|
|
|
players.get_mut(entity).map(|player| {
|
|
|
|
player.view_distance = Some(
|
|
|
|
settings
|
|
|
|
.max_view_distance
|
|
|
|
.map(|max| view_distance.min(max))
|
|
|
|
.unwrap_or(view_distance),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2020-07-01 09:45:39 +00:00
|
|
|
if settings
|
|
|
|
.max_view_distance
|
2020-07-07 11:42:05 +00:00
|
|
|
.map(|max| view_distance > max)
|
|
|
|
.unwrap_or(false)
|
2020-07-01 09:45:39 +00:00
|
|
|
{
|
|
|
|
client.notify(ServerMsg::SetViewDistance(
|
|
|
|
settings.max_view_distance.unwrap_or(0),
|
|
|
|
));
|
|
|
|
}
|
2020-07-01 09:51:37 +00:00
|
|
|
}
|
2020-07-01 09:45:39 +00:00
|
|
|
},
|
|
|
|
ClientMsg::Character(character_id) => match client.client_state {
|
|
|
|
// Become Registered first.
|
|
|
|
ClientState::Connected => client.error_state(RequestStateError::Impossible),
|
|
|
|
ClientState::Registered | ClientState::Spectator => {
|
|
|
|
// Only send login message if it wasn't already
|
|
|
|
// sent previously
|
2020-07-05 17:05:00 +00:00
|
|
|
if let Some(player) = players.get(entity) {
|
2020-07-01 09:45:39 +00:00
|
|
|
// Send a request to load the character's component data from the
|
|
|
|
// DB. Once loaded, persisted components such as stats and inventory
|
|
|
|
// will be inserted for the entity
|
|
|
|
character_loader.load_character_data(
|
|
|
|
entity,
|
|
|
|
player.uuid().to_string(),
|
|
|
|
character_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start inserting non-persisted/default components for the entity
|
|
|
|
// while we load the DB data
|
|
|
|
server_emitter.emit(ServerEvent::InitCharacterData {
|
|
|
|
entity,
|
|
|
|
character_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Give the player a welcome message
|
|
|
|
if settings.server_description.len() > 0 {
|
|
|
|
client.notify(
|
|
|
|
ChatType::CommandInfo
|
|
|
|
.server_msg(settings.server_description.clone()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only send login message if it wasn't already
|
|
|
|
// sent previously
|
|
|
|
if !client.login_msg_sent {
|
|
|
|
new_chat_msgs.push((None, ChatMsg {
|
|
|
|
chat_type: ChatType::Online,
|
|
|
|
message: format!("[{}] is now online.", &player.alias), // TODO: Localize this
|
2020-07-05 17:05:00 +00:00
|
|
|
}));
|
2020-07-01 09:45:39 +00:00
|
|
|
|
|
|
|
client.login_msg_sent = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client.notify(ServerMsg::CharacterDataLoadError(String::from(
|
|
|
|
"Failed to fetch player entity",
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Character => client.error_state(RequestStateError::Already),
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
ClientMsg::ControllerInputs(inputs) => match client.client_state {
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientState::Connected | ClientState::Registered | ClientState::Spectator => {
|
2020-07-01 09:45:39 +00:00
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
},
|
|
|
|
ClientState::Character => {
|
|
|
|
if let Some(controller) = controllers.get_mut(entity) {
|
|
|
|
controller.inputs.update_with_new(inputs);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
ClientMsg::ControlEvent(event) => match client.client_state {
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientState::Connected | ClientState::Registered | ClientState::Spectator => {
|
2020-07-01 09:45:39 +00:00
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
},
|
|
|
|
ClientState::Character => {
|
|
|
|
// Skip respawn if client entity is alive
|
|
|
|
if let ControlEvent::Respawn = event {
|
|
|
|
if stats.get(entity).map_or(true, |s| !s.is_dead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(controller) = controllers.get_mut(entity) {
|
|
|
|
controller.events.push(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
ClientMsg::ControlAction(event) => match client.client_state {
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientState::Connected | ClientState::Registered | ClientState::Spectator => {
|
2020-07-01 09:45:39 +00:00
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
},
|
|
|
|
ClientState::Character => {
|
|
|
|
if let Some(controller) = controllers.get_mut(entity) {
|
|
|
|
controller.actions.push(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
ClientMsg::ChatMsg(message) => match client.client_state {
|
|
|
|
ClientState::Connected => client.error_state(RequestStateError::Impossible),
|
2020-07-01 09:51:37 +00:00
|
|
|
ClientState::Registered | ClientState::Spectator | ClientState::Character => {
|
|
|
|
match validate_chat_msg(&message) {
|
|
|
|
Ok(()) => {
|
|
|
|
if let Some(from) = uids.get(entity) {
|
|
|
|
let mode = chat_modes.get(entity).cloned().unwrap_or_default();
|
|
|
|
let msg = mode.new_message(*from, message);
|
|
|
|
new_chat_msgs.push((Some(entity), msg));
|
|
|
|
} else {
|
|
|
|
tracing::error!("Could not send message. Missing player uid");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(ChatMsgValidationError::TooLong) => {
|
|
|
|
let max = MAX_BYTES_CHAT_MSG;
|
|
|
|
let len = message.len();
|
|
|
|
tracing::warn!(
|
2020-07-01 09:45:39 +00:00
|
|
|
?len,
|
|
|
|
?max,
|
|
|
|
"Recieved a chat message that's too long"
|
|
|
|
)
|
2020-07-01 09:51:37 +00:00
|
|
|
},
|
|
|
|
}
|
2020-07-01 09:45:39 +00:00
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
ClientMsg::PlayerPhysics { pos, vel, ori } => match client.client_state {
|
|
|
|
ClientState::Character => {
|
|
|
|
if force_updates.get(entity).is_none()
|
|
|
|
&& stats.get(entity).map_or(true, |s| !s.is_dead)
|
|
|
|
{
|
|
|
|
let _ = positions.insert(entity, pos);
|
|
|
|
let _ = velocities.insert(entity, vel);
|
|
|
|
let _ = orientations.insert(entity, ori);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Only characters can send positions.
|
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
|
|
|
},
|
|
|
|
ClientMsg::BreakBlock(pos) => {
|
|
|
|
if can_build.get(entity).is_some() {
|
|
|
|
block_changes.set(pos, Block::empty());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientMsg::PlaceBlock(pos, block) => {
|
|
|
|
if can_build.get(entity).is_some() {
|
|
|
|
block_changes.try_set(pos, block);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientMsg::TerrainChunkRequest { key } => match client.client_state {
|
|
|
|
ClientState::Connected | ClientState::Registered => {
|
|
|
|
client.error_state(RequestStateError::Impossible);
|
|
|
|
},
|
|
|
|
ClientState::Spectator | ClientState::Character => {
|
|
|
|
let in_vd = if let (Some(view_distance), Some(pos)) = (
|
|
|
|
players.get(entity).and_then(|p| p.view_distance),
|
|
|
|
positions.get(entity),
|
|
|
|
) {
|
|
|
|
pos.0.xy().map(|e| e as f64).distance(
|
|
|
|
key.map(|e| e as f64 + 0.5)
|
|
|
|
* TerrainChunkSize::RECT_SIZE.map(|e| e as f64),
|
2020-07-01 09:51:37 +00:00
|
|
|
) < (view_distance as f64 + 1.5) * TerrainChunkSize::RECT_SIZE.x as f64
|
2020-07-01 09:45:39 +00:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
if in_vd {
|
|
|
|
match terrain.get_key(key) {
|
2020-07-01 09:51:37 +00:00
|
|
|
Some(chunk) => client.notify(ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
|
|
|
chunk: Ok(Box::new(chunk.clone())),
|
|
|
|
}),
|
|
|
|
None => server_emitter.emit(ServerEvent::ChunkRequest(entity, key)),
|
2020-07-01 09:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Pending => {},
|
|
|
|
},
|
|
|
|
// Always possible.
|
|
|
|
ClientMsg::Ping => client.notify(ServerMsg::Pong),
|
|
|
|
ClientMsg::Pong => {},
|
|
|
|
ClientMsg::Disconnect => {
|
|
|
|
client.notify(ServerMsg::Disconnect);
|
|
|
|
},
|
|
|
|
ClientMsg::Terminate => {
|
|
|
|
server_emitter.emit(ServerEvent::ClientDisconnect(entity));
|
|
|
|
},
|
|
|
|
ClientMsg::RequestCharacterList => {
|
|
|
|
if let Some(player) = players.get(entity) {
|
|
|
|
character_loader.load_character_list(entity, player.uuid().to_string())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientMsg::CreateCharacter { alias, tool, body } => {
|
2020-07-16 14:05:35 +00:00
|
|
|
if let Err(error) = alias_validator.validate(&alias) {
|
|
|
|
tracing::debug!(
|
|
|
|
?error,
|
|
|
|
?alias,
|
|
|
|
"denied alias as it contained a banned word"
|
|
|
|
);
|
|
|
|
client.notify(ServerMsg::CharacterActionError(error.to_string()));
|
|
|
|
} else if let Some(player) = players.get(entity) {
|
2020-07-01 09:45:39 +00:00
|
|
|
character_loader.create_character(
|
|
|
|
entity,
|
|
|
|
player.uuid().to_string(),
|
|
|
|
alias,
|
|
|
|
tool,
|
|
|
|
body,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientMsg::DeleteCharacter(character_id) => {
|
|
|
|
if let Some(player) = players.get(entity) {
|
|
|
|
character_loader.delete_character(
|
|
|
|
entity,
|
|
|
|
player.uuid().to_string(),
|
|
|
|
character_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2020-07-03 19:40:37 +00:00
|
|
|
ClientMsg::UnlockSkill(skill) => {
|
|
|
|
stats
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|s| s.skill_set.unlock_skill(skill));
|
|
|
|
},
|
|
|
|
ClientMsg::RefundSkill(skill) => {
|
|
|
|
stats
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|s| s.skill_set.refund_skill(skill));
|
|
|
|
},
|
|
|
|
ClientMsg::UnlockSkillGroup(skill_group_type) => {
|
|
|
|
stats
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|s| s.skill_set.unlock_skill_group(skill_group_type));
|
|
|
|
},
|
2020-07-01 09:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
|
|
|
|
/// This system will handle new messages from clients
|
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)] // TODO: Pending review in #587
|
2019-10-15 04:06:14 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
Read<'a, Time>,
|
2020-06-16 01:00:32 +00:00
|
|
|
ReadExpect<'a, CharacterLoader>,
|
2019-10-15 04:06:14 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2019-10-20 07:20:21 +00:00
|
|
|
Write<'a, SysTimer<Self>>,
|
2019-12-23 06:02:00 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-10-15 04:06:14 +00:00
|
|
|
ReadStorage<'a, CanBuild>,
|
2019-11-29 06:04:37 +00:00
|
|
|
ReadStorage<'a, ForceUpdate>,
|
2020-07-03 19:40:37 +00:00
|
|
|
WriteStorage<'a, Stats>,
|
2020-06-02 02:42:26 +00:00
|
|
|
ReadStorage<'a, ChatMode>,
|
2019-10-15 04:06:14 +00:00
|
|
|
WriteExpect<'a, AuthProvider>,
|
|
|
|
Write<'a, BlockChange>,
|
2020-06-24 03:29:46 +00:00
|
|
|
ReadExpect<'a, AdminList>,
|
|
|
|
WriteStorage<'a, Admin>,
|
2019-10-15 04:06:14 +00:00
|
|
|
WriteStorage<'a, Pos>,
|
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Ori>,
|
|
|
|
WriteStorage<'a, Player>,
|
|
|
|
WriteStorage<'a, Client>,
|
|
|
|
WriteStorage<'a, Controller>,
|
2020-06-25 11:20:09 +00:00
|
|
|
Read<'a, ServerSettings>,
|
2020-07-16 14:05:35 +00:00
|
|
|
ReadExpect<'a, AliasValidator>,
|
2019-10-15 04:06:14 +00:00
|
|
|
);
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::match_ref_pats)] // TODO: Pending review in #587
|
|
|
|
#[allow(clippy::single_char_pattern)] // TODO: Pending review in #587
|
|
|
|
#[allow(clippy::single_match)] // TODO: Pending review in #587
|
2019-10-15 04:06:14 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2020-03-22 04:49:32 +00:00
|
|
|
server_event_bus,
|
2019-10-15 04:06:14 +00:00
|
|
|
time,
|
2020-06-16 01:00:32 +00:00
|
|
|
character_loader,
|
2019-10-15 04:06:14 +00:00
|
|
|
terrain,
|
2019-10-20 07:20:21 +00:00
|
|
|
mut timer,
|
2019-12-23 06:02:00 +00:00
|
|
|
uids,
|
2019-10-15 04:06:14 +00:00
|
|
|
can_build,
|
2019-11-29 06:04:37 +00:00
|
|
|
force_updates,
|
2020-07-03 19:40:37 +00:00
|
|
|
mut stats,
|
2020-06-02 02:42:26 +00:00
|
|
|
chat_modes,
|
2019-10-15 04:06:14 +00:00
|
|
|
mut accounts,
|
|
|
|
mut block_changes,
|
2020-06-24 03:29:46 +00:00
|
|
|
admin_list,
|
|
|
|
mut admins,
|
2019-10-15 04:06:14 +00:00
|
|
|
mut positions,
|
|
|
|
mut velocities,
|
|
|
|
mut orientations,
|
|
|
|
mut players,
|
|
|
|
mut clients,
|
|
|
|
mut controllers,
|
2020-06-25 11:20:09 +00:00
|
|
|
settings,
|
2020-07-16 14:05:35 +00:00
|
|
|
alias_validator,
|
2019-10-15 04:06:14 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2019-10-20 07:20:21 +00:00
|
|
|
timer.start();
|
|
|
|
|
2020-03-22 04:49:32 +00:00
|
|
|
let mut server_emitter = server_event_bus.emitter();
|
|
|
|
|
2020-06-27 23:12:12 +00:00
|
|
|
let mut new_chat_msgs: Vec<(Option<specs::Entity>, ChatMsg)> = Vec::new();
|
2019-10-15 04:06:14 +00:00
|
|
|
|
2019-12-23 06:02:00 +00:00
|
|
|
// Player list to send new players.
|
2020-06-02 06:11:47 +00:00
|
|
|
let player_list = (&uids, &players, stats.maybe(), admins.maybe())
|
2019-12-23 06:02:00 +00:00
|
|
|
.join()
|
2020-06-02 06:11:47 +00:00
|
|
|
.map(|(uid, player, stats, admin)| {
|
2020-06-28 13:15:00 +00:00
|
|
|
(*uid, PlayerInfo {
|
2020-06-11 03:40:48 +00:00
|
|
|
is_online: true,
|
2020-06-02 06:11:47 +00:00
|
|
|
is_admin: admin.is_some(),
|
2020-05-20 11:59:44 +00:00
|
|
|
player_alias: player.alias.clone(),
|
2020-06-02 06:11:47 +00:00
|
|
|
character: stats.map(|stats| CharacterInfo {
|
2020-05-24 09:20:54 +00:00
|
|
|
name: stats.name.clone(),
|
|
|
|
level: stats.level.level(),
|
|
|
|
}),
|
2020-05-20 11:59:44 +00:00
|
|
|
})
|
|
|
|
})
|
2019-12-23 06:02:00 +00:00
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
// List of new players to update player lists of all clients.
|
|
|
|
let mut new_players = Vec::new();
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
for (entity, client) in (&entities, &mut clients).join() {
|
2020-07-01 09:45:39 +00:00
|
|
|
let mut cnt = 0;
|
|
|
|
|
2020-07-01 09:51:37 +00:00
|
|
|
let network_err: Result<(), crate::error::Error> = block_on(async {
|
2020-07-10 13:31:26 +00:00
|
|
|
//TIMEOUT 0.02 ms for msg handling
|
2020-07-01 09:45:39 +00:00
|
|
|
select!(
|
2020-07-10 13:31:26 +00:00
|
|
|
_ = Delay::new(std::time::Duration::from_micros(20)).fuse() => Ok(()),
|
2020-07-01 09:45:39 +00:00
|
|
|
err = Self::handle_client_msg(
|
|
|
|
&mut server_emitter,
|
|
|
|
&mut new_chat_msgs,
|
|
|
|
&player_list,
|
|
|
|
&mut new_players,
|
|
|
|
entity,
|
|
|
|
client,
|
|
|
|
&mut cnt,
|
|
|
|
|
|
|
|
&character_loader,
|
|
|
|
&terrain,
|
|
|
|
&uids,
|
|
|
|
&can_build,
|
|
|
|
&force_updates,
|
2020-07-03 19:40:37 +00:00
|
|
|
&mut stats,
|
2020-07-01 09:45:39 +00:00
|
|
|
&chat_modes,
|
|
|
|
&mut accounts,
|
|
|
|
&mut block_changes,
|
|
|
|
&admin_list,
|
|
|
|
&mut admins,
|
|
|
|
&mut positions,
|
|
|
|
&mut velocities,
|
|
|
|
&mut orientations,
|
|
|
|
&mut players,
|
|
|
|
&mut controllers,
|
|
|
|
&settings,
|
2020-07-16 14:05:35 +00:00
|
|
|
&alias_validator,
|
2020-07-01 09:45:39 +00:00
|
|
|
).fuse() => err,
|
|
|
|
)
|
|
|
|
});
|
2019-10-15 04:06:14 +00:00
|
|
|
|
|
|
|
// Update client ping.
|
2020-07-01 09:45:39 +00:00
|
|
|
if cnt > 0 {
|
2020-05-26 00:11:22 +00:00
|
|
|
client.last_ping = time.0
|
|
|
|
} else if time.0 - client.last_ping > CLIENT_TIMEOUT // Timeout
|
2020-07-01 09:45:39 +00:00
|
|
|
|| network_err.is_err()
|
2019-10-15 04:06:14 +00:00
|
|
|
// Postbox error
|
|
|
|
{
|
2020-03-08 08:54:29 +00:00
|
|
|
server_emitter.emit(ServerEvent::ClientDisconnect(entity));
|
2020-05-26 00:11:22 +00:00
|
|
|
} else if time.0 - client.last_ping > CLIENT_TIMEOUT * 0.5 {
|
2019-10-15 04:06:14 +00:00
|
|
|
// Try pinging the client if the timeout is nearing.
|
2020-07-01 09:45:39 +00:00
|
|
|
client.notify(ServerMsg::Ping);
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 06:02:00 +00:00
|
|
|
// Handle new players.
|
|
|
|
// Tell all clients to add them to the player list.
|
|
|
|
for entity in new_players {
|
2020-05-24 09:20:54 +00:00
|
|
|
if let (Some(uid), Some(player)) = (uids.get(entity), players.get(entity)) {
|
2020-06-28 13:15:00 +00:00
|
|
|
let msg = ServerMsg::PlayerListUpdate(PlayerListUpdate::Add(*uid, PlayerInfo {
|
|
|
|
player_alias: player.alias.clone(),
|
|
|
|
is_online: true,
|
|
|
|
is_admin: admins.get(entity).is_some(),
|
|
|
|
character: None, // new players will be on character select.
|
|
|
|
}));
|
2019-12-23 06:02:00 +00:00
|
|
|
for client in (&mut clients).join().filter(|c| c.is_registered()) {
|
|
|
|
client.notify(msg.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
// Handle new chat messages.
|
|
|
|
for (entity, msg) in new_chat_msgs {
|
2020-06-05 01:48:26 +00:00
|
|
|
// Handle chat commands.
|
|
|
|
if msg.message.starts_with("/") {
|
|
|
|
if let (Some(entity), true) = (entity, msg.message.len() > 1) {
|
|
|
|
let argv = String::from(&msg.message[1..]);
|
|
|
|
server_emitter.emit(ServerEvent::ChatCmd(entity, argv));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Send chat message
|
|
|
|
server_emitter.emit(ServerEvent::Chat(msg));
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 07:20:21 +00:00
|
|
|
|
|
|
|
timer.end()
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
|
|
|
}
|