zero_trust_rps/common/client/
state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::collections::HashMap;
use tokio::time::Instant;
use uuid::Uuid;

use crate::common::{
    blake3::B3Key,
    immutable::Immutable,
    message::{HashWithData, RoomState, RpsData, UserState},
    rps::state::RpsState,
    utils::get_random_bytes,
};

pub type ClientStateView = Box<Immutable<ClientState>>;

#[derive(Debug, Clone)]
pub struct ClientState {
    pub user: Uuid,
    pub state: RpsState,

    pub secret: B3Key,
    pub room: Option<RoomState>,
    pub cache: HashMap<Uuid, UserState>, // TOOD: can probably be removed

    pub timed_out: bool,

    pub current_plays: Option<Box<[(Uuid, HashWithData)]>>,
    pub last_server_message: Instant,

    pub last_play: Option<RpsData>,
    pub last_error: Option<String>,
}

impl Default for ClientState {
    fn default() -> Self {
        ClientState {
            state: RpsState::BeforeRoom,
            secret: get_random_bytes(),
            room: None,
            cache: HashMap::new(),
            current_plays: None,
            user: Uuid::from_bytes(get_random_bytes()),
            last_server_message: Instant::now(),
            last_play: None,
            timed_out: false,
            last_error: None,
        }
    }
}