From 2066a469dc76984fcecfa3f2a8034903bcf5a441 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Mon, 29 Aug 2022 09:38:51 +0000 Subject: [PATCH] get_game_url is added --- Cargo.toml | 4 +- src/api/game/service.rs | 167 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 168 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50d3a95..f82413e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ nats = { version = "0" } prost = { version = "0" } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.81-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.67-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.83-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.69-snapshot" } [build-dependencies] diff --git a/src/api/game/service.rs b/src/api/game/service.rs index 958109d..1cdbca3 100644 --- a/src/api/game/service.rs +++ b/src/api/game/service.rs @@ -25,7 +25,7 @@ impl Service { } pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { - futures::try_join!(self.list_games(),).map(|_| ()) + futures::try_join!(self.list_games(), self.get_game_url(),).map(|_| ()) } fn get_client_in_header( @@ -62,6 +62,57 @@ impl Service { } } + async fn get_member_session_with_renewal( + &self, + client: &bpr::models::core::network::Client, + ) -> Result, bcr::error::rpc::Error> { + let ss_get_member_session_req = bpr::ss::member_session::GetMemberSessionWithRenewalRequest { + client: Some(client.clone()), + request: Some(bpr::ss::member_session::get_member_session_with_renewal_request::Request {}), + }; + + let ss_get_member_session_res_msg = self + .connection_broker + .request( + bpr::ss::member_session::SUBJECT_GET_MEMBER_SESSION_WITH_RENEWAL, + ss_get_member_session_req.encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + let ss_get_member_session_res = + bpr::ss::member_session::GetMemberSessionWithRenewalResponse::decode( + ss_get_member_session_res_msg.data.as_slice(), + ) + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + if let Some(e) = ss_get_member_session_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_get_member_session_res.result { + return Ok(r.member_session); + } + + Ok(None) + } + async fn list_games(&self) -> Result<(), Box> { let s = self .connection_broker @@ -170,4 +221,118 @@ impl Service { Ok(()) } + + async fn get_game_url(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::frontend::api::game::SUBJECT_GET_GAME_URL, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + let member_session = match self.get_member_session_with_renewal(&client).await? { + Some(ms) => ms, + None => { + return Err(bcr::error::rpc::Error::InvalidRequest( + bcr::error::rpc::InvalidRequest { + message: "Unauthorized".to_string(), + }, + )); + } + }; + + let req = bpr::c2se::api::game::GetGameUrlRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_get_game_url_req = bpr::ss::api::game::GetGameUrlRequest { + client: Some(client), + request: Some(bpr::ss::api::game::get_game_url_request::Request { + vendor_key: req.vendor_key, + game_key: req.game_key, + member_id: member_session.member_id, + }), + }; + + let ss_get_game_url_res_msg = self + .connection_broker + .request( + bpr::ss::api::game::SUBJECT_GET_GAME_URL, + ss_get_game_url_req.encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + let ss_get_game_url_res = + bpr::ss::api::game::GetGameUrlResponse::decode(ss_get_game_url_res_msg.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + if let Some(e) = ss_get_game_url_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_get_game_url_res.result { + message + .respond( + bpr::c2se::api::game::GetGameUrlResponse { + error: None, + result: Some(bpr::c2se::api::game::get_game_url_response::Result { + user_id: r.user_id, + balance: r.balance, + url: r.url, + }), + } + .encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + } + + Ok::<(), bcr::error::rpc::Error>(()) + } + .await + { + message + .respond( + bpr::c2se::api::game::GetGameUrlResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } }