diff --git a/rc_core/src/factory/adapter_enum.rs b/rc_core/src/factory/adapter_enum.rs index 8714224..353496c 100644 --- a/rc_core/src/factory/adapter_enum.rs +++ b/rc_core/src/factory/adapter_enum.rs @@ -57,6 +57,14 @@ impl oj_rc_factory::VehicleFactoryAdapter for Factory { Self::None => Ok(()), } } + + async fn set_featured(&self, id: i32, is_featured: bool) -> Result<(), Box> { + match self { + Self::Arc(x) => x.set_featured(id, is_featured).await, + Self::Custom(x) => x.set_featured(id, is_featured).await, + Self::None => Ok(()), + } + } } impl Factory { diff --git a/rc_factory/src/arc/adapter.rs b/rc_factory/src/arc/adapter.rs index e323d3c..180ced8 100644 --- a/rc_factory/src/arc/adapter.rs +++ b/rc_factory/src/arc/adapter.rs @@ -321,4 +321,16 @@ impl crate::VehicleFactoryAdapter for ArcAdapter { } Ok(()) } + + async fn set_featured(&self, id: i32, is_featured: bool) -> Result<(), Box> { + if self.is_readonly { + return Ok(()); + } + super::entities::robot_metadata::ActiveModel { + id: sea_orm::ActiveValue::Set(id as u32), + featured: sea_orm::ActiveValue::Set(is_featured as i32), + ..Default::default() + }.update(&self.orm).await?; + Ok(()) + } } diff --git a/rc_factory/src/traits.rs b/rc_factory/src/traits.rs index f5c8e16..fb5257e 100644 --- a/rc_factory/src/traits.rs +++ b/rc_factory/src/traits.rs @@ -7,6 +7,7 @@ pub trait VehicleFactoryAdapter: Send + Sync + 'static { /// Just update any purchase trackers async fn purchase(&self, id: i32) -> Result<(), Box>; async fn update_vehicle(&self, id: i32, cube_data: Option>, colour_data: Option>) -> Result<(), Box>; + async fn set_featured(&self, id: i32, is_featured: bool) -> Result<(), Box>; } #[derive(Debug, Clone)] diff --git a/rc_services_room/src/operations/crf_make_featured.rs b/rc_services_room/src/operations/crf_make_featured.rs new file mode 100644 index 0000000..81cb32f --- /dev/null +++ b/rc_services_room/src/operations/crf_make_featured.rs @@ -0,0 +1,39 @@ +use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl}; +use polariton::operation::{ParameterTable, Typed}; +use oj_rc_factory::VehicleFactoryAdapter; + +const CODE: u8 = 99; + +const ID_PARAM_KEY: u8 = 94; + +pub(super) struct FactorySetFeatured { + factory: std::sync::Arc, +} + +#[async_trait::async_trait] +impl SimpleOperation for FactorySetFeatured { + type User = crate::UserTy; + const CODE: u8 = CODE; + + async fn handle(&self, mut params: ParameterTable, user: &Self::User) -> Result, SimpleOpError> { + if let Some(Typed::Int(factory_id)) = params.remove(&ID_PARAM_KEY) { + let user_info = user.user()?; + if user_info.is_dev() { + self.factory.set_featured(factory_id, true).await.map_err(|e| { + log::error!("Failed to make featured vehicle {} (for offset) from factory: {}", factory_id, e); + SimpleOpError::with_message( + oj_rc_core::data::error_codes::WebServicesError::DatabaseError as i16, + format!("Failed to make featured vehicle (for offset) from factory: {}", e) + ) + })?; + } + } + Ok(ParameterTable::with_capacity(1)) + } +} + +pub(super) fn factory_featured_provider(factory: &std::sync::Arc) -> SimpleOpImpl { + SimpleOpImpl::new(FactorySetFeatured { + factory: factory.to_owned() + }) +} diff --git a/rc_services_room/src/operations/crf_unmake_featured.rs b/rc_services_room/src/operations/crf_unmake_featured.rs new file mode 100644 index 0000000..eab2b21 --- /dev/null +++ b/rc_services_room/src/operations/crf_unmake_featured.rs @@ -0,0 +1,39 @@ +use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl}; +use polariton::operation::{ParameterTable, Typed}; +use oj_rc_factory::VehicleFactoryAdapter; + +const CODE: u8 = 100; + +const ID_PARAM_KEY: u8 = 94; + +pub(super) struct FactoryUnsetFeatured { + factory: std::sync::Arc, +} + +#[async_trait::async_trait] +impl SimpleOperation for FactoryUnsetFeatured { + type User = crate::UserTy; + const CODE: u8 = CODE; + + async fn handle(&self, mut params: ParameterTable, user: &Self::User) -> Result, SimpleOpError> { + if let Some(Typed::Int(factory_id)) = params.remove(&ID_PARAM_KEY) { + let user_info = user.user()?; + if user_info.is_dev() { + self.factory.set_featured(factory_id, false).await.map_err(|e| { + log::error!("Failed to make featured vehicle {} (for offset) from factory: {}", factory_id, e); + SimpleOpError::with_message( + oj_rc_core::data::error_codes::WebServicesError::DatabaseError as i16, + format!("Failed to make featured vehicle (for offset) from factory: {}", e) + ) + })?; + } + } + Ok(ParameterTable::with_capacity(1)) + } +} + +pub(super) fn factory_featured_provider(factory: &std::sync::Arc) -> SimpleOpImpl { + SimpleOpImpl::new(FactoryUnsetFeatured { + factory: factory.to_owned() + }) +} diff --git a/rc_services_room/src/operations/mod.rs b/rc_services_room/src/operations/mod.rs index 1e3dbc6..372ae59 100644 --- a/rc_services_room/src/operations/mod.rs +++ b/rc_services_room/src/operations/mod.rs @@ -103,6 +103,8 @@ mod item_shop_purchase; mod code_redeem; mod crf_rate_vehicle; mod crf_shift_vehicle; +mod crf_make_featured; +mod crf_unmake_featured; use polariton_server::operations::OperationsHandler; @@ -229,4 +231,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .add(steam_promo::steam_promos_provider()) .add(item_shop_purchase::item_purchase_provider(&init_ctx.cubes)) .add(code_redeem::code_redeem_provider(&init_ctx.cubes)) + .add(crf_make_featured::factory_featured_provider(&init_ctx.factory)) + .add(crf_unmake_featured::factory_featured_provider(&init_ctx.factory)) + .add(polariton_server::operations::Ack::<101, _>::default()) // RestoreCRFFeaturedRobot unused and also redundant??? }