Skip to content

Commit 5e52a5c

Browse files
committed
chore: add json schema for the settings
1 parent b36c399 commit 5e52a5c

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"$id": "https://git.uibk.ac.at/csba1761/bsc/-/blob/main/backend/assets/environments.schema.json",
4+
"$ref": "#/$defs/Root",
5+
"$defs": {
6+
"Root": {
7+
"type": "object",
8+
"properties": {
9+
"controls": {
10+
"$ref": "#/$defs/Controls"
11+
},
12+
"volume": {
13+
"type": "number",
14+
"minimum": 0.0,
15+
"maximum": 1.0
16+
},
17+
"discord": {
18+
"type": "boolean"
19+
},
20+
"api_url": {
21+
"type": "string"
22+
}
23+
},
24+
"required": [
25+
"volume"
26+
],
27+
"additionalProperties": false
28+
},
29+
"Controls": {
30+
"type": "object",
31+
"properties": {
32+
"selected": {
33+
"anyOf": [
34+
{
35+
"type": "number",
36+
"minimum": 0,
37+
"multipleOf": 1
38+
},
39+
{
40+
"type": "null"
41+
}
42+
]
43+
},
44+
"inputs": {
45+
"$ref": "#/$defs/Inputs"
46+
}
47+
},
48+
"required": [],
49+
"additionalProperties": false
50+
},
51+
"Inputs": {
52+
"type": "array",
53+
"items": {
54+
"$ref": "#/$defs/Input"
55+
},
56+
"additionalItems": false,
57+
"minItems": 0,
58+
"default": []
59+
},
60+
"Input": {
61+
"description": "TODO: this isn't fully specified",
62+
"anyOf": [
63+
{
64+
"$ref": "#/$defs/KeyboardInput"
65+
},
66+
{
67+
"type": "object",
68+
"additionalItems": true
69+
}
70+
]
71+
},
72+
"KeyboardInput": {
73+
"type": "object",
74+
"properties": {
75+
"type": {
76+
"const": "keyboard"
77+
},
78+
"drop": {
79+
"$ref": "#/$defs/KeyboardInputKey"
80+
},
81+
"hold": {
82+
"$ref": "#/$defs/KeyboardInputKey"
83+
},
84+
"move_down": {
85+
"$ref": "#/$defs/KeyboardInputKey"
86+
},
87+
"move_left": {
88+
"$ref": "#/$defs/KeyboardInputKey"
89+
},
90+
"move_right": {
91+
"$ref": "#/$defs/KeyboardInputKey"
92+
},
93+
"rotate_left": {
94+
"$ref": "#/$defs/KeyboardInputKey"
95+
},
96+
"rotate_right": {
97+
"$ref": "#/$defs/KeyboardInputKey"
98+
},
99+
"menu": {
100+
"type": "object",
101+
"properties": {
102+
"pause": {
103+
"$ref": "#/$defs/KeyboardInputKey"
104+
},
105+
"open_settings": {
106+
"$ref": "#/$defs/KeyboardInputKey"
107+
}
108+
},
109+
"additionalItems": false,
110+
"required": [
111+
"pause",
112+
"open_settings"
113+
]
114+
}
115+
},
116+
"required": [
117+
"menu",
118+
"rotate_right",
119+
"rotate_left",
120+
"move_right",
121+
"rotate_right",
122+
"move_left",
123+
"move_down",
124+
"hold",
125+
"drop",
126+
"type"
127+
]
128+
}
129+
},
130+
"KeyboardInputKey": {
131+
"type": "string"
132+
}
133+
}

settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "https://raw.githubusercontent.com/OpenBrickProtocolFoundation/oopetris/refs/heads/main/assets/schema/oopetris.config.schema.json",
23
"controls": {
34
"selected": null,
45
"inputs": [
@@ -75,6 +76,7 @@
7576
}
7677
]
7778
},
78-
"volume": 0.2,
79-
"discord": false
79+
"volume": 0.0,
80+
"discord": false,
81+
"api_url": "https://oopetris.totto.lt/api/"
8082
}

src/libs/core/helper/parse_json.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ std::string json::get_json_type(const nlohmann::json::value_t& type) {
3434
}
3535
}
3636

37+
bool json::is_meta_key(const std::string& key) {
38+
return key.starts_with("$");
39+
}
40+
3741
void json::check_for_no_additional_keys(const nlohmann::json& obj, const std::vector<std::string>& keys) {
3842

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

4751

4852
for (const auto& [key, _] : object) {
53+
if (is_meta_key(key)) {
54+
continue;
55+
}
4956
if (std::ranges::find(keys, key) == keys.cend()) {
5057
throw nlohmann::json::type_error::create(
5158
302, fmt::format("object may only contain expected keys, but contained '{}'", key), &obj

src/libs/core/helper/parse_json.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ namespace json {
131131

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

134+
OOPETRIS_CORE_EXPORTED [[nodiscard]] bool is_meta_key(const std::string& key);
135+
134136
OOPETRIS_CORE_EXPORTED void
135137
check_for_no_additional_keys(const nlohmann::json& obj, const std::vector<std::string>& keys);
136138

0 commit comments

Comments
 (0)