Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
gameinfo.cpp
1#include "gameinfo.h"
2#include <utility>
3GameInfo::GameInfo() = default;
4GameInfo::GameInfo(QString _id,
5 QString _display_name,
6 QString _description,
7 int _players_num,
8 QVector<QString> _role_names)
9 : id(std::move(_id)),
10 display_name(std::move(_display_name)),
11 description(std::move(_description)),
12 players_num(_players_num),
13 role_names(std::move(_role_names)) {
14}
15void GameInfo::read(const QJsonObject &json) {
16 if (json.contains(ID) && json[ID].isString())
17 id = json[ID].toString();
18
19 if (json.contains(DISPLAY_NAME) && json[DISPLAY_NAME].isString())
20 display_name = json[DISPLAY_NAME].toString();
21
22 if (json.contains(DESCRIPTION) && json[DESCRIPTION].isString())
23 description = json[DESCRIPTION].toString();
24
25 if (json.contains(PLAYERS_NUM) && json[PLAYERS_NUM].isDouble())
26 players_num = json[PLAYERS_NUM].toInt();
27
28 if (json.contains(ROLE_NAMES) && json[ROLE_NAMES].isArray()) {
29 auto objects = json[ROLE_NAMES].toArray();
30 std::transform(objects.begin(), objects.end(),
31 std::back_inserter(role_names),
32 [](const QJsonValueRef &obj) { return obj.toString(); });
33 }
34}
35void GameInfo::write(QJsonObject &json) const {
36 json[ID] = id;
37 json[DISPLAY_NAME] = display_name;
38 json[DESCRIPTION] = description;
39 json[PLAYERS_NUM] = players_num;
40 // Actually, write is not used
41}