diff --git a/rc_core/src/persist/config/cubes_json.rs b/rc_core/src/persist/config/cubes_json.rs index 0f1c001..7067987 100644 --- a/rc_core/src/persist/config/cubes_json.rs +++ b/rc_core/src/persist/config/cubes_json.rs @@ -187,30 +187,28 @@ impl super::ConfigProvider for CubeConfig { Typed::IntArr(keys_vec.into()) } - fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet) -> Typed { - let mut seen_cubes = std::collections::HashSet::with_capacity(self.cubes.len()); - let mut needed_cubes = std::collections::HashSet::with_capacity(self.cubes.len()); - let mut typed_nodes = Vec::new(); + fn tech_tree_nodes(&self) -> super::TechTreeNodeProvider { + let mut nodes = indexmap::IndexMap::with_capacity(self.cubes.len()); for cube in self.cubes.values() { if let Some(tree_data) = &cube.tree { - let is_unlocked = unlocked_cubes.contains(&cube.id); - let is_unlockable = tree_data.requires.iter().all(|id| unlocked_cubes.contains(id)); - tree_data.neighbours.iter().for_each(|id| { needed_cubes.insert(*id); }); - seen_cubes.insert(cube.id); - let node_data = tree_data.to_owned().into_data(cube.id, is_unlocked, is_unlockable); - typed_nodes.push(node_data.as_transmissible_key_val()); + nodes.insert(cube.id, tree_data.to_owned()); } } - for needed_cube_id in needed_cubes { - if !seen_cubes.contains(&needed_cube_id) { - log::warn!("Tech tree needs cube {} but it doesn't have tree info", needed_cube_id); + nodes.shrink_to_fit(); + super::TechTreeNodeProvider { + tree: nodes, + } + } + + fn tech_tree_costs(&self) -> std::collections::HashMap { // cube id (hex) -> tech point cost + let mut costs = std::collections::HashMap::with_capacity(self.cubes.len()); + for cube in self.cubes.values() { + if let Some(tree_data) = &cube.tree { + costs.insert(hex::encode(cube.id.to_be_bytes()), tree_data.tech_points); } } - Typed::Dict(Dict { - key_ty: TypePrefix::Str, - val_ty: TypePrefix::HashMap, - items: typed_nodes, - }) + costs.shrink_to_fit(); // probably unnecessary, but free memory usage reduction! + costs } fn ids(&self) -> Vec { diff --git a/rc_core/src/persist/config/mod.rs b/rc_core/src/persist/config/mod.rs index 2ac83d4..affcb4b 100644 --- a/rc_core/src/persist/config/mod.rs +++ b/rc_core/src/persist/config/mod.rs @@ -10,6 +10,9 @@ pub use validation::{SelfValidator, ValidationInfo, ValidationMessage}; mod campaign; pub use campaign::{CampaignResolver, CompleteCampaignProvider}; +mod tech_tree; +pub use tech_tree::TechTreeNodeProvider; + pub type ConfigImpl = CubeConfig; fn __must_impl>() {} diff --git a/rc_core/src/persist/config/tech_tree.rs b/rc_core/src/persist/config/tech_tree.rs new file mode 100644 index 0000000..0fae58a --- /dev/null +++ b/rc_core/src/persist/config/tech_tree.rs @@ -0,0 +1,33 @@ +use polariton::operation::{Typed, Dict}; +use polariton::serdes::TypePrefix; + +pub struct TechTreeNodeProvider { + // the order of this probably doesn't matter but for consistency... let's do it this way + pub(super) tree: indexmap::IndexMap, +} + +impl TechTreeNodeProvider { + pub fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet) -> Typed { + let mut seen_cubes = std::collections::HashSet::with_capacity(self.tree.len()); + let mut needed_cubes = std::collections::HashSet::with_capacity(self.tree.len()); + let mut typed_nodes = Vec::new(); + for (cube_id, tree_data) in self.tree.iter() { + let is_unlocked = unlocked_cubes.contains(&cube_id); + let is_unlockable = tree_data.requires.iter().all(|id| unlocked_cubes.contains(id)); + tree_data.neighbours.iter().for_each(|id| { needed_cubes.insert(*id); }); + seen_cubes.insert(cube_id); + let node_data = tree_data.to_owned().into_data(*cube_id, is_unlocked, is_unlockable); + typed_nodes.push(node_data.as_transmissible_key_val()); + } + for needed_cube_id in needed_cubes { + if !seen_cubes.contains(&needed_cube_id) { + log::warn!("Tech tree needs cube {} but it doesn't have tree info", needed_cube_id); + } + } + Typed::Dict(Dict { + key_ty: TypePrefix::Str, + val_ty: TypePrefix::HashMap, + items: typed_nodes, + }) + } +} diff --git a/rc_core/src/persist/config/traits.rs b/rc_core/src/persist/config/traits.rs index 05a0a9c..19c82f0 100644 --- a/rc_core/src/persist/config/traits.rs +++ b/rc_core/src/persist/config/traits.rs @@ -7,7 +7,8 @@ pub trait ConfigProvider { fn weapon_list(&self) -> Typed; fn weapon_upgrade_list(&self) -> Typed; fn weapon_keys(&self) -> Typed; - fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet) -> Typed; + fn tech_tree_nodes(&self) -> super::TechTreeNodeProvider; + fn tech_tree_costs(&self) -> std::collections::HashMap; // cube id (hex) -> tech point cost fn ids(&self) -> Vec; fn regen_config(&self) -> Typed; fn after_battle_vote_config(&self) -> Typed; diff --git a/rc_services_room/src/operations/mod.rs b/rc_services_room/src/operations/mod.rs index 372ae59..32ba177 100644 --- a/rc_services_room/src/operations/mod.rs +++ b/rc_services_room/src/operations/mod.rs @@ -105,6 +105,7 @@ mod crf_rate_vehicle; mod crf_shift_vehicle; mod crf_make_featured; mod crf_unmake_featured; +mod tech_tree_unlock_cube; use polariton_server::operations::OperationsHandler; @@ -234,4 +235,5 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler .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??? + .add(tech_tree_unlock_cube::tech_tree_cube_unlock_provider(&init_ctx.cubes)) } diff --git a/rc_services_room/src/operations/tech_tree_data.rs b/rc_services_room/src/operations/tech_tree_data.rs index 4ede528..3d31cca 100644 --- a/rc_services_room/src/operations/tech_tree_data.rs +++ b/rc_services_room/src/operations/tech_tree_data.rs @@ -1,17 +1,19 @@ -use polariton_server::operations::Immediate; -use oj_rc_core::ConfigProvider; +use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl}; +use polariton::operation::ParameterTable; -const PARAM_KEY: u8 = 210; +const CODE: u8 = 183; -pub(super) fn tech_tree_layout_provider(cubes: &oj_rc_core::ConfigImpl) -> Immediate<183, crate::UserTy> { +const NODES_PARAM_KEY: u8 = 210; + +/*pub(super) fn tech_tree_layout_provider(cubes: &oj_rc_core::ConfigImpl) -> Immediate<183, crate::UserTy> { Immediate::new(|| { let mut params = std::collections::HashMap::with_capacity(2); - params.insert(PARAM_KEY, cubes.tech_tree_nodes(&vec![ + params.insert(NODES_PARAM_KEY, cubes.tech_tree_nodes(&vec![ 227205318, 227917916, 1931676396, ].into_iter().collect())); - /*params.insert(PARAM_KEY, Typed::Dict(Dict { + /*params.insert(NODES_PARAM_KEY, Typed::Dict(Dict { key_ty: TypePrefix::Str, // str val_ty: TypePrefix::HashMap, // hashmap items: vec![ @@ -28,4 +30,29 @@ pub(super) fn tech_tree_layout_provider(cubes: &oj_rc_core::ConfigImpl) -> Immed }));*/ params.into() }) +}*/ + +pub(super) struct TechTreeNoder { + nodes: oj_rc_core::persist::config::TechTreeNodeProvider, +} + +#[async_trait::async_trait] +impl SimpleOperation for TechTreeNoder { + type User = crate::UserTy; + const CODE: u8 = CODE; + + async fn handle(&self, params: ParameterTable, user: &Self::User) -> Result, SimpleOpError> { + let mut params = params.to_dict(); + let user_info = user.user()?; + let unlocked = user_info.unlocked_parts().await; + let nodes = self.nodes.tech_tree_nodes(&unlocked.into_iter().collect()); + params.insert(NODES_PARAM_KEY, nodes); + Ok(params.into()) + } +} + +pub(super) fn tech_tree_layout_provider(cubes: &oj_rc_core::ConfigImpl) -> SimpleOpImpl { + SimpleOpImpl::new(TechTreeNoder { + nodes: >::tech_tree_nodes(cubes), + }) } diff --git a/rc_services_room/src/operations/tech_tree_unlock_cube.rs b/rc_services_room/src/operations/tech_tree_unlock_cube.rs new file mode 100644 index 0000000..4eff2c9 --- /dev/null +++ b/rc_services_room/src/operations/tech_tree_unlock_cube.rs @@ -0,0 +1,41 @@ +use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl}; +use polariton::operation::{ParameterTable, Typed}; + +const CODE: u8 = 184; + +const CUBE_PARAM_KEY: u8 = 211; // str (hex); in +const NODES_PARAM_KEY: u8 = 210; + +pub(super) struct TechTreeUnlocker { + cost_map: std::collections::HashMap, + nodes: oj_rc_core::persist::config::TechTreeNodeProvider, +} + +#[async_trait::async_trait] +impl SimpleOperation for TechTreeUnlocker { + type User = crate::UserTy; + const CODE: u8 = CODE; + + async fn handle(&self, params: ParameterTable, user: &Self::User) -> Result, SimpleOpError> { + let mut params = params.to_dict(); + if let Some(Typed::Str(cube_hex)) = params.remove(&CUBE_PARAM_KEY) { + if let Some(cost) = self.cost_map.get(&cube_hex.string) { + let user_info = user.user()?; + let bytes = hex::decode(cube_hex.string).unwrap(); + user_info.currency_debit(oj_rc_core::persist::user::CurrencyType::TechPoints, *cost as u64).await?; + user_info.unlock_parts(&[u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])]).await?; + let unlocked = user_info.unlocked_parts().await; + let nodes = self.nodes.tech_tree_nodes(&unlocked.into_iter().collect()); + params.insert(NODES_PARAM_KEY, nodes); + } + } + Ok(params.into()) + } +} + +pub(super) fn tech_tree_cube_unlock_provider(cubes: &oj_rc_core::ConfigImpl) -> SimpleOpImpl { + SimpleOpImpl::new(TechTreeUnlocker { + cost_map: >::tech_tree_costs(cubes), + nodes: >::tech_tree_nodes(cubes), + }) +}