From d7f5c301f6f6b307f2da333b466287ec3368856a Mon Sep 17 00:00:00 2001 From: "Dr. Dystopia" Date: Sat, 18 Dec 2021 13:38:22 +0100 Subject: [PATCH] Extract 'check_response' function --- server/src/persistence/character_updater.rs | 64 +++++++++------------ 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/server/src/persistence/character_updater.rs b/server/src/persistence/character_updater.rs index 2163ad093d..f5d590de87 100644 --- a/server/src/persistence/character_updater.rs +++ b/server/src/persistence/character_updater.rs @@ -8,7 +8,7 @@ use crate::persistence::{ PersistedComponents, VelorenConnection, }; use crossbeam_channel::TryIter; -use rusqlite::DropBehavior; +use rusqlite::{DropBehavior, Transaction}; use specs::Entity; use std::{ collections::HashMap, @@ -408,22 +408,14 @@ fn execute_character_create( connection: &mut VelorenConnection, ) -> Result { let mut transaction = connection.connection.transaction()?; - - let response = CharacterLoaderResponse { - entity, - result: CharacterLoaderResponseKind::CharacterCreation(super::character::create_character( + let result = + CharacterLoaderResponseKind::CharacterCreation(super::character::create_character( requesting_player_uuid, &alias, persisted_components, &mut transaction, - )), - }; - - if !response.is_err() { - transaction.commit()?; - }; - - Ok(response) + )); + check_response(entity, transaction, result) } fn execute_character_edit( @@ -435,23 +427,14 @@ fn execute_character_edit( connection: &mut VelorenConnection, ) -> Result { let mut transaction = connection.connection.transaction()?; - - let response = CharacterLoaderResponse { - entity, - result: CharacterLoaderResponseKind::CharacterEdit(super::character::edit_character( - editable_components, - &mut transaction, - character_id, - requesting_player_uuid, - &alias, - )), - }; - - if !response.is_err() { - transaction.commit()?; - }; - - Ok(response) + let result = CharacterLoaderResponseKind::CharacterEdit(super::character::edit_character( + editable_components, + &mut transaction, + character_id, + requesting_player_uuid, + &alias, + )); + check_response(entity, transaction, result) } fn execute_character_delete( @@ -461,15 +444,20 @@ fn execute_character_delete( connection: &mut VelorenConnection, ) -> Result { let mut transaction = connection.connection.transaction()?; + let result = CharacterLoaderResponseKind::CharacterList(super::character::delete_character( + requesting_player_uuid, + character_id, + &mut transaction, + )); + check_response(entity, transaction, result) +} - let response = CharacterLoaderResponse { - entity, - result: CharacterLoaderResponseKind::CharacterList(super::character::delete_character( - requesting_player_uuid, - character_id, - &mut transaction, - )), - }; +fn check_response( + entity: Entity, + transaction: Transaction, + result: CharacterLoaderResponseKind, +) -> Result { + let response = CharacterLoaderResponse { entity, result }; if !response.is_err() { transaction.commit()?;