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, /// The active game map pub map: Map, /// Input inbox (handled in-order each game tick) inbox: RwLock>, /// The time the match was initialised init_t: DateTime, /// The synced time the match was started start_t: Option>, } impl From 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) { 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() { // } } }