-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharimaa_com.cpp
248 lines (218 loc) · 8.15 KB
/
arimaa_com.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
#include <QNetworkReply>
#include <QNetworkCookieJar>
#include <QNetworkCookie>
#include <QCheckBox>
#include <QDesktopServices>
#include "arimaa_com.hpp"
#include "asip1.hpp"
#include "asip2.hpp"
#include "server.hpp"
#include "io.hpp"
#include "bots.hpp"
template<class Base>
Arimaa_com<Base>::Arimaa_com(QNetworkAccessManager& networkAccessManager,const QString& serverURL,QObject* const parent,typename Base::Data startingData) :
Base(networkAccessManager,serverURL,parent,startingData),
ratedMode(true)
{
Base::connect(this,&Base::sendGameList,[this](const typename Base::GameListCategory gameListCategory,const std::vector<typename Base::GameInfo>& games) {
auto& fixedCatGames=fixedGames[gameListCategory];
fixedCatGames.clear();
const bool alreadyJoined=(gameListCategory!=Base::INVITED_GAMES && gameListCategory!=Base::OPEN_GAMES);
for (const auto& game:games) {
if (alreadyJoined || !game.rated || std::none_of(std::begin(game.players),std::end(game.players),isBotName))
fixedCatGames.emplace(game.id);
}
});
}
template<class Base>
std::unique_ptr<::ASIP> Arimaa_com<Base>::create(QNetworkAccessManager& networkAccessManager,const QString& serverURL,QObject* const parent,typename Base::Data startingData) const
{
using namespace std;
return make_unique<Arimaa_com<Base> >(networkAccessManager,serverURL,parent,startingData);
}
template<class Base>
typename Base::Data Arimaa_com<Base>::processReply(QNetworkReply& networkReply)
{
return Base::processReply(networkReply);
}
template<>
typename ASIP::Data Arimaa_com<ASIP2>::processReply(QNetworkReply& networkReply)
{
try {
return ASIP::processReply(networkReply);
}
catch (const std::exception&) {
if (networkReply.property("action")=="cancelopengame") // SERVER BUG: Cancelling with ASIP 2.0 triggers error.
return ASIP::Data({{"ok","1"}});
throw;
}
}
template<class Base>
void Arimaa_com<Base>::enterGame(QObject* const requester,const QString& gameID,const Side side,const std::function<void(QNetworkReply*)> networkReplyAction)
{
Base::enterGame(requester,gameID,side,networkReplyAction);
}
template<>
void Arimaa_com<ASIP2>::enterGame(QObject* const requester,const QString& gameID,const Side side,const std::function<void(QNetworkReply*)> networkReplyAction)
{
if (side==NO_SIDE) { // SERVER BUG: Can't spectate with ASIP 2.0.
QReadLocker readLocker(&mostRecentData_mutex);
ASIP1 asip1(networkAccessManager,serverDirectory().toString()+"client1gr.cgi",nullptr,mostRecentData);
readLocker.unlock();
asip1.enterGame(requester,gameID,side,networkReplyAction);
synchronizeData(asip1);
return;
}
else if (possiblyDerateable(gameID)) {
const auto possible=doMeDependingAction([=] {
QReadLocker readLocker(&mostRecentData_mutex);
const auto me=mostRecentData.value("me").value<QVariantHash>();
const auto sid=mostRecentData.value("sid");
readLocker.unlock();
const auto server=serverDirectory();
const auto cookieJar=networkAccessManager.cookieJar();
const auto oldCookies=cookieJar->cookiesForUrl(server);
cookieJar->setCookiesFromUrl({QNetworkCookie("unrated"+me["id"].toByteArray(),"1"),QNetworkCookie("sid",sid.toByteArray())},server);
const auto networkReply=networkAccessManager.get(QNetworkRequest(QUrl(server.toString()+"opengamewin.cgi?gameid="+gameID+"&side="+QString(toLetter(side)))));
networkReply->setParent(this);
for (const auto& cookie:cookieJar->cookiesForUrl(server))
cookieJar->deleteCookie(cookie);
cookieJar->setCookiesFromUrl(oldCookies,server);
connect(networkReply,&QNetworkReply::finished,this,[=] {
networkReply->deleteLater();
ASIP::enterGame(requester,gameID,side,networkReplyAction);
});
});
if (possible)
return;
}
ASIP::enterGame(requester,gameID,side,networkReplyAction);
}
template<class Base>
std::unique_ptr<::ASIP> Arimaa_com<Base>::getGame(QNetworkReply& networkReply)
{
return Base::getGame(networkReply);
}
template<>
std::unique_ptr<::ASIP> Arimaa_com<ASIP2>::getGame(QNetworkReply& networkReply)
{
if (networkReply.property("role")=="v" && networkReply.property("action")=="reserveseat") { // SERVER BUG: Can't spectate with ASIP 2.0.
QReadLocker readLocker(&mostRecentData_mutex);
ASIP1 asip1(networkAccessManager,serverDirectory().toString()+"client1gr.cgi",nullptr,mostRecentData);
readLocker.unlock();
asip1.processReply(networkReply);
synchronizeData(asip1);
}
else
processReply(networkReply);
QWriteLocker writeLocker(&mostRecentData_mutex);
auto gsurl=mostRecentData.value("gsurl").toString();
if (QUrl(gsurl).isRelative()) // SERVER BUG: Joining with ASIP 2.0 returns partial game server URL.
gsurl=serverDirectory().toString()+"../java/ys/ms4/v5/"+gsurl;
gsurl=QUrl(gsurl).adjusted(QUrl::RemoveFilename).toString()+"client2gs.cgi"; // SERVER BUG: Returned game server always uses ASIP 1.0.
mostRecentData.insert("gsurl",gsurl);
writeLocker.unlock();
return ASIP::getGame();
}
template<class Base>
bool Arimaa_com<Base>::possiblyDerateable(const QString& gameID) const
{
if (ratedMode)
return false;
for (const auto& category:fixedGames)
if (category.second.find(gameID)!=category.second.end())
return false;
return true;
}
template<class Base>
std::unique_ptr<QWidget> Arimaa_com<Base>::siteWidget(Server& server)
{
const auto mainLayout=new QHBoxLayout;
mainLayout->addWidget(botWidget(server.mainWindow).release());
if (std::is_same<Base,ASIP2>::value) {
const auto ratedBox=new QCheckBox("Keep joined bot games rated");
mainLayout->addWidget(ratedBox,0,Qt::AlignCenter);
ratedBox->setChecked(ratedMode);
Base::connect(ratedBox,&QCheckBox::toggled,[this](const bool checked) {ratedMode=checked;});
mainLayout->addWidget(chatroomWidget().release());
}
using namespace std;
auto widget=make_unique<QWidget>();
widget->setLayout(mainLayout);
return widget;
}
template<class Base>
bool Arimaa_com<Base>::isBotName(const QString& playerName)
{
return playerName.indexOf("bot_")==0;
}
template<class Base>
QUrl Arimaa_com<Base>::serverDirectory() const
{
return Base::serverURL().adjusted(QUrl::RemoveFilename);
}
template<class Base> template<class Function>
bool Arimaa_com<Base>::doMeDependingAction(Function action)
{
static_cast<void>(action);
return false;
}
template<> template<class Function>
bool Arimaa_com<ASIP2>::doMeDependingAction(Function action)
{
QReadLocker readLocker(&mostRecentData_mutex);
if (mostRecentData.contains("me")) {
readLocker.unlock();
action();
}
else {
readLocker.unlock();
const auto networkReplies=state();
connect(networkReplies.front(),&QNetworkReply::finished,this,[this,action] {
const QReadLocker readLocker(&mostRecentData_mutex);
action();
});
}
return true;
}
template<class Base>
std::unique_ptr<QWidget> Arimaa_com<Base>::chatroomWidget()
{
return nullptr;
}
template<>
std::unique_ptr<QWidget> Arimaa_com<ASIP2>::chatroomWidget()
{
using namespace std;
auto chatroom=make_unique<QPushButton>("Enter chat room");
connect(chatroom.get(),&QPushButton::clicked,this,[this] {
doMeDependingAction([this] {
const auto me=mostRecentData.value("me").value<QVariantHash>();
const auto url=serverDirectory().toString()+"../chat/chat.php?user="+username()+"&auth="+me["auth"].toString();
QDesktopServices::openUrl(url);
});
});
return chatroom;
}
template<class Base>
std::unique_ptr<QWidget> Arimaa_com<Base>::botWidget(MainWindow& mainWindow)
{
using namespace std;
auto bots=make_unique<QPushButton>("Bot launcher");
const auto url=serverDirectory().toString()+"botLadderAll.cgi";
Base::connect(bots.get(),&QPushButton::clicked,this,[this,url,&mainWindow] {
#if 1
new Bots(*this,url+"?u="+Base::username(),mainWindow);
#else
const bool possible=doMeDependingAction([this,url,&mainWindow] {
const auto me=Base::mostRecentData.value("me").template value<QVariantHash>();
new Bots(*this,url+"?u="+me["id"].toString(),mainWindow);
});
if (!possible)
new Bots(*this,url,mainWindow);
#endif
});
return bots;
}
template class Arimaa_com<ASIP1>;
template class Arimaa_com<ASIP2>;