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

Allow specifying locale via the DATA_DRAGON_LOCALE envvar

This commit is contained in:
Steffo 2023-03-23 19:58:59 +01:00
parent 2c83a773e1
commit f469776a1d
Signed by: steffo
GPG key ID: 2A24051445686895
4 changed files with 29 additions and 18 deletions

View file

@ -71,7 +71,7 @@ pub fn create_globalindexes_from_wd() -> globals::LocalizedGlobalsIndexes {
.expect("a valid core bundle to exist");
let core = CoreBundle::load(&path)
.expect("to be able to load `core-en_us` bundle");
.expect("to be able to load CoreBundle bundle");
globals::LocalizedGlobalsIndexes::from(core.globals)
}
@ -80,15 +80,15 @@ pub fn create_globalindexes_from_wd() -> globals::LocalizedGlobalsIndexes {
/// Create [`globals::LocalizedGlobalsIndexes`] from the latest english data in Data Dragon.
///
/// This function tries to load data from `https://dd.b.pvp.net/latest`.
pub async fn create_globalindexes_from_dd_latest_en_us() -> globals::LocalizedGlobalsIndexes {
pub async fn create_globalindexes_from_dd_latest(locale: &str) -> globals::LocalizedGlobalsIndexes {
let client = reqwest::Client::new();
log::debug!("Fetching latest CoreBundle from Data Dragon...");
log::debug!("Fetching {} CoreBundle from Data Dragon...", locale);
let core = CoreBundle::fetch(&client, "https://dd.b.pvp.net/latest", "en_us").await
.expect("to be able to fetch `core-en_us` bundle");
let core = CoreBundle::fetch(&client, "https://dd.b.pvp.net/latest", locale).await
.expect("to be able to fetch CoreBundle");
log::debug!("Fetched latest CoreBundle: it defines {} regions, {} keywords, {} rarities, {} sets, {} spell speeds, and {} vocab terms!", &core.globals.regions.len(), &core.globals.keywords.len(), &core.globals.rarities.len(), &core.globals.sets.len(), &core.globals.spell_speeds.len(), &core.globals.vocab_terms.len());
log::debug!("Fetched {} CoreBundle: it defines {} regions, {} keywords, {} rarities, {} sets, {} spell speeds, and {} vocab terms!", locale, &core.globals.regions.len(), &core.globals.keywords.len(), &core.globals.rarities.len(), &core.globals.sets.len(), &core.globals.spell_speeds.len(), &core.globals.vocab_terms.len());
globals::LocalizedGlobalsIndexes::from(core.globals)
}

View file

@ -163,17 +163,17 @@ pub const DD_KNOWN_SET_CODES: [&str; 7] = [
/// Create a [`card::CardIndex`] from the latest known english data in Data Dragon.
///
/// This function tries to load data from `https://dd.b.pvp.net/latest`.
pub async fn create_cardindex_from_dd_latest_en_us() -> card::CardIndex {
pub async fn create_cardindex_from_dd_latest(locale: &str) -> card::CardIndex {
let client = reqwest::Client::new();
let mut index = card::CardIndex::new();
for set_code in DD_KNOWN_SET_CODES {
log::debug!("Fetching SetBundle with code {} from Data Dragon...", &set_code);
log::debug!("Fetching {} SetBundle with code {} from Data Dragon...", locale, &set_code);
let set = SetBundle::fetch(&client, "https://dd.b.pvp.net/latest", "en_us", set_code).await
let set = SetBundle::fetch(&client, "https://dd.b.pvp.net/latest", locale, set_code).await
.expect("to be able to fetch set bundle");
log::debug!("Fetched SetBundle with code {}: it defines {} cards!", &set_code, set.cards.len());
log::debug!("Fetched {} SetBundle with code {}: it defines {} cards!", locale, &set_code, set.cards.len());
for card in set.cards {
index.insert(card.code.clone(), card);

View file

@ -3,8 +3,8 @@
use std::env;
use log::*;
use serenity::prelude::*;
use crate::data::corebundle::create_globalindexes_from_dd_latest_en_us;
use crate::data::setbundle::create_cardindex_from_dd_latest_en_us;
use crate::data::corebundle::create_globalindexes_from_dd_latest;
use crate::data::setbundle::create_cardindex_from_dd_latest;
use crate::discord::handler::EventHandler;
use crate::search::cardsearch::CardSearchEngine;
@ -13,12 +13,17 @@ pub async fn main() {
pretty_env_logger::init();
debug!("Logger initialized successfully!");
debug!("Detecting locale to use...");
let locale = env::var("DATA_DRAGON_LOCALE")
.expect("DATA_DRAGON_LOCALE to be set");
debug!("Using {} locale!", &locale);
debug!("Creating LocalizedGlobalIndexes...");
let globals = create_globalindexes_from_dd_latest_en_us().await;
let globals = create_globalindexes_from_dd_latest(&locale).await;
debug!("Created LocalizedGlobalIndexes!");
debug!("Creating CardIndex...");
let cards = create_cardindex_from_dd_latest_en_us().await;
let cards = create_cardindex_from_dd_latest(&locale).await;
debug!("Created CardIndex!");
debug!("Creating CardSearchEngine...");

View file

@ -1,7 +1,8 @@
//! Module defining the [`main`] function for `patched_porobot_telegram`.
use crate::data::corebundle::create_globalindexes_from_dd_latest_en_us;
use crate::data::setbundle::create_cardindex_from_dd_latest_en_us;
use std::env;
use crate::data::corebundle::create_globalindexes_from_dd_latest;
use crate::data::setbundle::create_cardindex_from_dd_latest;
use crate::search::cardsearch::CardSearchEngine;
use crate::telegram::handler::{inline_query_handler, message_handler};
use log::*;
@ -13,12 +14,17 @@ pub async fn main() {
pretty_env_logger::init();
debug!("Logger initialized successfully!");
debug!("Detecting locale to use...");
let locale = env::var("DATA_DRAGON_LOCALE")
.expect("DATA_DRAGON_LOCALE to be set");
debug!("Using {} locale!", &locale);
debug!("Creating LocalizedGlobalIndexes...");
let globals = create_globalindexes_from_dd_latest_en_us().await;
let globals = create_globalindexes_from_dd_latest(&locale).await;
debug!("Created LocalizedGlobalIndexes!");
debug!("Creating CardIndex...");
let cards = create_cardindex_from_dd_latest_en_us().await;
let cards = create_cardindex_from_dd_latest(&locale).await;
debug!("Created CardIndex!");
debug!("Creating CardSearchEngine...");