pokebase-core 0.1.0

An embedded database of Pokémon TCG
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::card;
use crate::locale;
use crate::pokemon;
use crate::series;
use crate::set;
use crate::{Card, Locale, Map, Pokemon, Series, Set};

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::path::Path;

#[derive(Clone)]
pub struct Database {
    pub pokemon: Map<pokemon::Id, Pokemon>,
    pub series: Map<series::Id, Series>,
    pub sets: Map<set::Id, Set>,
    pub cards: Map<card::Id, Card>,
}

impl Database {
    pub async fn load() -> Result<Self, anywho::Error> {
        use tokio::task;

        Ok(task::spawn_blocking(|| {
            let pokemon = load_pokemon();
            let series: Vec<Series> = decompress(include_bytes!("../data/series.ron.gz"));
            let sets: Vec<Set> = decompress(include_bytes!("../data/sets.ron.gz"));
            let cards: Vec<Card> = decompress(include_bytes!("../data/cards.ron.gz"));

            log::info!("Loaded database with {} cards", cards.len());

            Self {
                pokemon: Map::new(pokemon, |pokemon| pokemon.id),
                series: Map::new(series, |series| series.id.clone()),
                sets: Map::new(sets, |set| set.id.clone()),
                cards: Map::new(cards, |card| card.id.clone()),
            }
        })
        .await?)
    }

