You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
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
|
|
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,
|
|
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_inbex(&mut self) {
|
|
for act in self.inbox.write().await.drain() {
|
|
|
|
}
|
|
}
|
|
}
|
|
|