1#include "network_manager.h"
3NetworkManager::NetworkManager(QObject *parent)
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(); });
19 queryUserId = QUuid::createUuid().toString(QUuid::WithoutBraces);
21 oauth2->setNetworkAccessManager(&manager);
24void NetworkManager::getHealth() {
25 auto reply = oauth2->get(HOST.resolved(HEALTH));
26 connect(reply, &QNetworkReply::finished,
this,
27 [reply,
this]() { gotHealth(reply); });
30void NetworkManager::gotHealth(QNetworkReply *reply) {
31 QString answer = reply->readAll();
37void NetworkManager::getGamesList() {
38 auto reply = oauth2->get(HOST.resolved(GAMES_LIST));
39 connect(reply, &QNetworkReply::finished,
this,
40 [reply,
this]() { gotGamesList(reply); });
43void NetworkManager::gotGamesList(QNetworkReply *reply) {
44 QByteArray answer = reply->readAll();
48 QJsonDocument response_wrapper = QJsonDocument::fromJson(answer);
49 emit finalizedGamesList(response_wrapper.array());
52void NetworkManager::getGamesConfig(
const QString &gameId) {
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); });
61void NetworkManager::gotGamesConfig(QNetworkReply *reply) {
63 qDebug() << reply->errorString();
66 QByteArray answer = reply->readAll();
71 gameInfo.read(QJsonDocument::fromJson(answer).
object());
72 emit gotGameInfo(gameInfo);
75void NetworkManager::getGamesClient(
const QString &gameId) {
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); });
85void NetworkManager::gotGamesClient(QNetworkReply *reply,
86 const QString &gameId) {
88 qDebug() << reply->errorString();
92 QFile *file =
new QTemporaryFile();
93 if (file->open(QFile::WriteOnly)) {
94 file->write(reply->readAll());
98 emit downloadedGameFile(file, gameId);
101 reply->deleteLater();
104void NetworkManager::sendMove(
const QString &jsonMove) {
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); });
114void NetworkManager::gotPostResponse(QNetworkReply *reply) {
115 QByteArray answer = reply->readAll();
117 reply->deleteLater();
118 qDebug() <<
"Got some post response: " << answer;
121void NetworkManager::getPlayState() {
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); });
131void NetworkManager::gotPlayState(QNetworkReply *reply) {
132 if (reply->error()) {
133 qDebug() << reply->errorString();
136 QString answer = reply->readAll();
138 reply->deleteLater();
140 emit gotGameUpdate(answer);
143void NetworkManager::joinSession(
const QString &sessionId) {
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); });
153void NetworkManager::gotSession(QNetworkReply *reply) {
154 QByteArray answer = reply->readAll();
155 if (reply->error()) {
156 qDebug() <<
"Session ERROR:" << reply->errorString();
159 reply->deleteLater();
160 qDebug() <<
"Got session: " << answer;
163 sessionInfo.read(QJsonDocument::fromJson(answer).
object());
164 sessionId = sessionInfo.session_id;
166 sessionInfo.host_id ==
170 emit gotSessionInfo(sessionInfo);
173void NetworkManager::validateSession() {
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); });
183void NetworkManager::gotValidatedSession(QNetworkReply *reply) {
184 QByteArray answer = reply->readAll();
186 reply->deleteLater();
190 validationResult.read(QJsonDocument::fromJson(answer).
object());
192 emit gotValidationResult(validationResult);
195void NetworkManager::getSessionInfo() {
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); });
205void NetworkManager::getSessionInfo(
const QString &other_sessionId) {
206 QUrl route = HOST.resolved(SESSIONS)
207 .resolved(other_sessionId +
"/")
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); });
216void NetworkManager::startSession() {
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); });
226void NetworkManager::leaveSession() {
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); });
236void NetworkManager::changeRoleInSession(
int newRole) {
238 HOST.resolved(SESSIONS).resolved(sessionId +
"/").resolved(CHANGE_ROLE);
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); });
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); });
256void NetworkManager::gotRoom(QNetworkReply *reply) {
257 QByteArray answer = reply->readAll();
259 reply->deleteLater();
260 qDebug() <<
"Got room: " << answer;
263 roomInfo.read(QJsonDocument::fromJson(answer).
object());
264 roomId = roomInfo.room_id;
265 roomInfo.isHost = roomInfo.host_id == queryUserId;
267 emit gotRoomInfo(roomInfo);
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); });
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); });
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); });
297void NetworkManager::roomCreateSession(
const QString &game_id) {
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); });
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); });
315void NetworkManager::gotMyself(QNetworkReply *reply) {
316 QByteArray answer = reply->readAll();
318 reply->deleteLater();
319 qDebug() <<
"Got my profile: " << answer;
322 userInfo.read(QJsonDocument::fromJson(answer).
object());
324 queryUserId = userInfo.user_id;
325 emit gotDisplayName(userInfo.display_name);
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); });
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); });
346void NetworkManager::gotUserStatistics(QNetworkReply *reply) {
347 QByteArray answer = reply->readAll();
349 reply->deleteLater();
350 qDebug() <<
"Got my user statistics: " << answer;
353 userStatistics.read(QJsonDocument::fromJson(answer).
object());
354 emit gotUserStatistics(userStatistics);
357void NetworkManager::getMyUserGameStatistics(
const QString &gameId) {
358 QUrl route = HOST.resolved(PROFILE)
359 .resolved(MY_USER_GAME_STATISTICS)
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); });
368void NetworkManager::gotUserGameStatistics(QNetworkReply *reply) {
369 QByteArray answer = reply->readAll();
371 reply->deleteLater();
372 qDebug() <<
"Got my user game statistics: " << answer;
375 userGameStatistics.read(QJsonDocument::fromJson(answer).
object());
376 emit gotUserGameStatistics(userGameStatistics);
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); });
388void NetworkManager::gotGameStatistics(QNetworkReply *reply) {
389 QByteArray answer = reply->readAll();
391 reply->deleteLater();
392 qDebug() <<
"Got game statistics: " << answer;
395 gameStatistics.read(QJsonDocument::fromJson(answer).
object());
396 emit gotGameStatistics(gameStatistics);
399void NetworkManager::startGamePolling() {
400 gamePollingTimer->start();
403void NetworkManager::stopGamePolling() {
404 gamePollingTimer->stop();
407void NetworkManager::startSessionPolling() {
408 sessionPollingTimer->start();
410void NetworkManager::stopSessionPolling() {
411 sessionPollingTimer->stop();
413void NetworkManager::startValidationPolling() {
414 validationPollingTimer->start();
417void NetworkManager::stopValidationPolling() {
418 validationPollingTimer->stop();
421void NetworkManager::startRoomPolling() {
422 roomPollingTimer->start();
425void NetworkManager::stopRoomPolling() {
426 roomPollingTimer->stop();
428void NetworkManager::changeHost(
const QUrl &newHost) {
432QString NetworkManager::getUserId() {