-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.cpp
142 lines (117 loc) · 3.91 KB
/
board.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "board.h"
#include "game.h"
#include <QJsonObject>
class Board::Private : public QObject
{
Q_OBJECT
public:
Private(Board *q)
: q(q)
{
}
Board *q;
QList<QObject*> categories;
};
Board::Board(QObject *parent) : QObject(parent), d(new Private(this))
{
}
Board::~Board() {
delete d;
}
QList<QObject *> Board::categories() const
{
return d->categories;
}
void Board::loadJson(const QJsonArray &catArray, Game *game)
{
for (QJsonValue const& catValue : catArray) {
QJsonObject catObj = catValue.toObject();
Category * cat = new Category(this);
cat->setLabel(catObj.value("label").toString());
int lastScore = 0;
for (QJsonValue questionValue : catObj.value("questions").toArray()) {
QJsonObject questionObject = questionValue.toObject();
lastScore = questionObject.value("points").toInt(lastScore + 100);
QString questionString = questionObject.value("question").toString();
Question * question = new Question(lastScore, questionString);
connect(question, &Question::questionStateChanged, this, &Board::questionStateChanged);
bool revealed = questionObject.value("revealed").toBool();
question->setRevealed(revealed);
QJsonValue font = questionObject.value("font");
if (!font.isUndefined()) {
question->setFont(font.toString());
}
QJsonValue fontSize = questionObject.value("fontSize");
if (!fontSize.isUndefined()) {
question->setFontSize(fontSize.toInt());
}
for (QJsonValue paValue : questionObject.value("playerAnswers").toArray()) {
QJsonObject paObj = paValue.toObject();
QJsonValue playerValue = paObj.value("player");
Player * player = nullptr;
if (playerValue.isString()) {
QString playerName = paObj.value("player").toString();
player = game->getByName(playerName);
}
int score = paObj.value("score").toInt();
question->addPlayerAnswer(player, score);
}
cat->addQuestion(question);
}
d->categories.append(cat);
}
emit categoriesChanged(d->categories);
}
QJsonArray Board::saveJson()
{
QJsonArray catArray;
for (auto const& catQObject : d->categories) {
QJsonArray questionsArray;
Category * cat = qobject_cast<Category*>(catQObject);
for (auto const& qQ : cat->questions()) {
Question * question = qobject_cast<Question*>(qQ);
QJsonArray paArray;
for (auto const& paQ : question->playerAnswers()) {
PlayerAnswer * pa = qobject_cast<PlayerAnswer*>(paQ);
QJsonObject paObj = {
{"player", pa->player() ? pa->player()->name() : QJsonValue()},
{"score", pa->score()},
};
paArray.append(paObj);
}
QJsonObject qObj = {
{"points", question->points()},
{"question", question->question()},
{"playerAnswers", paArray},
{"revealed", question->revealed()},
};
questionsArray.append(qObj);
}
QJsonObject catObj = {
{"label", cat->label()},
{"questions", questionsArray},
};
catArray.append(catObj);
}
return catArray;
}
void Board::reset()
{
for (auto cat : d->categories) {
delete cat;
}
d->categories.clear();
emit categoriesChanged(d->categories);
}
void Board::setCategories(QList<QObject *> categories)
{
if (d->categories == categories)
return;
d->categories = categories;
emit categoriesChanged(d->categories);
}
void Board::questionStateChanged()
{
emit boardStateChanged();
}
#include "board.moc"