load card data

This commit is contained in:
Adam 2024-08-04 03:59:16 -04:00
parent 44ff6adb8a
commit 863a6b5865
2 changed files with 38 additions and 16 deletions

View file

@ -1,5 +1,15 @@
use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock};
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
/// Card Set
#[derive(Debug)]
pub struct CardSet {
pub white: Option<Vec<CardWhite>>,
pub black: Option<Vec<CardBlack>>,
}
/// Card Pack Meta
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -16,10 +26,10 @@ pub struct CardPacksMeta {
pub unofficial_meta: Vec<CardPackMeta>,
}
/// Card Packs Meta
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug)]
pub struct CardPacks {
pub official: Vec<CardPack>,
pub unofficial: Vec<CardPack>,
pub official: HashMap<u8, CardSet>,
pub unofficial: HashMap<u8, CardSet>,
}
/// Games update

View file

@ -23,8 +23,8 @@ fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
let jayson: Vec<CardPack> = serde_json::from_str(&data)
.with_context(|| format!("The contents of \"{path}\" is not valid JSON."))?;
let mut official: Vec<CardPack> = vec![];
let mut unofficial: Vec<CardPack> = vec![];
let mut official: HashMap<u8, CardSet> = HashMap::new();
let mut unofficial: HashMap<u8, CardSet> = HashMap::new();
let mut official_meta: Vec<CardPackMeta> = vec![];
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
@ -32,18 +32,28 @@ fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
for set in jayson {
let mut num_white = 0;
let mut num_black = 0;
let mut newset = CardSet {
white: Option::None,
black: Option::None,
};
// No safe default for this so make it an Option
let mut pack: Option<u8> = Option::None;
if let Some(white) = set.white {
if let Some(ref white) = set.white {
num_white = white.len();
if num_white > 0 {
pack = Some(white[0].pack)
pack = Some(white[0].pack);
newset.white = Some(set.white.unwrap());
}
}
if let Some(black) = set.black {
if let Some(ref black) = set.black {
num_black = black.len();
if num_black > 0 {
pack = Some(black[0].pack)
pack = Some(black[0].pack);
newset.black = Some(set.black.unwrap());
}
}
@ -56,11 +66,19 @@ fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
if set.official {
official_meta.push(meta);
official.insert(pack.unwrap(), newset);
} else {
unofficial_meta.push(meta);
unofficial.insert(pack.unwrap(), newset);
}
}
official.shrink_to_fit();
unofficial.shrink_to_fit();
official_meta.shrink_to_fit();
unofficial_meta.shrink_to_fit();
tracing::debug!("{} official", official.len());
tracing::debug!("{} official meta", official_meta.len());
tracing::debug!("{} unofficial", unofficial.len());
@ -68,17 +86,11 @@ fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
tracing::debug!("{:#?}", official_meta[0]);
tracing::debug!("{:#?}", unofficial_meta[0]);
official.shrink_to_fit();
unofficial.shrink_to_fit();
let packs = CardPacks {
official,
unofficial,
};
official_meta.shrink_to_fit();
unofficial_meta.shrink_to_fit();
let packs_meta = CardPacksMeta {
official_meta,
unofficial_meta,