aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/_match.rs
blob: e1cc45374ebcccb2da39a265e17163156373c8df (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{
    data::Player,
    lobby::MetaLobby,
    mailbox::{Inbox, Outbox},
    map::Map,
    wire::{game::Action, LobbyUser, MatchId, UserId},
};
use async_std::sync::{Arc, RwLock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// Describes a match for the server
///
/// This type implements the partial [GameIf](crate::GameIf) API to
/// allow client to queue commands from players.  The server
/// implementation runs a simulation for each match that is running on
/// it.
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,
    /// Input inbox (handled in-order each game tick)
    inbox: Inbox,
    /// Update outbox
    outbox: Outbox,
    /// The time the match was initialised
    init_t: DateTime<Utc>,
    /// The synced time the match was started
    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(),
            inbox: Inbox::new(),
            outbox: Outbox::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);
    }

    /// Queue a new game action
    pub async fn queue(&self, cmd: Action) {
        self.inbox.queue(cmd).await;
    }

    pub async fn handle_inbox(&mut self) {
        // for act in self.inbox.write().await.drain() {

        // }
    }
}