aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/server.rs
blob: 68fa2a074e34d8df6d633362460c5cd56ce4bb85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Game server state handler
//!
//! A server can host many lobbies at the same time.  It listens for
//! connections according to a address given to the initialiser.

use crate::{
    data::Player,
    lobby::LobbyList,
    map::Map,
    users::UserStore,
    wire::{
        game::Action, AuthErr, Lobby, LobbyErr, LobbyId, LobbyUpdate, MatchErr, MatchId, RegErr,
        Response, UpdateState, User, UserId,
    },
    GameIf, Match,
};
use async_std::sync::{Arc, Mutex};
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<T> = Result<T, ServerErr>;
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<MatchId, Mutex<Match>>,
    users: UserStore,
    lobbies: LobbyList,
}

impl Server {
    /// Create a new game server
    pub(crate) fn new() -> Arc<Self> {
        Arc::new(Self {
            matches: Default::default(),
            users: UserStore::new(),
            lobbies: LobbyList::new(),
        })
    }

    /// Open the state dir of a game server
    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>) -> ServerResult<u64> {
        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<Self>) -> ServerResult<u64> {
        Ok(0)
    }

    // pub async fn update_map<F>(self: Arc<Self>, id: MatchId, cb: F) -> ServerResult<Response>
    // where
    //     F: Fn(&mut Map) -> ServerResult<Response>,
    // {
    //     match self.matches.get(&id) {
    //         Some(ref m) => m.lock().await.map.update(cb),
    //         None => Err(ServerErr::NoSuchMatch),
    //     }
    // }

    pub async fn update_players<F>(self: Arc<Self>, id: MatchId, cb: F) -> ServerResult<Response>
    where
        F: Fn(&mut Vec<Player>) -> ServerResult<Response>,
    {
        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<Self>, _name: String, _pw: String) -> Result<UserId, RegErr> {
        todo!()
    }

    async fn login(self: Arc<Self>, _name: String, _pw: String) -> Result<User, AuthErr> {
        todo!()
    }

    async fn logout(self: Arc<Self>, _user: User) -> Result<(), AuthErr> {
        todo!()
    }

    async fn anonymous(self: Arc<Self>, name: String) -> Result<User, AuthErr> {
        // let (_, auth) = self.users.add(name, None, true).await;
        // Ok(auth)
        todo!()
    }

    async fn join(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<Lobby, LobbyErr> {
        let mu = self.users.get(&user).await?;
        self.lobbies.get_mut(lobby, |l| l.join(&mu)).await
    }

    async fn leave(self: Arc<Self>, user: User, lobby: LobbyId) -> Result<(), LobbyErr> {
        let mu = self.users.get(&user).await?;
        self.lobbies.get_mut(lobby, |l| l.leave(&mu)).await
    }

    async fn ready(
        self: Arc<Self>,
        user: User,
        lobby: LobbyId,
        ready: bool,
    ) -> Result<LobbyUpdate, LobbyErr> {
        self.lobbies.get_mut(lobby, |l| l.ready(user, ready)).await
    }

    /// A start request was received
    async fn start_req(
        self: Arc<Self>,
        user: UserId,
        lobby: LobbyId,
    ) -> Result<DateTime<Utc>, LobbyErr> {
        self.lobbies.get_mut(lobby, |l| l.start(user)).await??;
        let _lob = self.lobbies.consume(lobby).await?;
        Ok(Utc::now())
    }

    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!()
    }
}