diff --git a/rc_core/src/cubes/mod.rs b/rc_core/src/cubes/mod.rs new file mode 100644 index 0000000..9205eb1 --- /dev/null +++ b/rc_core/src/cubes/mod.rs @@ -0,0 +1,20 @@ +pub(self) mod parser; + +mod weapon_list; +pub use weapon_list::WeaponListParser; + +pub struct CubeParsers { + weapon_list: std::sync::Arc, +} + +impl CubeParsers { + pub fn new(conf: &crate::ConfigImpl) -> Self { + Self { + weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(>::cubes(conf).values())), + } + } + + pub fn weapon_order(&self) -> std::sync::Arc { + self.weapon_list.clone() + } +} diff --git a/rc_core/src/cubes/parser.rs b/rc_core/src/cubes/parser.rs new file mode 100644 index 0000000..397f2ed --- /dev/null +++ b/rc_core/src/cubes/parser.rs @@ -0,0 +1,70 @@ +#![allow(dead_code)] + +pub struct Cube { + pub id: u32, + pub x: u8, + pub y: u8, + pub z: u8, + pub orientation: u8, +} + +impl Cube { + pub fn parse(r: &mut dyn std::io::Read) -> std::io::Result { + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + let id = u32::from_le_bytes(buf); + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + Ok(Self { + id, + x: buf[0], + y: buf[1], + z: buf[2], + orientation: buf[3], + }) + } + + pub fn parse_list(r: &mut dyn std::io::Read) -> std::io::Result> { + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + let count = u32::from_le_bytes(buf); + let mut cubes = Vec::with_capacity(count as _); + for _ in 0..count { + let cube = Self::parse(r)?; + cubes.push(cube); + } + Ok(cubes) + } +} + +pub struct Colour { + pub colour: u8, + pub x: u8, + pub y: u8, + pub z: u8, +} + +impl Colour { + pub fn parse(r: &mut dyn std::io::Read) -> std::io::Result { + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + Ok(Self { + colour: buf[0], + x: buf[1], + y: buf[2], + z: buf[3], + }) + } + + pub fn parse_list(r: &mut dyn std::io::Read) -> std::io::Result> { + let mut buf = [0u8; 4]; + r.read_exact(&mut buf)?; + let count = u32::from_le_bytes(buf); + let mut cubes = Vec::with_capacity(count as _); + for _ in 0..count { + let cube = Self::parse(r)?; + cubes.push(cube); + } + Ok(cubes) + } +} diff --git a/rc_core/src/cubes/weapon_list.rs b/rc_core/src/cubes/weapon_list.rs new file mode 100644 index 0000000..68c81e5 --- /dev/null +++ b/rc_core/src/cubes/weapon_list.rs @@ -0,0 +1,52 @@ +const MAX_WEAPON_SLOTS: usize = 3; + +struct WeaponInfo { + category: crate::data::weapon_list::ItemCategory, + tier: crate::data::cube_list::ItemTier, +} + +impl WeaponInfo { + fn weapon_order_key(&self) -> i32 { + self.category.but_bigger() + (self.tier as i32) + } +} + +pub struct WeaponListParser { + weapons: std::collections::HashMap, +} + +impl WeaponListParser { + pub fn with_cubes<'a, I: std::iter::Iterator>(iter: I) -> Self { + let mut weapons = std::collections::HashMap::new(); + for item in iter { + if let crate::persist::ItemType::Weapon = item.info.type_ { + weapons.insert(item.id, WeaponInfo { + category: item.info.category.into(), + tier: item.info.size.into(), + }); + } + } + Self { + weapons, + } + } + + pub fn guess_weapons(&self, r: &mut dyn std::io::Read) -> Vec { + match super::parser::Cube::parse_list(r) { + Ok(cubes) => { + let mut keys = std::collections::HashSet::new(); + for cube in cubes { + if let Some(weapon) = self.weapons.get(&cube.id) { + keys.insert(weapon.weapon_order_key()); + if keys.len() == MAX_WEAPON_SLOTS { break; } + } + } + keys.into_iter().collect::>() + } + Err(e) => { + log::error!("Failed to parse cube data to guess weapon order: {}", e); + Vec::default() + } + } + } +} diff --git a/rc_core/src/lib.rs b/rc_core/src/lib.rs index 9b63dd8..df69d04 100644 --- a/rc_core/src/lib.rs +++ b/rc_core/src/lib.rs @@ -11,3 +11,5 @@ pub use persist::config::{ConfigImpl, ConfigProvider}; pub mod polariton; pub mod factory; + +pub mod cubes; diff --git a/rc_core/src/persist/config/cubes_json.rs b/rc_core/src/persist/config/cubes_json.rs index 1e5576f..facba01 100644 --- a/rc_core/src/persist/config/cubes_json.rs +++ b/rc_core/src/persist/config/cubes_json.rs @@ -277,4 +277,8 @@ impl super::ConfigProvider for CubeConfig { async fn factory(&self) -> Result> { crate::factory::Factory::from_config(&self.factory).await } + + fn cubes(&self) -> &'_ std::collections::HashMap { + &self.cubes + } } diff --git a/rc_core/src/persist/config/traits.rs b/rc_core/src/persist/config/traits.rs index ece3866..cc43273 100644 --- a/rc_core/src/persist/config/traits.rs +++ b/rc_core/src/persist/config/traits.rs @@ -22,6 +22,7 @@ pub trait ConfigProvider { fn server_config(&self) -> ServerConfig; fn garage_upgrades(&self) -> GarageUpgrades; async fn factory(&self) -> Result>; + fn cubes(&self) -> &'_ std::collections::HashMap; } pub struct CompleteCampaignProvider { diff --git a/rc_core/src/persist/mod.rs b/rc_core/src/persist/mod.rs index 93513b2..79143b3 100644 --- a/rc_core/src/persist/mod.rs +++ b/rc_core/src/persist/mod.rs @@ -2,7 +2,7 @@ pub mod config; pub mod user; mod cube_data; -pub use cube_data::{Cube, ItemTier, ItemCategory}; +pub use cube_data::{Cube, ItemTier, ItemCategory, ItemType}; //pub use cube_data::{VisibilityMode, ItemType}; mod garage; diff --git a/rc_services_room/src/main.rs b/rc_services_room/src/main.rs index 5444ff7..02dd904 100644 --- a/rc_services_room/src/main.rs +++ b/rc_services_room/src/main.rs @@ -17,6 +17,7 @@ pub struct InitConfig { pub cubes: rc_core::persist::config::ConfigImpl, pub users: std::sync::Arc, pub factory: std::sync::Arc, + pub parsers: rc_core::cubes::CubeParsers, } #[tokio::main] @@ -28,10 +29,12 @@ async fn main() -> std::io::Result<()> { let cubes = rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data"); let users = std::sync::Arc::new(rc_core::persist::user::UserImpl::load(&args.data, &cubes).await.expect("Bad user data")); let factory = std::sync::Arc::new(>::factory::<'_, '_>(&cubes).await.expect("Bad vehicle factory (CRF) config")); + let parsers = rc_core::cubes::CubeParsers::new(&cubes); let init_ctx = std::sync::Arc::new(InitConfig { cubes, users, factory, + parsers, }); let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(&init_ctx), polariton_server::events::EventsHandler::new())); diff --git a/rc_services_room/src/operations/crf_purchase.rs b/rc_services_room/src/operations/crf_purchase.rs index 34c7a6a..50995d7 100644 --- a/rc_services_room/src/operations/crf_purchase.rs +++ b/rc_services_room/src/operations/crf_purchase.rs @@ -9,7 +9,7 @@ const SLOT_PARAM_KEY: u8 = 43; // in; int const FACTORY_ID_PARAM_KEY: u8 = 94; // in; int -async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc) -> Result { +async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: &std::sync::Arc, weapon_order: &std::sync::Arc) -> Result { let mut params = params.to_dict(); let user_info = user.user()?; let slot = if let Some(Typed::Int(slot)) = params.remove(&SLOT_PARAM_KEY) { @@ -25,12 +25,16 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: rc_core::data::error_codes::WebServicesError::DatabaseError as i16 })?; if let Some((vehicle_to_copy, vehicle_meta)) = vehicle { + // parse cube data for weapon order + let mut cursor = std::io::Cursor::new(&vehicle_to_copy.cube_data); + let weapons = weapon_order.guess_weapons(&mut cursor); + // save to database let to_save = rc_core::persist::user::VehicleData { name: Some(vehicle_meta.name), 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 + weapon_order: weapons, crf_id: Some(factory_id), }; user_info.save_slot(to_save).await?; @@ -46,6 +50,7 @@ async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, factory: pub struct CrfItemPurchaseProvider { factory: std::sync::Arc, + weapon_order: std::sync::Arc, } #[async_trait::async_trait] @@ -53,7 +58,7 @@ 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::(do_handling(params, user, &self.factory).await) + polariton_server::operations::result_to_op_resp::(do_handling(params, user, &self.factory, &self.weapon_order).await) } } @@ -64,8 +69,9 @@ impl polariton_server::operations::OperationCode for CrfItemPurchaseProvider { } -pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc) -> CrfItemPurchaseProvider { +pub(super) fn crf_copy_to_bay_provider(factory: &std::sync::Arc, weapon_order: std::sync::Arc) -> CrfItemPurchaseProvider { CrfItemPurchaseProvider { factory: factory.to_owned(), + weapon_order, } } diff --git a/rc_services_room/src/operations/mod.rs b/rc_services_room/src/operations/mod.rs index e7309a8..a21723b 100644 --- a/rc_services_room/src/operations/mod.rs +++ b/rc_services_room/src/operations/mod.rs @@ -201,5 +201,5 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .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)) + .add(crf_purchase::crf_copy_to_bay_provider(&init_ctx.factory, init_ctx.parsers.weapon_order())) }