@ -145,6 +145,7 @@ impl GameService {
}
async fn get_default_status ( & self , uuid : uuid ::Uuid ) -> Result < Option < MessageStatus > , Status > {
log ::info ! ( "Creating a new status for {}" , uuid ) ;
let lobby = {
let mut conn = self . conn . write ( ) . await ;
let lobby : u32 = match conn . get_lobby_for_user ( uuid ) . await {
@ -153,6 +154,7 @@ impl GameService {
} ;
lobby
} ;
let r =
Ok ( self
. running_games
. read ( )
@ -165,7 +167,9 @@ impl GameService {
. await
. 2
. get ( )
. clone ( ) )
. clone ( ) ) ;
log ::info ! ( "Created a new status for {}" , uuid ) ;
r
}
}
@ -175,6 +179,8 @@ impl game_server::Game for GameService {
& self ,
request : tonic ::Request < CardKind > ,
) -> Result < tonic ::Response < Image > , Status > {
log ::info ! ( "Getting image location" ) ;
let time = std ::time ::Instant ::now ( ) ;
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" ) ,
@ -194,16 +200,20 @@ impl game_server::Game for GameService {
} ;
let game = & self . games [ game_id as usize ] ;
let ( face , back ) = game . get_card_paths ( & request . into_inner ( ) . kind ) ;
log ::info ! ( "Loading face image [{:?}]" , time . elapsed ( ) ) ; // TODO Create a cache for loaded images, so that they don't have to be parsed and reformatted each time
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 ( ) ;
log ::info ! ( "Loading back image [{:?}]" , time . elapsed ( ) ) ;
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 ( ) ;
log ::info ! ( "Loaded images [{:?}]" , time . elapsed ( ) ) ;
Ok ( Response ::new ( Image {
face : face_buf ,
back : back_buf ,
@ -263,9 +273,10 @@ impl game_server::Game for GameService {
} ) ? ;
Ok ( Response ::new (
self . get_default_status ( uuid )
. await ?
. ok_or ( Status ::internal ( "No status has been set" ) ) ? ,
self . get_default_status ( uuid ) . await ? . unwrap_or ( {
self . update_status ( uuid ) . await ? ;
self . get_default_status ( uuid ) . await ? . unwrap ( )
} ) ,
) )
}
@ -286,7 +297,8 @@ impl game_server::Game for GameService {
lobby
} ;
use std ::convert ::TryInto ;
let has_changed = self . running_games
let has_changed = self
. running_games
. read ( )
. await
. get ( & lobby )