2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-01-07 20:25:12 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage, NullStorage};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2020-12-13 17:40:15 +00:00
|
|
|
use uuid::Uuid;
|
2019-04-10 17:23:27 +00:00
|
|
|
|
2021-07-26 13:52:43 +00:00
|
|
|
use crate::resources::BattleMode;
|
|
|
|
|
2019-06-29 15:04:06 +00:00
|
|
|
const MAX_ALIAS_LEN: usize = 32;
|
|
|
|
|
2021-05-06 09:43:10 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DisconnectReason {
|
|
|
|
Kicked,
|
|
|
|
NewerLogin,
|
|
|
|
NetworkError,
|
|
|
|
Timeout,
|
|
|
|
ClientRequested,
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Player {
|
|
|
|
pub alias: String,
|
2021-07-26 13:52:43 +00:00
|
|
|
pub battle_mode: BattleMode,
|
2020-01-11 21:04:49 +00:00
|
|
|
uuid: Uuid,
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Player {
|
2021-07-26 13:52:43 +00:00
|
|
|
pub fn new(alias: String, battle_mode: BattleMode, uuid: Uuid) -> Self {
|
|
|
|
Self {
|
|
|
|
alias,
|
|
|
|
battle_mode,
|
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Currently we allow attacking only if both players are opt-in to PvP.
|
|
|
|
///
|
|
|
|
/// Simple as tea, if they don't want the tea, don't make them drink the tea.
|
|
|
|
pub fn allow_harm(&self, other: &Player) -> bool {
|
|
|
|
// TODO: discuss if we want to keep self-harm
|
|
|
|
matches!(
|
|
|
|
(self.battle_mode, other.battle_mode),
|
|
|
|
(BattleMode::PvP, BattleMode::PvP)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inverse of `allow_harm`. Read its doc to learn more.
|
|
|
|
pub fn disallow_harm(&self, other: &Player) -> bool {
|
|
|
|
!self.allow_harm(other)
|
|
|
|
}
|
2019-06-29 15:04:06 +00:00
|
|
|
|
2021-04-01 14:37:13 +00:00
|
|
|
pub fn is_valid(&self) -> bool { Self::alias_validate(&self.alias).is_ok() }
|
2020-01-11 21:04:49 +00:00
|
|
|
|
2021-04-01 14:37:13 +00:00
|
|
|
pub fn alias_validate(alias: &str) -> Result<(), AliasError> {
|
2020-08-19 11:24:06 +00:00
|
|
|
// TODO: Expose auth name validation and use it here.
|
|
|
|
// See https://gitlab.com/veloren/auth/-/blob/master/server/src/web.rs#L20
|
2021-04-01 14:37:13 +00:00
|
|
|
if !alias
|
2020-08-19 11:24:06 +00:00
|
|
|
.chars()
|
|
|
|
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
|
2021-04-01 14:37:13 +00:00
|
|
|
{
|
|
|
|
Err(AliasError::ForbiddenCharacters)
|
|
|
|
} else if alias.len() > MAX_ALIAS_LEN {
|
|
|
|
Err(AliasError::TooLong)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-06-29 15:04:06 +00:00
|
|
|
}
|
2020-01-11 21:04:49 +00:00
|
|
|
|
|
|
|
/// Not to be confused with uid
|
|
|
|
pub fn uuid(&self) -> Uuid { self.uuid }
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Player {
|
2021-01-07 20:25:12 +00:00
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct Respawn;
|
|
|
|
impl Component for Respawn {
|
|
|
|
type Storage = NullStorage<Self>;
|
|
|
|
}
|
2021-04-01 14:37:13 +00:00
|
|
|
|
|
|
|
pub enum AliasError {
|
|
|
|
ForbiddenCharacters,
|
|
|
|
TooLong,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for AliasError {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
AliasError::ForbiddenCharacters => "Alias contains illegal characters.",
|
|
|
|
AliasError::TooLong => "Alias is too long.",
|
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
}
|