chore: restructure the structs and its functions a bit

This commit is contained in:
2024-01-08 20:56:02 +07:00
parent b757dfc13a
commit c347abd07d
7 changed files with 80 additions and 66 deletions

View File

@ -1,12 +1,12 @@
use crate::database;
use crate::structs::Card;
use crate::structs::Character;
use mongodb::Collection;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::task;
use tracing::trace;
pub static KATANA: OnceLock<Collection<Card>> = OnceLock::new();
pub static KATANA: OnceLock<Collection<Character>> = OnceLock::new();
///
/// Initialize the "katana" collection in MongoDB
@ -20,12 +20,12 @@ pub fn init() {
database::MONGO_DATABASE
.get()
.unwrap()
.collection::<Card>("katana"),
.collection::<Character>("katana"),
)
.unwrap();
}
pub async fn query_card(name: &String, series: &String) -> Option<Card> {
pub async fn query_character(name: &String, series: &String) -> Option<Character> {
KATANA
.get()
.unwrap()
@ -40,7 +40,7 @@ pub async fn query_card(name: &String, series: &String) -> Option<Card> {
.unwrap()
}
pub async fn query_card_regex(name: &String, series: &String) -> Option<Card> {
pub async fn query_character_regex(name: &String, series: &String) -> Option<Character> {
let mut name_regex = String::new();
let mut ascii_name = String::new();
for c in name.chars() {
@ -85,7 +85,7 @@ pub async fn query_card_regex(name: &String, series: &String) -> Option<Card> {
.unwrap()
}
pub async fn write_card(mut card: Card) -> Result<(), String> {
pub async fn write_character(mut card: Character) -> Result<(), String> {
let old_card = KATANA
.get()
.unwrap()
@ -136,12 +136,11 @@ pub async fn write_card(mut card: Card) -> Result<(), String> {
}
}
pub async fn write_cards(cards: Vec<Card>) -> Result<(), String> {
let mut new_cards: Vec<Card> = Vec::new();
let mut handles: Vec<task::JoinHandle<Result<Option<Card>, String>>> = Vec::new();
pub async fn write_characters(cards: Vec<Character>) -> Result<(), String> {
let mut new_cards: Vec<Character> = Vec::new();
let mut handles: Vec<task::JoinHandle<Result<Option<Character>, String>>> = Vec::new();
let start = SystemTime::now();
let current_time_ts = start
.duration_since(UNIX_EPOCH).unwrap();
let current_time_ts = start.duration_since(UNIX_EPOCH).unwrap();
for mut card in cards {
let current_time_ts_clone = current_time_ts.clone();
trace!("Writing card: {:?}", card);

View File

@ -1,10 +1,16 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Card {
pub struct Character {
pub wishlist: Option<u32>,
pub name: String,
pub series: String,
pub print: i32,
pub last_update_ts: i64,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct DroppedCard {
pub character: Character,
pub print: i32,
pub edition: i32,
}

View File

@ -1,9 +1,9 @@
use crate::structs::Card;
use crate::structs::Character;
use log::{error, trace};
// atopwl
pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec<Card> {
let mut cards: Vec<Card> = Vec::new();
pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec<Character> {
let mut cards: Vec<Character> = Vec::new();
for line in content.split("\n") {
trace!("Parsing line: {}", line);
let mut line_split = line.split(" · ");
@ -48,11 +48,10 @@ pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec<Card> {
}
None => continue,
};
let card = Card {
let card = Character {
wishlist: Some(wishlist),
name,
series,
print: 0,
last_update_ts: 0,
};
trace!("Parsed card: {:?}", card);
@ -62,8 +61,8 @@ pub fn parse_cards_from_qingque_atopwl(content: &String) -> Vec<Card> {
}
// kc o:w
pub fn parse_cards_from_katana_kc_ow(content: &String) -> Vec<Card> {
let mut cards: Vec<Card> = Vec::new();
pub fn parse_cards_from_katana_kc_ow(content: &String) -> Vec<Character> {
let mut cards: Vec<Character> = Vec::new();
for line in content.split("\n") {
trace!("Parsing line: {}", line);
if !line.ends_with("**") {
@ -103,11 +102,10 @@ pub fn parse_cards_from_katana_kc_ow(content: &String) -> Vec<Card> {
}
None => continue,
};
let card = Card {
let card = Character {
wishlist: Some(wishlist),
name,
series,
print: 0,
last_update_ts: 0,
};
trace!("Parsed card: {:?}", card);
@ -122,8 +120,8 @@ pub fn parse_cards_from_katana_kc_ow(content: &String) -> Vec<Card> {
///
/// "content" is `fields[0].value`
///
pub fn parse_cards_from_katana_klu_results(content: &String) -> Vec<Card> {
let mut cards: Vec<Card> = Vec::new();
pub fn parse_cards_from_katana_klu_results(content: &String) -> Vec<Character> {
let mut cards: Vec<Character> = Vec::new();
for line in content.split("\n") {
trace!("Parsing line: {}", line);
if !line.ends_with("**") {
@ -174,11 +172,10 @@ pub fn parse_cards_from_katana_klu_results(content: &String) -> Vec<Card> {
continue;
}
};
let card = Card {
let card = Character {
wishlist: Some(wishlist),
name,
series,
print: 0,
last_update_ts: 0,
};
trace!("Parsed card: {:?}", card);
@ -188,7 +185,7 @@ pub fn parse_cards_from_katana_klu_results(content: &String) -> Vec<Card> {
}
// klu (Character Lookup)
pub fn parse_cards_from_katana_klu_lookup(content: &String) -> Option<Card> {
pub fn parse_cards_from_katana_klu_lookup(content: &String) -> Option<Character> {
let mut lines = content.split("\n");
// Character
let mut line_split = lines.nth(0).unwrap().split(" · ");
@ -242,11 +239,10 @@ pub fn parse_cards_from_katana_klu_lookup(content: &String) -> Option<Card> {
}
None => return None,
};
Some(Card {
Some(Character {
wishlist: Some(wishlist),
name,
series,
print: 0,
last_update_ts: 0,
})
}