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-08-31 15:55:28 +00:00
|
|
|
use crate::resources::{BattleMode, Time};
|
2021-07-26 13:52:43 +00:00
|
|
|
|
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,
|
2021-08-31 15:55:28 +00:00
|
|
|
pub last_battlemode_change: Option<Time>,
|
2020-01-11 21:04:49 +00:00
|
|
|
uuid: Uuid,
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 11:34:05 +00:00
|
|
|
impl BattleMode {
|
2021-07-31 17:53:09 +00:00
|
|
|
pub fn may_harm(self, other: Self) -> bool {
|
2021-07-30 11:34:05 +00:00
|
|
|
matches!((self, other), (BattleMode::PvP, BattleMode::PvP))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
impl Player {
|
2021-08-31 15:55:28 +00:00
|
|
|
pub fn new(
|
|
|
|
alias: String,
|
|
|
|
battle_mode: BattleMode,
|
|
|
|
uuid: Uuid,
|
|
|
|
last_battlemode_change: Option<Time>,
|
|
|
|
) -> Self {
|
2021-07-26 13:52:43 +00:00
|
|
|
Self {
|
|
|
|
alias,
|
|
|
|
battle_mode,
|
2021-08-31 15:55:28 +00:00
|
|
|
last_battlemode_change,
|
2021-07-26 13:52:43 +00:00
|
|
|
uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Currently we allow attacking only if both players are opt-in to PvP.
|
|
|
|
///
|
2021-07-27 21:57:48 +00:00
|
|
|
/// Simple as tea, if they don't want the tea, don't make them drink the
|
|
|
|
/// tea.
|
2021-07-30 22:32:29 +00:00
|
|
|
/// You can make tea for yourself though.
|
2021-07-31 17:53:09 +00:00
|
|
|
pub fn may_harm(&self, other: &Player) -> bool { self.battle_mode.may_harm(other.battle_mode) }
|
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()
|
|
|
|
}
|
|
|
|
}
|