    pub fn generate(data: impl AsRef<Path>) -> Result<Self, anywho::Error> {
        use std::fs::{self, File};
        use std::io::BufReader;

        let pokemon = load_pokemon();

        let mut series: BTreeMap<String, Series> = BTreeMap::new();
        let mut sets: BTreeMap<String, Set> = BTreeMap::new();
        let mut cards: BTreeMap<String, Card> = BTreeMap::new();

        let entries = fs::read_dir(&data)?;

        for entry in entries {
            let entry = entry?;

            if !entry.metadata()?.is_dir() {
                continue;
            }

            let locale = Locale(entry.file_name().to_string_lossy().to_string());

            if locale == Locale("ko".to_owned()) {
                continue;
            }
            dbg!(&locale);

            // Series
            #[derive(Serialize, Deserialize)]
            #[serde(rename_all = "camelCase")]
            struct LocalizedSeries {
                id: String,
                name: String,
                release_date: String,
            }

            dbg!(entry.path().join("series.json"));

            let localized_series_list: Vec<LocalizedSeries> = {
                let file = BufReader::new(File::open(entry.path().join("series.json"))?);
                serde_json::from_reader(file)?
            };

            for localized_series in localized_series_list {
                let series = series
                    .entry(localized_series.id.clone())
                    .or_insert_with(|| Series {
                        id: series::Id(localized_series.id),
                        name: locale::Map::new(),
                        release_date: localized_series.release_date,
                    });

                series.name.insert(locale.clone(), localized_series.name);
            }

            // Sets
            #[derive(Serialize, Deserialize)]
            #[serde(rename_all = "camelCase")]
            struct LocalizedSet {
                id: String,
                name: String,
                serie: Serie,
                release_date: String,
                card_count: CardCount,
                abbreviation: Option<Abbreviation>,
            }

            #[derive(Serialize, Deserialize)]
            struct Serie {
                id: String,
            }

            #[derive(Serialize, Deserialize)]
            struct CardCount {
                total: usize,
            }

            #[derive(Serialize, Deserialize)]
            struct Abbreviation {
                official: String,
            }

            dbg!(entry.path().join("sets.json"));

            let localized_sets: Vec<LocalizedSet> = {
                let file = BufReader::new(File::open(entry.path().join("sets.json"))?);
                serde_json::from_reader(file)?
            };

            for localized_set in localized_sets {
                let set = sets.entry(localized_set.id.clone()).or_insert_with(|| Set {
                    id: set::Id(localized_set.id),
                    name: locale::Map::new(),
                    series: series::Id(localized_set.serie.id),
                    release_date: localized_set.release_date,
                    total_cards: localized_set.card_count.total,
                    abbreviation: None,
                });

                set.name.insert(locale.clone(), localized_set.name);

                match localized_set.abbreviation {
                    Some(abbreviation) if set.abbreviation.is_none() => {
                        set.abbreviation = Some(abbreviation.official);
                    }
                    _ => {}
                }
            }

            // Cards
            #[derive(Serialize, Deserialize)]
            #[serde(rename_all = "camelCase")]
            struct LocalizedCard {
                id: String,
                name: String,
                set: CardSet,
                #[serde(default)]
                rarity: Option<String>,
                #[serde(default)]
                types: Vec<String>,
                variants: LocalizedVariants,
                #[serde(default)]
                illustrator: Option<String>,
                #[serde(default)]
                dex_id: Vec<f64>,
            }

            #[derive(Serialize, Deserialize)]
            struct CardSet {
                id: String,
            }

            #[derive(Serialize, Deserialize)]
            #[serde(rename_all = "camelCase")]
            struct LocalizedVariants {
                first_edition: bool,
                holo: bool,
                normal: bool,
                reverse: bool,
                w_promo: bool,
            }

            dbg!(entry.path().join("cards.json"));

            let localized_cards: Vec<LocalizedCard> = {
                let file = BufReader::new(File::open(entry.path().join("cards.json"))?);
                serde_json::from_reader(file)?
            };

            for localized_card in localized_cards {
                let Some(set) = sets.get(&localized_card.set.id) else {
                    continue;
                };

                if set.series.as_str() == "tcgp" {
                    continue;
                }

                let card = cards
                    .entry(localized_card.id.clone())
                    .or_insert_with(|| Card {
                        id: card::Id(localized_card.id),
                        name: locale::Map::new(),
                        set: set::Id(localized_card.set.id),
                        types: BTreeSet::new(),
                        rarity: card::Rarity::None,
                        variants: card::Variants {
                            first_edition: localized_card.variants.first_edition,
                            holo: localized_card.variants.holo,
                            normal: localized_card.variants.normal,
                            reverse: localized_card.variants.reverse,
                            w_promo: localized_card.variants.w_promo,
                        },
                        illustrator: localized_card.illustrator,
                        pokedex: localized_card
                            .dex_id
                            .into_iter()
                            .map(|id| id as usize)
                            .map(pokemon::Id)
                            .collect(),
                    });

                // Fill in Pokedex entries
                if locale.0 == "en" && card.pokedex.is_empty() {
                    for pokemon in &pokemon {
                        let name = pokemon.name();

                        if let Some(start) = localized_card.name.find(name) {
                            let end = start + name.len();
                            let previous = start.saturating_sub(1);

                            let left = localized_card.name[previous..start].chars();
                            let right = localized_card.name
                                [end..(end + 1).min(localized_card.name.len())]
                                .chars();

                            if left.chain(right).all(|c| c.is_whitespace() || c == '-') {
                                card.pokedex = vec![pokemon.id];
                                break;
                            }
                        }
                    }
                }

                card.name.insert(locale.clone(), localized_card.name);
                card.rarity = card.rarity.max(
                    localized_card
                        .rarity
                        .and_then(|rarity| parse_rarity(rarity).ok())
                        .unwrap_or_default(),
                );

                for type_ in localized_card.types {
                    if let Ok(type_) = parse_type(type_) {
                        card.types.insert(type_);
                    }
                }
            }
        }

        let mut cards: Vec<_> = cards.into_values().collect();
        cards.sort_by_key(|card| {
            sets.get(&card.set.0)
                .map(|set| {
                    format!(
                        "{release_date}-{:0>5}",
                        card.id.0.split("-").last().unwrap_or_default(),
                        release_date = set.release_date,
                    )
                })
                .unwrap_or_default()
        });

        let mut series: Vec<_> = series.into_values().collect();
        series.sort_by(|a, b| a.release_date.cmp(&b.release_date));

        let mut sets: Vec<_> = sets.into_values().collect();
        sets.sort_by(|a, b| a.release_date.cmp(&b.release_date));

        Ok(Self {
            pokemon: Map::new(pokemon, |pokemon| pokemon.id),
            series: Map::new(series, |series| series.id.clone()),
            sets: Map::new(sets, |set| set.id.clone()),
            cards: Map::new(cards, |card| card.id.clone()),
        })
    }

