From 5e52a5c623393b034491c1abdb0a375a16542017 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 28 Oct 2024 16:17:59 +0100 Subject: [PATCH] chore: add json schema for the settings --- assets/schema/oopetris.config.schema.json | 133 ++++++++++++++++++++++ settings.json | 6 +- src/libs/core/helper/parse_json.cpp | 7 ++ src/libs/core/helper/parse_json.hpp | 2 + 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 assets/schema/oopetris.config.schema.json diff --git a/assets/schema/oopetris.config.schema.json b/assets/schema/oopetris.config.schema.json new file mode 100644 index 000000000..531f2bb83 --- /dev/null +++ b/assets/schema/oopetris.config.schema.json @@ -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" + } +} diff --git a/settings.json b/settings.json index 34c9bdd67..5ab6e5bad 100644 --- a/settings.json +++ b/settings.json @@ -1,4 +1,5 @@ { + "$schema": "https://raw.githubusercontent.com/OpenBrickProtocolFoundation/oopetris/refs/heads/main/assets/schema/oopetris.config.schema.json", "controls": { "selected": null, "inputs": [ @@ -75,6 +76,7 @@ } ] }, - "volume": 0.2, - "discord": false + "volume": 0.0, + "discord": false, + "api_url": "https://oopetris.totto.lt/api/" } diff --git a/src/libs/core/helper/parse_json.cpp b/src/libs/core/helper/parse_json.cpp index 1a05b99de..34b5f6bab 100644 --- a/src/libs/core/helper/parse_json.cpp +++ b/src/libs/core/helper/parse_json.cpp @@ -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& keys) { if (not obj.is_object()) { @@ -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 diff --git a/src/libs/core/helper/parse_json.hpp b/src/libs/core/helper/parse_json.hpp index 7070ecd1e..845959436 100644 --- a/src/libs/core/helper/parse_json.hpp +++ b/src/libs/core/helper/parse_json.hpp @@ -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& keys);