From 9cbdc25df498a05a061ccc2d7a4b2a273fbb8bb6 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 25 Feb 2025 04:00:13 +0100 Subject: [PATCH] Rewrites the example parser --- cli_parser/src/lib.rs | 2 + weaponry/examples/usage_scenario.rs | 68 ++++++-- weaponry/src/error.rs | 3 + .../equipement_advanced_repr.rs | 147 +++++++++--------- 4 files changed, 133 insertions(+), 87 deletions(-) diff --git a/cli_parser/src/lib.rs b/cli_parser/src/lib.rs index f23ffdd..1a04de7 100644 --- a/cli_parser/src/lib.rs +++ b/cli_parser/src/lib.rs @@ -42,6 +42,8 @@ pub enum Commands { Make(MakeConfig), /// Make all graphs Graphs(GraphConfig), + // /// Generate analysis data + // Analysis(AnalysisConfig) } #[derive(Debug, Args)] diff --git a/weaponry/examples/usage_scenario.rs b/weaponry/examples/usage_scenario.rs index a3fc0f9..bd52679 100644 --- a/weaponry/examples/usage_scenario.rs +++ b/weaponry/examples/usage_scenario.rs @@ -4,20 +4,22 @@ //! //! use clap::Parser; +use std::cmp::Ordering; use std::path::Path; +use weaponry::error::FatalEquipementError; use weaponry::interfaces::ToAnalysis; use weaponry::sort_by; use weaponry::sort_by_cat_then; use weaponry::weaponry_structs::RefinedEquipement; -// TODO : All of these should likely be in the prelude if used like this +// TODO[!] : All of these should likely be in the prelude if used like this use weaponry::weaponry_structs::CrossRecipeScenario; use weaponry::weaponry_structs::equipement_scenario::ConfigEquipementScenario; use weaponry::{ interfaces::{Reportable, Scenario, ToCSV}, weaponry_structs::RefinedEquipementList, }; -// use weaponry::interfaces:: + fn main() { let args = CLIArgs::parse(); let save_path = args.path.unwrap_or("data/first_data".into()); @@ -42,15 +44,15 @@ fn main() { }) }); - // TODO[!]: Have an enum - match args.sort.as_str() { - "avg_huge" => refined_list.inner_sort(sort_by!(avg_damage_single_huge_hit)), - "avg_safe" => refined_list.inner_sort(sort_by!(avg_damage_2a_safe)), - "max_armor" => refined_list.inner_sort(sort_by!(armor_break_per_attack)), - "cat+avg" => refined_list.inner_sort(sort_by_cat_then!(avg_damage_2a_safe)), - _ => (), + match cmd_to_filter(&args.sort) { + Ok(cmd) => { + refined_list.inner_sort(cmd); + } + Err(FatalEquipementError::CLIParseError(a)) => { + println!("Argument incorrect '{a}'. Ignoring the sort.") + } + Err(e) => panic!("{e}"), } - // if args.html_export{} refined_list.write_html(&save_path).unwrap(); @@ -76,6 +78,17 @@ pub struct CLIArgs { pub finishing: bool, #[arg(long, default_value_t = String::from("safe"))] pub sort: String, + // An html export path -h or -o + // A csv export path -c or --csv + // A presort --sort + // A prefilter with some filters ? + // - weapons + // - shields + // - armors + // - damage + // Data manipulation ?? + // - decimate + // - average per category } #[allow(dead_code)] @@ -87,3 +100,38 @@ fn export_as_csv(refined_list: &RefinedEquipementList, save_path: &str) { refined_list.write_csv(Path::new(&format!("{save_path}.csv"))) ); } + +type DynamicFilterType = Box Ordering>; + +fn cmd_to_filter(og_entry: &str) -> Result { + let (category, entry) = if og_entry.starts_with("cat+") { + (true, og_entry.split_at(4).1) + } else { + (false, og_entry) + }; + + macro_rules! return_if_starts_with { + ($iden:ident) => { + if stringify!($iden).starts_with(entry) { + return match category { + true => Ok(Box::new(sort_by_cat_then!($iden))), + false => Ok(Box::new(sort_by!($iden))), + }; + }; + }; + } + return_if_starts_with!(max_damage_per_attack); + return_if_starts_with!(armor_break_per_attack); + return_if_starts_with!(expected_armor_break_per_attack); + return_if_starts_with!(avg_damage_2a_safe); + return_if_starts_with!(avg_damage_single_huge_hit); + return_if_starts_with!(speed); + return_if_starts_with!(scenario_damage); + return_if_starts_with!(defensive); + return_if_starts_with!(earlygame_sustained_damage_per_turn); + return_if_starts_with!(midgame_sustained_damage_per_turn); + return_if_starts_with!(endgame_sustained_damage_per_turn); + return_if_starts_with!(variance); + return_if_starts_with!(variance_pct); + Err(FatalEquipementError::CLIParseError(og_entry.to_string())) +} diff --git a/weaponry/src/error.rs b/weaponry/src/error.rs index fba7e58..d2396e7 100644 --- a/weaponry/src/error.rs +++ b/weaponry/src/error.rs @@ -39,6 +39,9 @@ pub enum FatalEquipementError { #[error("Couldn't save the chart as an error occured during the process : {0}")] SaveCharmingChart(#[from] EchartsError), + + #[error("Couldn't parse the CLI argument given \"{0}\"")] + CLIParseError(String), } // #[derive(Error, Debug)] diff --git a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs index fd1de68..982b22c 100644 --- a/weaponry/src/weaponry_structs/equipement_advanced_repr.rs +++ b/weaponry/src/weaponry_structs/equipement_advanced_repr.rs @@ -8,28 +8,27 @@ //! nothing prevents us to implement the trait for other objects and avoid massive computation //! of all the fields even when the user will not be interested in them #![allow(dead_code)] -use crate::weaponry_structs::equipement_thin_types::Dices; -use crate::weaponry_structs::equipement_thin_types::Precision; +use super::{ + equipement_advanced_tools::{ + BestDamageScenarios, get_damage_with_simulation, get_sustained_damage, + }, + equipement_thin_types::{Actions, Attacks}, +}; use crate::{ add_field_to_chart, error::FatalEquipementError, export::initial_chart, interfaces::{RecipeCollection, RefinedList, ToAnalysis, ToTex}, weaponry_structs::{ - Equipement, InitialEquipementList, equipement_structs::InnerEquipementStats, + Equipement, InitialEquipementList, + equipement_structs::InnerEquipementStats, + equipement_thin_types::{Dices, Precision}, }, }; -use core::f64; -use std::cmp::Ordering; - use charming::Chart; +use core::f64; use serde::Serialize; - -use super::equipement_advanced_tools::{ - BestDamageScenarios, get_damage_with_simulation, get_sustained_damage, -}; -use super::equipement_thin_types::Actions; -use super::equipement_thin_types::Attacks; +use std::cmp::Ordering; /// The final representation of the equipement inplementing all the fields needed for processing // TODO: Add a public getter for the inner data @@ -45,24 +44,6 @@ pub struct RefinedEquipement { pub advanced: AdvancedStats, } -impl Serialize for RefinedEquipement { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - self.advanced.serialize(serializer) - } -} - -impl From for RefinedEquipement { - fn from(value: Equipement) -> Self { - Self { - advanced: AdvancedStats::from(&value.stats), - inner: value.stats, - } - } -} - /// The complete equipement list, refined with all advanced stats pub struct RefinedEquipementList { /// Inner list of Refined Equipements @@ -78,28 +59,6 @@ impl ToTex for RefinedEquipementList { } } -#[macro_export] -/// A simple macro to sort the inner advanced field by a specific field -// TODO[6] find a way to not have to import the refinedequipement -macro_rules! sort_by { - ($field:ident) => { - |a: &RefinedEquipement, b: &RefinedEquipement| { - a.advanced.$field.total_cmp(&b.advanced.$field) - } - }; -} -#[macro_export] -/// A simple macro to sort the inner advanced field by a specific field -// TODO[6] find a way to not have to import the refinedequipement -macro_rules! sort_by_cat_then { - ($field:ident) => { - |a, b| match a.inner.id[0].cmp(&b.inner.id[0]) { - std::cmp::Ordering::Equal => a.advanced.$field.total_cmp(&b.advanced.$field), - ord => ord, - } - }; -} - impl ToAnalysis for RefinedEquipementList { type InnerData = RefinedEquipement; @@ -179,29 +138,7 @@ impl ToAnalysis for RefinedEquipementList { } } -impl From for RefinedEquipementList { - fn from(value: InitialEquipementList) -> Self { - Self { - inner: value - .inner - .into_iter() - .map(RefinedEquipement::from) - .collect(), - } - } -} - -// #[cfg(test)] // Used in exemples -impl Default for RefinedEquipementList { - fn default() -> Self { - let eq = InitialEquipementList::from_vectored(vec![Equipement::default(); 10]); - RefinedEquipementList::from(eq) - } -} - /// The list of advanced stats infered from an Equipement -// TODO: The two philosophies to drop the old inner or convert the useful fields are considered right now -// TODO: This whole public might be overkill ? A good inner view or scenario could implement what is needed #[derive(Serialize)] pub struct AdvancedStats { /// A simple name created by merging all components @@ -302,6 +239,62 @@ impl From<&InnerEquipementStats> for AdvancedStats { } } } -// mini_scenario_damage: (value.damage + value.p_damage > 0) -// .then(|| get_sustained_damage(value)) -// .unwrap_or_default(), + +impl Serialize for RefinedEquipement { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.advanced.serialize(serializer) + } +} + +impl From for RefinedEquipement { + fn from(value: Equipement) -> Self { + Self { + advanced: AdvancedStats::from(&value.stats), + inner: value.stats, + } + } +} + +impl From for RefinedEquipementList { + fn from(value: InitialEquipementList) -> Self { + Self { + inner: value + .inner + .into_iter() + .map(RefinedEquipement::from) + .collect(), + } + } +} + +// #[cfg(test)] // Used in exemples +impl Default for RefinedEquipementList { + fn default() -> Self { + let eq = InitialEquipementList::from_vectored(vec![Equipement::default(); 10]); + RefinedEquipementList::from(eq) + } +} +#[macro_export] +/// A simple macro to sort the inner advanced field by a specific field +// TODO[6] find a way to not have to import the refinedequipement +macro_rules! sort_by { + ($field:ident) => { + |a: &RefinedEquipement, b: &RefinedEquipement| { + a.advanced.$field.total_cmp(&b.advanced.$field) + } + }; +} +#[macro_export] +/// A simple macro to sort the inner advanced field by a specific field +// TODO[6] find a way to not have to import the refinedequipement +macro_rules! sort_by_cat_then { + ($field:ident) => { + |a, b| match a.inner.id[0].cmp(&b.inner.id[0]) { + std::cmp::Ordering::Equal => a.advanced.$field.total_cmp(&b.advanced.$field), + ord => ord, + } + }; +}