From 5fa17ea6e0421d80aee02016a917f00f0fd23517 Mon Sep 17 00:00:00 2001 From: Jacobtread Date: Mon, 25 Sep 2023 20:46:18 +1300 Subject: [PATCH] Made use of destructuring on blaze handlers --- src/session/routes/auth.rs | 18 ++++---- src/session/routes/game_manager.rs | 67 ++++++++++++++++------------- src/session/routes/user_sessions.rs | 15 +++---- src/session/routes/util.rs | 14 +++--- 4 files changed, 60 insertions(+), 54 deletions(-) diff --git a/src/session/routes/auth.rs b/src/session/routes/auth.rs index 6ffaf690..32b703aa 100644 --- a/src/session/routes/auth.rs +++ b/src/session/routes/auth.rs @@ -65,10 +65,10 @@ pub async fn handle_silent_login( session: SessionLink, Extension(db): Extension, Extension(sessions): Extension>, - Blaze(req): Blaze, + Blaze(SilentLoginRequest { token }): Blaze, ) -> ServerResult> { // Verify the authentication token - let player_id = sessions.verify_token(&req.token).map_err(|err| match err { + let player_id = sessions.verify_token(&token).map_err(|err| match err { VerifyError::Expired => AuthenticationError::ExpiredToken, VerifyError::Invalid => AuthenticationError::InvalidToken, })?; @@ -83,7 +83,7 @@ pub async fn handle_silent_login( Ok(Blaze(AuthResponse { player, - session_token: req.token, + session_token: token, silent: true, })) } @@ -94,7 +94,7 @@ pub async fn handle_origin_login( Extension(config): Extension>, Extension(sessions): Extension>, Extension(retriever): Extension>, - Blaze(req): Blaze, + Blaze(OriginLoginRequest { token, .. }): Blaze, ) -> ServerResult> { // Obtain an origin flow let mut flow = retriever.origin_flow().await.map_err(|err| { @@ -102,7 +102,7 @@ pub async fn handle_origin_login( GlobalError::System })?; - let player: Player = flow.login(&db, req.token, &config).await.map_err(|err| { + let player: Player = flow.login(&db, token, &config).await.map_err(|err| { error!("Failed to login with origin: {}", err); GlobalError::System })?; @@ -209,9 +209,8 @@ static ENTITLEMENTS: &[Entitlement; 34] = &[ /// } /// ``` pub async fn handle_list_entitlements( - Blaze(req): Blaze, + Blaze(ListEntitlementsRequest { tag }): Blaze, ) -> Option> { - let tag: String = req.tag; if !tag.is_empty() { return None; } @@ -286,9 +285,8 @@ pub async fn handle_create_account( Extension(db): Extension, Extension(config): Extension>, Extension(sessions): Extension>, - Blaze(req): Blaze, + Blaze(CreateAccountRequest { email, password }): Blaze, ) -> ServerResult> { - let email = req.email; if !EmailAddress::is_valid(&email) { return Err(AuthenticationError::InvalidEmail.into()); } @@ -299,7 +297,7 @@ pub async fn handle_create_account( } // Hash the proivded plain text password using Argon2 - let hashed_password: String = hash_password(&req.password).map_err(|err| { + let hashed_password: String = hash_password(&password).map_err(|err| { error!("Failed to hash password for creating account: {}", err); GlobalError::System })?; diff --git a/src/session/routes/game_manager.rs b/src/session/routes/game_manager.rs index f6ce6785..868b0028 100644 --- a/src/session/routes/game_manager.rs +++ b/src/session/routes/game_manager.rs @@ -17,13 +17,13 @@ use std::sync::Arc; pub async fn handle_join_game( player: GamePlayer, + Blaze(JoinGameRequest { user }): Blaze, Extension(sessions): Extension>, Extension(game_manager): Extension>, - Blaze(req): Blaze, ) -> ServerResult> { // Lookup the session join target let session = sessions - .lookup_session(req.user.id) + .lookup_session(user.id) .await .ok_or(GameManagerError::JoinPlayerFailed)?; @@ -63,22 +63,16 @@ pub async fn handle_join_game( } pub async fn handle_get_game_data( - Blaze(mut req): Blaze, + Blaze(GetGameDataRequest { game_list }): Blaze, Extension(game_manager): Extension>, ) -> ServerResult { - if req.game_list.is_empty() { - return Err(GlobalError::System.into()); - } - - let game_id = req.game_list.remove(0); - + let game_id = game_list.first().copied().ok_or(GlobalError::System)?; let game = game_manager .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; let game = &*game.read().await; - let body = game.game_data().await; Ok(body) @@ -138,9 +132,12 @@ pub async fn handle_get_game_data( pub async fn handle_create_game( player: GamePlayer, Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(CreateGameRequest { + attributes, + setting, + }): Blaze, ) -> ServerResult> { - let (link, game_id) = game_manager.create_game(req.attributes, req.setting).await; + let (link, game_id) = game_manager.create_game(attributes, setting).await; // Notify matchmaking of the new game tokio::spawn(async move { @@ -183,16 +180,19 @@ pub async fn handle_create_game( /// ``` pub async fn handle_set_attributes( Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(SetAttributesRequest { + attributes, + game_id, + }): Blaze, ) -> ServerResult<()> { let link = game_manager - .get_game(req.game_id) + .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; { let game = &mut *link.write().await; - game.set_attributes(req.attributes); + game.set_attributes(attributes); } // Update matchmaking for the changed game @@ -202,7 +202,7 @@ pub async fn handle_set_attributes( game.joinable_state(None) }; if let GameJoinableState::Joinable = join_state { - game_manager.process_queue(link, req.game_id).await; + game_manager.process_queue(link, game_id).await; } }); @@ -221,15 +221,15 @@ pub async fn handle_set_attributes( /// ``` pub async fn handle_set_state( Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(SetStateRequest { game_id, state }): Blaze, ) -> ServerResult<()> { let link = game_manager - .get_game(req.game_id) + .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; let game = &mut *link.write().await; - game.set_state(req.state); + game.set_state(state); Ok(()) } @@ -246,15 +246,15 @@ pub async fn handle_set_state( /// ``` pub async fn handle_set_setting( Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(SetSettingRequest { game_id, setting }): Blaze, ) -> ServerResult<()> { let link = game_manager - .get_game(req.game_id) + .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; let game = &mut *link.write().await; - game.set_settings(req.setting); + game.set_settings(setting); Ok(()) } @@ -274,15 +274,19 @@ pub async fn handle_set_setting( /// ``` pub async fn handle_remove_player( Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(RemovePlayerRequest { + game_id, + player_id, + reason, + }): Blaze, ) -> ServerResult<()> { let link = game_manager - .get_game(req.game_id) + .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; let game = &mut *link.write().await; - game.remove_player(req.player_id, req.reason); + game.remove_player(player_id, reason); Ok(()) } @@ -306,15 +310,18 @@ pub async fn handle_remove_player( pub async fn handle_update_mesh_connection( SessionAuth(player): SessionAuth, Extension(game_manager): Extension>, - Blaze(mut req): Blaze, + Blaze(UpdateMeshRequest { + game_id, + mut targets, + }): Blaze, ) -> ServerResult<()> { - let target = match req.targets.pop() { + let target = match targets.pop() { Some(value) => value, None => return Ok(()), }; let link = game_manager - .get_game(req.game_id) + .get_game(game_id) .await .ok_or(GameManagerError::InvalidGameId)?; @@ -448,14 +455,14 @@ pub async fn handle_update_mesh_connection( pub async fn handle_start_matchmaking( player: GamePlayer, Extension(game_manager): Extension>, - Blaze(req): Blaze, + Blaze(MatchmakingRequest { rules }): Blaze, ) -> ServerResult> { let session_id = player.player.id; info!("Player {} started matchmaking", player.player.display_name); tokio::spawn(async move { - let rule_set = Arc::new(req.rules); + let rule_set = Arc::new(rules); // If adding failed attempt to queue instead if let Err(player) = game_manager.try_add(player, &rule_set).await { game_manager.queue(player, rule_set).await; diff --git a/src/session/routes/user_sessions.rs b/src/session/routes/user_sessions.rs index 7305d27b..7f8d1195 100644 --- a/src/session/routes/user_sessions.rs +++ b/src/session/routes/user_sessions.rs @@ -64,10 +64,8 @@ pub async fn handle_resume_session( session: SessionLink, Extension(db): Extension, Extension(sessions): Extension>, - Blaze(req): Blaze, + Blaze(ResumeSessionRequest { session_token }): Blaze, ) -> ServerResult> { - let session_token = req.session_token; - // Verify the authentication token let player_id = sessions .verify_token(&session_token) @@ -121,9 +119,10 @@ pub async fn handle_resume_session( /// ``` pub async fn handle_update_network( session: SessionLink, - Blaze(mut req): Blaze, + Blaze(UpdateNetworkRequest { mut address, qos }): Blaze, ) { - if let NetworkAddress::AddressPair(pair) = &mut req.address { + // TODO: This won't be required after QoS servers are correctly functioning + if let NetworkAddress::AddressPair(pair) = &mut address { let ext = &mut pair.external; // If address is missing @@ -135,7 +134,7 @@ pub async fn handle_update_network( } tokio::spawn(async move { - session.set_network_info(req.address, req.qos).await; + session.set_network_info(address, qos).await; }); } @@ -150,9 +149,9 @@ pub async fn handle_update_network( /// ``` pub async fn handle_update_hardware_flag( session: SessionLink, - Blaze(req): Blaze, + Blaze(UpdateHardwareFlagsRequest { hardware_flags }): Blaze, ) { tokio::spawn(async move { - session.set_hardware_flags(req.hardware_flags).await; + session.set_hardware_flags(hardware_flags).await; }); } diff --git a/src/session/routes/util.rs b/src/session/routes/util.rs index e80e5c0b..638855d7 100644 --- a/src/session/routes/util.rs +++ b/src/session/routes/util.rs @@ -148,9 +148,9 @@ const ME3_DIME: &str = include_str!("../../resources/data/dime.xml"); /// } /// ``` pub async fn handle_fetch_client_config( - Blaze(req): Blaze, + Blaze(FetchConfigRequest { id }): Blaze, ) -> ServerResult> { - let config = match req.id.as_str() { + let config = match id.as_str() { "ME3_DATA" => data_config(), "ME3_MSG" => messages(), "ME3_ENT" => load_entitlements(), @@ -472,8 +472,10 @@ fn data_config() -> TdfMap { /// "TVAL": 90000000 /// } /// ``` -pub async fn handle_suspend_user_ping(Blaze(req): Blaze) -> BlazeError { - let res = match req.time_value.cmp(&90000000) { +pub async fn handle_suspend_user_ping( + Blaze(SuspendPingRequest { time_value }): Blaze, +) -> BlazeError { + let res = match time_value.cmp(&90000000) { Ordering::Less => UtilError::SuspendPingTimeTooSmall, Ordering::Greater => UtilError::SuspendPingTimeTooLarge, Ordering::Equal => UtilError::PingSuspended, @@ -495,9 +497,9 @@ pub async fn handle_suspend_user_ping(Blaze(req): Blaze) -> pub async fn handle_user_settings_save( SessionAuth(player): SessionAuth, Extension(db): Extension, - Blaze(req): Blaze, + Blaze(SettingsSaveRequest { value, key }): Blaze, ) -> ServerResult<()> { - PlayerData::set(&db, player.id, req.key, req.value).await?; + PlayerData::set(&db, player.id, key, value).await?; Ok(()) }