Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
network_manager.cpp
1#include "network_manager.h"
2
3NetworkManager::NetworkManager(QObject *parent)
4 : manager{this},
5 oauth2{&cavoke::auth::AuthenticationManager::getInstance().oauth2} {
6 gamePollingTimer = new QTimer(this);
7 gamePollingTimer->setInterval(500);
8 gamePollingTimer->callOnTimeout([this]() { getPlayState(); });
9 sessionPollingTimer = new QTimer(this);
10 sessionPollingTimer->setInterval(500);
11 sessionPollingTimer->callOnTimeout([this]() { getSessionInfo(); });
12 validationPollingTimer = new QTimer(this);
13 validationPollingTimer->setInterval(500);
14 validationPollingTimer->callOnTimeout([this]() { validateSession(); });
15 roomPollingTimer = new QTimer(this);
16 roomPollingTimer->setInterval(500);
17 roomPollingTimer->callOnTimeout([this]() { getRoomInfo(); });
18 // generate randomly for local server mode
19 queryUserId = QUuid::createUuid().toString(QUuid::WithoutBraces);
20
21 oauth2->setNetworkAccessManager(&manager);
22}
23
24void NetworkManager::getHealth() {
25 auto reply = oauth2->get(HOST.resolved(HEALTH));
26 connect(reply, &QNetworkReply::finished, this,
27 [reply, this]() { gotHealth(reply); });
28}
29
30void NetworkManager::gotHealth(QNetworkReply *reply) {
31 QString answer = reply->readAll();
32 reply->close();
33 reply->deleteLater();
34 qDebug() << answer;
35}
36
37void NetworkManager::getGamesList() {
38 auto reply = oauth2->get(HOST.resolved(GAMES_LIST));
39 connect(reply, &QNetworkReply::finished, this,
40 [reply, this]() { gotGamesList(reply); });
41}
42
43void NetworkManager::gotGamesList(QNetworkReply *reply) {
44 QByteArray answer = reply->readAll();
45 reply->close();
46 reply->deleteLater();
47 qDebug() << answer;
48 QJsonDocument response_wrapper = QJsonDocument::fromJson(answer);
49 emit finalizedGamesList(response_wrapper.array()); // What is it?
50}
51
52void NetworkManager::getGamesConfig(const QString &gameId) {
53 QUrl route =
54 HOST.resolved(GAMES).resolved(gameId + "/").resolved(GET_CONFIG);
55 route.setQuery({{"user_id", getUserId()}});
56 auto reply = oauth2->get(route);
57 connect(reply, &QNetworkReply::finished, this,
58 [reply, this]() { gotGamesConfig(reply); });
59}
60
61void NetworkManager::gotGamesConfig(QNetworkReply *reply) {
62 if (reply->error()) {
63 qDebug() << reply->errorString();
64 return;
65 }
66 QByteArray answer = reply->readAll();
67 reply->close();
68 reply->deleteLater();
69 qDebug() << answer;
70 GameInfo gameInfo;
71 gameInfo.read(QJsonDocument::fromJson(answer).object());
72 emit gotGameInfo(gameInfo);
73}
74
75void NetworkManager::getGamesClient(const QString &gameId) {
76 QUrl route =
77 HOST.resolved(GAMES).resolved(gameId + "/").resolved(GET_CLIENT);
78 route.setQuery({{"user_id", getUserId()}});
79 qDebug() << route.toString();
80 auto reply = oauth2->get(route);
81 connect(reply, &QNetworkReply::finished, this,
82 [reply, gameId, this]() { gotGamesClient(reply, gameId); });
83}
84
85void NetworkManager::gotGamesClient(QNetworkReply *reply,
86 const QString &gameId) {
87 if (reply->error()) {
88 qDebug() << reply->errorString();
89 return;
90 }
91 // Probably next part should be in some other place
92 QFile *file = new QTemporaryFile();
93 if (file->open(QFile::WriteOnly)) {
94 file->write(reply->readAll());
95 file->flush();
96 file->close();
97 }
98 emit downloadedGameFile(file, gameId);
99 // End of the part
100 reply->close();
101 reply->deleteLater();
102}
103
104void NetworkManager::sendMove(const QString &jsonMove) {
105 QUrl route =
106 HOST.resolved(PLAY).resolved(sessionId + "/").resolved(SEND_MOVE);
107 route.setQuery({{"user_id", getUserId()}});
108 qDebug() << route.toString();
109 auto reply = oauth2->post(route, jsonMove.toUtf8());
110 connect(reply, &QNetworkReply::finished, this,
111 [reply, this]() { gotPostResponse(reply); });
112}
113
114void NetworkManager::gotPostResponse(QNetworkReply *reply) {
115 QByteArray answer = reply->readAll();
116 reply->close();
117 reply->deleteLater();
118 qDebug() << "Got some post response: " << answer;
119}
120
121void NetworkManager::getPlayState() {
122 QUrl route =
123 HOST.resolved(PLAY).resolved(sessionId + "/").resolved(GET_STATE);
124 route.setQuery({{"user_id", getUserId()}});
125 qDebug() << route.toString();
126 auto reply = oauth2->get(route);
127 connect(reply, &QNetworkReply::finished, this,
128 [reply, this]() { gotPlayState(reply); });
129}
130
131void NetworkManager::gotPlayState(QNetworkReply *reply) {
132 if (reply->error()) {
133 qDebug() << reply->errorString();
134 return;
135 }
136 QString answer = reply->readAll();
137 reply->close();
138 reply->deleteLater();
139 qDebug() << answer;
140 emit gotGameUpdate(answer);
141}
142
143void NetworkManager::joinSession(const QString &sessionId) {
144 QUrl route =
145 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(JOIN);
146 route.setQuery({{"user_id", getUserId()}});
147 qDebug() << route.toString();
148 auto reply = oauth2->post(route, "{}");
149 connect(reply, &QNetworkReply::finished, this,
150 [reply, this]() { gotSession(reply); });
151}
152
153void NetworkManager::gotSession(QNetworkReply *reply) {
154 QByteArray answer = reply->readAll();
155 if (reply->error()) {
156 qDebug() << "Session ERROR:" << reply->errorString();
157 }
158 reply->close();
159 reply->deleteLater();
160 qDebug() << "Got session: " << answer;
161
162 SessionInfo sessionInfo;
163 sessionInfo.read(QJsonDocument::fromJson(answer).object());
164 sessionId = sessionInfo.session_id;
165 sessionInfo.isHost =
166 sessionInfo.host_id ==
167 queryUserId; // using explicitly query user_id, as in prod mode it is
168 // returned from the server
169
170 emit gotSessionInfo(sessionInfo);
171}
172
173void NetworkManager::validateSession() {
174 QUrl route =
175 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(VALIDATE);
176 route.setQuery({{"user_id", getUserId()}});
177 qDebug() << route.toString();
178 auto reply = oauth2->post(route, "{}");
179 connect(reply, &QNetworkReply::finished, this,
180 [reply, this]() { gotValidatedSession(reply); });
181}
182
183void NetworkManager::gotValidatedSession(QNetworkReply *reply) {
184 QByteArray answer = reply->readAll();
185 reply->close();
186 reply->deleteLater();
187 // qDebug() << answer;
188
189 ValidationResult validationResult;
190 validationResult.read(QJsonDocument::fromJson(answer).object());
191
192 emit gotValidationResult(validationResult);
193}
194
195void NetworkManager::getSessionInfo() {
196 QUrl route =
197 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(GET_INFO);
198 route.setQuery({{"user_id", getUserId()}});
199 qDebug() << route.toString();
200 auto reply = oauth2->get(route);
201 connect(reply, &QNetworkReply::finished, this,
202 [reply, this]() { gotSession(reply); });
203}
204
205void NetworkManager::getSessionInfo(const QString &other_sessionId) {
206 QUrl route = HOST.resolved(SESSIONS)
207 .resolved(other_sessionId + "/")
208 .resolved(GET_INFO);
209 route.setQuery({{"user_id", getUserId()}});
210 qDebug() << route.toString();
211 auto reply = oauth2->get(route);
212 connect(reply, &QNetworkReply::finished, this,
213 [reply, this]() { gotSession(reply); });
214}
215
216void NetworkManager::startSession() {
217 QUrl route =
218 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(START);
219 route.setQuery({{"user_id", getUserId()}});
220 qDebug() << route.toString();
221 auto reply = oauth2->post(route, "{}");
222 connect(reply, &QNetworkReply::finished, this,
223 [reply, this]() { gotPostResponse(reply); });
224}
225
226void NetworkManager::leaveSession() {
227 QUrl route =
228 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(LEAVE);
229 route.setQuery({{"user_id", getUserId()}});
230 qDebug() << route.toString();
231 auto reply = oauth2->post(route, "{}");
232 connect(reply, &QNetworkReply::finished, this,
233 [reply, this]() { gotPostResponse(reply); });
234}
235
236void NetworkManager::changeRoleInSession(int newRole) {
237 QUrl route =
238 HOST.resolved(SESSIONS).resolved(sessionId + "/").resolved(CHANGE_ROLE);
239 route.setQuery(
240 {{"user_id", getUserId()}, {"new_role", QString::number(newRole)}});
241 qDebug() << route.toString();
242 auto reply = oauth2->post(route, "{}");
243 connect(reply, &QNetworkReply::finished, this,
244 [reply, this]() { gotPostResponse(reply); });
245}
246
247void NetworkManager::getRoomInfo() {
248 QUrl route = HOST.resolved(ROOMS).resolved(roomId + "/").resolved(GET_INFO);
249 route.setQuery({{"user_id", getUserId()}});
250 qDebug() << route.toString();
251 auto reply = oauth2->get(route);
252 connect(reply, &QNetworkReply::finished, this,
253 [reply, this]() { gotRoom(reply); });
254}
255
256void NetworkManager::gotRoom(QNetworkReply *reply) {
257 QByteArray answer = reply->readAll();
258 reply->close();
259 reply->deleteLater();
260 qDebug() << "Got room: " << answer;
261
262 RoomInfo roomInfo;
263 roomInfo.read(QJsonDocument::fromJson(answer).object());
264 roomId = roomInfo.room_id;
265 roomInfo.isHost = roomInfo.host_id == queryUserId;
266
267 emit gotRoomInfo(roomInfo);
268}
269
270void NetworkManager::createRoom(const QString &display_name) {
271 QUrl route = HOST.resolved(ROOMS_CREATE);
272 route.setQuery({{"display_name", display_name}, {"user_id", getUserId()}});
273 qDebug() << route.toString();
274 auto reply = oauth2->post(route, "{}");
275 connect(reply, &QNetworkReply::finished, this,
276 [reply, this]() { gotRoom(reply); });
277}
278
279void NetworkManager::joinRoom(const QString &inviteCode) {
280 QUrl route = HOST.resolved(ROOMS_JOIN);
281 route.setQuery({{"user_id", getUserId()}, {"invite_code", inviteCode}});
282 qDebug() << route.toString();
283 auto reply = oauth2->post(route, "{}");
284 connect(reply, &QNetworkReply::finished, this,
285 [reply, this]() { gotRoom(reply); });
286}
287
288void NetworkManager::leaveRoom() {
289 QUrl route = HOST.resolved(ROOMS).resolved(roomId + "/").resolved(LEAVE);
290 route.setQuery({{"user_id", getUserId()}});
291 qDebug() << route.toString();
292 auto reply = oauth2->post(route, "{}");
293 connect(reply, &QNetworkReply::finished, this,
294 [reply, this]() { gotPostResponse(reply); });
295}
296
297void NetworkManager::roomCreateSession(const QString &game_id) {
298 QUrl route =
299 HOST.resolved(ROOMS).resolved(roomId + "/").resolved(CREATE_SESSION);
300 route.setQuery({{"user_id", getUserId()}, {"game_id", game_id}});
301 auto reply = oauth2->post(route, "{}");
302 connect(reply, &QNetworkReply::finished, this,
303 [reply, this]() { gotSession(reply); });
304}
305
306void NetworkManager::getMe() {
307 QUrl route = HOST.resolved(PROFILE).resolved(GET_ME);
308 route.setQuery({{"user_id", getUserId()}});
309 qDebug() << route.toString();
310 auto reply = oauth2->get(route);
311 connect(reply, &QNetworkReply::finished, this,
312 [reply, this]() { gotMyself(reply); });
313}
314
315void NetworkManager::gotMyself(QNetworkReply *reply) {
316 QByteArray answer = reply->readAll();
317 reply->close();
318 reply->deleteLater();
319 qDebug() << "Got my profile: " << answer;
320
321 User userInfo;
322 userInfo.read(QJsonDocument::fromJson(answer).object());
323 // update my user id used in query params
324 queryUserId = userInfo.user_id;
325 emit gotDisplayName(userInfo.display_name);
326}
327
328void NetworkManager::changeName(const QString &new_name) {
329 QUrl route = HOST.resolved(PROFILE).resolved(CHANGE_NAME);
330 route.setQuery({{"user_id", getUserId()}, {"new_name", new_name}});
331 qDebug() << route.toString();
332 auto reply = oauth2->post(route);
333 connect(reply, &QNetworkReply::finished, this,
334 [reply, this]() { gotPostResponse(reply); });
335}
336
337void NetworkManager::getMyUserStatistics() {
338 QUrl route = HOST.resolved(PROFILE).resolved(MY_USER_STATISTICS);
339 route.setQuery({{"user_id", getUserId()}});
340 qDebug() << route.toString();
341 auto reply = oauth2->get(route);
342 connect(reply, &QNetworkReply::finished, this,
343 [reply, this]() { gotUserStatistics(reply); });
344}
345
346void NetworkManager::gotUserStatistics(QNetworkReply *reply) {
347 QByteArray answer = reply->readAll();
348 reply->close();
349 reply->deleteLater();
350 qDebug() << "Got my user statistics: " << answer;
351
352 UserStatistics userStatistics;
353 userStatistics.read(QJsonDocument::fromJson(answer).object());
354 emit gotUserStatistics(userStatistics);
355}
356
357void NetworkManager::getMyUserGameStatistics(const QString &gameId) {
358 QUrl route = HOST.resolved(PROFILE)
359 .resolved(MY_USER_GAME_STATISTICS)
360 .resolved(gameId);
361 route.setQuery({{"user_id", getUserId()}});
362 qDebug() << route.toString();
363 auto reply = oauth2->get(route);
364 connect(reply, &QNetworkReply::finished, this,
365 [reply, this]() { gotUserGameStatistics(reply); });
366}
367
368void NetworkManager::gotUserGameStatistics(QNetworkReply *reply) {
369 QByteArray answer = reply->readAll();
370 reply->close();
371 reply->deleteLater();
372 qDebug() << "Got my user game statistics: " << answer;
373
374 UserGameStatistics userGameStatistics;
375 userGameStatistics.read(QJsonDocument::fromJson(answer).object());
376 emit gotUserGameStatistics(userGameStatistics);
377}
378
379void NetworkManager::getGameStatistics(const QString &gameId) {
380 QUrl route = HOST.resolved(STATISTICS_GAME).resolved(gameId);
381 route.setQuery({{"user_id", getUserId()}});
382 qDebug() << route.toString();
383 auto reply = oauth2->get(route);
384 connect(reply, &QNetworkReply::finished, this,
385 [reply, this]() { gotGameStatistics(reply); });
386}
387
388void NetworkManager::gotGameStatistics(QNetworkReply *reply) {
389 QByteArray answer = reply->readAll();
390 reply->close();
391 reply->deleteLater();
392 qDebug() << "Got game statistics: " << answer;
393
394 GameStatistics gameStatistics;
395 gameStatistics.read(QJsonDocument::fromJson(answer).object());
396 emit gotGameStatistics(gameStatistics);
397}
398
399void NetworkManager::startGamePolling() {
400 gamePollingTimer->start();
401}
402
403void NetworkManager::stopGamePolling() {
404 gamePollingTimer->stop();
405}
406
407void NetworkManager::startSessionPolling() {
408 sessionPollingTimer->start();
409}
410void NetworkManager::stopSessionPolling() {
411 sessionPollingTimer->stop();
412}
413void NetworkManager::startValidationPolling() {
414 validationPollingTimer->start();
415}
416
417void NetworkManager::stopValidationPolling() {
418 validationPollingTimer->stop();
419}
420
421void NetworkManager::startRoomPolling() {
422 roomPollingTimer->start();
423}
424
425void NetworkManager::stopRoomPolling() {
426 roomPollingTimer->stop();
427}
428void NetworkManager::changeHost(const QUrl &newHost) {
429 HOST = newHost;
430 getGamesList();
431}
432QString NetworkManager::getUserId() {
433 return queryUserId;
434}
Definition: user.h:6