    pub fn find(&self, set: &str, number: &str) -> Option<&Card> {
        if set.len() < 2 {
            return None;
        }

        let mut set_matches: Vec<_> = self
            .sets
            .values()
            .iter()
            .filter_map(|candidate| {
                let abbreviation = candidate.abbreviation.as_ref()?;

                if abbreviation == set {
                    return Some((candidate, 0));
                }

                let distance = abbreviation
                    .chars()
                    .zip(set.chars())
                    .map(|(a, b)| if a == b { 0 } else { 1 })
                    .sum::<u64>();

                Some((candidate, distance + 1))
            })
            .collect();

        set_matches.sort_by_key(|(_, distance)| *distance);

        let (best_set, _) = set_matches.first()?;
        let card_id = card::Id(format!("{}-{number}", best_set.id));

        self.cards.get(&card_id)
    }
}

fn parse_type(type_: String) -> Result<card::Type, String> {
    Ok(match type_.as_str() {
        "Grass" => card::Type::Grass,
        "Fire" => card::Type::Fire,
        "Water" => card::Type::Water,
        "Lightning" => card::Type::Lightning,
        "Psychic" => card::Type::Psychic,
        "Fighting" => card::Type::Fighting,
        "Darkness" => card::Type::Darkness,
        "Metal" => card::Type::Metal,
        "Fairy" => card::Type::Fairy,
        "Dragon" => card::Type::Dragon,
        "Colorless" => card::Type::Colorless,
        _ => Err(format!("invalid type: {type_}"))?,
    })
}

fn parse_rarity(rarity: String) -> Result<card::Rarity, String> {
    Ok(match rarity.as_str() {
        "None" => card::Rarity::None,
        "Common" | "One Diamond" => card::Rarity::Common,
        "Uncommon" | "Two Diamond" => card::Rarity::Uncommon,
        "Rare" | "Three Diamond" => card::Rarity::Rare,
        "Holo Rare" | "Rare Holo" => card::Rarity::HoloRare,
        "Rare Holo LV.X" => card::Rarity::HoloRareLvx,
        "Holo Rare V" => card::Rarity::HoloRareV,
        "Holo Rare VMAX" => card::Rarity::HoloRareVmax,
        "Holo Rare VSTAR" => card::Rarity::HoloRareVstar,
        "Shiny rare" | "One Shiny" => card::Rarity::ShinyRare,
        "Shiny rare V" => card::Rarity::ShinyRareV,
        "Shiny rare VMAX" => card::Rarity::ShinyRareVmax,
        "Double rare" => card::Rarity::DoubleRare,
        "ACE SPEC Rare" => card::Rarity::AceSpecRare,
        "Amazing Rare" => card::Rarity::AmazingRare,
        "Radiant Rare" => card::Rarity::RadiantRare,
        "Rare PRIME" => card::Rarity::RarePrime,
        "LEGEND" => card::Rarity::Legend,
        "Classic Collection" => card::Rarity::ClassicCollection,
        "Ultra Rare" | "Four Diamond" => card::Rarity::UltraRare,
        "Shiny Ultra Rare" | "Two Shiny" => card::Rarity::ShinyUltraRare,
        "Secret Rare" => card::Rarity::SecretRare,
        "Full Art Trainer" => card::Rarity::FullArtTrainer,
        "Illustration rare" | "One Star" => card::Rarity::IllustrationRare,
        "Special illustration rare" | "Two Star" | "Three Star" => {
            card::Rarity::SpecialIllustrationRare
        }
        "Hyper rare" | "Crown" => card::Rarity::HyperRare,
        _ => {
            dbg!(&rarity);

            Err(format!("invalid rarity: {rarity}"))?
        }
    })
}

fn load_pokemon() -> Vec<Pokemon> {
    let pokemon: Vec<String> = decompress(include_bytes!("../data/pokemon.ron.gz"));

    pokemon
        .into_iter()
        .enumerate()
        .map(|(i, name)| Pokemon {
            id: pokemon::Id(i + 1),
            name: locale::Map::from_iter([(Locale("en".to_owned()), name)]),
        })
        .collect()
}

fn decompress<T: DeserializeOwned>(bytes: &[u8]) -> T {
    use flate2::read::GzDecoder;

    let decoder = GzDecoder::new(bytes);

    ron::de::from_reader(decoder).expect("Database is corrupt! Decompression failed.")
}

impl fmt::Debug for Database {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Database")
            .field("pokemon", &self.pokemon.len())
            .field("series", &self.series.len())
            .field("sets", &self.sets.len())
            .field("cards", &self.cards.len())
            .finish()
    }
}