parent
885fcaf2f2
commit
a55c5bded0
@ -0,0 +1,43 @@ |
||||
//! A common trait interface between the server and the client
|
||||
|
||||
use crate::wire::{ |
||||
Action, AuthErr, Lobby, LobbyId, MatchErr, MatchId, RegErr, RoomErr, RoomUpdate, UpdateState, |
||||
User, UserId, |
||||
}; |
||||
use async_trait::async_trait; |
||||
use async_std::sync::Arc; |
||||
use chrono::{DateTime, Utc}; |
||||
|
||||
/// The main game interface implemented by the server and client
|
||||
#[async_trait] |
||||
pub trait GameIf { |
||||
/// Register a new user on a game server
|
||||
async fn register(self: Arc<Self>, name: String, pw: String) -> Result<UserId, RegErr>; |
||||
|
||||
/// Login for an existing user
|
||||
async fn login(self: Arc<Self>, name: String, pw: String) -> Result<User, AuthErr>; |
||||
|
||||
/// End a user session (go offline)
|
||||
async fn logout(self: Arc<Self>, user: User) -> Result<(), AuthErr>; |
||||
|
||||
/// Register as an anonymous player
|
||||
async fn anonymous(self: Arc<Self>, name: String) -> Result<User, AuthErr>; |
||||
|
||||
/// Join a match-making lobby
|
||||
async fn join(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<Lobby, RoomErr>; |
||||
|
||||
/// Leave a match-making lobby
|
||||
async fn leave(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<(), RoomErr>; |
||||
|
||||
/// Set the player's ready state
|
||||
async fn ready(self: Arc<Self>, user: User, lobby: LobbyId, ready: bool) -> RoomUpdate; |
||||
|
||||
/// Send a start request (as lobby admin)
|
||||
async fn start_req(self: Arc<Self>, user: User, lobby: LobbyId) -> DateTime<Utc>; |
||||
|
||||
/// Perform a game action as a user
|
||||
async fn perform_action(self: Arc<Self>, user: User, mtch: MatchId, act: Action) -> UpdateState; |
||||
|
||||
/// Leave a match
|
||||
async fn leave_match(self: Arc<Self>, user: User, mtch: MatchId) -> Result<(), MatchErr>; |
||||
} |
@ -0,0 +1,61 @@ |
||||
//! A users abstraction module
|
||||
|
||||
use crate::{ |
||||
wire::{User, UserId}, |
||||
Id, |
||||
}; |
||||
use async_std::sync::{Arc, RwLock}; |
||||
use std::{ |
||||
collections::BTreeMap, |
||||
sync::atomic::{AtomicUsize, Ordering}, |
||||
}; |
||||
|
||||
pub struct MetaUser { |
||||
pub id: UserId, |
||||
pub name: String, |
||||
pub pw: String, |
||||
pub auth: User, |
||||
} |
||||
|
||||
pub struct UserStore { |
||||
max: AtomicUsize, |
||||
users: RwLock<BTreeMap<UserId, Arc<MetaUser>>>, |
||||
} |
||||
|
||||
impl UserStore { |
||||
/// Currently resuming a userstore isn't possible
|
||||
pub fn new() -> Self { |
||||
UserStore { |
||||
max: 0.into(), |
||||
users: Default::default(), |
||||
} |
||||
} |
||||
|
||||
pub async fn add<S: Into<Option<String>>>( |
||||
&self, |
||||
name: String, |
||||
pw: S, |
||||
registered: bool, |
||||
) -> (UserId, User) { |
||||
let id = self.max.fetch_add(1, Ordering::Relaxed); |
||||
let token = Id::random(); |
||||
let pw = pw.into().unwrap_or("".into()); |
||||
let auth = User { |
||||
id, |
||||
token, |
||||
registered, |
||||
}; |
||||
|
||||
self.users.write().await.insert( |
||||
id, |
||||
MetaUser { |
||||
id, |
||||
name, |
||||
pw, |
||||
auth: auth.clone(), |
||||
} |
||||
.into(), |
||||
); |
||||
(id, auth.clone()) |
||||
} |
||||
} |
Loading…
Reference in new issue