Skip to content

Commit

Permalink
chore: add json schema for the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Totto16 committed Oct 28, 2024
1 parent b36c399 commit 5e52a5c
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
133 changes: 133 additions & 0 deletions assets/schema/oopetris.config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://git.uibk.ac.at/csba1761/bsc/-/blob/main/backend/assets/environments.schema.json",
"$ref": "#/$defs/Root",
"$defs": {
"Root": {
"type": "object",
"properties": {
"controls": {
"$ref": "#/$defs/Controls"
},
"volume": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0
},
"discord": {
"type": "boolean"
},
"api_url": {
"type": "string"
}
},
"required": [
"volume"
],
"additionalProperties": false
},
"Controls": {
"type": "object",
"properties": {
"selected": {
"anyOf": [
{
"type": "number",
"minimum": 0,
"multipleOf": 1
},
{
"type": "null"
}
]
},
"inputs": {
"$ref": "#/$defs/Inputs"
}
},
"required": [],
"additionalProperties": false
},
"Inputs": {
"type": "array",
"items": {
"$ref": "#/$defs/Input"
},
"additionalItems": false,
"minItems": 0,
"default": []
},
"Input": {
"description": "TODO: this isn't fully specified",
"anyOf": [
{
"$ref": "#/$defs/KeyboardInput"
},
{
"type": "object",
"additionalItems": true
}
]
},
"KeyboardInput": {
"type": "object",
"properties": {
"type": {
"const": "keyboard"
},
"drop": {
"$ref": "#/$defs/KeyboardInputKey"
},
"hold": {
"$ref": "#/$defs/KeyboardInputKey"
},
"move_down": {
"$ref": "#/$defs/KeyboardInputKey"
},
"move_left": {
"$ref": "#/$defs/KeyboardInputKey"
},
"move_right": {
"$ref": "#/$defs/KeyboardInputKey"
},
"rotate_left": {
"$ref": "#/$defs/KeyboardInputKey"
},
"rotate_right": {
"$ref": "#/$defs/KeyboardInputKey"
},
"menu": {
"type": "object",
"properties": {
"pause": {
"$ref": "#/$defs/KeyboardInputKey"
},
"open_settings": {
"$ref": "#/$defs/KeyboardInputKey"
}
},
"additionalItems": false,
"required": [
"pause",
"open_settings"
]
}
},
"required": [
"menu",
"rotate_right",
"rotate_left",
"move_right",
"rotate_right",
"move_left",
"move_down",
"hold",
"drop",
"type"
]
}
},
"KeyboardInputKey": {
"type": "string"
}
}
6 changes: 4 additions & 2 deletions settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/OpenBrickProtocolFoundation/oopetris/refs/heads/main/assets/schema/oopetris.config.schema.json",
"controls": {
"selected": null,
"inputs": [
Expand Down Expand Up @@ -75,6 +76,7 @@
}
]
},
"volume": 0.2,
"discord": false
"volume": 0.0,
"discord": false,
"api_url": "https://oopetris.totto.lt/api/"
}
7 changes: 7 additions & 0 deletions src/libs/core/helper/parse_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ std::string json::get_json_type(const nlohmann::json::value_t& type) {
}
}

bool json::is_meta_key(const std::string& key) {
return key.starts_with("$");
}

void json::check_for_no_additional_keys(const nlohmann::json& obj, const std::vector<std::string>& keys) {

if (not obj.is_object()) {
Expand All @@ -46,6 +50,9 @@ void json::check_for_no_additional_keys(const nlohmann::json& obj, const std::ve


for (const auto& [key, _] : object) {
if (is_meta_key(key)) {
continue;
}
if (std::ranges::find(keys, key) == keys.cend()) {
throw nlohmann::json::type_error::create(
302, fmt::format("object may only contain expected keys, but contained '{}'", key), &obj
Expand Down
2 changes: 2 additions & 0 deletions src/libs/core/helper/parse_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ namespace json {

OOPETRIS_CORE_EXPORTED std::string get_json_type(const nlohmann::json::value_t& type);

OOPETRIS_CORE_EXPORTED [[nodiscard]] bool is_meta_key(const std::string& key);

OOPETRIS_CORE_EXPORTED void
check_for_no_additional_keys(const nlohmann::json& obj, const std::vector<std::string>& keys);

Expand Down

0 comments on commit 5e52a5c

Please sign in to comment.