1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-12-23 01:54:22 +00:00

Improve the telegram::display module

This commit is contained in:
Steffo 2022-08-07 16:47:29 +02:00
parent 08388c59b3
commit c2146f662a
Signed by: steffo
GPG key ID: 6965406171929D01

View file

@ -1,40 +1,72 @@
//! This module defines functions to convert [patched-porobot] structs to [String]s formatted with [Telegram Bot HTML](https://core.telegram.org/bots/api#html-style). //! Module defining functions to format Legends of Runeterra data in [Telegram Bot HTML].
//! //!
//! TODO: Add support for non-latin languages. //! [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
//!
//! TODO: Preferably refactor everything in here, as the code is poor quality.
use std::collections::HashMap; use std::collections::HashMap;
use itertools::Itertools; use itertools::Itertools;
use teloxide::utils::html::escape; use teloxide::utils::html::escape;
use crate::data::setbundle::card::Card; use crate::data::setbundle::card::Card;
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;
/// Render a [Card] to a [String] formatted with [Telegram Bot HTML](https://core.telegram.org/bots/api#html-style). /// Render a [Card] in [Telegram Bot HTML].
pub fn display_card(card: &Card, mg: &MappedGlobals) -> String { ///
let title = format!(r#"<a href="{}"><b><i>{}</b></i></a>"#, &card.main_art().card_png, escape(&card.name)); /// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
pub fn display_card(card: &Card, globals: &LocalizedGlobalsIndexes) -> String {
let title = format!(
r#"<a href="{}"><b><i>{}</b></i></a>"#,
&card.main_art().card_png,
escape(&card.name),
);
let stats = match &card.r#type { let stats = match &card.r#type {
CardType::Spell => format!("{} mana", escape(&card.cost.to_string())), CardType::Spell => format!(
CardType::Unit => format!("{} mana {}|{}", escape(&card.cost.to_string()), escape(&card.attack.to_string()), escape(&card.health.to_string())), "{} mana",
CardType::Ability => "".to_string(), escape(&card.cost.to_string()),
CardType::Landmark => format!("{} mana", &card.cost), ),
CardType::Trap => "".to_string(), CardType::Unit => format!(
CardType::Unsupported => "".to_string(), "{} mana {}|{}",
escape(&card.cost.to_string()),
escape(&card.attack.to_string()),
escape(&card.health.to_string()),
),
CardType::Landmark => format!(
"{} mana",
&card.cost
),
_ => "".to_string(),
}; };
let set = format!("<i>{}</i>", escape(&display_set(card.set, &mg.sets))); let set = display_set(&card.set, &globals.sets);
let regions = format!("<i>{}</i>", escape(&display_regions(&card.regions, &mg.regions))); let regions = display_regions(&card.regions, &globals.regions);
let r#type = format!("<i>{}</i>", escape(&display_type(card.r#type))); let r#type = display_types(&card.r#type, &card.supertype, &card.subtypes);
let breadcrumbs = format!("{} {} {}", &set, &regions, &r#type); let breadcrumbs = format!("{} {} {}", &set, &regions, &r#type);
let description = card.localized_description_text.clone(); let description = escape(&card.localized_description_text);
let flavor = format!("<i>{}</i>", &card.localized_flavor_text);
let artist = format!(r#"<a href="{}">Illustration by {}</a>"#, &card.main_art().full_png, &card.artist_name); let flavor = format!(
"<i>{}</i>",
escape(&card.localized_flavor_text)
);
let artist = format!(
r#"<a href="{}">Illustration by {}</a>"#,
&card.main_art().full_png,
escape(&card.artist_name)
);
format!( format!(
"{title} {stats}\n{breadcrumbs}\n\n{description}\n\n-----\n{flavor}\n\n{artist}", "{title} {stats}\n{breadcrumbs}\n\n{keywords}\n{description}\n\n-----\n{flavor}\n\n{artist}",
title=title, title=title,
stats=stats, stats=stats,
breadcrumbs=breadcrumbs, breadcrumbs=breadcrumbs,
@ -45,36 +77,81 @@ pub fn display_card(card: &Card, mg: &MappedGlobals) -> String {
} }
/// Render a [CardSet] to a [String]. /// Render a [CardSet] in [Telegram Bot HTML].
fn display_set(set: CardSet, hm: &HashMap<CardSet, CoreSet>) -> String { ///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_set(set: &CardSet, hm: &LocalizedCardSetIndex) -> String {
format!(
"<i>{}</i>",
set set
.localized(&hm) .localized(hm)
.map(|o| o.name.clone()) .map(|o| format!("<i>{}</i>", escape(&o.name)))
.unwrap_or_else(|| "Unknown".to_string()) .unwrap_or_else(|| "Unknown".to_string())
)
} }
/// Render a slice of [CardRegion]s to a [String]. /// Render a slice of [CardRegion]s in [Telegram Bot HTML].
fn display_regions(regions: &[CardRegion], hm: &HashMap<CardRegion, CoreRegion>) -> String { ///
/// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
fn display_regions(regions: &[CardRegion], hm: &LocalizedCardRegionIndex) -> String {
regions regions
.iter() .iter()
.map(|region| region .map(|region| region
.localized(&hm) .localized(hm)
.map(|o| o.name.clone()) .map(|o| format!("<i>{}</i>", escape(&o.name)))
.unwrap_or_else(|| "Unknown".to_string()) .unwrap_or_else(|| "Unknown".to_string())
) )
.join(", ") .join(", ")
} }
/// Render a [CardType] to a [String]. /// Render the [CardType], the [CardSupertype] and the [CardSubtype]s in [Telegram Bot HTML].
fn display_type(r#type: CardType) -> String { ///
match r#type { /// [Telegram Bot HTML]: https://core.telegram.org/bots/api#html-style
CardType::Spell => "Spell", fn display_types(r#type: &CardType, supertype: &CardSupertype, subtypes: &[CardSubtype]) -> String {
CardType::Unit => "Unit", let mut result = String::new();
CardType::Ability => "Ability",
CardType::Landmark => "Landmark", if supertype != "" {
CardType::Trap => "Trap", result.push_str(&*format!(
CardType::Unsupported => "Unknown", "<i>{}</i> ",
}.to_string() escape(&supertype),
));
};
result.push_str(&*format!(
"<i>{}</i>",
escape(&String::from(r#type)),
));
if subtypes.len() > 0 {
result.push_str(
&*format!(
" {}",
subtypes.iter()
.map(|subtype| subtype
.map(|o| format!("<i>{}</i>", escape(&o)))
.unwrap_or_else(|| "Unknown".to_string())
)
.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 {
keywords
.iter()
.map(|keyword| keyword
.localized(hm)
.map(|o| format!("[<b>{}</b>]", escape(&o.name)))
.unwrap_or_else(|| "Unknown".to_string())
)
.join(" ")
} }