mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add minimal robot factory functionality and arc archive support for #6
This commit is contained in:
15
rc_services_room/src/operations/crf_earnings.rs
Normal file
15
rc_services_room/src/operations/crf_earnings.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use polariton_server::operations::SimpleFunc;
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
const PARAM_KEY: u8 = 96;
|
||||
|
||||
pub(super) fn robot_shop_user_earnings_provider() -> SimpleFunc<88, crate::UserTy, impl (Fn(ParameterTable, &crate::UserTy) -> Result<ParameterTable, i16>) + Sync + Sync> {
|
||||
SimpleFunc::new(|params, _| {
|
||||
let mut params = params.to_dict();
|
||||
params.insert(PARAM_KEY, Typed::HashMap(vec![
|
||||
(Typed::Str("buyCount".into()), Typed::Int(0)),
|
||||
(Typed::Str("earnings".into()), Typed::Int(0)),
|
||||
].into()));
|
||||
Ok(params.into())
|
||||
})
|
||||
}
|
||||
51
rc_services_room/src/operations/crf_list_query.rs
Normal file
51
rc_services_room/src/operations/crf_list_query.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
use polariton::operation::{Typed, ParameterTable};
|
||||
use rc_factory::VehicleFactoryAdapter;
|
||||
|
||||
const CODE: u8 = 86;
|
||||
|
||||
const FILTERS_PARAM_KEY: u8 = 92;
|
||||
const ITEMS_PARAM_KEY: u8 = 93;
|
||||
|
||||
async fn do_handling(params: ParameterTable<()>, _user: &crate::UserTy, factory: &std::sync::Arc<rc_core::factory::Factory>) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Bytes(filters)) = params.remove(&FILTERS_PARAM_KEY) {
|
||||
let mut cursor = std::io::Cursor::new(filters.vec);
|
||||
let filters = crate::data::crf::ShopItemListFilters::parse(&mut cursor).map_err(|e| {
|
||||
log::error!("Failed to parse factory item query: {}", e);
|
||||
rc_core::data::error_codes::WebServicesError::UnexpectedError as i16
|
||||
})?;
|
||||
let vehicles = factory.list(filters.into_core()).await.map_err(|e| {
|
||||
log::error!("Failed to retrieve vehicles from factory: {}", e);
|
||||
rc_core::data::error_codes::WebServicesError::DatabaseError as i16
|
||||
})?;
|
||||
let vehicles: Vec<_> = vehicles.into_iter().map(|x| crate::data::crf::ItemResult::from(x)).collect();
|
||||
params.insert(ITEMS_PARAM_KEY, crate::data::crf::ItemResult::as_transmissible(&vehicles));
|
||||
}
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct CrfItemListProvider {
|
||||
factory: std::sync::Arc<rc_core::factory::Factory>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Operation<()> for CrfItemListProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: polariton::operation::ParameterTable<()>, user: &Self::User) -> polariton::operation::OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.factory).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationCode for CrfItemListProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn crf_item_list_query_provider(factory: &std::sync::Arc<rc_core::factory::Factory>) -> CrfItemListProvider {
|
||||
CrfItemListProvider {
|
||||
factory: factory.to_owned(),
|
||||
}
|
||||
}
|
||||
69
rc_services_room/src/operations/crf_purchase.rs
Normal file
69
rc_services_room/src/operations/crf_purchase.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use polariton::operation::{ParameterTable, Typed, OperationResponse};
|
||||
use rc_factory::VehicleFactoryAdapter;
|
||||
|
||||
const CODE: u8 = 166;
|
||||
|
||||
const SLOT_PARAM_KEY: u8 = 43; // in; int
|
||||
//const FREE_CURRENCY_COST_PARAM_KEY: u8 = 5; // in; int
|
||||
//const PREMIUM_CURRENCY_COST_PARAM_KEY: u8 = 6; // in; int
|
||||
const FACTORY_ID_PARAM_KEY: u8 = 94; // in; int
|
||||
|
||||
|
||||
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc<rc_core::factory::Factory>) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
let user_info = user.user()?;
|
||||
let slot = if let Some(Typed::Int(slot)) = params.remove(&SLOT_PARAM_KEY) {
|
||||
user_info.new_slot(Some(slot)).await?;
|
||||
slot
|
||||
} else {
|
||||
user_info.new_slot(None).await?.slot_i
|
||||
};
|
||||
if let Some(Typed::Int(factory_id)) = params.remove(&FACTORY_ID_PARAM_KEY) {
|
||||
// TODO charge for robot?
|
||||
let vehicle_to_copy = factory.vehicle(factory_id as _).await.map_err(|e| {
|
||||
log::error!("Failed to retrieve vehicle {} (for copy-construct) from factory: {}", factory_id, e);
|
||||
rc_core::data::error_codes::WebServicesError::DatabaseError as i16
|
||||
})?;
|
||||
if let Some(vehicle_to_copy) = vehicle_to_copy {
|
||||
let to_save = rc_core::persist::user::VehicleData {
|
||||
slot,
|
||||
robot_data: vehicle_to_copy.cube_data,
|
||||
colour_data: vehicle_to_copy.colour_data,
|
||||
weapon_order: Vec::default(), // FIXME calculate this somehow? Or maybe get the factory adapter to calculate this
|
||||
};
|
||||
user_info.save_slot(to_save).await?;
|
||||
} else {
|
||||
log::warn!("Failed to retrieve (for copy-construct) non-existent factory vehicle {}", factory_id);
|
||||
return Err(rc_core::data::error_codes::WebServicesError::DatabaseError as i16);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct CrfItemPurchaseProvider {
|
||||
factory: std::sync::Arc<rc_core::factory::Factory>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl polariton_server::operations::Operation<()> for CrfItemPurchaseProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.factory).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl polariton_server::operations::OperationCode for CrfItemPurchaseProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc<rc_core::factory::Factory>) -> CrfItemPurchaseProvider {
|
||||
CrfItemPurchaseProvider {
|
||||
factory: factory.to_owned(),
|
||||
}
|
||||
}
|
||||
51
rc_services_room/src/operations/crf_vehicle_data.rs
Normal file
51
rc_services_room/src/operations/crf_vehicle_data.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use polariton_server::operations::{Operation, OperationCode};
|
||||
use polariton::operation::{Typed, ParameterTable};
|
||||
use rc_factory::VehicleFactoryAdapter;
|
||||
|
||||
const CODE: u8 = 87;
|
||||
|
||||
const ID_PARAM_KEY: u8 = 94;
|
||||
const DATA_PARAM_KEY: u8 = 95;
|
||||
|
||||
async fn do_handling(params: ParameterTable<()>, _user: &crate::UserTy, factory: &std::sync::Arc<rc_core::factory::Factory>) -> Result<ParameterTable, i16> {
|
||||
let mut params = params.to_dict();
|
||||
if let Some(Typed::Int(id)) = params.remove(&ID_PARAM_KEY) {
|
||||
let vehicle = factory.vehicle(id as _).await.map_err(|e| {
|
||||
log::error!("Failed to retrieve vehicle {} from factory: {}", id, e);
|
||||
rc_core::data::error_codes::WebServicesError::DatabaseError as i16
|
||||
})?;
|
||||
if let Some(vehicle) = vehicle {
|
||||
let vehicle_data = crate::data::crf::ItemData::from(vehicle);
|
||||
params.insert(DATA_PARAM_KEY, vehicle_data.as_transmissible());
|
||||
} else {
|
||||
log::warn!("Failed to retrieve non-existent factory vehicle {}", id);
|
||||
return Err(rc_core::data::error_codes::WebServicesError::InvalidRobot as i16);
|
||||
}
|
||||
}
|
||||
Ok(params.into())
|
||||
}
|
||||
|
||||
pub struct CrfItemDataProvider {
|
||||
factory: std::sync::Arc<rc_core::factory::Factory>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Operation<()> for CrfItemDataProvider {
|
||||
type User = crate::UserTy;
|
||||
|
||||
async fn handle_async(&self, params: polariton::operation::ParameterTable<()>, user: &Self::User) -> polariton::operation::OperationResponse<()> {
|
||||
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.factory).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl OperationCode for CrfItemDataProvider {
|
||||
fn op_code() -> u8 {
|
||||
CODE
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn crf_item_data_provider(factory: &std::sync::Arc<rc_core::factory::Factory>) -> CrfItemDataProvider {
|
||||
CrfItemDataProvider {
|
||||
factory: factory.to_owned(),
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,10 @@ mod garage_slots_order;
|
||||
mod garage_slot_select;
|
||||
mod garage_slot_dismantle;
|
||||
mod garage_slot_upgrade;
|
||||
mod crf_earnings;
|
||||
mod crf_list_query;
|
||||
mod crf_vehicle_data;
|
||||
mod crf_purchase;
|
||||
|
||||
use polariton_server::operations::OperationsHandler;
|
||||
|
||||
@@ -194,4 +198,8 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy>
|
||||
.add(garage_slot_select::garage_slot_selector())
|
||||
.add(garage_slot_dismantle::garage_slot_dismantler())
|
||||
.add(garage_slot_upgrade::garage_slot_upgrage_provider())
|
||||
.add(crf_earnings::robot_shop_user_earnings_provider())
|
||||
.add(crf_list_query::crf_item_list_query_provider(&init_ctx.factory))
|
||||
.add(crf_vehicle_data::crf_item_data_provider(&init_ctx.factory))
|
||||
.add(crf_purchase::crf_copy_to_bay_provider(&init_ctx.factory))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user