aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorthecharlesblake2018-04-04 00:02:21 +0100
committerthecharlesblake2018-04-04 00:02:21 +0100
commit65f99c718f43a80de7df2a884027755a27aadc49 (patch)
tree1ecfdbe1883df6325007e21af84ffed92d53888a /src
parentMerge pull request #46 from thecharlesblake/zero-seed-bugfix (diff)
downloadmetasol-65f99c718f43a80de7df2a884027755a27aadc49.tar.gz
metasol-65f99c718f43a80de7df2a884027755a27aadc49.zip

Re-add schemas for rule and deal parsing

Diffstat (limited to 'src')
-rw-r--r--src/main/game/global_cache.cpp2
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp44
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.h1
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.cpp16
-rw-r--r--src/main/input-output/input/json-parsing/json_helper.h1
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp54
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.h1
-rw-r--r--src/main/input-output/input/sol_preset_types.cpp1
8 files changed, 119 insertions, 1 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index 048cbc9..29e8d25 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -20,6 +20,8 @@ typedef sol_rules::stock_deal_type sdt;
///////////////////////
cached_game_state::cached_game_state(const game_state& gs) {
+ data.reserve(52+18); // Enough for each card and up to 18 piles
+
if (gs.rules.hole) {
#ifdef NO_REDUCED_STATE
add_pile(gs.hole, gs);
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) {
diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h
index 98916cd..bfb7246 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.h
+++ b/src/main/input-output/input/json-parsing/deal_parser.h
@@ -23,6 +23,7 @@ private:
static void parse_stock(game_state&, const rapidjson::Document&);
static void parse_reserve(game_state&, const rapidjson::Document&);
static void fill_foundations(game_state&);
+ static void apply_deal_schema(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
index f53da6c..927787c 100644
--- a/src/main/input-output/input/json-parsing/json_helper.cpp
+++ b/src/main/input-output/input/json-parsing/json_helper.cpp
@@ -4,6 +4,7 @@
#include <fstream>
#include <sstream>
+#include <rapidjson/stringbuffer.h>
#include "json_helper.h"
#include "../../output/log_helper.h"
@@ -40,3 +41,18 @@ void json_helper::json_parse_err(const string& msg) {
void json_helper::json_parse_warning(const string& msg) {
LOG_WARNING("Error in JSON doc: " + msg);
}
+
+const string json_helper::schema_err_str(const SchemaValidator& validator) {
+ string ret = "Input JSON failed to match the required schema. Schema Validator Error = ";
+
+ StringBuffer sb;
+ validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
+ ret += validator.GetInvalidSchemaKeyword();
+ ret += ": ";
+
+ sb.Clear();
+ validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
+
+ ret += sb.GetString();
+ return ret;
+}
diff --git a/src/main/input-output/input/json-parsing/json_helper.h b/src/main/input-output/input/json-parsing/json_helper.h
index c9c971c..9a9500a 100644
--- a/src/main/input-output/input/json-parsing/json_helper.h
+++ b/src/main/input-output/input/json-parsing/json_helper.h
@@ -14,6 +14,7 @@ public:
static rapidjson::Document get_file_json(const std::string&);
static void json_parse_warning(const std::string& msg);
static void json_parse_err(const std::string& msg);
+ static const std::string schema_err_str(const rapidjson::SchemaValidator&);
};
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index 6fa89bc..800f90e 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -56,6 +56,12 @@ sol_rules rules_parser::get_default() {
}
void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
+ // There are two stages to reading in a rules document. It is both put
+ // through the rule schema validator, and also parsed into
+ // the game rules 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
+
if (!d.IsObject()) {
json_helper::json_parse_err("JSON doc must be object");
}
@@ -265,4 +271,52 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
json_helper::json_parse_err("one and only one of [hole] and [foundations] "
"must be true");
}
+
+ apply_rules_schema(d); // Throws an error if it fails
+}
+
+void rules_parser::apply_rules_schema(const rapidjson::Document &doc) {
+ const char* schema_json = R"(
+{
+ "$schema": "http://json-schema.org/draft-04/schema#",
+ "description": "JSON Schema representing a generic solitaire game",
+
+ "type": "object", "properties": {
+
+ "tableau piles": {
+ "type": "object", "properties": {
+ "count": {"type": "integer", "minimum": 0},
+ "build policy": {"type": "string", "enum": ["any-suit", "red-black", "same-suit", "no-build"]},
+ "spaces policy": {"type": "string", "enum": ["any", "no-build"]},
+ "diagonal deal": {"type": "boolean"},
+ "move built group": {"type": "boolean"},
+ "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]}
+ }, "additionalProperties": false
+ },
+ "max rank": {"type": "integer", "minimum": 1, "maximum": 13},
+ "two decks": {"type": "boolean"},
+ "hole": {"type": "boolean"},
+ "foundations": {"type": "boolean"},
+ "foundations initial card": {"type": "boolean"},
+ "foundations removable": {"type": "boolean"},
+ "foundations complete piles": {"type": "boolean"},
+ "cells": {"type": "integer", "minimum": 0},
+ "stock size": {"type": "integer", "minimum": 0},
+ "stock deal type": {"type": "string", "enum": ["waste", "tableau piles"]},
+ "reserve size": {"type": "integer", "minimum": 0},
+ "reserve stacked": {"type": "boolean"}
+
+ }, "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));
+ }
}
diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h
index 21dc635..497caaf 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.h
+++ b/src/main/input-output/input/json-parsing/rules_parser.h
@@ -15,6 +15,7 @@ public:
private:
static sol_rules get_default();
static void modify_sol_rules(sol_rules&, rapidjson::Document&);
+ static void apply_rules_schema(const rapidjson::Document&);
};
diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp
index 1fa6009..7519243 100644
--- a/src/main/input-output/input/sol_preset_types.cpp
+++ b/src/main/input-output/input/sol_preset_types.cpp
@@ -21,7 +21,6 @@ std::map<string, string> sol_preset_types::mp = {
{
"tableau piles": {
"count": 8,
- "build order": "descending",
"build policy": "any-suit",
"spaces policy": "any",
"diagonal deal": false,