aboutsummaryrefslogtreecommitdiff
path: root/src/_match.rs
blob: 23d3be8aeb0d495922cf6989010358c12be93532 (plain)
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
49
50
51
52
use crate::{
    data::Player,
    lobby::MetaLobby,
    map::Map,
    wire::{LobbyUser, MatchId, UserId},
};
use async_std::sync::Arc;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Describes a match for the server
pub struct Match {
    /// The match id
    pub id: MatchId,
    /// The list of active players
    pub players: Vec<Player>,
    /// The active game map
    pub map: Map,
    /// The time the match was initialised
    pub init_t: DateTime<Utc>,
    /// The synced time the match was started
    pub start_t: Option<DateTime<Utc>>,
}

impl From<MetaLobby> for Match {
    fn from(ml: MetaLobby) -> Self {
        Self {
            id: ml.inner.id,
            players: ml
                .inner
                .players
                .into_iter()
                .map(|lu| Player {
                    id: lu.id,
                    name: lu.name,
                    color: lu.color,
                    money: 0.into(),
                })
                .collect(),
            map: Map::new(),
            init_t: Utc::now(),
            start_t: None,
        }
    }
}

impl Match {
    /// Set the start time of the match, which may be in the future
    pub fn set_start(&mut self, t: DateTime<Utc>) {
        self.start_t = Some(t);
    }    
}