aboutsummaryrefslogtreecommitdiff
path: root/src/main/input-output/input/json-parsing/deal_parser.cpp
blob: e7f8209545dadeffccfba27f1899dcd4b455e8f9 (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
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
//
// Created by thecharlesblake on 10/21/17.
//

#include "deal_parser.h"
#include "../../../game/game_state.h"
#include "json_helper.h"

using namespace std;
using namespace rapidjson;

typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;

void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
    // Construct tableau piles
    if (gs.rules.tableau_pile_count > 0) {
        parse_tableau_piles(gs, doc);
    }

    // Construct hole card
    if (gs.rules.hole) {
        parse_hole(gs, doc);
    }

    // Construct filled cells
    if (gs.rules.cells) {
        parse_cells(gs, doc);
    }

    // Construct stock
    if (gs.rules.stock_size > 0) {
        parse_stock(gs, doc);
    }

    // If the foundations begin with cards in them, fill them
    if (gs.rules.foundations_init_card) {
        fill_foundations(gs);
    }
}

void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) {
    assert(doc.HasMember("tableau piles"));
    const Value& json_tab_piles = doc["tableau piles"];
    assert(json_tab_piles.IsArray());

    if (gs.tableau_piles.size() != gs.rules.tableau_pile_count) {
        json_helper::json_parse_err("Incorrect number of tableau piles");
    }

    for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles));
         p.second != end(gs.tableau_piles);
         ++p.first, ++p.second) {

        for (auto& json_card : p.first->GetArray()) {
            assert(json_card.IsString());
            gs.piles[*p.second].place(card(json_card.GetString()));
        }
    }
}

void deal_parser::parse_hole(game_state &gs, const Document& doc) {
    if (doc.HasMember("hole")) {
        const Value &json_hole = doc["hole"];
        assert(json_hole.IsString());

        gs.piles[gs.hole].place(card(json_hole.GetString()));
    }
}

void deal_parser::parse_cells(game_state &gs, const Document& doc) {
    if (doc.HasMember("cells")) {
        const Value &json_cells = doc["cells"];
        assert(json_cells.IsArray());

        const auto json_cell_arr = json_cells.GetArray();

        if (json_cell_arr.Empty()) return;
        else if (gs.rules.cells != json_cell_arr.Size()) {
            json_helper::json_parse_err("Incorrect number of cells");
        }

        for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells));
             p.second != end(gs.cells);
             ++p.first, ++p.second) {

            auto json_card = p.first;
            assert(json_card->IsString());
            gs.piles[*p.second].place(card(json_card->GetString()));
        }
    }
}

void deal_parser::parse_stock(game_state &gs, const Document& doc) {
    if (doc.HasMember("stock")) {
        const Value &json_stock = doc["stock"];
        assert(json_stock.IsArray());

        if (gs.rules.stock_size != json_stock.Size()) {
            json_helper::json_parse_err("Incorrect stock size");
        }

        for (const Value& json_card : json_stock.GetArray()) {
            gs.piles[gs.stock].place(card(json_card.GetString()));
        }
    }
}

void deal_parser::fill_foundations(game_state &gs) {
    for (uint8_t f_idx = 0; f_idx < 4; f_idx++) {
        gs.piles[gs.foundations[f_idx]].place(
                card(card::to_suit(f_idx), 1)
        );
    }
}