From 64a0db905d24270a2af0faeef99c11f33aa601a0 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Thu, 19 Mar 2020 07:58:54 +0100 Subject: Starting work on the game server --- src/server.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/server.rs diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 000000000000..d8be64ee7b95 --- /dev/null +++ b/src/server.rs @@ -0,0 +1,73 @@ +use crate::{ + map::Map, + wire::{Match, MatchId, MatchUser, Response, User, UserId}, +}; +use async_std::sync::{Arc, Mutex}; +use std::{collections::BTreeMap, path::Path}; + +/// A convenience result wrapper for server actions +pub type Result = std::result::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: BTreeMap, +} + +impl Server { + /// Create a new game server + fn new() -> Self { + Self { + matches: Default::default(), + users: Default::default(), + } + } + + /// Open the state dir of a game server + pub async fn open(self: Arc, path: &Path) -> Result<()> { + Ok(()) + } + + /// Stop accepting new game connections and shutdown gracefully + /// + /// Returns the number of matches still going on. + pub async fn shutdown(self: Arc) -> Result { + 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) -> Result { + Ok(0) + } + + pub async fn update_map(self: Arc, id: MatchId, cb: F) -> Result + where + F: Fn(&mut Map) -> Result, + { + match self.matches.get(&id) { + Some(ref mut m) => m.lock().await.map.update(cb), + None => Err(ServerErr::NoSuchMatch), + } + } + + pub async fn update_players(self: Arc, id: MatchId, cb: F) -> Result + where + F: Fn(&mut Vec) -> Result, + { + match self.matches.get(&id) { + Some(ref mut m) => cb(&mut m.lock().await.players), + None => Err(ServerErr::NoSuchMatch), + } + } +} -- cgit v1.2.3