runecast_protocol/
player.rs

1//! Player identity and context types.
2//!
3//! These types are the single source of truth for player identity information,
4//! shared across the handlers crate and backend.
5
6use crate::ServerMessage;
7
8/// Core player identity - the immutable parts that identify a player.
9///
10/// This is embedded in all player-related structs to avoid field duplication.
11/// Contains the fields that are constant for a player's session.
12pub trait PlayerIdentity {
13    /// Get player ID.
14    fn player_id(&self) -> i64;
15
16    /// Get username.
17    fn username(&self) -> &str;
18
19    /// Get avatar URL.
20    fn avatar_url(&self) -> &str;
21}
22
23#[async_trait::async_trait]
24pub trait PlayerContext: Send + Sync {
25    async fn send_message(&self, msg: ServerMessage);
26
27    /// Get the player's identity.
28    fn identity(&self) -> &dyn PlayerIdentity;
29
30    /// Check if the player has admin privileges.
31    fn is_admin(&self) -> bool;
32
33    /// Get the current lobby ID if in a lobby.
34    fn lobby_id(&self) -> Option<&str>;
35
36    /// Get the current game ID if in a game.
37    fn game_id(&self) -> Option<&str>;
38
39    /// Check if the player is spectating.
40    fn is_spectating(&self) -> bool;
41
42    /// Check if the player is connected.
43    fn is_connected(&self) -> bool;
44
45    // =========================================================================
46    // Convenience methods with default implementations
47    // =========================================================================
48
49    /// Get player ID directly (convenience for `identity().player_id()`).
50    fn player_id(&self) -> i64 {
51        self.identity().player_id()
52    }
53
54    /// Check if the player is in a lobby.
55    fn in_lobby(&self) -> bool {
56        self.lobby_id().is_some()
57    }
58
59    /// Check if the player is in a game.
60    fn in_game(&self) -> bool {
61        self.game_id().is_some()
62    }
63
64    // /// setters
65    // fn set_player_id(&mut self, player_id: i64);
66    // fn set_username(&mut self, username: &str);
67    // fn set_avatar_url(&mut self, avatar_url: Option<String>);
68    // fn set_is_admin(&mut self, is_admin: bool);
69    // fn set_lobby_id(&mut self, lobby_id: &str);
70    // fn set_game_id(&mut self, game_id: &str);
71    // fn set_is_spectating(&mut self, is_spectating: bool);
72    // fn set_is_connected(&mut self, is_connected: bool);
73}