forked from tobyp/queopardy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
412 lines (335 loc) · 9.23 KB
/
game.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "game.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QtDebug>
#include <cmath>
#include <QDateTime>
#include "category.h"
#include "player.h"
#include "playeranswer.h"
class Game::Private : public QObject
{
Q_OBJECT
public:
Private(Game *q)
: q(q)
, board(new Board(q))
{
connect(board, &Board::boardStateChanged, this, &Game::Private::saveBackup);
}
Game *q;
QString baseFileName;
Board * board;
int minPlayers = 2;
int maxPlayers = 8;
QList<Player *> players;
Player * buzzerPlayer = nullptr;
Question * openQuestion = nullptr;
bool joinable = true;
bool loading = false; //when this is true, disable the backup saving logic
public slots:
void saveBackup();
void onPlayerColorChanged(QColor const& color);
void onPlayerNameChanged(QString const& name);
};
Game::Game(QObject *parent) : QAbstractListModel(parent), d(new Private(this))
{
}
Game::~Game()
{
delete d;
}
QColor parseColor(QJsonValue const& value) {
QJsonObject colorObj = value.toObject();
int red = colorObj.value("red").toInt();
int green = colorObj.value("green").toInt();
int blue = colorObj.value("blue").toInt();
return QColor(red, green, blue);
}
QJsonValue unparseColor(QColor const& color) {
return QJsonObject {
{"red", color.red()},
{"green", color.green()},
{"blue", color.blue()},
};
}
void Game::load(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
qCritical("Failed to open board file: %s", fileName.toUtf8().data());
return;
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
file.close();
if (doc.isNull()) {
qCritical("Failed to parse board file: %s: %s", fileName.toUtf8().data(), error.errorString().toUtf8().data());
return;
}
if (!doc.isObject()) {
qCritical("Document is not an Object");
return;
}
QJsonObject docObject = doc.object();
d->loading = true;
reset();
QJsonArray playersArray = docObject.value("players").toArray();
for (QJsonValue playerValue : playersArray) {
QJsonObject playerObject = playerValue.toObject();
QString playerName = playerObject.value("name").toString();
addPlayer(playerName, parseColor(playerObject.value("color").toObject())); //this won't add if it already exists
}
d->board->loadJson(docObject.value("categories").toArray(), this);
d->baseFileName = fileName.chopped(5); // cut off json
d->loading = false;
}
void Game::save(const QString &fileName)
{
QJsonArray catArray = d->board->saveJson();
QJsonArray playersArray;
for (Player * player : d->players) {
QJsonObject playerObj = {
{"name", player->name()},
{"color", unparseColor(player->color())},
};
playersArray.append(playerObj);
}
QJsonObject gameObj{
{"categories", catArray},
{"players", playersArray},
};
QJsonDocument doc(gameObj);
QFile file(fileName);
if (!file.open(QFile::WriteOnly)) {
qCritical() << "Couldn't open" << fileName << "for writing";
return;
}
file.write(doc.toJson());
file.close();
}
void Game::reset()
{
d->board->reset();
for (auto player : d->players) {
player->setScore(0);
}
}
Board *Game::board() const
{
return d->board;
}
void Game::setBoard(Board *board)
{
if (d->board == board)
return;
d->board = board;
emit boardChanged(d->board);
}
void Game::setMinPlayers(int minPlayers)
{
if (d->minPlayers == minPlayers) {
return;
}
d->minPlayers = minPlayers;
emit minPlayersChanged(minPlayers);
}
int Game::minPlayers() const
{
return d->minPlayers;
}
void Game::setMaxPlayers(int maxPlayers)
{
if (d->maxPlayers == maxPlayers) {
return;
}
d->maxPlayers = maxPlayers;
emit maxPlayersChanged(maxPlayers);
}
void Game::setBuzzerPlayer(Player *buzzerPlayer)
{
if (d->buzzerPlayer == buzzerPlayer)
return;
d->buzzerPlayer = buzzerPlayer;
emit buzzerPlayerChanged(d->buzzerPlayer);
}
void Game::setJoinable(bool joinable)
{
if (d->joinable == joinable)
return;
d->joinable = joinable;
emit joinableChanged(d->joinable);
}
void Game::setOpenQuestion(Question *openQuestion)
{
if (d->openQuestion == openQuestion)
return;
if (d->openQuestion == nullptr) { // close buzzer if still open before opening this question
setBuzzerPlayer(nullptr);
}
d->openQuestion = openQuestion;
if (d->openQuestion == nullptr) { // close buzzer if still open after closing this question
setBuzzerPlayer(nullptr);
}
emit openQuestionChanged(d->openQuestion);
}
int Game::maxPlayers() const
{
return d->maxPlayers;
}
Player *Game::get(int idx) const
{
if (idx < 0 || idx >= d->players.size()) {
return nullptr;
}
Player *player = d->players.at(idx);
return player;
}
int Game::indexOf(Player *player) const
{
return d->players.indexOf(player);
}
Player *Game::getByName(const QString &name)
{
for (auto p : d->players) {
if (p->name() == name) return p;
}
return nullptr;
}
Player *Game::addPlayer(const QString &name, const QColor &color)
{
for(Player *p : d->players) {
if (p->name() == name) {
return p;
}
}
if (!d->joinable) return nullptr;
Player *player = new Player(name, color, this);
connect(player, &Player::nameChanged, d, &Game::Private::onPlayerNameChanged);
connect(player, &Player::colorChanged, d, &Game::Private::onPlayerColorChanged);
int pos = d->players.size();
beginInsertRows(QModelIndex(), pos, pos);
d->players.append(player);
endInsertRows();
emit countChanged(d->players.size());
return player;
}
void Game::removePlayer(Player *player)
{
int idx = d->players.indexOf(player);
if (idx != -1) {
beginRemoveRows(QModelIndex(), idx, idx);
d->players.removeAt(idx);
endRemoveRows();
emit countChanged(d->players.size());
}
}
bool Game::buzz(Player *player)
{
if (d->openQuestion == nullptr || d->buzzerPlayer != nullptr) {
return false;
}
d->buzzerPlayer = player;
emit buzzerPlayerChanged(d->buzzerPlayer);
return true;
}
QString Game::nextFreePlayerName()
{
QString name;
for (int i=1; i<d->players.size() + 2; ++i) {
// https://gamedev.stackexchange.com/questions/46463/how-can-i-find-an-optimum-set-of-colors-for-10-players
name = QString("Player %1").arg(i);
if (std::find_if(d->players.cbegin(), d->players.cend(), [&name](Player *const p){ return p->name() == name; }) == d->players.cend()) {
break;
}
}
return name;
}
QColor Game::nextFreePlayerColor()
{
QColor color;
for (int i=0; i<d->players.size() + 1; ++i) {
// https://gamedev.stackexchange.com/questions/46463/how-can-i-find-an-optimum-set-of-colors-for-10-players
color = QColor::fromHsvF(fmod(i * 0.618033988749895, 1.0), 0.5, 1.0);
if (std::find_if(d->players.cbegin(), d->players.cend(), [&color](Player *const p){ return p->color() == color; }) == d->players.cend()) {
break;
}
}
return color;
}
int Game::count() const
{
return d->players.size();
}
QHash<int, QByteArray> Game::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames.insert(NameRole, "name");
roleNames.insert(ColorRole, "color");
roleNames.insert(ScoreRole, "score");
roleNames.insert(KeyRole , "key");
return roleNames;
}
int Game::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return d->players.size();
}
QVariant Game::data(const QModelIndex &index, int role) const
{
int row = index.row();
if (row < 0 || row >= d->players.size()) {
return QVariant();
}
const Player *player = d->players.at(row);
switch(static_cast<Role>(role)) {
case Game::NameRole:
return player->name();
case Game::ColorRole:
return player->color();
case Game::ScoreRole:
return player->score();
case Game::KeyRole:
return player->key();
}
return QVariant();
}
bool Game::joinable() const
{
return d->joinable;
}
Question *Game::openQuestion() const
{
return d->openQuestion;
}
Player *Game::buzzerPlayer() const
{
return d->buzzerPlayer;
}
void Game::Private::saveBackup()
{
if (loading)
return;
QDateTime now = QDateTime::currentDateTime();
QString backupFileName = baseFileName + "." + now.toString("yyyy-MM-ddTHH-mm-ss") + ".json";
q->save(backupFileName);
}
void Game::Private::onPlayerColorChanged(const QColor &color)
{
Q_UNUSED(color)
Player * player = qobject_cast<Player*>(sender());
int idx = players.indexOf(player);
emit q->dataChanged(q->index(idx), q->index(idx), {Role::ColorRole});
}
void Game::Private::onPlayerNameChanged(const QString &name)
{
Q_UNUSED(name)
Player * player = qobject_cast<Player*>(sender());
int idx = players.indexOf(player);
emit q->dataChanged(q->index(idx), q->index(idx), {Role::NameRole});
}
#include "game.moc"