First working commit on the macro

This commit is contained in:
Dandaedre 2025-02-23 06:14:51 +01:00
parent f3bb2c9d80
commit 8825587e53
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
5 changed files with 216 additions and 107 deletions

View File

@ -29,6 +29,7 @@ fn main() {
.unwrap();
let refined_list = scenarios.generate_refined_recipes().unwrap();
let b = RefinedEquipementList::default();
// if args.html_export{}
refined_list.write_html(&save_path).unwrap();

109
weaponry/src/export.rs Normal file
View File

@ -0,0 +1,109 @@
// Workspace: weaponry
// File: weaponry/src/export.rs
//! Module regrouping all generic function related to the exporting
//!
//! Mostly graphs and csv auto conversion
/// A very complex macro created to add criterias to the graph
/// without repeating self explanatory parameter names in strings
///
/// The syntax involved in the usage of this macro hence become esoteric
///
/// # Example
/// ```
/// # use weaponry::interfaces::RecipeCollection;
/// # use weaponry::add_field_to_chart;
/// # use weaponry::weaponry_structs::RefinedEquipementList;
/// # use weaponry::export::initial_chart;
/// # let self_ = RefinedEquipementList::default();
/// # let initial_chart: charming::Chart = initial_chart("Damage for all cross-gen products");
/// add_field_to_chart!(
/// self_, // Usually used inside the implementation and therefore self there
/// initial_chart,
/// (
/// index # &String,
/// dps_safe # f64,
/// max_damage_per_action # f64,
/// armor_break_per_turn # f64,
/// ),
/// |ref_equipement| {
/// (
/// &ref_equipement.advanced.display_name,
/// ref_equipement.advanced.max_damage_per_action,
/// ref_equipement.advanced.damage_per_turn,
/// ref_equipement.advanced.armor_break_per_turn,
/// )
/// }
/// );
/// ```
///
/// # Complexity and maintanability
///
/// While the added complexity cannot be understated, the whole existence of this macro is
/// first and foremost based on the desire NOT to iterate multiple times on a dataset
/// which could easily reach 600k elements
///
/// Knowing this tradeoff, the difficulty to maintain is considered an acceptable tradeoff
#[macro_export]
macro_rules! add_field_to_chart {
($base_struct:expr, $chart:ident, ($names:ident # $name_type:ty, $($table_names:ident # $table_type:ty),* $(,)*), | $closure_name:ident | {($($fields:expr,)*)}
) => {
let data_collection = $base_struct.inner.iter().map(|$closure_name| ( $($fields,)*) );
let ($names, $($table_names),*) = data_collection.collect::<(Vec<$name_type>, $(Vec<$table_type>,)*)>();
let $chart = $chart
.x_axis(
charming::component::Axis::new()
.data($names.to_vec())
.type_(charming::element::AxisType::Category),
);
$(
let $chart = $chart
.series(
charming::series::Line::new()
.data($table_names)
.name(stringify!($table_names)), // .line_style(LineStyle::new().color("paleblue")),
);
)*
}
}
/// Generate a generic graph, ready to be filled with data
pub fn initial_chart(chart_name: &str) -> charming::Chart {
charming::Chart::new()
.title(
charming::component::Title::new()
.text(chart_name)
.left("center"),
)
.legend(
charming::component::Legend::new()
.left(10)
.top("middle")
.orient(charming::element::Orient::Vertical)
.data(vec![
"dps_safe",
"max_damage_per_action",
"max_damage_per_turn",
"armor_break_per_turn",
]),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.data_zoom(
charming::component::DataZoom::new()
.type_(charming::component::DataZoomType::Inside)
.start(0)
.end(100),
)
.data_zoom(charming::component::DataZoom::new().start(0).end(100))
.toolbox(
charming::component::Toolbox::new().feature(
charming::component::Feature::new()
.data_zoom(charming::component::ToolboxDataZoom::new().y_axis_index("none"))
.restore(charming::component::Restore::new())
.save_as_image(charming::component::SaveAsImage::new()), // .brush(Brush::new().type_(vec![BrushType::LineX, BrushType::Clear])),
),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.y_axis(charming::component::Axis::new())
.grid(charming::component::Grid::new().contain_label(true))
}

View File

@ -7,6 +7,7 @@
mod database;
pub mod error;
pub mod export;
pub mod interfaces;
mod strategy;
pub mod weaponry_structs;

View File

@ -9,6 +9,7 @@
//! of all the fields even when the user will not be interested in them
#![allow(dead_code)]
use crate::{add_field_to_chart, export::initial_chart, interfaces::RecipeCollection};
use core::f64;
use std::fmt::Display;
@ -16,7 +17,7 @@ use crate::{
error::FatalEquipementError,
interfaces::{RefinedList, ToAnalysis, ToTex},
};
use charming::element::Orient;
use charming::Chart;
use comfy_table::{Table, modifiers::UTF8_ROUND_CORNERS};
use probability::prelude::{Binomial, Discrete};
use serde::Serialize;
@ -73,119 +74,116 @@ impl ToTex for RefinedEquipementList {
todo!()
}
}
impl ToAnalysis for RefinedEquipementList {
// TODO[3]: Consider wether the naming should be a parameter,
// or a config should be given or if the scenario should be fed again the Refined and ...
fn write_html(&self, save_path: &str) -> Result<(), FatalEquipementError> {
let mut renderer = charming::HtmlRenderer::new("Ravenous Balancing", 4000, 2000)
.theme(charming::theme::Theme::Dark);
// Save the chart as HTML file.
renderer.save(&make_chart(self), format!("{save_path}.html"))?;
return Ok(());
// TODO: Migrate this into the view / renderer
// TODO: End the view idea and only focus on this renderer
pub fn make_chart(data: &RefinedEquipementList) -> charming::Chart {
// TODO: Add back sorting
// data.inner
// .sort_by(|a, b| a.advanced.dps_safe.total_cmp(&b.advanced.dps_safe));
let initial_chart: Chart = initial_chart("Damage for all cross-gen products");
// TODO: Convert to macro
let raw_data = data.inner.iter().map(|refeq| {
// TODO[!]: Add sorting
// data.inner
// .sort_by(|a, b| a.advanced.dps_safe.total_cmp(&b.advanced.dps_safe));
// let index: Vec<_>;
// let data_list: Vec<_>;
// (index, data_list) = self
// .inner
// .iter()
// .map(|ref_equipement| {
// (
// &ref_equipement.advanced.display_name,
// &ref_equipement.advanced.damage_per_turn,
// )
// })
// .collect();
// // TODO[5]: This macro work is interesting while flawed and the optimisations are not worth more time invested now
// // TODO: Add this as a function and a macro
// // TODO: Convert to macro
// // TODO: Call this iteratively with all elements in macro
// let raw_data = self.inner.iter().map(|refeq| {
// (
// &refeq.advanced.display_name,
// refeq.advanced.dps_safe,
// refeq.advanced.max_damage_per_action,
// refeq.advanced.damage_per_turn,
// refeq.advanced.armor_break_per_turn,
// )
// });
add_field_to_chart!(
self,
initial_chart,
(
index # &String,
dps_safe # f64,
max_damage_per_action # f64,
armor_break_per_turn # f64,
),
|ref_equipement| {
(
&refeq.advanced.display_name,
refeq.advanced.dps_safe,
refeq.advanced.max_damage_per_action,
refeq.advanced.damage_per_turn,
refeq.advanced.armor_break_per_turn,
&ref_equipement.advanced.display_name,
ref_equipement.advanced.max_damage_per_action,
ref_equipement.advanced.damage_per_turn,
ref_equipement.advanced.armor_break_per_turn,
)
});
let mut data_list = vec![];
let mut dps_safe = vec![];
let mut max_damage_per_action = vec![];
let mut max_damage_per_turn = vec![];
let mut armor_break_per_turn = vec![];
}
);
// add_field_to_chart!(dps_safe, graph);
raw_data
.into_iter()
.for_each(|(index, dps_s, dpa, dpt, abpt)| {
data_list.push(index);
dps_safe.push(dps_s);
max_damage_per_action.push(dpa);
max_damage_per_turn.push(dpt);
armor_break_per_turn.push(abpt);
});
// let (data_list, dps_safe, max_damage_per_action, max_damage_per_turn, armor_break_per_turn) =
// raw_data.collect::<(Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>)>();
// let mut data_list = vec![];
// let mut dps_safe = vec![];
// let mut max_damage_per_action = vec![];
// let mut max_damage_per_turn = vec![];
// let mut armor_break_per_turn = vec![];
// TODO: End of macro conversion
// raw_data
// .into_iter()
// .for_each(|(index, dps_s, dpa, dpt, abpt)| {
// data_list.push(index);
// dps_safe.push(dps_s);
// max_damage_per_action.push(dpa);
// max_damage_per_turn.push(dpt);
// armor_break_per_turn.push(abpt);
// });
// TODO: Call this iteratively with all elements in macro
charming::Chart::new()
.title(
charming::component::Title::new()
.text("Damage for all cross-gen products")
.left("center"),
)
.legend(
charming::component::Legend::new()
.left(10)
.top("middle")
.orient(Orient::Vertical)
.data(vec![
"dps_safe",
"max_damage_per_action",
"max_damage_per_turn",
"armor_break_per_turn",
]),
)
.tooltip(
charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis),
)
.x_axis(
charming::component::Axis::new()
.data(data_list.to_vec())
.type_(charming::element::AxisType::Category),
)
.y_axis(charming::component::Axis::new())
.grid(charming::component::Grid::new().contain_label(true))
.series(
charming::series::Line::new()
.data(dps_safe)
.name("dps_safe"), // .line_style(LineStyle::new().color("paleblue")),
)
.series(
charming::series::Line::new()
.data(max_damage_per_action)
.name("max_damage_per_action"),
)
.series(
charming::series::Line::new()
.data(max_damage_per_turn)
.name("max_damage_per_turn"),
)
.series(
charming::series::Line::new()
.data(armor_break_per_turn)
.name("armor_break_per_turn"),
)
.data_zoom(
charming::component::DataZoom::new()
.type_(charming::component::DataZoomType::Inside)
.start(0)
.end(100),
)
.data_zoom(charming::component::DataZoom::new().start(0).end(100))
.toolbox(
charming::component::Toolbox::new().feature(
charming::component::Feature::new()
.data_zoom(
charming::component::ToolboxDataZoom::new().y_axis_index("none"),
)
.restore(charming::component::Restore::new())
.save_as_image(charming::component::SaveAsImage::new()), // .brush(Brush::new().type_(vec![BrushType::LineX, BrushType::Clear])),
),
)
.tooltip(
charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis),
)
}
// TODO: End of macro conversion
// let initial_chart = initial_chart
// .x_axis(
// charming::component::Axis::new()
// .data(data_list.to_vec())
// .type_(charming::element::AxisType::Category),
// )
// .series(
// charming::series::Line::new()
// .data(dps_safe)
// .name("dps_safe"), // .line_style(LineStyle::new().color("paleblue")),
// )
// .series(
// charming::series::Line::new()
// .data(max_damage_per_action)
// .name("max_damage_per_action"),
// )
// .series(
// charming::series::Line::new()
// .data(max_damage_per_turn)
// .name("max_damage_per_turn"),
// )
// .series(
// charming::series::Line::new()
// .data(armor_break_per_turn)
// .name("armor_break_per_turn"),
// );
renderer.save(&initial_chart, format!("{save_path}.html"))?;
Ok(())
}
}
@ -201,10 +199,8 @@ impl From<InitialEquipementList> for RefinedEquipementList {
}
}
#[cfg(test)]
use crate::interfaces::RecipeCollection;
#[cfg(test)]
// #[cfg(test)]
// Used in exemples
impl Default for RefinedEquipementList {
fn default() -> Self {
let eq = InitialEquipementList::from_vectored(vec![Equipement::default(); 10]);

View File

@ -61,6 +61,7 @@ pub struct EquipementFixedRecipe {
#[allow(missing_docs)]
pub struct MockRecipe {}
// TODO[1] : Add fixed recipes
impl FixedRecipe for EquipementFixedRecipe {
type KeyList = MockRecipe;
@ -73,6 +74,7 @@ impl FixedRecipe for EquipementFixedRecipe {
}
}
// TODO[3]: Migrate ASAP from this manual implementation
/// Macro converting a value using serde and mapping an in house error
macro_rules! from_serde {
($value: expr) => {