|
|
@ -2,6 +2,7 @@ use anyhow::Result; |
|
|
use tokio_stream::wrappers::UnboundedReceiverStream; |
|
|
use tokio_stream::wrappers::UnboundedReceiverStream; |
|
|
// use tonic::{Request, Response, Status};
|
|
|
// use tonic::{Request, Response, Status};
|
|
|
|
|
|
|
|
|
|
|
|
use anyhow::anyhow; |
|
|
use log::info; |
|
|
use log::info; |
|
|
|
|
|
|
|
|
use tokio::sync::{mpsc, RwLock}; |
|
|
use tokio::sync::{mpsc, RwLock}; |
|
|
@ -14,29 +15,119 @@ use super::protos::connection::{Game, LobbyCode, LobbyConfig, UserId}; |
|
|
|
|
|
|
|
|
// pub use super::protos::connection_service::connection_server::ConnectionServer;
|
|
|
// pub use super::protos::connection_service::connection_server::ConnectionServer;
|
|
|
|
|
|
|
|
|
use super::{ServiceData, votes}; |
|
|
use super::protos::protocol::{Games, LobbyCodes}; |
|
|
|
|
|
use super::{votes, ServiceData}; |
|
|
|
|
|
|
|
|
// use super::client_id;
|
|
|
// use super::client_id;
|
|
|
|
|
|
|
|
|
use crate::server::protos::protocol::ServerClientPacket; |
|
|
|
|
|
use crate::server::protos::protocol::server_client_packet::Data; |
|
|
use crate::server::protos::protocol::server_client_packet::Data; |
|
|
|
|
|
use crate::server::protos::protocol::ServerClientPacket; |
|
|
use crate::{db, games::RunningGame}; |
|
|
use crate::{db, games::RunningGame}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn connect(data: &mut ServiceData, name: Name) -> Result<()> { |
|
|
pub(super) async fn connect(data: &mut ServiceData, name: Name) -> Result<()> { |
|
|
// let mut conn = self.conn.acquire().await;
|
|
|
let uuid = data.db.add_user(name.name.clone()).await?; |
|
|
|
|
|
info!("Connected {}[{}] from {}", name.name, uuid, data.addr); |
|
|
let uuid = data.db.add_user(name.name.clone()).await?; |
|
|
data.user_id = Some(uuid); |
|
|
info!("Connected {}[{}] from {}", name.name, uuid, data.addr); |
|
|
data.writer |
|
|
data.user_id = Some(uuid); |
|
|
.write(Data::ReturnConnect(UserId { |
|
|
data.writer.write(ServerClientPacket { |
|
|
id: uuid.to_string(), |
|
|
data: Some(Data::ReturnConnect(UserId { |
|
|
})) |
|
|
id: uuid.to_string(), |
|
|
.await?; |
|
|
})) |
|
|
Ok(()) |
|
|
}).await?; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn join_lobby_with_code( |
|
|
|
|
|
data: &mut ServiceData, |
|
|
|
|
|
lobby_code: LobbyCode, |
|
|
|
|
|
) -> Result<()> { |
|
|
|
|
|
if data |
|
|
|
|
|
.running_games |
|
|
|
|
|
.read() |
|
|
|
|
|
.await |
|
|
|
|
|
.contains_key(&lobby_code.code) |
|
|
|
|
|
{ |
|
|
|
|
|
return Err(anyhow!("Can't join a lobby where a game is running")); |
|
|
|
|
|
} |
|
|
|
|
|
data.db |
|
|
|
|
|
.join_lobby( |
|
|
|
|
|
data.user_id.ok_or(anyhow!("User should have connected"))?, |
|
|
|
|
|
lobby_code.code, |
|
|
|
|
|
) |
|
|
|
|
|
.await; |
|
|
|
|
|
data.voting.updated_users(&lobby_code.code).await; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn create_lobby(data: &mut ServiceData, config: LobbyConfig) -> Result<()> { |
|
|
|
|
|
// let uuid = client_id::get(request.metadata()).map_err(|x| match x {
|
|
|
|
|
|
// client_id::Error::NotSet => Status::failed_precondition("client_id must be set"),
|
|
|
|
|
|
// client_id::Error::MalformedUuid => Status::failed_precondition("malformed client_id"),
|
|
|
|
|
|
// })?;
|
|
|
|
|
|
let lobby = data.db.create_lobby(config.public).await; |
|
|
|
|
|
data.db |
|
|
|
|
|
.join_lobby( |
|
|
|
|
|
data.user_id.ok_or(anyhow!("User should have connected"))?, |
|
|
|
|
|
lobby, |
|
|
|
|
|
) |
|
|
|
|
|
.await; |
|
|
|
|
|
data.voting.updated_users(&lobby).await; |
|
|
|
|
|
data.writer |
|
|
|
|
|
.write(Data::ReturnCreateLobby(LobbyCode { code: lobby })) |
|
|
|
|
|
.await?; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn name(data: &mut ServiceData) -> Result<()> { |
|
|
|
|
|
data.writer |
|
|
|
|
|
.write(Data::ReturnName(Name { |
|
|
|
|
|
name: data.properties.name.clone(), |
|
|
|
|
|
})) |
|
|
|
|
|
.await |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn games(data: &mut ServiceData) -> Result<()> { |
|
|
|
|
|
data.writer |
|
|
|
|
|
.write(Data::ReturnGames(Games { |
|
|
|
|
|
games: data |
|
|
|
|
|
.games |
|
|
|
|
|
.iter() |
|
|
|
|
|
.enumerate() |
|
|
|
|
|
.map(|(id, game)| Game { |
|
|
|
|
|
id: id as u32, |
|
|
|
|
|
name: game.name.clone(), |
|
|
|
|
|
version: game.version.clone(), |
|
|
|
|
|
authors: game.authors.clone(), |
|
|
|
|
|
}) |
|
|
|
|
|
.collect(), |
|
|
|
|
|
})) |
|
|
|
|
|
.await?; |
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn get_public_lobbies(data: &mut ServiceData) -> Result<()> { |
|
|
|
|
|
data.writer |
|
|
|
|
|
.write(Data::ReturnPublicLobbies(LobbyCodes { |
|
|
|
|
|
lobby_codes: data |
|
|
|
|
|
.db |
|
|
|
|
|
.get_public_lobbies() |
|
|
|
|
|
.await |
|
|
|
|
|
.into_iter() |
|
|
|
|
|
.map(|code| LobbyCode { code }) |
|
|
|
|
|
.collect(), |
|
|
|
|
|
})) |
|
|
|
|
|
.await |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(super) async fn disconnect(data: &mut ServiceData) -> Result<()> { |
|
|
|
|
|
data.db |
|
|
|
|
|
.disconnect( |
|
|
|
|
|
data.user_id |
|
|
|
|
|
.ok_or(anyhow!("Expected user to be connected"))?, |
|
|
|
|
|
) |
|
|
|
|
|
.await; |
|
|
|
|
|
// log::warn!("Disconnected");
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// pub struct ConnectionService {
|
|
|
// pub struct ConnectionService {
|
|
|
// pub conn: RwLock<db::DbClient>,
|
|
|
// pub conn: RwLock<db::DbClient>,
|
|
|
@ -46,9 +137,6 @@ pub(super) async fn connect(data: &mut ServiceData, name: Name) -> Result<()> { |
|
|
// pub voting: votes::VotingSystem,
|
|
|
// pub voting: votes::VotingSystem,
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #[tonic::async_trait]
|
|
|
// #[tonic::async_trait]
|
|
|
// impl Connection for ConnectionService {
|
|
|
// impl Connection for ConnectionService {
|
|
|
// async fn connect(&self, request: Request<Name>) -> Result<Response<UserId>, Status> {
|
|
|
// async fn connect(&self, request: Request<Name>) -> Result<Response<UserId>, Status> {
|
|
|
|