ravenous-tools/crates/weaponry/examples/usage_scenario.rs

138 lines
4.6 KiB
Rust

// Workspace: weaponry
// File: examples/usage_scenario.rs
//! Test use case of a scenario
//!
//!
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
use weaponry::weaponry_structs::CrossRecipeScenario;
use weaponry::weaponry_structs::equipement_scenario::ConfigEquipementScenario;
use weaponry::{
interfaces::{Reportable, Scenario, ToCSV},
weaponry_structs::RefinedEquipementList,
};
fn main() {
let args = CLIArgs::parse();
let save_path = args.path.unwrap_or("data/first_data".into());
let scenarios = CrossRecipeScenario::new_with_config(ConfigEquipementScenario {
table_name: "Equipements".into(),
db_path: "./databases/weapons.db".into(),
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(|e| {
e.inner.category_base.as_ref().is_some_and(|v| {
!matches!(v, weaponry::weaponry_structs::WeaponBaseShape::Shields)
&& !matches!(v, weaponry::weaponry_structs::WeaponBaseShape::Armors)
})
});
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();
// if args.csv_export{}
// export_as_csv(&refined_list, save_path);
}
#[derive(Parser, Debug)]
/// Basic cli
pub struct CLIArgs {
/// The path to save to.
pub path: Option<String>,
/// Wether to generate shapes
#[arg(short('S'), long, default_value_t)]
pub no_shapes: bool,
/// Wether to generate materials
#[arg(short, long)]
pub materials: Option<usize>,
/// Wether to generate all natures
#[arg(short('N'), long)]
pub no_natures: bool,
/// Wether to generate all finishing
#[arg(short, long, default_value_t = false)]
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)]
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")))
);
}
type DynamicFilterType = Box<dyn FnMut(&RefinedEquipement, &RefinedEquipement) -> Ordering>;
pub fn cmd_to_filter(og_entry: &str) -> Result<DynamicFilterType, 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(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_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()))
}