Tries to simplify the impl process

This commit is contained in:
Dandaedre 2025-02-28 03:49:35 +01:00
parent 352940486e
commit 01842fc64a
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
8 changed files with 151 additions and 15 deletions

Binary file not shown.

View File

@ -234,6 +234,12 @@ pub trait Scenario {
/// Inner configuration
type Config;
// /// The initial element List
// type InitialElementList: RecipeCollection;
// /// The Inner Database struct to deserialize initially
// type InitialDatabaseStruct;
/// Create a Scenario from a given config
fn new_with_config(config: Self::Config) -> Result<Self, FatalEquipementError>
where
@ -241,4 +247,36 @@ pub trait Scenario {
/// Generate the final list type
fn generate_refined_recipes(&self) -> Result<Self::FinalListType, FatalEquipementError>;
// /// Blanket implementation deserialising all element from the database into the initial inner list used by the scenario
// fn inital_list_from_database<T>(
// &self,
// extra_sql_filter: Option<String>,
// ) -> Result<Self::FinalListType, FatalEquipementError>
// where
// T: TryFrom<InnerEquipementStats> + From<InnerEquipementStats> + Into<Equipement>,
// {
// // TODO: This likely has too high a complexity and should either be a blanket implementation
// // : or require the inner workings to implement methods to easily allow this deserialisation
// Ok(Self::InitialElementList::from_vectored(
// self.get_database()
// .serialize_vec_from_table::<Self::InitialDatabaseStruct>(
// &self.get_tablename(),
// Some(&format!("META_CATEGORY LIKE '{}'", name)),
// )?
// .into_iter()
// .map(|v| {
// InnerEquipementStats::try_from(v)
// .map(|w| T::from(w))
// .map(|v| v.into())
// })
// .collect::<Result<Vec<_>, FatalEquipementError>>()?,
// ))
// }
/// Retrieve a reference to the inner database
fn get_database(&self) -> &Self::DatabaseType;
/// Retrieve A config
fn get_tablename(&self) -> &str;
}

View File

@ -55,6 +55,8 @@ pub struct RefinedEquipementList {
impl RefinedList for RefinedEquipementList {}
impl ToTex for RefinedEquipementList {
fn to_tex(&self) -> String {
// TODO[!] : Export everything to one tsv
// : This export is only supposed to be the final export of a pseudo recipes book
todo!()
}
}

View File

@ -21,7 +21,6 @@ use super::equipement_thin_types::Precision;
/// - 2 turns safe attacks (5 dice, 50%p)
/// - 1 turn highspeed/range hit (7 dice, 80% precision)
/// - 4 turns normal damage (7 dice, 90% precision)
// TODO[!] Implementation
pub(super) fn get_damage_with_simulation(
value: &InnerEquipementStats,
actions: Actions,

View File

@ -24,6 +24,7 @@ pub struct EquipementDatabase {
}
impl RecipeDatabase for EquipementDatabase {
// TODO[:] : This element is partly responsible for the difficulty simplifying and generalizing deserialisation
fn serialize_vec_from_table<T: DeserializeOwned + more_fields::FieldsAsSlice>(
&self,
table: &str,

View File

@ -4,7 +4,7 @@
//!
use super::equipement_structs::InnerEquipementStats;
use crate::weaponry_structs::SpecialField;
use crate::weaponry_structs::{SpecialField, WeaponBaseShape};
/// Add a new vector to a clone of a inner field
macro_rules! add_to_vec {
@ -55,7 +55,15 @@ impl std::ops::Add for &InnerEquipementStats {
precision: self.precision + rhs.precision,
rmax: self.rmax + rhs.rmax,
armor_break: self.armor_break + rhs.armor_break,
armor_pierce: self.armor_pierce + rhs.armor_pierce,
armor_pierce: {
if self.armor_pierce > 0
|| matches!(self.category_base, Some(WeaponBaseShape::Ranged))
{
self.armor_pierce + rhs.armor_pierce
} else {
0
}
},
antimaterial: self.antimaterial + rhs.antimaterial,
damage_type: add_two_options!(self.damage_type, rhs.damage_type),
protection: self.protection + rhs.protection,

View File

@ -40,30 +40,51 @@ pub struct ConfigEquipementScenario {
/// A simple cross recipe scenario
#[allow(clippy::missing_docs_in_private_items)]
pub struct CrossRecipeScenario {
// TODO: Should obv be inside the config
database: EquipementDatabase,
config: ConfigEquipementScenario,
}
/// All recipes
#[allow(clippy::missing_docs_in_private_items, dead_code)]
pub struct RecipesScenario {
database: EquipementDatabase,
config: ConfigEquipementScenario,
}
/// A scenario exporting all individual elements
#[allow(clippy::missing_docs_in_private_items, dead_code)]
pub struct IndividualElementsScenario {
database: EquipementDatabase,
config: ConfigEquipementScenario,
}
/// The initial equipement type, to be converted into post deserialisation
type InitialEquipementType = InnerEquipementStats;
/// The initial database typ
type InitialDatabaseType = DatabaseEquipementStats;
impl CrossRecipeScenario {
/// Automatically deserialize into an equipement
// TODO: Add to trait
// TODO: Add an optionnal initial filter ?
fn deserde_into_equipement<T>(
&self,
name: &str,
sql_filter: Option<&str>,
) -> Result<InitialEquipementList, FatalEquipementError>
where
T: TryFrom<InnerEquipementStats> + From<InnerEquipementStats> + Into<Equipement>,
T: From<InitialEquipementType> + Into<Equipement>, // TryFrom<InitialEquipementType> +
{
// TODO: This likely has too high a complexity and should either be a blanket implementation
// : or require the inner workings to implement methods to easily allow this deserialisation
// : Most of this complexity comes from the fact that no clear definition of flow is given to
// : the whole implementation
// : Beside many struct "need" to know way too much of the inner workings of the other to funtion
// : A very long and tedious overhall is now needed
// : At the very least, The InitialEquipementList sould be able to initialize itself with only
// : the intermediate representation <EqShape, etc> and nothing more
Ok(InitialEquipementList::from_vectored(
self.database
.serialize_vec_from_table::<DatabaseEquipementStats>(
&self.config.table_name,
Some(&format!("META_CATEGORY LIKE '{}'", name)),
)?
self.get_database()
.serialize_vec_from_table::<InitialDatabaseType>(self.get_tablename(), sql_filter)?
.into_iter()
.map(|v| {
InnerEquipementStats::try_from(v)
@ -74,6 +95,33 @@ impl CrossRecipeScenario {
))
}
// /// Automatically deserialize into an equipement
// fn better_deserde_into_equipement<IntermediateType>(
// &self,
// sql_filter: Option<&str>,
// ) -> Result<InitialEquipementList, FatalEquipementError>
// where
// IntermediateType: From<InitialEquipementType> + Into<Equipement>, // TryFrom<InitialEquipementType> +
// {
// let mut items = self
// .get_database()
// .serialize_vec_from_table::<InitialDatabaseType>(self.get_tablename(), sql_filter)?
// .into_iter();
// InitialEquipementList::from_iter_with_intermediate::<InitialDatabaseType, IntermediateType>(
// &mut items,
// );
// // Ok(InitialEquipementList::from_vectored(
// // items
// // .map(|v| {
// // InnerEquipementStats::try_from(v)
// // .map(|w| T::from(w))
// // .map(|v| v.into())
// // })
// // .collect::<Result<Vec<_>, FatalEquipementError>>()?,
// // ))
// todo!()
// }
/// Generate all the operations to be processed with the current config
// TODO: Defer this to a custom type in the config some sort of OperationSelector
fn generate_selected_operation(
@ -81,18 +129,27 @@ impl CrossRecipeScenario {
) -> Result<Vec<InitialEquipementList>, FatalEquipementError> {
let mut operations = Vec::with_capacity(8);
if self.config.shape.is_some() {
operations.push(self.deserde_into_equipement::<EqShape>("Shape")?);
operations
.push(self.deserde_into_equipement::<EqShape>(Some("META_CATEGORY LIKE 'Shape'"))?);
}
if let Some(qt) = self.config.materials {
for _ in 0..qt {
operations.push(self.deserde_into_equipement::<EqMaterial>("Materials")?);
operations.push(self.deserde_into_equipement::<EqMaterial>(Some(
"META_CATEGORY LIKE 'Materials'",
))?);
}
}
if self.config.nature.is_some() {
operations.push(self.deserde_into_equipement::<EqNature>("Nature")?);
operations.push(
self.deserde_into_equipement::<EqNature>(Some("META_CATEGORY LIKE 'Nature'"))?,
);
}
if self.config.finishing.is_some() {
operations.push(self.deserde_into_equipement::<EqFinishing>("Finishing")?);
operations.push(
self.deserde_into_equipement::<EqFinishing>(Some(
"META_CATEGORY LIKE 'Finishing'",
))?,
);
}
if operations.is_empty() {
return Err(FatalEquipementError::CLISelection(
@ -104,9 +161,11 @@ impl CrossRecipeScenario {
}
impl Scenario for CrossRecipeScenario {
// TODO[3]: Could be skipped
type DatabaseType = EquipementDatabase;
type FinalListType = RefinedEquipementList;
type Config = ConfigEquipementScenario;
// type InitialInnerList = InitialEquipementList;
fn new_with_config(config: Self::Config) -> Result<Self, FatalEquipementError>
where
@ -128,4 +187,16 @@ impl Scenario for CrossRecipeScenario {
Ok(refined_list.into())
}
// type InitialElementList;
// type InitialDatabaseStruct;
fn get_tablename(&self) -> &str {
&self.config.table_name
}
fn get_database(&self) -> &Self::DatabaseType {
&self.database
}
}

View File

@ -25,6 +25,23 @@ pub struct InitialEquipementList {
pub(super) inner: Vec<Equipement>,
}
// impl InitialEquipementList {
// pub fn from_iter_with_intermediate<IntoEquipementType, IntermediateType>(
// iterator: &mut dyn Iterator<Item = IntoEquipementType>,
// ) -> Self
// where
// IntermediateType: Into<Equipement>,
// IntoEquipementType: Into<IntermediateType>,
// {
// Self {
// inner: iterator
// .into_iter()
// .map(|element| IntermediateType::from(element.into()))
// .collect(),
// }
// }
// }
/// The final equipement, obtained by combining all parts
#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq))]