Skip to content

Commit

Permalink
Made use of destructuring on blaze handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtread committed Sep 25, 2023
1 parent f6b82ad commit 5fa17ea
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 54 deletions.
18 changes: 8 additions & 10 deletions src/session/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ pub async fn handle_silent_login(
session: SessionLink,
Extension(db): Extension<DatabaseConnection>,
Extension(sessions): Extension<Arc<Sessions>>,
Blaze(req): Blaze<SilentLoginRequest>,
Blaze(SilentLoginRequest { token }): Blaze<SilentLoginRequest>,
) -> ServerResult<Blaze<AuthResponse>> {
// 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,
})?;
Expand All @@ -83,7 +83,7 @@ pub async fn handle_silent_login(

Ok(Blaze(AuthResponse {
player,
session_token: req.token,
session_token: token,
silent: true,
}))
}
Expand All @@ -94,15 +94,15 @@ pub async fn handle_origin_login(
Extension(config): Extension<Arc<RuntimeConfig>>,
Extension(sessions): Extension<Arc<Sessions>>,
Extension(retriever): Extension<Arc<Retriever>>,
Blaze(req): Blaze<OriginLoginRequest>,
Blaze(OriginLoginRequest { token, .. }): Blaze<OriginLoginRequest>,
) -> ServerResult<Blaze<AuthResponse>> {
// Obtain an origin flow
let mut flow = retriever.origin_flow().await.map_err(|err| {
error!("Failed to obtain origin flow: {}", err);
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
})?;
Expand Down Expand Up @@ -209,9 +209,8 @@ static ENTITLEMENTS: &[Entitlement; 34] = &[
/// }
/// ```
pub async fn handle_list_entitlements(
Blaze(req): Blaze<ListEntitlementsRequest>,
Blaze(ListEntitlementsRequest { tag }): Blaze<ListEntitlementsRequest>,
) -> Option<Blaze<ListEntitlementsResponse>> {
let tag: String = req.tag;
if !tag.is_empty() {
return None;
}
Expand Down Expand Up @@ -286,9 +285,8 @@ pub async fn handle_create_account(
Extension(db): Extension<DatabaseConnection>,
Extension(config): Extension<Arc<RuntimeConfig>>,
Extension(sessions): Extension<Arc<Sessions>>,
Blaze(req): Blaze<CreateAccountRequest>,
Blaze(CreateAccountRequest { email, password }): Blaze<CreateAccountRequest>,
) -> ServerResult<Blaze<AuthResponse>> {
let email = req.email;
if !EmailAddress::is_valid(&email) {
return Err(AuthenticationError::InvalidEmail.into());
}
Expand All @@ -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
})?;
Expand Down
67 changes: 37 additions & 30 deletions src/session/routes/game_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use std::sync::Arc;

pub async fn handle_join_game(
player: GamePlayer,
Blaze(JoinGameRequest { user }): Blaze<JoinGameRequest>,
Extension(sessions): Extension<Arc<Sessions>>,
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<JoinGameRequest>,
) -> ServerResult<Blaze<JoinGameResponse>> {
// Lookup the session join target
let session = sessions
.lookup_session(req.user.id)
.lookup_session(user.id)
.await
.ok_or(GameManagerError::JoinPlayerFailed)?;

Expand Down Expand Up @@ -63,22 +63,16 @@ pub async fn handle_join_game(
}

pub async fn handle_get_game_data(
Blaze(mut req): Blaze<GetGameDataRequest>,
Blaze(GetGameDataRequest { game_list }): Blaze<GetGameDataRequest>,
Extension(game_manager): Extension<Arc<GameManager>>,
) -> ServerResult<RawBlaze> {
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)
Expand Down Expand Up @@ -138,9 +132,12 @@ pub async fn handle_get_game_data(
pub async fn handle_create_game(
player: GamePlayer,
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<CreateGameRequest>,
Blaze(CreateGameRequest {
attributes,
setting,
}): Blaze<CreateGameRequest>,
) -> ServerResult<Blaze<CreateGameResponse>> {
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 {
Expand Down Expand Up @@ -183,16 +180,19 @@ pub async fn handle_create_game(
/// ```
pub async fn handle_set_attributes(
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<SetAttributesRequest>,
Blaze(SetAttributesRequest {
attributes,
game_id,
}): Blaze<SetAttributesRequest>,
) -> 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
Expand All @@ -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;
}
});

Expand All @@ -221,15 +221,15 @@ pub async fn handle_set_attributes(
/// ```
pub async fn handle_set_state(
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<SetStateRequest>,
Blaze(SetStateRequest { game_id, state }): Blaze<SetStateRequest>,
) -> 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(())
}
Expand All @@ -246,15 +246,15 @@ pub async fn handle_set_state(
/// ```
pub async fn handle_set_setting(
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<SetSettingRequest>,
Blaze(SetSettingRequest { game_id, setting }): Blaze<SetSettingRequest>,
) -> 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(())
}
Expand All @@ -274,15 +274,19 @@ pub async fn handle_set_setting(
/// ```
pub async fn handle_remove_player(
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<RemovePlayerRequest>,
Blaze(RemovePlayerRequest {
game_id,
player_id,
reason,
}): Blaze<RemovePlayerRequest>,
) -> 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(())
}
Expand All @@ -306,15 +310,18 @@ pub async fn handle_remove_player(
pub async fn handle_update_mesh_connection(
SessionAuth(player): SessionAuth,
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(mut req): Blaze<UpdateMeshRequest>,
Blaze(UpdateMeshRequest {
game_id,
mut targets,
}): Blaze<UpdateMeshRequest>,
) -> 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)?;

Expand Down Expand Up @@ -448,14 +455,14 @@ pub async fn handle_update_mesh_connection(
pub async fn handle_start_matchmaking(
player: GamePlayer,
Extension(game_manager): Extension<Arc<GameManager>>,
Blaze(req): Blaze<MatchmakingRequest>,
Blaze(MatchmakingRequest { rules }): Blaze<MatchmakingRequest>,
) -> ServerResult<Blaze<MatchmakingResponse>> {
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;
Expand Down
15 changes: 7 additions & 8 deletions src/session/routes/user_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ pub async fn handle_resume_session(
session: SessionLink,
Extension(db): Extension<DatabaseConnection>,
Extension(sessions): Extension<Arc<Sessions>>,
Blaze(req): Blaze<ResumeSessionRequest>,
Blaze(ResumeSessionRequest { session_token }): Blaze<ResumeSessionRequest>,
) -> ServerResult<Blaze<AuthResponse>> {
let session_token = req.session_token;

// Verify the authentication token
let player_id = sessions
.verify_token(&session_token)
Expand Down Expand Up @@ -121,9 +119,10 @@ pub async fn handle_resume_session(
/// ```
pub async fn handle_update_network(
session: SessionLink,
Blaze(mut req): Blaze<UpdateNetworkRequest>,
Blaze(UpdateNetworkRequest { mut address, qos }): Blaze<UpdateNetworkRequest>,
) {
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
Expand All @@ -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;
});
}

Expand All @@ -150,9 +149,9 @@ pub async fn handle_update_network(
/// ```
pub async fn handle_update_hardware_flag(
session: SessionLink,
Blaze(req): Blaze<UpdateHardwareFlagsRequest>,
Blaze(UpdateHardwareFlagsRequest { hardware_flags }): Blaze<UpdateHardwareFlagsRequest>,
) {
tokio::spawn(async move {
session.set_hardware_flags(req.hardware_flags).await;
session.set_hardware_flags(hardware_flags).await;
});
}
14 changes: 8 additions & 6 deletions src/session/routes/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ const ME3_DIME: &str = include_str!("../../resources/data/dime.xml");
/// }
/// ```
pub async fn handle_fetch_client_config(
Blaze(req): Blaze<FetchConfigRequest>,
Blaze(FetchConfigRequest { id }): Blaze<FetchConfigRequest>,
) -> ServerResult<Blaze<FetchConfigResponse>> {
let config = match req.id.as_str() {
let config = match id.as_str() {
"ME3_DATA" => data_config(),
"ME3_MSG" => messages(),
"ME3_ENT" => load_entitlements(),
Expand Down Expand Up @@ -472,8 +472,10 @@ fn data_config() -> TdfMap<String, String> {
/// "TVAL": 90000000
/// }
/// ```
pub async fn handle_suspend_user_ping(Blaze(req): Blaze<SuspendPingRequest>) -> BlazeError {
let res = match req.time_value.cmp(&90000000) {
pub async fn handle_suspend_user_ping(
Blaze(SuspendPingRequest { time_value }): Blaze<SuspendPingRequest>,
) -> BlazeError {
let res = match time_value.cmp(&90000000) {
Ordering::Less => UtilError::SuspendPingTimeTooSmall,
Ordering::Greater => UtilError::SuspendPingTimeTooLarge,
Ordering::Equal => UtilError::PingSuspended,
Expand All @@ -495,9 +497,9 @@ pub async fn handle_suspend_user_ping(Blaze(req): Blaze<SuspendPingRequest>) ->
pub async fn handle_user_settings_save(
SessionAuth(player): SessionAuth,
Extension(db): Extension<DatabaseConnection>,
Blaze(req): Blaze<SettingsSaveRequest>,
Blaze(SettingsSaveRequest { value, key }): Blaze<SettingsSaveRequest>,
) -> ServerResult<()> {
PlayerData::set(&db, player.id, req.key, req.value).await?;
PlayerData::set(&db, player.id, key, value).await?;
Ok(())
}

Expand Down

0 comments on commit 5fa17ea

Please sign in to comment.