Skip to content

Commit

Permalink
add deck validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrimpuff committed Sep 28, 2024
1 parent e9b6a8b commit 7387f6e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ pub enum CardType {
#[component]
fn DeckPreview() -> Element {
let deck = COMMON_DECK.read();
let map = CARDS_INFO.read();

let Some(deck) = deck.as_ref() else {
return rsx! {};
};
Expand All @@ -279,7 +281,23 @@ fn DeckPreview() -> Element {
}
});

let warnings = deck.validate(&map);

rsx! {
if !warnings.is_empty() {
article { class: "message is-warning",
div { class: "message-header",
p { "Warning" }
}
div { class: "message-body content",
ul {
for warn in warnings {
li { "{warn}" }
}
}
}
}
}
h2 { class: "title is-4", "Deck preview" }
p { class: "subtitle is-6 is-spaced",
if let Some(name) = &deck.name {
Expand Down
47 changes: 47 additions & 0 deletions src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,53 @@ impl CommonDeck {
acc
})
}

pub fn validate(&self, map: &CardsInfoMap) -> Vec<String> {
let mut errors = vec![];

// check for unreleased or invalid cards
if self.oshi.manage_id.is_none()
|| self.main_deck.iter().any(|c| c.manage_id.is_none())
|| self.cheer_deck.iter().any(|c| c.manage_id.is_none())
{
errors.push("Contains unknown cards.".into());
}

// check for card amount
if self.oshi.manage_id.is_none() {
errors.push("Missing an Oshi card.".into());
}
if self.main_deck.iter().map(|c| c.amount).sum::<u32>() > 50 {
errors.push("Too many cards in main deck.".into());
}
if self.main_deck.iter().map(|c| c.amount).sum::<u32>() < 50 {
errors.push("Not enough cards in main deck.".into());
}
if self.cheer_deck.iter().map(|c| c.amount).sum::<u32>() > 20 {
errors.push("Too many cards in cheer deck.".into());
}
if self.cheer_deck.iter().map(|c| c.amount).sum::<u32>() < 20 {
errors.push("Not enough cards in cheer deck.".into());
}

// check for unlimited cards
for card in &self.main_deck {
if card.amount
> card
.manage_id
.as_ref()
.and_then(|m| {
map.get(&m.parse().expect("should be a number"))
.map(|i| i.max)
})
.unwrap_or(50)
{
errors.push(format!("Too many {} in main deck.", card.card_number));
}
}

errors
}
}

pub type CardsInfoMap = BTreeMap<u32, CardInfoEntry>;
Expand Down

0 comments on commit 7387f6e

Please sign in to comment.