From fb74bfe5685970d990b6a3b1c8f7419aa2090e1a Mon Sep 17 00:00:00 2001 From: tretrauit Date: Sun, 7 Jan 2024 14:07:22 +0700 Subject: [PATCH] feat(katana): c o:w parser --- swordfish-common/src/database/katana.rs | 1 - swordfish-common/src/utils/katana.rs | 56 +++++++++++++++++++++++++ swordfish/src/config.rs | 9 ++++ swordfish/src/debug.rs | 44 ++++++++++++++++++- swordfish/src/main.rs | 5 ++- 5 files changed, 111 insertions(+), 4 deletions(-) diff --git a/swordfish-common/src/database/katana.rs b/swordfish-common/src/database/katana.rs index 05b9ce0..144f0d7 100644 --- a/swordfish-common/src/database/katana.rs +++ b/swordfish-common/src/database/katana.rs @@ -26,7 +26,6 @@ pub fn init() { } pub async fn query_card(name: &str, series: &str) -> Option { - // todo!("Query card from database"); KATANA .get() .unwrap() diff --git a/swordfish-common/src/utils/katana.rs b/swordfish-common/src/utils/katana.rs index 4352133..f876fb4 100644 --- a/swordfish-common/src/utils/katana.rs +++ b/swordfish-common/src/utils/katana.rs @@ -1,6 +1,7 @@ use crate::structs::Card; use log::{error, trace}; +// atopwl pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec { let mut cards: Vec = Vec::new(); for line in content.split("\n") { @@ -59,3 +60,58 @@ pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec { } cards } + +// kc o:w +pub fn parse_cards_from_katana_kc_ow(content: &String) -> Vec { + let mut cards: Vec = Vec::new(); + for line in content.split("\n") { + trace!("Parsing line: {}", line); + if !line.ends_with("**") { + continue; + } + let mut line_split = line.split(" · "); + let tag_wl_block = line_split.nth(0).unwrap(); + let mut wl_block = match tag_wl_block.split("`").nth(1) { + Some(wl_block) => { + // If one does not start with ♡, it is not a wishlist command + // then we'll just break entirely. + if !wl_block.starts_with("♡") { + break; + } + wl_block.to_string() + }, + None => break, + }; + wl_block.remove(0); + wl_block = wl_block.trim().to_string(); + let wishlist = match wl_block.parse::() { + Ok(wishlist) => wishlist, + Err(_) => { + error!("Failed to parse wishlist number: {}", wl_block); + continue; + } + }; + let series = match line_split.nth(4) { + Some(series) => series.to_string(), + None => continue, + }; + let name = match line_split.next() { + Some(name) => { + let mut name_string = name.to_string(); + name_string.remove_matches("**"); + name_string + } + None => continue, + }; + let card = Card { + wishlist: Some(wishlist), + name, + series, + print: 0, + last_update_ts: 0, + }; + trace!("Parsed card: {:?}", card); + cards.push(card); + } + cards +} diff --git a/swordfish/src/config.rs b/swordfish/src/config.rs index f57069d..9877c3c 100644 --- a/swordfish/src/config.rs +++ b/swordfish/src/config.rs @@ -42,12 +42,18 @@ pub struct Features { pub sofa_drop_analysis: DropAnalyzer, } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct General { + pub prefix: String, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Config { pub log: Log, pub tesseract: Tesseract, pub debug: Debug, pub features: Features, + pub general: General, } impl Config { @@ -94,6 +100,9 @@ impl Config { }, }, }, + general: General { + prefix: "~".to_string(), + }, } } pub fn save(&self, path: &str) { diff --git a/swordfish/src/debug.rs b/swordfish/src/debug.rs index 30fa388..7426210 100644 --- a/swordfish/src/debug.rs +++ b/swordfish/src/debug.rs @@ -127,6 +127,48 @@ pub async fn dbg_parse_qingque_atopwl(ctx: &Context, msg: &Message) -> CommandRe Ok(()) } +pub async fn dbg_parse_katana_kc_ow(ctx: &Context, msg: &Message) -> CommandResult { + let target_msg = match dbg_get_message("embed", ctx, msg).await { + Ok(msg) => msg, + Err(_) => { + return Ok(()); + } + }; + if target_msg.embeds.len() == 0 { + helper::error_message( + ctx, + msg, + "Message does not contain any embeds".to_string(), + None, + ) + .await; + return Ok(()); + } + let embed = &target_msg.embeds[0]; + let embed_description = match embed.description { + Some(ref description) => description, + None => { + helper::error_message( + ctx, + msg, + "Embed does not contain a description".to_string(), + None, + ) + .await; + return Ok(()); + } + }; + let cards = utils::katana::parse_cards_from_katana_kc_ow(embed_description); + helper::info_message( + ctx, + msg, + format!("Parsed cards: ```\n{:?}\n```", cards), + None, + ) + .await; + Ok(()) +} + pub async fn dbg_embed(ctx: &Context, msg: &Message) -> CommandResult { let target_msg = match dbg_get_message("embed", ctx, msg).await { Ok(msg) => msg, @@ -158,7 +200,7 @@ pub async fn dbg_embed(ctx: &Context, msg: &Message) -> CommandResult { msg, format!( "Title: \n\ - ```\ + ```\n\ {}\n\ ```\n\ Description: \n\ diff --git a/swordfish/src/main.rs b/swordfish/src/main.rs index 9b3ee5c..1ccce3d 100644 --- a/swordfish/src/main.rs +++ b/swordfish/src/main.rs @@ -203,7 +203,7 @@ async fn main() { swordfish_common::database::init().await; info!("Initializing Discord client..."); let framework = StandardFramework::new().group(&GENERAL_GROUP); - framework.configure(Configuration::new().prefix("~")); // set the bot's prefix to "~" + framework.configure(Configuration::new().prefix(CONFIG.get().unwrap().general.prefix.clone())); // Login with a bot token from the environment let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT; @@ -229,7 +229,7 @@ async fn ping(ctx: &Context, msg: &Message) -> CommandResult { #[command] async fn debug(ctx: &Context, msg: &Message) -> CommandResult { let config = CONFIG.get().unwrap(); - if ["debug", "trace"].contains(&config.log.level.as_str()){ + if !["debug", "trace"].contains(&config.log.level.as_str()){ return Ok(()); } if !config.debug.allowed_users.contains(&msg.author.id.get()) { @@ -255,6 +255,7 @@ async fn debug(ctx: &Context, msg: &Message) -> CommandResult { "kda" => debug::dbg_kdropanalyze(ctx, msg).await?, "embed" => debug::dbg_embed(ctx, msg).await?, "parse-qingque-atopwl" => debug::dbg_parse_qingque_atopwl(ctx, msg).await?, + "parse-katana-kc_ow" => debug::dbg_parse_katana_kc_ow(ctx, msg).await?, _ => { helper::error_message( ctx,