My personal project and infrastructure archive
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.
 
 
 
 
 
 
nomicon/games/rstnode/rst-core/src/_match.rs

72 lines
1.9 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
///
/// 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() {
// }
}
}