2025-06-23 09:48:28 +01:00
|
|
|
use core::{fmt::Display, num::NonZeroU8};
|
|
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-10-06 20:45:15 +01:00
|
|
|
use crate::{character::CharacterId, diedto::DiedTo, player::PlayerId, role::RoleTitle};
|
2025-06-23 09:48:28 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct Identification {
|
|
|
|
|
pub player_id: PlayerId,
|
|
|
|
|
pub public: PublicIdentity,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:52:12 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
|
2025-06-23 09:48:28 +01:00
|
|
|
pub struct PublicIdentity {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub pronouns: Option<String>,
|
2025-10-02 17:52:12 +01:00
|
|
|
pub number: Option<NonZeroU8>,
|
2025-06-23 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 13:07:59 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct CharacterIdentity {
|
|
|
|
|
pub character_id: CharacterId,
|
2025-10-02 17:52:12 +01:00
|
|
|
pub name: String,
|
|
|
|
|
pub pronouns: Option<String>,
|
|
|
|
|
pub number: NonZeroU8,
|
2025-09-30 13:07:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:52:12 +01:00
|
|
|
impl From<CharacterIdentity> for PublicIdentity {
|
|
|
|
|
fn from(c: CharacterIdentity) -> Self {
|
2025-09-30 13:07:59 +01:00
|
|
|
Self {
|
2025-10-02 17:52:12 +01:00
|
|
|
name: c.name,
|
|
|
|
|
pronouns: c.pronouns,
|
|
|
|
|
number: Some(c.number),
|
2025-09-30 13:07:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-02 17:52:12 +01:00
|
|
|
impl From<&CharacterIdentity> for PublicIdentity {
|
|
|
|
|
fn from(c: &CharacterIdentity) -> Self {
|
2025-06-23 09:48:28 +01:00
|
|
|
Self {
|
2025-10-02 17:52:12 +01:00
|
|
|
name: c.name.clone(),
|
|
|
|
|
pronouns: c.pronouns.clone(),
|
|
|
|
|
number: Some(c.number),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for CharacterIdentity {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
let CharacterIdentity {
|
|
|
|
|
character_id,
|
|
|
|
|
name,
|
|
|
|
|
pronouns,
|
|
|
|
|
number,
|
|
|
|
|
} = self;
|
|
|
|
|
let pronouns = pronouns
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|p| format!(" ({p})"))
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
write!(f, "[{number}] {name}{pronouns} <<{character_id}>>")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CharacterIdentity {
|
|
|
|
|
pub const fn new(
|
|
|
|
|
character_id: CharacterId,
|
|
|
|
|
name: String,
|
|
|
|
|
pronouns: Option<String>,
|
|
|
|
|
number: NonZeroU8,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
name,
|
|
|
|
|
number,
|
|
|
|
|
pronouns,
|
|
|
|
|
character_id,
|
2025-06-23 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for PublicIdentity {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
let PublicIdentity {
|
|
|
|
|
name,
|
|
|
|
|
pronouns,
|
|
|
|
|
number,
|
|
|
|
|
} = self;
|
|
|
|
|
let pronouns = pronouns
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|p| format!(" ({p})"))
|
|
|
|
|
.unwrap_or_default();
|
2025-10-02 17:52:12 +01:00
|
|
|
let number = number
|
|
|
|
|
.as_ref()
|
|
|
|
|
.map(|n| format!("[{n}] "))
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
write!(f, "{number}{name}{pronouns}")
|
2025-06-23 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl core::hash::Hash for Identification {
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.player_id.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for Identification {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
let Identification { player_id, public } = self;
|
|
|
|
|
write!(f, "{public} [{player_id}]")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct PlayerState {
|
|
|
|
|
pub identification: Identification,
|
|
|
|
|
pub connected: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct CharacterState {
|
|
|
|
|
pub player_id: PlayerId,
|
2025-10-02 17:52:12 +01:00
|
|
|
pub identity: CharacterIdentity,
|
2025-06-23 09:48:28 +01:00
|
|
|
pub role: RoleTitle,
|
|
|
|
|
pub died_to: Option<DiedTo>,
|
|
|
|
|
}
|