164 lines
6.4 KiB
Rust
164 lines
6.4 KiB
Rust
//! Monster export module
|
||
|
||
use anyhow::{Context, Result};
|
||
use cli_parser::Config;
|
||
use config::monsters::ExportArgs;
|
||
use handlebars::{Handlebars, handlebars_helper};
|
||
use indexmap::IndexMap;
|
||
use rusqlite::Connection;
|
||
use std::{fmt::Write, fs::File, io::Write as OtherWrite};
|
||
|
||
use crate::monster_database::fetch_all_creature_data;
|
||
use crate::monster_structs::{MonsterAllAttacks, MonsterFullAttack, MonsterSingleAttack};
|
||
|
||
/// Load a template from a struct field into a `templates` collection.
|
||
///
|
||
/// $templates – the variable that holds the template collection
|
||
/// $cfg_path – any path (e.g. `config`, `self.cfg`, …) that points to a
|
||
/// struct containing the field we want
|
||
/// $name – the identifier of the field / template name
|
||
macro_rules! template_load {
|
||
($templates:ident, $cfg_path:expr, $name:ident) => {{
|
||
$templates
|
||
.register_template_string(
|
||
stringify!($name), // "intro"
|
||
&$cfg_path.$name, // &config.monster.intro
|
||
)
|
||
.context(concat!("Registering ", stringify!($name), " template"))? // ? propagates errors
|
||
}};
|
||
}
|
||
|
||
handlebars_helper!(plus: |x:i64 ,y:Option<i64>| x+y.unwrap_or(0));
|
||
|
||
fn load_templates(config: &Config) -> Result<Handlebars<'_>> {
|
||
let mut templates = Handlebars::new();
|
||
template_load!(templates, &config.monster, common_part);
|
||
template_load!(templates, &config.monster, master_part);
|
||
templates.register_helper("plus", Box::new(plus));
|
||
Ok(templates)
|
||
}
|
||
|
||
/// Export a tex with the given config and arguments
|
||
pub fn generate_monsters_tex(
|
||
args: &ExportArgs,
|
||
config: &Config,
|
||
connection: Connection,
|
||
) -> std::result::Result<(), anyhow::Error> {
|
||
let all_monsters = fetch_all_creature_data(&connection, config, args.filter.filter.as_deref())?;
|
||
let templates = load_templates(config)?;
|
||
println!("Processing");
|
||
let mut m_out = String::new();
|
||
let mut p_out = String::new();
|
||
|
||
for creature in &all_monsters {
|
||
if !creature.data.show_to_master.is_none_or(|v| v == 0) {
|
||
println!(" > {}", creature.name);
|
||
};
|
||
write!(p_out, "{}", templates.render("common_part", &creature)?)?;
|
||
if args.master || args.all {
|
||
write!(m_out, "{}", templates.render("common_part", &creature)?)?;
|
||
write!(m_out, "{}", templates.render("master_part", &creature)?)?;
|
||
}
|
||
}
|
||
|
||
if args.player || args.all {
|
||
println!("Generating Player Bestiaire");
|
||
let path = format!("./{}/player_bestiaire.tex", args.destination);
|
||
let mut f = File::create(&path).context(format!("Opening file {}", path))?;
|
||
write!(f, "{}", p_out).context(format!("Writing {}", path))?;
|
||
}
|
||
if args.master || args.all {
|
||
println!("Generating Master Bestiaire");
|
||
let path = format!("./{}/master_bestiaire.tex", args.destination);
|
||
let mut f = File::create(&path).context(format!("Opening file {}", path))?;
|
||
write!(f, "{}", m_out).context(format!("Writing {}", path))?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
impl MonsterAllAttacks {
|
||
pub fn from_retrieved_data(
|
||
original_data: &[MonsterSingleAttack],
|
||
config: &Config,
|
||
) -> MonsterAllAttacks {
|
||
let mut initial_phases: IndexMap<&String, Vec<&MonsterSingleAttack>> = IndexMap::new();
|
||
let mut phases: IndexMap<String, Vec<MonsterFullAttack>> = IndexMap::new();
|
||
// Intermediate split with hashmaps
|
||
for atk in original_data {
|
||
if let Some(values) = initial_phases.get_mut(&atk.phase) {
|
||
values.push(atk);
|
||
} else {
|
||
initial_phases.insert(&atk.phase, vec![atk]);
|
||
}
|
||
}
|
||
for (phase, phase_atks) in initial_phases.iter() {
|
||
let mut grouped_attacks: IndexMap<&Option<String>, Vec<&MonsterSingleAttack>> =
|
||
IndexMap::new();
|
||
let phase_name = phase.to_string();
|
||
phases.insert(phase_name.clone(), Vec::new());
|
||
// Pregroup attacks
|
||
for &atk in phase_atks {
|
||
if let Some(values) = grouped_attacks.get_mut(&atk.attack_group) {
|
||
values.push(atk);
|
||
} else {
|
||
grouped_attacks.insert(&atk.attack_group, vec![atk]);
|
||
}
|
||
}
|
||
// Ungroup them to have nice bundles
|
||
for (group, atk) in grouped_attacks {
|
||
if group.is_some() {
|
||
let monster_first_atk = atk.first().unwrap();
|
||
let color = color(monster_first_atk, config);
|
||
let group_attack = MonsterFullAttack {
|
||
color,
|
||
attacklist: atk.iter().map(|&v| v.clone()).collect(),
|
||
size: atk.len() + number_of_description(monster_first_atk),
|
||
description_final: monster_first_atk
|
||
.description_all
|
||
.clone()
|
||
.unwrap_or_default(),
|
||
};
|
||
// println!("group: {:#?}", group_attack.description_final);
|
||
phases.get_mut(&phase_name).unwrap().push(group_attack);
|
||
} else {
|
||
for single_atk in atk {
|
||
// println!("single: {:#?}", single_atk.description_all);
|
||
phases
|
||
.get_mut(&phase_name)
|
||
.unwrap()
|
||
.push(MonsterFullAttack {
|
||
color: color(single_atk, config),
|
||
attacklist: vec![single_atk.clone()],
|
||
size: 1 + number_of_description(&single_atk),
|
||
description_final: single_atk
|
||
.description_all
|
||
.clone()
|
||
.unwrap_or_default(),
|
||
});
|
||
}
|
||
};
|
||
}
|
||
}
|
||
Self { phases }
|
||
}
|
||
}
|
||
|
||
fn color(atk: &MonsterSingleAttack, config: &Config) -> String {
|
||
config
|
||
.monster
|
||
.color_table
|
||
.inner
|
||
.get(&atk.attack_type)
|
||
.unwrap_or(&config.monster.default_color)
|
||
.clone()
|
||
}
|
||
|
||
/// The number of description fields
|
||
fn number_of_description(monster_first_atk: &&MonsterSingleAttack) -> usize {
|
||
if monster_first_atk.description_all.is_some() {
|
||
1
|
||
} else {
|
||
0
|
||
}
|
||
}
|