From dbc168a95550d31c6677f0ec11b58a1d3de8b4fc Mon Sep 17 00:00:00 2001 From: tretrauit Date: Sat, 6 Jan 2024 14:34:45 +0700 Subject: [PATCH] feat(common): implement database reading Also fix some bugs --- Cargo.lock | 41 +++++++++++++------------ swordfish-common/Cargo.toml | 1 + swordfish-common/src/database/katana.rs | 12 ++++++-- swordfish-common/src/database/mod.rs | 13 ++++++-- swordfish-common/src/structs.rs | 4 ++- swordfish/src/katana.rs | 23 +++++++++++--- swordfish/src/main.rs | 18 ++++++++--- 7 files changed, 77 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab3307a..d3cc3c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,7 +71,7 @@ checksum = "fdf6721fb0140e4f897002dd086c06f6c27775df19cfe1fccb21181a48fd2c98" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -702,7 +702,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -1498,9 +1498,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.71" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] @@ -1533,9 +1533,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1880,9 +1880,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] @@ -1898,13 +1898,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2190,6 +2190,7 @@ dependencies = [ "log", "mongodb", "rusty-tesseract", + "serde", "tracing", "tracing-subscriber", ] @@ -2207,9 +2208,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.43" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -2308,7 +2309,7 @@ checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2403,7 +2404,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2506,7 +2507,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2674,7 +2675,7 @@ checksum = "0b122284365ba8497be951b9a21491f70c9688eb6fddc582931a0703f6a00ece" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2811,7 +2812,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-shared", ] @@ -2845,7 +2846,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3126,7 +3127,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] diff --git a/swordfish-common/Cargo.toml b/swordfish-common/Cargo.toml index 90593e6..6aabb4d 100644 --- a/swordfish-common/Cargo.toml +++ b/swordfish-common/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" leptess = "0.14.0" log = "0.4.20" rusty-tesseract = "1.1.9" +serde = "1.0.195" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/swordfish-common/src/database/katana.rs b/swordfish-common/src/database/katana.rs index 2664b26..c953aeb 100644 --- a/swordfish-common/src/database/katana.rs +++ b/swordfish-common/src/database/katana.rs @@ -23,7 +23,13 @@ pub fn init() { .unwrap(); } -fn query_card() { - todo!("Query card from database"); - println!("{:?}", card); +pub async fn query_card(name: &str, series: &str) -> Option { + // todo!("Query card from database"); + KATANA.get().unwrap().find_one( + mongodb::bson::doc! { + "name": name, + "series": series + }, + None, + ).await.unwrap() } diff --git a/swordfish-common/src/database/mod.rs b/swordfish-common/src/database/mod.rs index 0735f60..57ad2e9 100644 --- a/swordfish-common/src/database/mod.rs +++ b/swordfish-common/src/database/mod.rs @@ -1,5 +1,6 @@ pub mod katana; +use mongodb::bson::doc; use mongodb::options::ClientOptions; use mongodb::{Client, Database}; use std::env; @@ -9,11 +10,14 @@ use tracing::info; static MONGO_CLIENT: OnceLock = OnceLock::new(); static MONGO_DATABASE: OnceLock = OnceLock::new(); -async fn init() { +pub async fn init() { let mut options = ClientOptions::parse(env::var("MONGODB_URL").expect("MongoDB url must be provided")) .await .unwrap(); + options.direct_connection = Some(true); + options.app_name = Some("swordfish".to_string()); + options.default_database = Some("swordfish".to_string()); match env::var("MONGODB_USERNAME") { Ok(username) => { options.credential = Some( @@ -29,9 +33,12 @@ async fn init() { info!("No MongoDB username provided, using authentication provided in the url"); } } + let client = Client::with_options(options).unwrap(); + let db = client.database("swordfish"); + db.run_command(doc! { "ping": 1 }, None).await.expect("Failed to connect to MongoDB"); + MONGO_DATABASE.set(db).unwrap(); MONGO_CLIENT - .set(Client::with_options(options).unwrap()) + .set(client) .unwrap(); - MONGO_DATABASE.set(MONGO_CLIENT.get().unwrap().database("swordfish")); katana::init(); } diff --git a/swordfish-common/src/structs.rs b/swordfish-common/src/structs.rs index 0abf3aa..56ae860 100644 --- a/swordfish-common/src/structs.rs +++ b/swordfish-common/src/structs.rs @@ -1,4 +1,6 @@ -#[derive(Debug)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize)] pub struct Card { pub wishlist: Option, pub name: String, diff --git a/swordfish/src/katana.rs b/swordfish/src/katana.rs index 6e24bd6..54fdede 100644 --- a/swordfish/src/katana.rs +++ b/swordfish/src/katana.rs @@ -7,6 +7,7 @@ use serenity::model::channel::Message; use std::io::Cursor; use std::sync::LazyLock; use swordfish_common::structs::Card; +use swordfish_common::database::katana as db; use swordfish_common::tesseract::{libtesseract, subprocess}; use swordfish_common::{trace, warn}; use tokio::task; @@ -208,10 +209,17 @@ pub async fn analyze_card_libtesseract(card: image::DynamicImage, count: u32) -> trace!("Name: {}", name); let series = series_thread.await.unwrap(); trace!("Series: {}", series); + let mut wishlist: Option = None; // TODO: Read the print number - // TODO: Read the wishlist number (from our database) + // Read the wishlist number + match db::query_card(name.as_str(), series.as_str()).await { + Some(card) => { + wishlist = card.wishlist; + } + None => {} + } return Card { - wishlist: None, + wishlist, name, series, print: 0, @@ -249,10 +257,17 @@ pub async fn analyze_card_subprocess(card: image::DynamicImage, count: u32) -> C trace!("Name: {}", name); let series = series_thread.await.unwrap(); trace!("Series: {}", series); + let mut wishlist: Option = None; // TODO: Read the print number - // TODO: Read the wishlist number (from our database) + // Read the wishlist number + match db::query_card(name.as_str(), series.as_str()).await { + Some(card) => { + wishlist = card.wishlist; + } + None => {} + } return Card { - wishlist: None, + wishlist, name, series, print: 0, diff --git a/swordfish/src/main.rs b/swordfish/src/main.rs index 9c3d509..60783f7 100644 --- a/swordfish/src/main.rs +++ b/swordfish/src/main.rs @@ -86,8 +86,8 @@ async fn main() { setup_logger(&log_level).expect("Failed to setup logger"); info!("Swordfish v{} - {}", env!("CARGO_PKG_VERSION"), GITHUB_URL); info!("Log level: {}", log_level); - info!("Loading database..."); - warn!("Databases are not implemented yet"); + info!("Initializing database..."); + 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 "~" @@ -193,10 +193,20 @@ async fn kdropanalyze(ctx: &Context, msg: &Message) -> CommandResult { let mut reply_str = String::new(); for card in cards { // reply_str.push_str(&format!("{:?}\n", card)); + let wishlist_str: String = match card.wishlist { + Some(wishlist) => { + let mut out_str = wishlist.to_string(); + while out_str.len() < 5 { + out_str.push(' '); + } + out_str + }, + None => "None ".to_string(), + }; reply_str.push_str( format!( - ":heart: `{:?}` • `{}` • **{}** • {}\n", - card.wishlist, card.print, card.name, card.series + ":heart: `{}` • `{}` • **{}** • {}\n", + wishlist_str, card.print, card.name, card.series ) .as_str(), )