You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
use tonic::transport::Server;
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
use std::{collections::HashMap, sync::Arc};
|
|
|
|
use crate::{db, games::RunningGame};
|
|
|
|
mod connection;
|
|
mod game;
|
|
mod grpc;
|
|
mod lobby;
|
|
mod votes;
|
|
|
|
use connection::{ConnectionServer, ConnectionService};
|
|
use game::{GameServer, GameService};
|
|
use lobby::{LobbyServer, LobbyService};
|
|
|
|
pub async fn start(
|
|
mut pool: db::DbClient,
|
|
games: Vec<crate::games::Game>,
|
|
properties: crate::server_properties::ServerProperties,
|
|
) {
|
|
let properties = Arc::new(properties);
|
|
let games = Arc::new(games);
|
|
let voting = votes::VotingSystem::new(pool.clone().await);
|
|
let running_games: Arc<RwLock<HashMap<u32, RwLock<(u32, RunningGame)>>>> = Default::default();
|
|
let connection = ConnectionService {
|
|
conn: RwLock::new(pool.clone().await),
|
|
properties: properties.clone(),
|
|
games: games.clone(),
|
|
voting: voting.clone(),
|
|
running_games: running_games.clone(),
|
|
};
|
|
let lobby = LobbyService::new(
|
|
pool.clone().await,
|
|
voting,
|
|
games.clone(),
|
|
running_games.clone(),
|
|
);
|
|
let game = GameService::new(pool, games, running_games);
|
|
|
|
Server::builder()
|
|
.add_service(ConnectionServer::new(connection))
|
|
.add_service(LobbyServer::new(lobby))
|
|
.add_service(GameServer::new(game))
|
|
.serve(properties.addr)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
mod client_id {
|
|
pub fn get(metadata: &tonic::metadata::MetadataMap) -> Result<uuid::Uuid, Error> {
|
|
metadata
|
|
.get("client_id")
|
|
.ok_or(Error::NotSet)
|
|
.and_then(|x| {
|
|
uuid::Uuid::parse_str(x.to_str().map_err(|_| Error::MalformedUuid)?)
|
|
.map_err(|_| Error::MalformedUuid)
|
|
})
|
|
}
|
|
|
|
pub enum Error {
|
|
MalformedUuid,
|
|
NotSet,
|
|
}
|
|
}
|
|
|