Skip to content

Commit

Permalink
new card info format
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrimpuff committed Dec 15, 2024
1 parent d776d6e commit 89d20e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/sources/deck_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,16 @@ impl CommonCardsConversion for Cards {
Cards {
card_number: cards.card_number,
num: cards.amount,
manage_id: cards.manage_id.expect("should be a valid card in deck log"),
manage_id: cards
.manage_id
.expect("should be a valid card in deck log")
.to_string(),
}
}

fn to_common_cards(value: Self, _info: &CardsInfo) -> CommonCards {
CommonCards {
manage_id: Some(value.manage_id),
manage_id: value.manage_id.parse().ok(),
card_number: value.card_number,
amount: value.num,
}
Expand Down
24 changes: 17 additions & 7 deletions src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait CommonDeckConversion {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CommonCards {
pub manage_id: Option<String>,
pub manage_id: Option<u32>,
pub card_number: String,
pub amount: u32,
}
Expand All @@ -58,9 +58,10 @@ impl CommonCards {
pub fn from_card_number(card_number: String, amount: u32, info: &CardsInfo) -> Self {
let card = info
.values()
.flatten()
.find(|c| c.card_number.eq_ignore_ascii_case(&card_number));
CommonCards {
manage_id: card.map(|c| c.manage_id.clone()),
manage_id: card.and_then(|c| c.manage_id),
card_number: card.map(|c| c.card_number.clone()).unwrap_or(card_number),
amount,
}
Expand All @@ -75,6 +76,7 @@ impl CommonCards {
// grouped by card image
let rarities: IndexMap<_, _> = info
.values()
.flatten()
.filter(|c| c.card_number.eq_ignore_ascii_case(&card_number))
.fold(Default::default(), |mut acc, c| {
acc.entry(&c.img).or_insert(c);
Expand All @@ -83,7 +85,7 @@ impl CommonCards {
let card = rarities.values().nth(rarity_order as usize);
if let Some(card) = card {
CommonCards {
manage_id: Some(card.manage_id.clone()),
manage_id: card.manage_id,
card_number: card.card_number.clone(),
amount,
}
Expand All @@ -98,6 +100,7 @@ impl CommonCards {
// grouped by card image
let rarities: IndexMap<_, _> = info
.values()
.flatten()
.filter(|c| c.card_number.eq_ignore_ascii_case(&self.card_number))
.fold(Default::default(), |mut acc, c| {
acc.entry(&c.img).or_insert(c);
Expand All @@ -120,11 +123,17 @@ impl CommonCards {
}

pub fn card_info<'a>(&self, info: &'a CardsInfo) -> Option<&'a CardEntry> {
info.get(&self.manage_id.as_ref()?.parse::<u32>().ok()?)
info.get(&self.card_number)
.into_iter()
.flatten()
.find(|c| c.manage_id == self.manage_id)
}

pub fn card_info_mut<'a>(&self, info: &'a mut CardsInfo) -> Option<&'a mut CardEntry> {
info.get_mut(&self.manage_id.as_ref()?.parse::<u32>().ok()?)
info.get_mut(&self.card_number)
.into_iter()
.flatten()
.find(|c| c.manage_id == self.manage_id)
}

pub fn price(&self, info: &CardsInfo, prices: &PriceCache) -> Option<u32> {
Expand All @@ -142,9 +151,10 @@ impl CommonCards {

pub fn alt_cards(&self, info: &CardsInfo) -> Vec<Self> {
info.values()
.flatten()
.filter(|c| c.card_number.eq_ignore_ascii_case(&self.card_number))
.map(|c| Self {
manage_id: Some(c.manage_id.clone()),
manage_id: c.manage_id,
card_number: c.card_number.clone(),
amount: self.amount,
})
Expand Down Expand Up @@ -185,7 +195,7 @@ impl MergeCommonCards for Vec<CommonCards> {
let mut map = IndexMap::with_capacity(self.len());

for card in self {
map.entry((card.card_number.clone(), card.manage_id.clone()))
map.entry((card.card_number.clone(), card.manage_id))
.and_modify(|c: &mut CommonCards| c.amount += card.amount)
.or_insert(card);
}
Expand Down
4 changes: 2 additions & 2 deletions src/sources/price_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn Export(
.sorted_by_key(|c| {
u32::MAX - c.price(&info.read(), &prices.read()).expect("it's some")
}) // this is the highest price
.map(|c| c.manage_id.clone())
.map(|c| c.manage_id)
.next()
{
card.manage_id = manage_id;
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn Export(
.into_iter()
.filter(|c| c.price(&info.read(), &prices.read()).is_some())
.sorted_by_key(|c| c.price(&info.read(), &prices.read()).expect("it's some")) // this is the lowest price
.map(|c| c.manage_id.clone())
.map(|c| c.manage_id)
.next()
{
card.manage_id = manage_id;
Expand Down
4 changes: 2 additions & 2 deletions src/sources/proxy_sheets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn generate_pdf(
::image::load_from_memory_with_format(&image_bytes, ImageFormat::WebP).unwrap();
let image = image.resize_exact(CARD_WIDTH_PX, CARD_HEIGHT_PX, FilterType::CatmullRom);
let image = Image::from_dynamic_image(&image);
img_cache.lock().await.insert(card.manage_id.clone(), image);
img_cache.lock().await.insert(card.manage_id, image);
}
});
join_all(download_images).await;
Expand Down Expand Up @@ -123,7 +123,7 @@ async fn generate_pdf(
}

// place the image on the page
manage_id = card.manage_id.clone();
manage_id = card.manage_id;
image_transforms.push(ImageTransform {
dpi: Some(DPI),
translate_x: Some(
Expand Down

0 comments on commit 89d20e9

Please sign in to comment.