diff options
| author | Charlie Blake | 2018-04-04 00:03:37 +0100 |
|---|---|---|
| committer | GitHub | 2018-04-04 00:03:37 +0100 |
| commit | bb243681fa6f9d05e4e19757431310f5cbb61831 (patch) | |
| tree | 1ecfdbe1883df6325007e21af84ffed92d53888a /src/main/input-output/input/json-parsing/deal_parser.cpp | |
| parent | Merge pull request #46 from thecharlesblake/zero-seed-bugfix (diff) | |
| parent | Re-add schemas for rule and deal parsing (diff) | |
| download | metasol-bb243681fa6f9d05e4e19757431310f5cbb61831.tar.gz metasol-bb243681fa6f9d05e4e19757431310f5cbb61831.zip | |
Merge pull request #47 from thecharlesblake/re-add-schema
Re-add schemas for rule and deal parsing
Diffstat (limited to 'src/main/input-output/input/json-parsing/deal_parser.cpp')
| -rw-r--r-- | src/main/input-output/input/json-parsing/deal_parser.cpp | 44 |
1 files changed, 44 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 index 0112cda..17a8ae4 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -12,6 +12,12 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { + // There are two stages to reading in the supplied deal. It is both put + // through the deal schema validator, and also parsed into + // the game state object (with some further checking done). The parsing is + // done first because the errors it detects tend to give clearer error + // messages than the schema validator + // Construct tableau piles if (gs.rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); @@ -41,6 +47,44 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { if (gs.rules.foundations_init_card) { fill_foundations(gs); } + + apply_deal_schema(doc); // Throws an error if it fails +} + +void deal_parser::apply_deal_schema(const rapidjson::Document &doc) { + const char* schema_json = R"( +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "JSON Schema representing a solitaire game state to solve", + + "definitions": { + "card": {"type": "string", "pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K)(c|C|d|D|h|H|s|S))$"}, + "cardarray": {"type": "array", "items": {"$ref": "#/definitions/card"}} + }, + + "type": "object", "properties": { + + "tableau piles": { + "type": "array", "items": {"$ref": "#/definitions/cardarray"} + }, + "hole": {"$ref": "#/definitions/card"}, + "stock": {"$ref": "#/definitions/cardarray"}, + "waste": {"$ref": "#/definitions/cardarray"}, + "reserve": {"$ref": "#/definitions/cardarray"} + + }, "additionalProperties": false +} +)"; + + Document d; + d.Parse(schema_json); + assert(!d.HasParseError()); + SchemaDocument schema(d); + SchemaValidator validator(schema); + + if(!doc.Accept(validator)) { + throw runtime_error(json_helper::schema_err_str(validator)); + } } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { |
