-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cpp
99 lines (90 loc) · 3.65 KB
/
server.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
#include <QNetworkReply>
#include <QHeaderView>
#include "server.hpp"
#include "globals.hpp"
#include "gamelist.hpp"
#include "creategame.hpp"
#include "opengame.hpp"
#include "messagebox.hpp"
#include "mainwindow.hpp"
Server::Server(Globals& globals_,ASIP& session_,MainWindow& mainWindow_) :
session(session_),
mainWindow(mainWindow_),
globals(globals_),
vBoxLayout(this),
newGame(tr("&Create open game")),
openGame(tr("&Open existing game")),
refresh(tr("&Refresh page"))
{
session.setParent(this);
const QString gameListCategoryTitles[]={tr("My games"),tr("Invited games"),tr("Open games"),tr("Live games"),tr("Recent games")};
for (const auto gameListCategory:session.availableGameListCategories()) {
using namespace std;
gameLists.insert(make_pair(gameListCategory,make_unique<GameList>(this,gameListCategoryTitles[gameListCategory])));
}
connect(&newGame,&QPushButton::clicked,this,[&]{openDialog(new CreateGame(globals,session,*this));});
connect(&openGame,&QPushButton::clicked,this,[&]{openDialog(new OpenGame(*this));});
connect(&refresh,&QPushButton::clicked,this,&Server::refreshPage);
hBoxLayout.addWidget(&newGame);
hBoxLayout.addWidget(&openGame);
hBoxLayout.addWidget(&refresh);
vBoxLayout.addLayout(&hBoxLayout);
auto extraSiteWidget=session.siteWidget(*this);
if (extraSiteWidget!=nullptr)
vBoxLayout.addWidget(extraSiteWidget.release());
connect(&session,&ASIP::error,this,[this](const std::exception& exception) {
MessageBox(QMessageBox::Critical,tr("Error from game room"),exception.what(),QMessageBox::NoButton,this).exec();
});
connect(&session,&ASIP::sendGameList,this,[&](const ASIP::GameListCategory gameListCategory,const std::vector<ASIP::GameInfo>& games) {
const auto gameList=gameLists.find(gameListCategory);
if (gameList!=gameLists.end())
gameList->second->receiveGames(games);
});
connect(&session,&ASIP::childStatusChanged,this,[this](const ASIP::Status oldStatus,const ASIP::Status newStatus) {
if (oldStatus==ASIP::UNSTARTED && newStatus==ASIP::LIVE)
return;
refreshPage();
});
refreshPage();
for (auto& gameList:gameLists)
vBoxLayout.addLayout(gameList.second.get());
}
Server::~Server()
{
QNetworkReply* const networkReply=session.logout(nullptr);
if (networkReply!=nullptr)
connect(networkReply,&QNetworkReply::finished,networkReply,&QNetworkReply::deleteLater);
}
void Server::refreshPage() const
{
session.state();
}
void Server::enterGame(const ASIP::GameInfo& game,const Side role,const Side viewpoint)
{
session.enterGame(this,game.id,role,[=](QNetworkReply* const networkReply) {
connect(networkReply,&QNetworkReply::finished,this,[=] {
try {
addGame(*networkReply,viewpoint);
if (role!=NO_SIDE && game.players[role].isEmpty())
refreshPage();
}
catch (const std::exception& exception) {
MessageBox(QMessageBox::Critical,tr("Error opening game"),exception.what(),QMessageBox::NoButton,this).exec();
refreshPage();
}
});
});
}
void Server::addGame(QNetworkReply& networkReply,const Side viewpoint,const bool guaranteedUnique) const
{
mainWindow.addGame(session.getGame(networkReply),viewpoint,guaranteedUnique);
}
void Server::resizeEvent(QResizeEvent*)
{
for (const auto& gameList:gameLists) {
gameList.second.get()->tableWidget.horizontalHeader()->setStretchLastSection(false);
gameList.second.get()->tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
gameList.second.get()->tableWidget.horizontalHeader()->setStretchLastSection(true);
gameList.second.get()->tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
}
}