diff options
Diffstat (limited to 'games/rstnode/rst-core/src/_match.rs')
-rw-r--r-- | games/rstnode/rst-core/src/_match.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/_match.rs b/games/rstnode/rst-core/src/_match.rs new file mode 100644 index 000000000000..ce75af5af393 --- /dev/null +++ b/games/rstnode/rst-core/src/_match.rs @@ -0,0 +1,72 @@ +use crate::{ + data::Player, + lobby::MetaLobby, + map::Map, + wire::{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: RwLock<VecDeque<Action>>, + /// 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: Default::default(), + 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.write().await.push_back(cmd); + } + + pub async fn handle_inbox(&mut self) { + // for act in self.inbox.write().await.drain() { + + // } + } +} |