use crate::{ _if::GameIf, _match::Match, data::Player, lobby::LobbyList, map::Map, users::UserStore, wire::{ Action, AuthErr, Lobby, LobbyErr, LobbyId, LobbyUpdate, MatchErr, MatchId, RegErr, Response, UpdateState, User, UserId, }, }; use async_std::sync::{Arc, Mutex, RwLock}; use async_trait::async_trait; use chrono::{DateTime, Utc}; use std::{collections::BTreeMap, path::Path}; /// A convenience result wrapper for server actions pub type ServerResult = Result; pub enum ServerErr { /// The requested directory is corrupted NoSuchDir, /// Corrupted game state Corrupted, /// No such match found NoSuchMatch, } /// The game's server backend pub struct Server { matches: BTreeMap>, users: UserStore, lobbies: LobbyList, } impl Server { /// Create a new game server fn new() -> Self { Self { matches: Default::default(), users: UserStore::new(), lobbies: LobbyList::new(), } } /// Open the state dir of a game server pub async fn open(self: Arc, path: &Path) -> ServerResult<()> { Ok(()) } /// Stop accepting new game connections and shutdown gracefully /// /// Returns the number of matches still going on. pub async fn shutdown(self: Arc) -> ServerResult { Ok(0) } /// Save and close the statedir and kicking all players /// /// Returns the number of players that were kicked off the server /// prematurely. pub async fn kill(self: Arc) -> ServerResult { Ok(0) } pub async fn update_map(self: Arc, id: MatchId, cb: F) -> ServerResult where F: Fn(&mut Map) -> ServerResult, { match self.matches.get(&id) { Some(ref m) => m.lock().await.map.update(cb), None => Err(ServerErr::NoSuchMatch), } } pub async fn update_players(self: Arc, id: MatchId, cb: F) -> ServerResult where F: Fn(&mut Vec) -> ServerResult, { match self.matches.get(&id) { Some(ref mut m) => cb(&mut m.lock().await.players), None => Err(ServerErr::NoSuchMatch), } } } #[async_trait] impl GameIf for Server { async fn register(self: Arc, name: String, pw: String) -> Result { unimplemented!() } async fn login(self: Arc, name: String, pw: String) -> Result { unimplemented!() } async fn logout(self: Arc, user: User) -> Result<(), AuthErr> { unimplemented!() } async fn anonymous(self: Arc, name: String) -> Result { let (_, auth) = self.users.add(name, None, true).await; Ok(auth) } async fn join(self: Arc, user: User, lobby: LobbyId) -> Result { let mu = self.users.get(&user).await?; self.lobbies.get_mut(lobby, |mut l| l.join(&mu)).await } async fn leave(self: Arc, user: User, lobby: LobbyId) -> Result<(), LobbyErr> { let mu = self.users.get(&user).await?; self.lobbies.get_mut(lobby, |mut l| l.leave(&mu)).await } async fn ready( self: Arc, user: User, lobby: LobbyId, ready: bool, ) -> Result { self.lobbies .get_mut(lobby, |mut l| l.ready(user, ready)) .await } /// A start request was received async fn start_req( self: Arc, user: UserId, lobby: LobbyId, ) -> Result, LobbyErr> { self.lobbies.get_mut(lobby, |mut l| l.start(user)).await?; let lob = self.lobbies.consume(lobby).await?; Ok(Utc::now()) } async fn perform_action( self: Arc, user: User, mtch: MatchId, act: Action, ) -> UpdateState { unimplemented!() } async fn leave_match(self: Arc, user: User, mtch: MatchId) -> Result<(), MatchErr> { unimplemented!() } }