Remove old loadout_builder::LoadoutSpec

This commit is contained in:
juliancoffee 2022-02-20 17:46:17 +02:00
parent f791dace7f
commit 77f8d6da6f

View File

@ -13,7 +13,6 @@ use crate::{
}, },
trade::SiteInformation, trade::SiteInformation,
}; };
use hashbrown::HashMap;
use rand::{self, distributions::WeightedError, seq::SliceRandom, Rng}; use rand::{self, distributions::WeightedError, seq::SliceRandom, Rng};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::EnumIter; use strum::EnumIter;
@ -42,14 +41,6 @@ pub enum Preset {
HuskSummon, HuskSummon,
} }
#[derive(Debug, Deserialize, Clone)]
pub struct LoadoutSpec(HashMap<EquipSlot, ItemSpec>);
impl assets::Asset for LoadoutSpec {
type Loader = assets::RonLoader;
const EXTENSION: &'static str = "ron";
}
type Weight = u8; type Weight = u8;
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
@ -61,16 +52,15 @@ enum Base {
Choice(Vec<(Weight, Base)>), Choice(Vec<(Weight, Base)>),
} }
impl Base { impl Base {
fn to_spec(&self, rng: &mut impl Rng) -> Result<LoadoutSpecNew, LoadoutBuilderError> { fn to_spec(&self, rng: &mut impl Rng) -> Result<LoadoutSpec, LoadoutBuilderError> {
match self { match self {
Base::Asset(asset_specifier) => LoadoutSpecNew::load_cloned(asset_specifier) Base::Asset(asset_specifier) => LoadoutSpec::load_cloned(asset_specifier)
.map_err(LoadoutBuilderError::LoadoutAssetError), .map_err(LoadoutBuilderError::LoadoutAssetError),
Base::Combine(bases) => { Base::Combine(bases) => {
let bases = bases.iter().map(|b| b.to_spec(rng)?.eval(rng)); let bases = bases.iter().map(|b| b.to_spec(rng)?.eval(rng));
// Get first base of combined // Get first base of combined
let mut current = LoadoutSpecNew::default(); let mut current = LoadoutSpec::default();
for base in bases { for base in bases {
current = current.merge(base?); current = current.merge(base?);
} }
@ -162,7 +152,7 @@ impl ItemSpecNew {
// FIXME: remove clone // FIXME: remove clone
#[derive(Debug, Deserialize, Clone, Default)] #[derive(Debug, Deserialize, Clone, Default)]
pub struct LoadoutSpecNew { pub struct LoadoutSpec {
// Meta fields // Meta fields
inherit: Option<Base>, inherit: Option<Base>,
// Armor // Armor
@ -189,14 +179,14 @@ pub struct LoadoutSpecNew {
inactive_hands: Option<Hands>, inactive_hands: Option<Hands>,
} }
impl LoadoutSpecNew { impl LoadoutSpec {
/// It just merges `self` with `base`. /// It just merges `self` with `base`.
/// If some field exists in `self` it will be used, /// If some field exists in `self` it will be used,
/// if no, it will be taken from `base`. /// if no, it will be taken from `base`.
/// ///
/// NOTE: it won't recursively load bases. /// NOTE: it won't recursively load bases.
/// For example if A inherits from B and B inherits from C - /// For example if A inherits from B and B inherits from C -
/// as result we will get LoadoutSpec A that inherits from C, /// as result we will get `LoadoutSpec` A that inherits from C,
/// but C will left as is. /// but C will left as is.
fn merge(self, base: Self) -> Self { fn merge(self, base: Self) -> Self {
Self { Self {
@ -268,7 +258,7 @@ pub enum LoadoutBuilderError {
BaseChoiceError(WeightedError), BaseChoiceError(WeightedError),
} }
impl assets::Asset for LoadoutSpecNew { impl assets::Asset for LoadoutSpec {
type Loader = assets::RonLoader; type Loader = assets::RonLoader;
const EXTENSION: &'static str = "ron"; const EXTENSION: &'static str = "ron";
@ -760,9 +750,9 @@ impl LoadoutBuilder {
} }
#[must_use = "Method consumes builder and returns updated builder."] #[must_use = "Method consumes builder and returns updated builder."]
fn with_loadout_spec_new<R: Rng>( fn with_loadout_spec<R: Rng>(
mut self, mut self,
spec: LoadoutSpecNew, spec: LoadoutSpec,
rng: &mut R, rng: &mut R,
) -> Result<Self, LoadoutBuilderError> { ) -> Result<Self, LoadoutBuilderError> {
// Include any inheritance // Include any inheritance
@ -859,14 +849,14 @@ impl LoadoutBuilder {
} }
#[must_use = "Method consumes builder and returns updated builder."] #[must_use = "Method consumes builder and returns updated builder."]
pub fn with_asset_new( pub fn with_asset(
self, self,
asset_specifier: &str, asset_specifier: &str,
rng: &mut impl Rng, rng: &mut impl Rng,
) -> Result<Self, LoadoutBuilderError> { ) -> Result<Self, LoadoutBuilderError> {
let spec = LoadoutSpecNew::load_cloned(asset_specifier) let spec = LoadoutSpec::load_cloned(asset_specifier)
.map_err(LoadoutBuilderError::LoadoutAssetError)?; .map_err(LoadoutBuilderError::LoadoutAssetError)?;
self.with_loadout_spec_new(spec, rng) self.with_loadout_spec(spec, rng)
} }
/// # Usage /// # Usage
@ -876,79 +866,9 @@ impl LoadoutBuilder {
/// # Panics /// # Panics
/// 1) Will panic if there is no asset with such `asset_specifier` /// 1) Will panic if there is no asset with such `asset_specifier`
/// 2) Will panic if path to item specified in loadout file doesn't exist /// 2) Will panic if path to item specified in loadout file doesn't exist
/// 3) Will panic while runs in tests and asset doesn't have "correct" form
#[must_use = "Method consumes builder and returns updated builder."] #[must_use = "Method consumes builder and returns updated builder."]
pub fn with_asset_expect(mut self, asset_specifier: &str, rng: &mut impl Rng) -> Self { pub fn with_asset_expect(self, asset_specifier: &str, rng: &mut impl Rng) -> Self {
let spec = LoadoutSpec::load_expect_cloned(asset_specifier).0; self.with_asset(asset_specifier, rng).expect("failed loading loadout config")
for (key, entry) in spec {
let item = match entry.try_to_item(asset_specifier, rng) {
Some(item) => item,
None => continue,
};
match key {
EquipSlot::ActiveMainhand => {
self = self.active_mainhand(Some(item));
},
EquipSlot::ActiveOffhand => {
self = self.active_offhand(Some(item));
},
EquipSlot::InactiveMainhand => {
self = self.inactive_mainhand(Some(item));
},
EquipSlot::InactiveOffhand => {
self = self.inactive_offhand(Some(item));
},
EquipSlot::Armor(ArmorSlot::Head) => {
self = self.head(Some(item));
},
EquipSlot::Armor(ArmorSlot::Shoulders) => {
self = self.shoulder(Some(item));
},
EquipSlot::Armor(ArmorSlot::Chest) => {
self = self.chest(Some(item));
},
EquipSlot::Armor(ArmorSlot::Hands) => {
self = self.hands(Some(item));
},
EquipSlot::Armor(ArmorSlot::Legs) => {
self = self.pants(Some(item));
},
EquipSlot::Armor(ArmorSlot::Feet) => {
self = self.feet(Some(item));
},
EquipSlot::Armor(ArmorSlot::Belt) => {
self = self.belt(Some(item));
},
EquipSlot::Armor(ArmorSlot::Back) => {
self = self.back(Some(item));
},
EquipSlot::Armor(ArmorSlot::Neck) => {
self = self.neck(Some(item));
},
EquipSlot::Armor(ArmorSlot::Ring1) => {
self = self.ring1(Some(item));
},
EquipSlot::Armor(ArmorSlot::Ring2) => {
self = self.ring2(Some(item));
},
EquipSlot::Lantern => {
self = self.lantern(Some(item));
},
EquipSlot::Armor(ArmorSlot::Tabard) => {
self = self.tabard(Some(item));
},
EquipSlot::Glider => {
self = self.glider(Some(item));
},
EquipSlot::Armor(
slot @ (ArmorSlot::Bag1 | ArmorSlot::Bag2 | ArmorSlot::Bag3 | ArmorSlot::Bag4),
) => {
self = self.bag(slot, Some(item));
},
};
}
self
} }
/// Set default armor items for the loadout. This may vary with game /// Set default armor items for the loadout. This may vary with game
@ -1132,13 +1052,4 @@ mod tests {
} }
} }
} }
#[test]
fn test_new_config() {
let dummy_rng = &mut rand::thread_rng();
let new_spec =
LoadoutSpecNew::load_expect_cloned("common.loadout_new.test.choice_base");
let new_spec = new_spec.eval(dummy_rng).unwrap();
panic!("{new_spec:#?}");
}
} }