|
|
@ -1,8 +1,9 @@ |
|
|
use crate::db; |
|
|
|
|
|
use crate::games::RunningGame; |
|
|
use crate::games::RunningGame; |
|
|
|
|
|
use crate::{db, games::Game}; |
|
|
|
|
|
|
|
|
use super::grpc::game::game_server; |
|
|
use super::{client_id, grpc::game::{CardKind, Image, game_server}}; |
|
|
pub use game_server::GameServer; |
|
|
pub use game_server::GameServer; |
|
|
|
|
|
use tonic::{Response, Status}; |
|
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
use std::collections::HashMap; |
|
|
use std::sync::Arc; |
|
|
use std::sync::Arc; |
|
|
@ -11,17 +12,20 @@ use tokio::sync::RwLock; |
|
|
|
|
|
|
|
|
pub struct GameService { |
|
|
pub struct GameService { |
|
|
conn: RwLock<db::DbClient>, |
|
|
conn: RwLock<db::DbClient>, |
|
|
|
|
|
games: Arc<Vec<Game>>, |
|
|
running_games: Arc<RwLock<HashMap<u32, RwLock<(u32, RunningGame)>>>>, |
|
|
running_games: Arc<RwLock<HashMap<u32, RwLock<(u32, RunningGame)>>>>, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl GameService { |
|
|
impl GameService { |
|
|
pub fn new( |
|
|
pub fn new( |
|
|
conn: db::DbClient, |
|
|
conn: db::DbClient, |
|
|
|
|
|
games: Arc<Vec<Game>>, |
|
|
running_games: Arc<RwLock<HashMap<u32, RwLock<(u32, RunningGame)>>>>, |
|
|
running_games: Arc<RwLock<HashMap<u32, RwLock<(u32, RunningGame)>>>>, |
|
|
) -> Self { |
|
|
) -> Self { |
|
|
Self { |
|
|
Self { |
|
|
conn: RwLock::new(conn), |
|
|
conn: RwLock::new(conn), |
|
|
running_games, |
|
|
running_games, |
|
|
|
|
|
games, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -30,15 +34,44 @@ impl GameService { |
|
|
impl game_server::Game for GameService { |
|
|
impl game_server::Game for GameService { |
|
|
async fn get_card_image( |
|
|
async fn get_card_image( |
|
|
&self, |
|
|
&self, |
|
|
request: tonic::Request<super::grpc::game::CardKind>, |
|
|
request: tonic::Request<CardKind>, |
|
|
) -> Result<tonic::Response<super::grpc::game::Image>, tonic::Status> { |
|
|
) -> Result<tonic::Response<Image>, Status> { |
|
|
todo!() |
|
|
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 mut conn = self.conn.write().await; |
|
|
|
|
|
let lobby: u32 = match conn.get_lobby_for_user(uuid).await { |
|
|
|
|
|
Some(l) => l, |
|
|
|
|
|
None => return Err(Status::failed_precondition("User isn't in a lobby")), |
|
|
|
|
|
}; |
|
|
|
|
|
let game_id = match self.running_games.read().await.get(&lobby) { |
|
|
|
|
|
Some(x) => x.read().await.0, |
|
|
|
|
|
None => { |
|
|
|
|
|
return Err(Status::failed_precondition( |
|
|
|
|
|
"User isn't in a lobby with a running game", |
|
|
|
|
|
)) |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
let game = &self.games[game_id as usize]; |
|
|
|
|
|
let (face, back) = game.get_card_paths(&request.into_inner().kind); |
|
|
|
|
|
let mut face_buf = Vec::new(); |
|
|
|
|
|
image::open(&face) |
|
|
|
|
|
.expect(&format!("Error loading the image in {:?}", face)) |
|
|
|
|
|
.write_to(&mut face_buf, image::ImageOutputFormat::Png) |
|
|
|
|
|
.unwrap(); |
|
|
|
|
|
let mut back_buf = Vec::new(); |
|
|
|
|
|
image::open(&back) |
|
|
|
|
|
.expect(&format!("Error loading the image in {:?}", back)) |
|
|
|
|
|
.write_to(&mut back_buf, image::ImageOutputFormat::Png) |
|
|
|
|
|
.unwrap(); |
|
|
|
|
|
Ok(Response::new(Image { face: face_buf, back: back_buf })) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async fn on_click( |
|
|
async fn on_click( |
|
|
&self, |
|
|
&self, |
|
|
request: tonic::Request<super::grpc::game::CardId>, |
|
|
request: tonic::Request<super::grpc::game::CardId>, |
|
|
) -> Result<tonic::Response<()>, tonic::Status> { |
|
|
) -> Result<tonic::Response<()>, Status> { |
|
|
todo!() |
|
|
todo!() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|