-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreategame.cpp
74 lines (69 loc) · 2.4 KB
/
creategame.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
#include <QNetworkReply>
#include "creategame.hpp"
#include "globals.hpp"
#include "server.hpp"
#include "messagebox.hpp"
using namespace std;
CreateGame::CreateGame(Globals& globals_,ASIP& session_,Server& server_) :
QDialog(&server_),
globals(globals_),
server(server_),
session(session_),
vBoxLayout(this),
timed(tr("timed")),
rated(tr("rated")),
yourSide(tr("Your side:")),
sideButtons{make_unique<QRadioButton>(tr("white (Gold)")),
make_unique<QRadioButton>(tr("black (Silver)")),
make_unique<QRadioButton>(tr("random"))},
timeControl(tr("Time control")),
dialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel)
{
setWindowTitle(tr("Create new game"));
timed.setChecked(true);
hBoxLayout.addWidget(&timed);
rated.setChecked(true);
hBoxLayout.addWidget(&rated);
connect(&timed,&QCheckBox::toggled,[=](bool checked) {
timeControl.setEnabled(checked);
rated.setEnabled(checked);
});
hBoxLayout.addWidget(&yourSide,0,Qt::AlignCenter);
hBoxLayout.addWidget(sideButtons[0].get());
hBoxLayout.addWidget(sideButtons[1].get());
hBoxLayout.addWidget(sideButtons[2].get());
sideButtons[2]->setChecked(true);
vBoxLayout.addLayout(&hBoxLayout);
vBoxLayout.addWidget(&timeControl);
connect(&dialogButtonBox,&QDialogButtonBox::accepted,[&]{
setEnabled(false);
Side side;
if (sideButtons[0]->isChecked())
side=FIRST_SIDE;
else if (sideButtons[1]->isChecked())
side=SECOND_SIDE;
else {
assert(sideButtons[2]->isChecked());
side=Side(globals.rand(NUM_SIDES));
}
const auto networkReply=session.createGame(this,timeControl.toString(timed.isChecked()),timed.isChecked() && rated.isChecked(),side);
connect(networkReply,&QNetworkReply::finished,this,[=]{creationAttempt(*networkReply);});
});
connect(&dialogButtonBox,&QDialogButtonBox::rejected,this,&CreateGame::close);
vBoxLayout.addWidget(&dialogButtonBox);
timeControl.readSettings(globals.settings);
timeControl.moveTime.seconds.setFocus();
}
void CreateGame::creationAttempt(QNetworkReply& networkReply)
{
try {
server.addGame(networkReply,session.role(),true);
server.refreshPage();
close();
timeControl.writeSettings(globals.settings);
}
catch (const std::exception& exception) {
MessageBox(QMessageBox::Critical,tr("Error creating game"),exception.what(),QMessageBox::NoButton,this).exec();
setEnabled(true);
}
}