-
Notifications
You must be signed in to change notification settings - Fork 2
/
gamelist.cpp
145 lines (132 loc) · 5.08 KB
/
gamelist.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
#include <QHeaderView>
#include <QNetworkReply>
#include <QMenu>
#include "gamelist.hpp"
#include "server.hpp"
#include "io.hpp"
#include "messagebox.hpp"
GameList::GameList(Server* const server,const QString& labelText) :
description(labelText)
{
addWidget(&description,0,1,Qt::AlignCenter);
addWidget(&lastUpdated,0,2,Qt::AlignRight);
QStringList m_TableHeader;
m_TableHeader<<sideWord(FIRST_SIDE)<<sideWord(SECOND_SIDE)<<tr("Time control");
tableWidget.setColumnCount(3);
tableWidget.setHorizontalHeaderLabels(m_TableHeader);
tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableWidget.horizontalHeader()->setStretchLastSection(true);
tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
tableWidget.verticalHeader()->setVisible(false);
tableWidget.setShowGrid(false);
tableWidget.setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget.setFocusPolicy(Qt::NoFocus);
tableWidget.setSelectionMode(QAbstractItemView::NoSelection);
const int height=tableWidget.horizontalHeader()->height()+2;
tableWidget.setMaximumHeight(height);
tableWidget.setCursor(Qt::PointingHandCursor);
addWidget(&tableWidget,1,0,3,0,Qt::AlignTop);
connect(&tableWidget,&QTableWidget::itemClicked,this,[=](const QTableWidgetItem* item){
const auto& game=games[item->row()];
const auto column=item->column();
const auto& self=server->session.username();
const auto& players=game.players;
Side role=NO_SIDE;
const Side selectedSide=(column<NUM_SIDES ? static_cast<Side>(column) : NO_SIDE);
if (selectedSide==NO_SIDE) {
for (Side side=FIRST_SIDE;side<NUM_SIDES;increment(side))
if (self==players[side]) {
role=side;
break;
}
}
else {
const auto& playerName=players[selectedSide];
if (playerName.isEmpty() || self==playerName)
role=selectedSide;
else {
const Side nonselectedSide=otherSide(selectedSide);
if (self==players[nonselectedSide])
role=nonselectedSide;
}
}
Side viewpoint;
if (selectedSide!=NO_SIDE)
viewpoint=selectedSide;
else if (role==NO_SIDE)
viewpoint=FIRST_SIDE;
else
viewpoint=role;
server->enterGame(game,role,viewpoint);
});
tableWidget.setContextMenuPolicy(Qt::CustomContextMenu);
connect(&tableWidget,&QTableWidget::customContextMenuRequested,this,[=](const QPoint pos){
QModelIndex index=tableWidget.indexAt(pos);
const auto& game=games[index.row()];
bool hasEmptySeat=false;
bool hasOtherPlayer=false;
for (const auto& player:game.players) {
if (player.isEmpty())
hasEmptySeat=true;
else if (player!=server->session.username())
hasOtherPlayer=true;
}
auto menu=new QMenu(server);
menu->setAttribute(Qt::WA_DeleteOnClose);
const auto spectateGame=new QAction("Open as spectator",menu);
const auto column=index.column();
const auto viewpoint=(column<NUM_SIDES ? static_cast<Side>(column) : FIRST_SIDE);
connect(spectateGame,&QAction::triggered,this,[=] {
server->enterGame(game,NO_SIDE,viewpoint);
});
menu->addAction(spectateGame);
if (!hasOtherPlayer && hasEmptySeat) {
const auto cancelGame=new QAction("Cancel",menu);
connect(cancelGame,&QAction::triggered,this,[=] {
const auto networkReply=server->session.cancelGame(this,game.id);
connect(networkReply,&QNetworkReply::finished,this,[=] {
try {
server->session.processReply(*networkReply);
}
catch (const std::exception& exception) {
MessageBox(QMessageBox::Critical,tr("Error cancelling game"),exception.what(),QMessageBox::NoButton,server).exec();
}
server->refreshPage();
});
});
menu->addAction(cancelGame);
}
menu->popup(tableWidget.viewport()->mapToGlobal(pos));
});
}
void GameList::receiveGames(std::vector<ASIP::GameInfo> games_)
{
games.swap(games_);
tableWidget.setRowCount(static_cast<int>(games.size()));
unsigned int gameIndex=0;
for (const auto& game:games) {
for (Side side=FIRST_SIDE;side<NUM_SIDES;increment(side)) {
const auto& playerName=game.players[side];
auto playerItem=new QTableWidgetItem(playerName);
playerItem->setTextAlignment(Qt::AlignCenter);
if (playerName.isEmpty()) {
playerItem->setText("join");
auto font=playerItem->font();
font.setItalic(true);
font.setBold(true);
playerItem->setFont(font);
}
tableWidget.setItem(gameIndex,side==FIRST_SIDE ? 0 : 1,playerItem);
}
auto timeControl=new QTableWidgetItem(game.timeControl);
timeControl->setTextAlignment(Qt::AlignCenter);
timeControl->setFlags(timeControl->flags()&~Qt::ItemIsSelectable);
tableWidget.setItem(gameIndex,2,timeControl);
++gameIndex;
}
int height=tableWidget.horizontalHeader()->height()+2;
for (int i=0;i<tableWidget.rowCount();++i)
height+=tableWidget.rowHeight(i);
tableWidget.setMaximumHeight(height);
lastUpdated.setText(tr("Last updated: ")+QDateTime::currentDateTime().toString("HH:mm:ss"));
}