1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-12-23 18:14:20 +00:00
patched-porobot/src/telegram/display.rs

179 lines
5.3 KiB
Rust
Raw Normal View History

2022-08-07 14:47:29 +00:00
//! Module defining functions to format Legends of Runeterra data in [Telegram Bot HTML].
2022-08-05 00:47:57 +00:00
//!
2022-08-07 14:47:29 +00:00
//! [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
2022-08-05 00:47:57 +00:00
use itertools::Itertools;
use teloxide::utils::html::escape;
2022-08-06 17:44:44 +00:00
use crate::data::setbundle::card::Card;
2022-08-07 14:47:29 +00:00
use crate::data::setbundle::r#type::CardType;
use crate::data::corebundle::globals::LocalizedGlobalsIndexes;
use crate::data::corebundle::keyword::LocalizedCardKeywordIndex;
use crate::data::corebundle::region::LocalizedCardRegionIndex;
use crate::data::corebundle::set::LocalizedCardSetIndex;
use crate::data::setbundle::keyword::CardKeyword;
use crate::data::setbundle::region::CardRegion;
use crate::data::setbundle::set::CardSet;
use crate::data::setbundle::subtype::CardSubtype;
use crate::data::setbundle::supertype::CardSupertype;
2022-08-05 00:47:57 +00:00
2022-08-07 14:47:29 +00:00
/// Render a [Card] in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
pub fn display_card(globals: &LocalizedGlobalsIndexes, card: &Card) -> String {
2022-08-07 14:47:29 +00:00
let title = format!(
2022-08-08 02:10:59 +00:00
"<b><u>{}</u></b>\n",
2022-08-07 14:47:29 +00:00
escape(&card.name),
);
2022-08-05 00:47:57 +00:00
let stats = match &card.r#type {
2022-08-07 14:47:29 +00:00
CardType::Spell => format!(
2022-08-08 02:55:07 +00:00
"{} mana\n\n",
2022-08-07 14:47:29 +00:00
escape(&card.cost.to_string()),
),
CardType::Unit => format!(
2022-08-08 02:55:07 +00:00
"{} mana {}|{}\n\n",
2022-08-07 14:47:29 +00:00
escape(&card.cost.to_string()),
escape(&card.attack.to_string()),
escape(&card.health.to_string()),
),
CardType::Landmark => format!(
2022-08-08 02:55:07 +00:00
"{} mana\n\n",
2022-08-07 14:47:29 +00:00
&card.cost
),
_ => "".to_string(),
2022-08-05 00:47:57 +00:00
};
2022-08-07 14:47:29 +00:00
let set = display_set(&card.set, &globals.sets);
let regions = display_regions(&card.regions, &globals.regions);
let r#type = display_types(&card.r#type, &card.supertype, &card.subtypes);
2022-08-05 00:47:57 +00:00
2022-08-08 02:55:07 +00:00
let breadcrumbs = format!("{} {} {}\n\n", &set, &regions, &r#type);
2022-08-05 00:47:57 +00:00
let keywords = display_keywords(&card.keywords, &globals.keywords);
2022-08-08 02:55:07 +00:00
let description = display_description(&card.localized_description_text);
let levelup = display_levelup(&card.localized_levelup_text);
2022-08-07 14:47:29 +00:00
let flavor = format!(
2022-08-08 02:10:59 +00:00
"<i>{}</i>\n",
2022-08-07 14:47:29 +00:00
escape(&card.localized_flavor_text)
);
let artist = format!(
2022-08-08 02:10:59 +00:00
r#"<a href="{}">Illustration</a> by {}"#,
&card.main_art().expect("Card to have at least one illustration").full_png,
2022-08-07 14:47:29 +00:00
escape(&card.artist_name)
);
2022-08-05 00:47:57 +00:00
format!(
2022-08-08 02:55:07 +00:00
"{title}{breadcrumbs}{keywords}{stats}{description}{levelup}-----\n{flavor}{artist}",
2022-08-05 00:47:57 +00:00
)
}
2022-08-07 14:47:29 +00:00
/// Render a [CardSet] in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_set(set: &CardSet, hm: &LocalizedCardSetIndex) -> String {
format!(
"<i>{}</i>",
set
.localized(hm)
.map(|o| format!("<i>{}</i>", escape(&o.name)))
.unwrap_or_else(|| "Unknown".to_string())
)
2022-08-05 00:47:57 +00:00
}
2022-08-07 14:47:29 +00:00
/// Render a slice of [CardRegion]s in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_regions(regions: &[CardRegion], hm: &LocalizedCardRegionIndex) -> String {
2022-08-05 00:47:57 +00:00
regions
.iter()
.map(|region| region
2022-08-07 14:47:29 +00:00
.localized(hm)
.map(|o| format!("<i>{}</i>", escape(&o.name)))
2022-08-05 00:47:57 +00:00
.unwrap_or_else(|| "Unknown".to_string())
)
.join(", ")
}
2022-08-07 14:47:29 +00:00
/// Render the [CardType], the [CardSupertype] and the [CardSubtype]s in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_types(r#type: &CardType, supertype: &CardSupertype, subtypes: &[CardSubtype]) -> String {
let mut result = String::new();
if supertype != "" {
result.push_str(&*format!(
"<i>{}</i> ",
escape(&supertype),
));
};
result.push_str(&*format!(
"<i>{}</i>",
escape(&*String::from(r#type)),
2022-08-07 14:47:29 +00:00
));
if subtypes.len() > 0 {
result.push_str(
&*format!(
" {}",
subtypes.iter()
.map(|subtype| format!("<i>{}</i>", escape(&subtype)))
2022-08-07 14:47:29 +00:00
.join(", ")
)
)
}
result
}
/// Render a slice of [CardKeyword]s in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_keywords(keywords: &[CardKeyword], hm: &LocalizedCardKeywordIndex) -> String {
2022-08-08 02:10:59 +00:00
format!(
"{}\n",
keywords
.iter()
.map(|keyword| keyword
.localized(hm)
.map(|o| format!("[<b>{}</b>]", escape(&o.name)))
.unwrap_or_else(|| "Unknown".to_string())
)
.join(" ")
)
2022-08-05 00:47:57 +00:00
}
2022-08-08 02:55:07 +00:00
/// Render a [Card::localized_description_text] in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
2022-08-08 02:55:07 +00:00
fn display_description(description: &String) -> String {
if description == "" {
"".to_string()
}
else {
format!("{}\n\n", escape(&description))
}
}
/// Render a [Card::localized_levelup_text] in [Telegram Bot HTML].
///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
2022-08-08 02:55:07 +00:00
fn display_levelup(levelup: &String) -> String {
if levelup == "" {
"".to_string()
}
else {
format!("<u>Level up</u>: {}\n\n", escape(&levelup))
}
}