211 lines
7.9 KiB
Rust
211 lines
7.9 KiB
Rust
//! Monster export module
|
||
|
||
use anyhow::{Context, Result};
|
||
use cli_parser::Config;
|
||
use config::monsters::MonsterConfig;
|
||
use handlebars::Handlebars;
|
||
use indexmap::IndexMap;
|
||
use rusqlite::Connection;
|
||
use serde::Serialize;
|
||
use std::{collections::HashMap, fmt::Write};
|
||
|
||
use crate::{
|
||
monster_database::fetch_all_data,
|
||
monster_structs::{
|
||
MonsterAllAttacks, MonsterFullAttack, MonsterRepetableInitRow, 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
|
||
}};
|
||
}
|
||
|
||
pub trait ToTex {
|
||
fn export_to_tex(self) -> String;
|
||
}
|
||
|
||
fn load_templates(config: &Config) -> Result<Handlebars<'_>> {
|
||
let mut templates = Handlebars::new();
|
||
template_load!(templates, &config.monster, intro);
|
||
template_load!(templates, &config.monster, body_parts_row);
|
||
template_load!(templates, &config.monster, body_parts_end);
|
||
template_load!(templates, &config.monster, blood_reward);
|
||
template_load!(templates, &config.monster, rewards_row);
|
||
template_load!(templates, &config.monster, init_rows);
|
||
template_load!(templates, &config.monster, resists_row);
|
||
template_load!(templates, &config.monster, stats);
|
||
template_load!(templates, &config.monster, phase_row);
|
||
template_load!(templates, &config.monster, attack_color);
|
||
template_load!(templates, &config.monster, attack_repeatable_row);
|
||
template_load!(templates, &config.monster, attack_info_row);
|
||
template_load!(templates, &config.monster, intent_table);
|
||
Ok(templates)
|
||
}
|
||
|
||
// TODO: Create a method on the struct instead of this inane bs
|
||
/// Export a tex with the given config and arguments
|
||
pub fn generate_monsters_tex(
|
||
args: &MonsterConfig,
|
||
config: &Config,
|
||
connection: Connection,
|
||
) -> std::result::Result<(), anyhow::Error> {
|
||
let all_monsters = fetch_all_data(connection)?;
|
||
let templates = load_templates(config)?;
|
||
|
||
let mut out = String::new();
|
||
let creature = &all_monsters[0];
|
||
write!(out, "{}", templates.render("intro", &creature.data)?)?;
|
||
write!(
|
||
out,
|
||
"{}",
|
||
templates.render("blood_reward", &creature.stats)?
|
||
)?;
|
||
for reward in creature.rewards.rewards.iter() {
|
||
write!(out, "{}", templates.render("rewards_row", reward)?)?;
|
||
}
|
||
write!(out, "{}", &config.monster.rewards_end)?;
|
||
write!(out, "{}", &config.monster.body_parts_intro)?;
|
||
for part in creature.body_parts.body_parts.iter() {
|
||
write!(out, "{}", templates.render("body_parts_row", part)?)?;
|
||
}
|
||
write!(
|
||
out,
|
||
"{}",
|
||
templates.render("body_parts_end", &creature.data)?
|
||
)?;
|
||
// TODO: Maybe add modularity and not print empty resists
|
||
write!(out, "{}", &config.monster.resists_intro)?;
|
||
for part in creature.body_parts.body_parts.iter() {
|
||
write!(out, "{}", templates.render("resists_row", part)?)?;
|
||
}
|
||
write!(out, "{}", &config.monster.resists_end)?;
|
||
write!(out, "{}", &config.monster.stats)?;
|
||
write!(out, "{}", templates.render("stats", &creature.stats)?)?;
|
||
for init in MonsterRepetableInitRow::vec_from(
|
||
&creature.stats.p1_init,
|
||
&creature.stats.p2_init,
|
||
&creature.stats.p1_actions,
|
||
&creature.stats.p2_actions,
|
||
)
|
||
.iter()
|
||
.skip(1)
|
||
{
|
||
write!(out, "{}", templates.render("init_rows", init)?)?;
|
||
}
|
||
|
||
let all_attacks = MonsterAllAttacks::from_retrieved_data(&creature.attacks.attacks);
|
||
println!("{:#?}", all_attacks);
|
||
all_attacks.to_tex(&templates, config)?;
|
||
write!(out, "{}", &config.monster.attacks_intro)?;
|
||
|
||
write!(out, "{}", &config.monster.attacks_end)?;
|
||
write!(out, "{}", templates.render("intent_table", &creature.data)?)?;
|
||
|
||
// println!("{}", out);
|
||
Ok(())
|
||
}
|
||
impl MonsterAllAttacks {
|
||
pub fn from_retrieved_data(original_data: &[MonsterSingleAttack]) -> 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());
|
||
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]);
|
||
}
|
||
}
|
||
for (group, atk) in grouped_attacks {
|
||
if group.is_some() {
|
||
let group_attack = MonsterFullAttack {
|
||
attacklist: atk.iter().map(|&v| v.clone()).collect(),
|
||
color: atk.first().unwrap().short_name.clone(),
|
||
description: atk.first().unwrap().effects.clone(),
|
||
};
|
||
phases.get_mut(&phase_name).unwrap().push(group_attack);
|
||
} else {
|
||
for single_atk in atk {
|
||
phases
|
||
.get_mut(&phase_name)
|
||
.unwrap()
|
||
.push(MonsterFullAttack {
|
||
attacklist: vec![single_atk.clone()],
|
||
color: single_atk.short_name.clone(),
|
||
description: single_atk.effects.clone(),
|
||
});
|
||
}
|
||
};
|
||
}
|
||
}
|
||
Self { phases }
|
||
}
|
||
}
|
||
#[derive(Serialize)]
|
||
struct Coloring<'a> {
|
||
color: &'a str,
|
||
}
|
||
impl MonsterAllAttacks {
|
||
pub fn to_tex(&self, templates: &Handlebars<'_>, config: &Config) -> Result<String> {
|
||
let mut out = String::new();
|
||
for (phase, list_full_attacks) in &self.phases {
|
||
// Print phase name
|
||
write!(out, "{}", templates.render("phase_row", phase)?)?;
|
||
for attack in list_full_attacks {
|
||
write!(out, "{}", config.monster.attack_before_repeat)?;
|
||
for sub_attack in &attack.attacklist {
|
||
let color = Coloring {
|
||
color: config
|
||
.monster
|
||
.color_table
|
||
.inner
|
||
.get(&attack.color)
|
||
.unwrap_or(&config.monster.default_color),
|
||
};
|
||
write!(out, "{}", templates.render("attack_color", &color)?)?;
|
||
write!(
|
||
out,
|
||
"{}",
|
||
templates.render("attack_repeatable_row", sub_attack)?
|
||
)?;
|
||
}
|
||
|
||
write!(
|
||
out,
|
||
"{}",
|
||
templates.render("attack_info_row", &list_full_attacks[0])?
|
||
)?;
|
||
}
|
||
// Print all attacks in that phase
|
||
|
||
println!("{}", out);
|
||
}
|
||
Ok(out)
|
||
}
|
||
}
|