aboutsummaryrefslogtreecommitdiff
path: root/src/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs80
1 files changed, 68 insertions, 12 deletions
diff --git a/src/server.rs b/src/server.rs
index d8be64ee7b95..6ddfb70a9d41 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,12 +1,19 @@
use crate::{
+ _if::GameIf,
map::Map,
- wire::{Match, MatchId, MatchUser, Response, User, UserId},
+ users::UserStore,
+ wire::{
+ Action, AuthErr, Lobby, LobbyId, Match, MatchErr, MatchId, MatchUser, RegErr, Response,
+ RoomErr, RoomUpdate, UpdateState, User, UserId,
+ },
};
-use async_std::sync::{Arc, Mutex};
+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 Result<T> = std::result::Result<T, ServerErr>;
+pub type ServerResult<T> = Result<T, ServerErr>;
pub enum ServerErr {
/// The requested directory is corrupted
NoSuchDir,
@@ -19,7 +26,7 @@ pub enum ServerErr {
/// The game's server backend
pub struct Server {
matches: BTreeMap<MatchId, Mutex<Match>>,
- users: BTreeMap<UserId, User>,
+ users: UserStore,
}
impl Server {
@@ -27,19 +34,19 @@ impl Server {
fn new() -> Self {
Self {
matches: Default::default(),
- users: Default::default(),
+ users: UserStore::new(),
}
}
/// Open the state dir of a game server
- pub async fn open(self: Arc<Self>, path: &Path) -> Result<()> {
+ pub async fn open(self: Arc<Self>, 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<Self>) -> Result<u64> {
+ pub async fn shutdown(self: Arc<Self>) -> ServerResult<u64> {
Ok(0)
}
@@ -47,13 +54,13 @@ impl Server {
///
/// Returns the number of players that were kicked off the server
/// prematurely.
- pub async fn kill(self: Arc<Self>) -> Result<u64> {
+ pub async fn kill(self: Arc<Self>) -> ServerResult<u64> {
Ok(0)
}
- pub async fn update_map<F>(self: Arc<Self>, id: MatchId, cb: F) -> Result<Response>
+ pub async fn update_map<F>(self: Arc<Self>, id: MatchId, cb: F) -> ServerResult<Response>
where
- F: Fn(&mut Map) -> Result<Response>,
+ F: Fn(&mut Map) -> ServerResult<Response>,
{
match self.matches.get(&id) {
Some(ref mut m) => m.lock().await.map.update(cb),
@@ -61,9 +68,9 @@ impl Server {
}
}
- pub async fn update_players<F>(self: Arc<Self>, id: MatchId, cb: F) -> Result<Response>
+ pub async fn update_players<F>(self: Arc<Self>, id: MatchId, cb: F) -> ServerResult<Response>
where
- F: Fn(&mut Vec<MatchUser>) -> Result<Response>,
+ F: Fn(&mut Vec<MatchUser>) -> ServerResult<Response>,
{
match self.matches.get(&id) {
Some(ref mut m) => cb(&mut m.lock().await.players),
@@ -71,3 +78,52 @@ impl Server {
}
}
}
+
+#[async_trait]
+impl GameIf for Server {
+ async fn register(self: Arc<Self>, name: String, pw: String) -> Result<UserId, RegErr> {
+ unimplemented!()
+ }
+
+ async fn login(self: Arc<Self>, name: String, pw: String) -> Result<User, AuthErr> {
+ unimplemented!()
+ }
+
+ async fn logout(self: Arc<Self>, user: User) -> Result<(), AuthErr> {
+ unimplemented!()
+ }
+
+ async fn anonymous(self: Arc<Self>, name: String) -> Result<User, AuthErr> {
+ let (_, auth) = self.users.add(name, None, true).await;
+ Ok(auth)
+ }
+
+ async fn join(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<Lobby, RoomErr> {
+ unimplemented!()
+ }
+
+ async fn leave(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<(), RoomErr> {
+ unimplemented!()
+ }
+
+ async fn ready(self: Arc<Self>, user: User, lobby: LobbyId, ready: bool) -> RoomUpdate {
+ unimplemented!()
+ }
+
+ async fn start_req(self: Arc<Self>, user: User, lobby: LobbyId) -> DateTime<Utc> {
+ unimplemented!()
+ }
+
+ async fn perform_action(
+ self: Arc<Self>,
+ user: User,
+ mtch: MatchId,
+ act: Action,
+ ) -> UpdateState {
+ unimplemented!()
+ }
+
+ async fn leave_match(self: Arc<Self>, user: User, mtch: MatchId) -> Result<(), MatchErr> {
+ unimplemented!()
+ }
+}