mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement tech tree cube unlock
This commit is contained in:
@@ -187,30 +187,28 @@ impl <C: Clone + Send> super::ConfigProvider<C> for CubeConfig {
|
|||||||
Typed::IntArr(keys_vec.into())
|
Typed::IntArr(keys_vec.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet<u32>) -> Typed<C> {
|
fn tech_tree_nodes(&self) -> super::TechTreeNodeProvider {
|
||||||
let mut seen_cubes = std::collections::HashSet::with_capacity(self.cubes.len());
|
let mut nodes = indexmap::IndexMap::with_capacity(self.cubes.len());
|
||||||
let mut needed_cubes = std::collections::HashSet::with_capacity(self.cubes.len());
|
|
||||||
let mut typed_nodes = Vec::new();
|
|
||||||
for cube in self.cubes.values() {
|
for cube in self.cubes.values() {
|
||||||
if let Some(tree_data) = &cube.tree {
|
if let Some(tree_data) = &cube.tree {
|
||||||
let is_unlocked = unlocked_cubes.contains(&cube.id);
|
nodes.insert(cube.id, tree_data.to_owned());
|
||||||
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 {
|
nodes.shrink_to_fit();
|
||||||
if !seen_cubes.contains(&needed_cube_id) {
|
super::TechTreeNodeProvider {
|
||||||
log::warn!("Tech tree needs cube {} but it doesn't have tree info", needed_cube_id);
|
tree: nodes,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tech_tree_costs(&self) -> std::collections::HashMap<String, u32> { // 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 {
|
costs.shrink_to_fit(); // probably unnecessary, but free memory usage reduction!
|
||||||
key_ty: TypePrefix::Str,
|
costs
|
||||||
val_ty: TypePrefix::HashMap,
|
|
||||||
items: typed_nodes,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ids(&self) -> Vec<u32> {
|
fn ids(&self) -> Vec<u32> {
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ pub use validation::{SelfValidator, ValidationInfo, ValidationMessage};
|
|||||||
mod campaign;
|
mod campaign;
|
||||||
pub use campaign::{CampaignResolver, CompleteCampaignProvider};
|
pub use campaign::{CampaignResolver, CompleteCampaignProvider};
|
||||||
|
|
||||||
|
mod tech_tree;
|
||||||
|
pub use tech_tree::TechTreeNodeProvider;
|
||||||
|
|
||||||
pub type ConfigImpl = CubeConfig;
|
pub type ConfigImpl = CubeConfig;
|
||||||
|
|
||||||
fn __must_impl<T: ConfigProvider<()>>() {}
|
fn __must_impl<T: ConfigProvider<()>>() {}
|
||||||
|
|||||||
33
rc_core/src/persist/config/tech_tree.rs
Normal file
33
rc_core/src/persist/config/tech_tree.rs
Normal file
@@ -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<u32, crate::persist::TechTreeData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TechTreeNodeProvider {
|
||||||
|
pub fn tech_tree_nodes<C>(&self, unlocked_cubes: &std::collections::HashSet<u32>) -> Typed<C> {
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,8 @@ pub trait ConfigProvider<C: Clone> {
|
|||||||
fn weapon_list(&self) -> Typed<C>;
|
fn weapon_list(&self) -> Typed<C>;
|
||||||
fn weapon_upgrade_list(&self) -> Typed<C>;
|
fn weapon_upgrade_list(&self) -> Typed<C>;
|
||||||
fn weapon_keys(&self) -> Typed<C>;
|
fn weapon_keys(&self) -> Typed<C>;
|
||||||
fn tech_tree_nodes(&self, unlocked_cubes: &std::collections::HashSet<u32>) -> Typed<C>;
|
fn tech_tree_nodes(&self) -> super::TechTreeNodeProvider;
|
||||||
|
fn tech_tree_costs(&self) -> std::collections::HashMap<String, u32>; // cube id (hex) -> tech point cost
|
||||||
fn ids(&self) -> Vec<u32>;
|
fn ids(&self) -> Vec<u32>;
|
||||||
fn regen_config(&self) -> Typed<C>;
|
fn regen_config(&self) -> Typed<C>;
|
||||||
fn after_battle_vote_config(&self) -> Typed<C>;
|
fn after_battle_vote_config(&self) -> Typed<C>;
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ mod crf_rate_vehicle;
|
|||||||
mod crf_shift_vehicle;
|
mod crf_shift_vehicle;
|
||||||
mod crf_make_featured;
|
mod crf_make_featured;
|
||||||
mod crf_unmake_featured;
|
mod crf_unmake_featured;
|
||||||
|
mod tech_tree_unlock_cube;
|
||||||
|
|
||||||
use polariton_server::operations::OperationsHandler;
|
use polariton_server::operations::OperationsHandler;
|
||||||
|
|
||||||
@@ -234,4 +235,5 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy>
|
|||||||
.add(crf_make_featured::factory_featured_provider(&init_ctx.factory))
|
.add(crf_make_featured::factory_featured_provider(&init_ctx.factory))
|
||||||
.add(crf_unmake_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(polariton_server::operations::Ack::<101, _>::default()) // RestoreCRFFeaturedRobot unused and also redundant???
|
||||||
|
.add(tech_tree_unlock_cube::tech_tree_cube_unlock_provider(&init_ctx.cubes))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
use polariton_server::operations::Immediate;
|
use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl};
|
||||||
use oj_rc_core::ConfigProvider;
|
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(|| {
|
Immediate::new(|| {
|
||||||
let mut params = std::collections::HashMap::with_capacity(2);
|
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,
|
227205318,
|
||||||
227917916,
|
227917916,
|
||||||
1931676396,
|
1931676396,
|
||||||
].into_iter().collect()));
|
].into_iter().collect()));
|
||||||
/*params.insert(PARAM_KEY, Typed::Dict(Dict {
|
/*params.insert(NODES_PARAM_KEY, Typed::Dict(Dict {
|
||||||
key_ty: TypePrefix::Str, // str
|
key_ty: TypePrefix::Str, // str
|
||||||
val_ty: TypePrefix::HashMap, // hashmap
|
val_ty: TypePrefix::HashMap, // hashmap
|
||||||
items: vec![
|
items: vec![
|
||||||
@@ -28,4 +30,29 @@ pub(super) fn tech_tree_layout_provider(cubes: &oj_rc_core::ConfigImpl) -> Immed
|
|||||||
}));*/
|
}));*/
|
||||||
params.into()
|
params.into()
|
||||||
})
|
})
|
||||||
|
}*/
|
||||||
|
|
||||||
|
pub(super) struct TechTreeNoder {
|
||||||
|
nodes: oj_rc_core::persist::config::TechTreeNodeProvider,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl <C: Send + 'static> SimpleOperation<C> for TechTreeNoder {
|
||||||
|
type User = crate::UserTy;
|
||||||
|
const CODE: u8 = CODE;
|
||||||
|
|
||||||
|
async fn handle(&self, params: ParameterTable<C>, user: &Self::User) -> Result<ParameterTable<C>, 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<C: Send + 'static>(cubes: &oj_rc_core::ConfigImpl) -> SimpleOpImpl<C, crate::UserTy, TechTreeNoder> {
|
||||||
|
SimpleOpImpl::new(TechTreeNoder {
|
||||||
|
nodes: <oj_rc_core::ConfigImpl as oj_rc_core::ConfigProvider<()>>::tech_tree_nodes(cubes),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
41
rc_services_room/src/operations/tech_tree_unlock_cube.rs
Normal file
41
rc_services_room/src/operations/tech_tree_unlock_cube.rs
Normal file
@@ -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<String, u32>,
|
||||||
|
nodes: oj_rc_core::persist::config::TechTreeNodeProvider,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl <C: Send + 'static> SimpleOperation<C> for TechTreeUnlocker {
|
||||||
|
type User = crate::UserTy;
|
||||||
|
const CODE: u8 = CODE;
|
||||||
|
|
||||||
|
async fn handle(&self, params: ParameterTable<C>, user: &Self::User) -> Result<ParameterTable<C>, 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<C: Send + 'static>(cubes: &oj_rc_core::ConfigImpl) -> SimpleOpImpl<C, crate::UserTy, TechTreeUnlocker> {
|
||||||
|
SimpleOpImpl::new(TechTreeUnlocker {
|
||||||
|
cost_map: <oj_rc_core::ConfigImpl as oj_rc_core::ConfigProvider<()>>::tech_tree_costs(cubes),
|
||||||
|
nodes: <oj_rc_core::ConfigImpl as oj_rc_core::ConfigProvider<()>>::tech_tree_nodes(cubes),
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user