diff options
| author | thecharlesblake | 2018-01-03 13:05:31 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-01-03 13:05:31 +0000 |
| commit | 7f4712b292632becfc83bc0e887271b25fefd21a (patch) | |
| tree | 0dd0201057886c7fda86a53b447cce5d04948d25 /src/main/input-output/input/json-parsing | |
| parent | Tidy up card class (diff) | |
| download | metasol-7f4712b292632becfc83bc0e887271b25fefd21a.tar.gz metasol-7f4712b292632becfc83bc0e887271b25fefd21a.zip | |
Refactor io dir structure
Diffstat (limited to 'src/main/input-output/input/json-parsing')
6 files changed, 357 insertions, 0 deletions
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp new file mode 100644 index 0000000..0e89f43 --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -0,0 +1,82 @@ +// +// 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); + } +} + +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())); + } + } +} diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h new file mode 100644 index 0000000..9db9e2b --- /dev/null +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -0,0 +1,27 @@ +// +// Created by thecharlesblake on 10/21/17. +// + +#ifndef SOLVITAIRE_DEAL_PARSER_H +#define SOLVITAIRE_DEAL_PARSER_H + +#include <vector> + +#include <boost/optional.hpp> +#include <rapidjson/document.h> +#include <rapidjson/schema.h> + +#include "../../../game/sol_rules.h" + +class game_state; + +class deal_parser { +public: + static void parse(game_state&, const rapidjson::Document&); +private: + static void parse_tableau_piles(game_state&, const rapidjson::Document&); + static void parse_hole(game_state&, const rapidjson::Document&); + static void parse_cells(game_state&, const rapidjson::Document&); +}; + +#endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/json-parsing/json_helper.cpp b/src/main/input-output/input/json-parsing/json_helper.cpp new file mode 100644 index 0000000..89f4564 --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.cpp @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#include <fstream> +#include <sstream> + +#include "json_helper.h" + +using namespace std; +using namespace rapidjson; + +Document json_helper::get_file_json(const string& filename) { + // Reads the file into a string + std::ifstream ifstr(filename); + std::stringstream buf; + buf << ifstr.rdbuf(); + + if (ifstr.fail()) { + throw runtime_error("could not read file " + filename); + } else { + Document d; + + d.Parse(buf.str().c_str()); + if (d.HasParseError()) { + throw runtime_error(filename + " not valid json"); + } else { + return d; + } + } +} + +void json_helper::json_parse_err(const string& msg) { + throw runtime_error("Error in JSON doc: " + msg); +} diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h new file mode 100644 index 0000000..5ab24bc --- /dev/null +++ b/src/main/input-output/input/json-parsing/json_helper.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 11/13/17. +// + +#ifndef SOLVITAIRE_UTIL_H +#define SOLVITAIRE_UTIL_H + +#include <string> + +#include <rapidjson/schema.h> + +class json_helper { +public: + static rapidjson::Document get_file_json(const std::string&); + static void json_parse_err(const std::string& msg); +}; + + +#endif //SOLVITAIRE_UTIL_H diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp new file mode 100644 index 0000000..200dbab --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -0,0 +1,173 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#include <vector> +#include <algorithm> + +#include "rules_parser.h" +#include "json_helper.h" +#include "../sol_preset_types.h" + +using namespace std; +using namespace rapidjson; + +//////////// +// Static // +//////////// + +typedef sol_rules::build_order ord; +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; + +const sol_rules rules_parser::from_file(const string rules_file) { + sol_rules sr = get_default(); + Document d = json_helper::get_file_json(rules_file); + + modify_sol_rules(sr, d); + + return sr; +} + +const sol_rules rules_parser::from_preset(const string sol_type) { + sol_rules sr = get_default(); + + const string preset_json = sol_preset_types::get(sol_type); + + Document d; + d.Parse(preset_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +sol_rules rules_parser::get_default() { + sol_rules sr; + const string default_json = sol_preset_types::get("default"); + + Document d; + d.Parse(default_json.c_str()); + assert(!d.HasParseError()); + + modify_sol_rules(sr, d); + return sr; +} + +void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { + + if (!d.IsObject()) { + json_helper::json_parse_err("JSON doc must be object"); + } + + if (d.HasMember("tableau piles")) { + if (d["tableau piles"].IsObject()) { + + if (d["tableau piles"].HasMember("count")) { + if (d["tableau piles"]["count"].IsInt()) { + sr.tableau_pile_count = static_cast<uint8_t>(d["tableau piles"]["count"].GetInt()); + } else { + json_helper::json_parse_err("[tableau piles][count] must be an integer"); + } + } + + if (d["tableau piles"].HasMember("build order")) { + if (d["tableau piles"]["build order"].IsString()) { + string bo_str = d["tableau piles"]["build order"].GetString(); + + if (bo_str == "ascending") { + sr.build_ord = ord::ASCENDING; + } else if (bo_str == "descending") { + sr.build_ord = ord::DESCENDING; + } else { + json_helper::json_parse_err("[tableau piles][build order] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build order] must be an string"); + } + } + + if (d["tableau piles"].HasMember("build policy")) { + if (d["tableau piles"]["build policy"].IsString()) { + string bp_str = d["tableau piles"]["build policy"].GetString(); + + if (bp_str == "any-suit") { + sr.build_pol = pol::ANY_SUIT; + } else if (bp_str == "red-black") { + sr.build_pol = pol::RED_BLACK; + } else if (bp_str == "same-suit") { + sr.build_pol = pol::SAME_SUIT; + } else if (bp_str == "no-build") { + sr.build_pol = pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][build policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][build policy] must be an string"); + } + } + + if (d["tableau piles"].HasMember("spaces policy")) { + if (d["tableau piles"]["spaces policy"].IsString()) { + string sp_str = d["tableau piles"]["spaces policy"].GetString(); + + if (sp_str == "any") { + sr.spaces_pol = s_pol::ANY; + } else if (sp_str == "no-build") { + sr.spaces_pol = s_pol::NO_BUILD; + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] is invalid"); + } + + } else { + json_helper::json_parse_err("[tableau piles][spaces policy] must be an string"); + } + } + + } else { + json_helper::json_parse_err("[tableau piles] must be an object"); + } + } + + if (d.HasMember("max rank")) { + if (d["max rank"].IsInt()) { + sr.max_rank = static_cast<card::rank_t>(d["max rank"].GetInt()); + } else { + json_helper::json_parse_err("[max rank] must be an integer"); + } + } + + if (d.HasMember("hole")) { + if (d["hole"].IsBool()) { + sr.hole = d["hole"].GetBool(); + } else { + json_helper::json_parse_err("[hole] must be a boolean"); + } + } + + if (d.HasMember("foundations")) { + if (d["foundations"].IsBool()) { + sr.foundations = d["foundations"].GetBool(); + } else { + json_helper::json_parse_err("[foundations] must be a boolean"); + } + } + + if (d.HasMember("cells")) { + if (d["cells"].IsInt()) { + sr.cells = static_cast<uint8_t>(d["cells"].GetInt()); + } else { + json_helper::json_parse_err("[cells] must be an integer"); + } + } + + if (d.HasMember("stock size")) { + if (d["stock size"].IsInt()) { + sr.stock_size = static_cast<uint8_t>(d["stock size"].GetInt()); + } else { + json_helper::json_parse_err("[stock size] must be an integer"); + } + } +} diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h new file mode 100644 index 0000000..1e28867 --- /dev/null +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -0,0 +1,21 @@ +// +// Created by thecharlesblake on 1/3/18. +// + +#ifndef SOLVITAIRE_RULES_PARSER_H +#define SOLVITAIRE_RULES_PARSER_H + +#include "../../game/sol_rules.h" + +class rules_parser { +public: + static const sol_rules from_file(std::string); + static const sol_rules from_preset(std::string); + +private: + static sol_rules get_default(); + static void modify_sol_rules(sol_rules&, rapidjson::Document&); +}; + + +#endif //SOLVITAIRE_RULES_PARSER_H |
