ravenous-tools/crates/weaponry/src/analysis.rs

157 lines
5.6 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/analysis.rs
//! The one stop shop for analysis
use std::{cmp::Ordering, path::Path};
use cli_parser::Config;
use config::analysis::AnalysisConfig;
use crate::{
error::FatalEquipementError,
interfaces::{Reportable, Scenario, ToAnalysis, ToCSV},
weaponry_structs::{
CrossRecipeScenario, RefinedEquipement, RefinedEquipementList,
equipement_scenario::ConfigEquipementScenario,
},
};
/// Launch the analysis of the selected data and export it to html for human processing
pub fn launch_analysis(args: &AnalysisConfig, config: &Config) -> Result<(), FatalEquipementError> {
match args.target {
config::analysis::AnalysisTarget::Weapon => create_graphs(
args,
config,
Box::new(|e: &RefinedEquipement| {
e.inner.category_base.as_ref().is_some_and(|v| {
!matches!(v, crate::weaponry_structs::WeaponBaseShape::Shields)
&& !matches!(v, crate::weaponry_structs::WeaponBaseShape::Armors)
})
}),
),
config::analysis::AnalysisTarget::Armors => create_graphs(
args,
config,
Box::new(|e: &RefinedEquipement| {
e.inner
.category_base
.as_ref()
.is_some_and(|v| matches!(v, crate::weaponry_structs::WeaponBaseShape::Armors))
}),
),
config::analysis::AnalysisTarget::Shields => create_graphs(
args,
config,
Box::new(|e: &RefinedEquipement| {
e.inner
.category_base
.as_ref()
.is_some_and(|v| matches!(v, crate::weaponry_structs::WeaponBaseShape::Shields))
}),
),
}
Ok(())
}
/// Create all graphs with
fn create_graphs(args: &AnalysisConfig, config: &Config, filter: DynamicFilterType) {
let save_path = args.path.clone().unwrap_or("data/first_data".into());
let scenarios = CrossRecipeScenario::new_with_config(ConfigEquipementScenario {
table_name: "Equipements".into(),
db_path: config.data_generation.database.clone(),
shape: (!args.no_shapes).then_some(()),
materials: args.materials,
nature: (!args.no_natures).then_some(()),
finishing: args.finishing.then_some(()),
})
.unwrap();
let mut refined_list = scenarios
.generate_refined_recipes()
.unwrap()
// .inner_filter(|e| (e.advanced.max_damage_per_attack > 0.))
.inner_filter(filter);
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}"),
}
refined_list.write_html(&save_path).unwrap();
}
#[allow(dead_code)]
/// Export as a csv a refine equipement list
///
/// Rarely used and likely on it's way to deprecation
fn export_as_csv(refined_list: &RefinedEquipementList, save_path: &str) {
println!("{}", refined_list.get_report(Some("Refined List")));
println!(
"Saved initial data as {}.html: {:?}",
save_path,
refined_list.write_csv(Path::new(&format!("{save_path}.csv")))
);
}
// /// Export as a tex file in the given directory
// ///
// /// Generic function dispatching any specific demand to different functions
// /// TODO: FINISH IMPLEMENTATION
// #[allow(dead_code)]
// fn export_as_tex(refined_list: &RefinedEquipementList, config: &Config, save_path: &str) {
// let _ = save_path;
// println!("{}", refined_list.to_tex(config)); // TexType
// // println!(
// // "Saved initial data as {}.html: {:?}",
// // save_path,
// // refined_list.write_csv(Path::new(&format!("{save_path}.csv")))
// // );
// }
/// A dynamic filtertype, as a boxed function over two equipement references
// #[allow(dead_code)]
type DynamicSortType = Box<dyn FnMut(&RefinedEquipement, &RefinedEquipement) -> Ordering>;
/// A dynamic filter type
type DynamicFilterType = Box<dyn FnMut(&RefinedEquipement) -> bool>;
/// Create a filter from a user typed filter
///
/// This informal implementation function as a mini language,
/// without any form of formalism, consistency or fixed API
pub fn cmd_to_filter(og_entry: &str) -> Result<DynamicSortType, FatalEquipementError> {
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(crate::sort_by_cat_then!($iden))),
false => Ok(Box::new(crate::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_2a_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()))
}