summary refs log tree commit diff stats
path: root/cardset.cpp
blob: 8097834f969d787b01640e9b968d37d9ee2d8d6d (plain) (blame)
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
#include "cardset.h"

#include <hkutil/string.h>

#include <fstream>
#include <json.hpp>
#include <sstream>

cardset::cardset(std::string filename) {
  std::ifstream in(filename, std::ios::in | std::ios::binary);
  std::ostringstream contents;
  contents << in.rdbuf();

  nlohmann::json cardsJson = nlohmann::json::parse(contents.str());

  for (const auto& cardJson : cardsJson) {
    // We're pre-processing the cardset now to reduce the work done here (but
    // mostly the space taken by the data).
    card_frame frame;

    if (cardJson["frame"] == "2015") {
      frame = card_frame::m2015;
    } else if (cardJson["frame"] == "2003") {
      frame = card_frame::modern;
    } else {
      continue;
    }

    size_t cardId = cards_.size();
    cards_.emplace_back(cardId, cardJson["name"], cardJson["imageUri"], frame,
                        cardJson["id"], cardJson["artist"]);

    std::string canon = hatkirby::lowercase(cardJson["name"]);

    for (int i = 0; i < canon.length(); i++) {
      titles_.add(canon, {cardId, i}, i);

      chars_.insert(canon.at(i));
    }
  }
}