team04_server/lobby/state/
players.rs1use tokio::sync::OwnedSemaphorePermit;
2use uuid::Uuid;
3
4use crate::{
5 board::Coord,
6 messages::{client_role::PlayerRole, player_character::PlayerCharacter},
7 server::Connection,
8 unit::{LightsaberType, UnitType},
9};
10
11use super::clients::ReconnectToken;
12
13#[derive(Debug)]
14pub struct Player {
15 pub(crate) con: Option<Connection>,
17 name: String,
18 pub role: PlayerRole,
19 pub(super) reconnect_token: ReconnectToken,
20 pub character: Option<PlayerCharacter>,
22 pub ready: bool,
23 pub player_in_matchup: PlayerInMatchup,
26
27 pub lives: i64,
28 pub fights_won: u64,
29 pub red_lightsabers: Vec<f64>,
30 pub green_lightsabers: Vec<f64>,
31 pub blue_lightsabers: Vec<f64>,
32 pub unit_bank: Vec<UnitType>,
33 pub unit_placement: Vec<PlacedUnit>,
34
35 pub(crate) lightsaber_choice: Result<LightsaberType, Option<OwnedSemaphorePermit>>,
36 pub(crate) lightsaber_choice_allowed: bool,
37 pub(crate) unit_choice: Result<UnitType, Option<OwnedSemaphorePermit>>,
38 pub(crate) unit_choice_allowed: bool,
39 pub(crate) new_placement:
40 Result<(Vec<PlacedUnit>, Vec<UnitType>), Option<OwnedSemaphorePermit>>,
41 pub(crate) new_placement_allowed: bool,
42}
43impl Player {
44 pub fn new(name: String, role: PlayerRole, con: Connection) -> Self {
45 Self {
46 con: Some(con),
47 name,
48 role,
49 reconnect_token: ReconnectToken::new(Uuid::nil()),
50 character: None,
51 ready: false,
52 player_in_matchup: PlayerInMatchup::Player1,
53
54 lives: 0,
55 fights_won: 0,
56 red_lightsabers: Vec::new(),
57 green_lightsabers: Vec::new(),
58 blue_lightsabers: Vec::new(),
59 unit_bank: vec![],
60 unit_placement: vec![],
61
62 lightsaber_choice: Err(None),
63 lightsaber_choice_allowed: false,
64 unit_choice: Err(None),
65 unit_choice_allowed: false,
66 new_placement: Err(None),
67 new_placement_allowed: false,
68 }
69 }
70 pub fn name(&self) -> &str {
71 &self.name
72 }
73 pub fn reconnect_token(&self) -> &ReconnectToken {
74 &self.reconnect_token
75 }
76}
77
78#[derive(Clone, Copy, PartialEq, Eq, Debug)]
79pub enum PlayerInMatchup {
80 Player1,
81 Player2,
82}
83
84impl Player {
85 pub async fn send_message(&mut self, message: &str) {
86 if let Some(con) = &mut self.con {
87 if con.send(message).await.is_err() {
88 self.con = None;
89 }
90 }
91 }
92}
93
94#[derive(Clone, Copy, PartialEq, Eq, Debug)]
95pub struct PlacedUnit {
96 pub unit_type: UnitType,
97 pub position: Coord,
98}