1#include "cavokeclientcontroller.h"
2#include <QOAuthHttpServerReplyHandler>
4CavokeClientController::CavokeClientController(QObject *parent)
7 networkManager{parent},
21 connect(&testWindowView, SIGNAL(shownStartView()),
this,
22 SLOT(showStartView()));
23 connect(&joinGameView, SIGNAL(shownStartView()),
this,
24 SLOT(showStartView()));
25 connect(&createGameView, SIGNAL(shownStartView()),
this,
26 SLOT(showStartView()));
27 connect(&gamesListView, SIGNAL(shownStartView()),
this,
28 SLOT(showStartView()));
29 connect(&settingsView, SIGNAL(shownStartView()),
this,
30 SLOT(showStartView()));
31 connect(&statisticsView, SIGNAL(shownStartView()),
this,
32 SLOT(showStartView()));
33 connect(&roomView, SIGNAL(shownStartView()),
this, SLOT(showStartView()));
36 connect(&startView, SIGNAL(shownTestWindowView()),
this,
37 SLOT(showTestWindowView()));
38 connect(&startView, SIGNAL(shownJoinGameView()),
this,
39 SLOT(showJoinGameView()));
40 connect(&startView, SIGNAL(shownCreateGameView()),
this,
41 SLOT(showCreateGameView()));
42 connect(&startView, SIGNAL(shownGamesListView()),
this,
43 SLOT(showGamesListView()));
44 connect(&startView, SIGNAL(shownSettingsView()),
this,
45 SLOT(showSettingsView()));
46 connect(&startView, SIGNAL(shownStatisticsView()),
this,
47 SLOT(showStatisticsView()));
50 connect(&startView, SIGNAL(clickedExitButton()),
this,
51 SLOT(exitApplication()), Qt::QueuedConnection);
54 connect(&testWindowView, SIGNAL(startGame(QString)), &model,
55 SLOT(loadQmlGame(QString)));
56 connect(&testWindowView, SIGNAL(testHealthConnectionButton()),
57 &networkManager, SLOT(getHealth()));
60 connect(&createGameView, SIGNAL(startedCreateGameRoutine(QString)),
this,
61 SLOT(createGameStart(QString)));
64 connect(&joinGameView, SIGNAL(joinedGame(QString)),
this,
65 SLOT(joinGameStart(QString)));
68 connect(&gamesListView, SIGNAL(currentIndexChanged(
int)), &model,
69 SLOT(receivedGameIndexChangeInList(
int)));
70 connect(&gamesListView, SIGNAL(requestGameStatistics(QString)),
71 &networkManager, SLOT(getGameStatistics(QString)));
72 connect(&model, SIGNAL(updateSelectedGameInList(
GameInfo)), &gamesListView,
74 connect(&networkManager, SIGNAL(gotGameStatistics(
GameStatistics)),
76 connect(&gamesListView, SIGNAL(requestedDownloadGame(
int)), &model,
77 SLOT(gotIndexToDownload(
int)));
78 connect(
this, SIGNAL(clearScreens()), &gamesListView, SLOT(displayEmpty()));
81 connect(&networkManager, SIGNAL(gotSessionInfo(
SessionInfo)),
this,
83 connect(&networkManager, SIGNAL(gotGameInfo(
GameInfo)),
this,
85 connect(
this, SIGNAL(setGameName(QString)), &sessionView,
86 SLOT(updateGameName(QString)));
87 connect(&networkManager, SIGNAL(gotRoomInfo(
RoomInfo)),
this,
91 connect(
this, SIGNAL(loadGamesList()), &networkManager,
92 SLOT(getGamesList()));
93 connect(&networkManager, SIGNAL(finalizedGamesList(QJsonArray)), &model,
94 SLOT(updateGamesList(QJsonArray)));
95 connect(&model, SIGNAL(gamesListUpdated(std::vector<GameInfo>)),
96 &gamesListView, SLOT(gotGamesListUpdate(std::vector<GameInfo>)));
97 connect(&model, SIGNAL(gamesListUpdated(std::vector<GameInfo>)),
98 &statisticsView, SLOT(gotGamesListUpdate(std::vector<GameInfo>)));
99 connect(&model, SIGNAL(gamesListUpdated(std::vector<GameInfo>)), &roomView,
100 SLOT(gotGamesListUpdate(std::vector<GameInfo>)));
103 connect(&model, SIGNAL(downloadGame(QString)), &networkManager,
104 SLOT(getGamesClient(QString)));
105 connect(&networkManager, SIGNAL(downloadedGameFile(QFile *, QString)),
this,
106 SLOT(unpackDownloadedQml(QFile *, QString)));
109 connect(&roomView, SIGNAL(leftRoom()),
this, SLOT(leftRoom()));
110 connect(&roomView, SIGNAL(createdSession(QString)),
this,
111 SLOT(createSessionStart(QString)));
112 connect(&roomView, SIGNAL(joinedSession(QString)),
this,
113 SLOT(joinSessionStart(QString)));
114 connect(&roomView, SIGNAL(requestedSessionUpdate(QString)), &networkManager,
115 SLOT(getSessionInfo(QString)));
118 connect(&sessionView, SIGNAL(joinedCreatedGame()),
this,
119 SLOT(startLoadedQml()));
122 connect(&sessionView, SIGNAL(createdGame()), &networkManager,
123 SLOT(startSession()));
124 connect(
this, SIGNAL(createdAvailableRolesList(std::vector<Role>)),
125 &sessionView, SLOT(gotRolesListUpdate(std::vector<Role>)));
126 connect(&sessionView, SIGNAL(newRoleChosen(
int)), &networkManager,
127 SLOT(changeRoleInSession(
int)));
128 connect(&sessionView, SIGNAL(leftSession()),
this, SLOT(leftSession()));
129 connect(&sessionView, SIGNAL(shownRoomView()),
this, SLOT(showRoomView()));
132 connect(
this, SIGNAL(initSettingsValues(QString, QString)), &settingsView,
133 SLOT(initStartValues(QString, QString)));
134 connect(&settingsView, SIGNAL(updatedSettings(QString, QString)),
this,
135 SLOT(updateSettings(QString, QString)));
136 connect(&networkManager, SIGNAL(gotDisplayName(QString)), &settingsView,
137 SLOT(updateDisplayName(QString)));
140 connect(&networkManager, SIGNAL(gotUserStatistics(
UserStatistics)),
142 connect(&statisticsView, SIGNAL(requestedRefresh()), &networkManager,
143 SLOT(getMyUserStatistics()));
147 connect(&statisticsView, SIGNAL(statisticsGameChanged(QString)),
148 &networkManager, SLOT(getMyUserGameStatistics(QString)));
150 defaultSettingsInitialization();
153 auto replyHandler =
new QOAuthHttpServerReplyHandler(1337,
this);
155 auth.oauth2.setReplyHandler(replyHandler);
158 connect(&auth, SIGNAL(authenticated()), &networkManager, SLOT(getMe()));
159 connect(&auth, SIGNAL(authenticated()), &networkManager,
160 SLOT(getMyUserStatistics()));
162 QTimer::singleShot(0, [&]() { auth.init(); });
167void CavokeClientController::defaultSettingsInitialization() {
168 settings.setValue(PLAYER_NICKNAME,
169 settings.value(PLAYER_NICKNAME, DEFAULT_NICKNAME));
172 settings.value(NETWORK_HOST, NetworkManager::DEFAULT_HOST));
174 networkManager.changeHost(
175 QUrl::fromUserInput(settings.value(NETWORK_HOST).toString()));
178 networkManager.getMe();
181 emit initSettingsValues(settings.value(PLAYER_NICKNAME).toString(),
182 settings.value(NETWORK_HOST).toString());
185void CavokeClientController::showTestWindowView() {
186 testWindowView.show();
189void CavokeClientController::leftSession(
bool real_leave) {
191 networkManager.leaveSession();
193 networkManager.stopGamePolling();
194 networkManager.stopSessionPolling();
195 networkManager.stopValidationPolling();
196 qmlDownloadStatus = QMLDownloadStatus::NOT_STARTED;
197 hostGuestStatus = HostGuestStatus::NOT_IN;
202void CavokeClientController::leftRoom() {
203 networkManager.stopRoomPolling();
204 networkManager.leaveRoom();
205 roomHostGuestStatus = HostGuestStatus::NOT_IN;
209void CavokeClientController::showStartView() {
213void CavokeClientController::showJoinGameView() {
217void CavokeClientController::showGamesListView() {
218 gamesListView.show();
221void CavokeClientController::showCreateGameView() {
222 createGameView.show();
225void CavokeClientController::showRoomView() {
226 displacement = UserDisplacement::ROOM;
230void CavokeClientController::showSessionView() {
231 displacement = UserDisplacement::SESSION;
235void CavokeClientController::showStatisticsView() {
236 statisticsView.requestUpdates();
237 statisticsView.show();
240void CavokeClientController::showSettingsView() {
244void CavokeClientController::startQmlApplication(
246 auto *qmlView =
new QQuickView();
248 connect(qmlView, SIGNAL(closing(QQuickCloseEvent *)), gameModel,
249 SLOT(getClosingFromQml(QQuickCloseEvent *)));
251 qmlView->rootContext()->setContextProperty(
"cavoke", gameModel);
252 qmlView->setSource(gameModel->qmlPath);
253 if (!qmlView->errors().empty()) {
254 qDebug() <<
"An error(s) occurred while opening the QML app:" <<
'\n';
255 for (
const auto &error : qmlView->errors()) {
264void CavokeClientController::exitApplication() {
265 testWindowView.close();
266 joinGameView.close();
267 gamesListView.close();
268 createGameView.close();
271 settingsView.close();
275void CavokeClientController::startQmlByGameId(
const QString &gameId) {
276 currentQmlGameModel =
278 startQmlApplication(currentQmlGameModel);
279 connect(currentQmlGameModel, SIGNAL(sendMoveToNetwork(QString)),
280 &networkManager, SLOT(sendMove(QString)));
281 connect(&networkManager, SIGNAL(gotGameUpdate(QString)),
282 currentQmlGameModel, SLOT(getUpdateFromNetwork(QString)));
283 connect(currentQmlGameModel, SIGNAL(closingQml()),
this, SLOT(stopQml()));
284 networkManager.startGamePolling();
287void CavokeClientController::startLoadedQml() {
288 displacement = UserDisplacement::GAME;
289 networkManager.stopSessionPolling();
290 networkManager.stopValidationPolling();
291 networkManager.stopRoomPolling();
292 startQmlByGameId(currentGameInfo.id);
295void CavokeClientController::stopQml() {
296 networkManager.stopGamePolling();
297 networkManager.startRoomPolling();
300 currentQmlGameModel->deleteLater();
303void CavokeClientController::unpackDownloadedQml(QFile *file,
304 const QString &gameId) {
305 cache_manager::save_zip_to_cache(file, gameId);
306 qDebug() <<
"UnpackDownloadedQml Finished";
309 qmlDownloadStatus = QMLDownloadStatus::DOWNLOADED;
312void CavokeClientController::createGameStart(
const QString &roomName) {
313 qDebug() <<
"Now we are creating room with name:" << roomName;
315 createGameView.close();
317 networkManager.createRoom(roomName);
320void CavokeClientController::joinGameStart(
const QString &inviteCode) {
321 qDebug() <<
"Now we are joining room with inviteCode:" << inviteCode;
323 joinGameView.close();
325 networkManager.joinRoom(inviteCode);
328void CavokeClientController::createSessionStart(
const QString &gameId) {
329 qDebug() <<
"Now we are creating session with gameId:" << gameId;
333 networkManager.roomCreateSession(gameId);
336void CavokeClientController::joinSessionStart(
const QString &sessionId) {
337 qDebug() <<
"Now we are joining session with sessionId:" << sessionId;
341 networkManager.joinSession(sessionId);
344void CavokeClientController::gotRoomInfo(
const RoomInfo &roomInfo) {
345 qDebug() <<
"Got room info";
347 if (displacement != UserDisplacement::ROOM) {
351 currentRoomInfo = roomInfo;
353 if (roomHostGuestStatus == HostGuestStatus::NOT_IN) {
354 networkManager.startRoomPolling();
357 if (currentRoomInfo.isHost &&
358 roomHostGuestStatus != HostGuestStatus::HOST) {
360 }
else if (!currentRoomInfo.isHost &&
361 roomHostGuestStatus != HostGuestStatus::GUEST) {
365 roomView.updateRoomInfo(currentRoomInfo);
368void CavokeClientController::gotSessionInfo(
const SessionInfo &sessionInfo) {
369 qDebug() <<
"Now we got session info";
371 if (displacement == UserDisplacement::ROOM) {
372 roomView.updateSessionInfo(sessionInfo);
376 if (displacement != UserDisplacement::SESSION) {
380 currentSessionInfo = sessionInfo;
382 if (hostGuestStatus == HostGuestStatus::NOT_IN) {
383 networkManager.startSessionPolling();
386 if (currentSessionInfo.isHost && hostGuestStatus != HostGuestStatus::HOST) {
388 }
else if (!currentSessionInfo.isHost &&
389 hostGuestStatus != HostGuestStatus::GUEST) {
393 if (qmlDownloadStatus == QMLDownloadStatus::NOT_STARTED) {
394 networkManager.getGamesClient(currentSessionInfo.game_id);
395 qmlDownloadStatus = QMLDownloadStatus::DOWNLOADING;
396 sessionView.updateStatus(SessionView::CreatingGameStatus::DOWNLOAD);
399 sessionView.updateSessionInfo(currentSessionInfo);
401 if (currentGameInfo.players_num == 0) {
402 networkManager.getGamesConfig(currentSessionInfo.game_id);
403 }
else if (qmlDownloadStatus == QMLDownloadStatus::DOWNLOADED) {
404 sessionView.updateStatus(SessionView::CreatingGameStatus::DONE);
405 collectListOfAvailableRoles();
409void CavokeClientController::gotCurrentGameInfo(
const GameInfo &gameInfo) {
410 currentGameInfo = gameInfo;
411 emit setGameName(currentGameInfo.display_name);
414void CavokeClientController::updateSettings(
const QString &displayName,
415 const QString &host) {
416 settings.setValue(PLAYER_NICKNAME, displayName);
417 settings.setValue(NETWORK_HOST, host);
419 networkManager.changeHost(QUrl::fromUserInput(host));
420 networkManager.changeName(displayName);
422void CavokeClientController::collectListOfAvailableRoles() {
423 std::vector<bool> isFree(currentGameInfo.players_num,
true);
425 QString userId = networkManager.getUserId();
426 for (
const auto &player : currentSessionInfo.players) {
427 isFree[player.player_id] =
false;
428 if (player.user.user_id == userId) {
429 ourRole = player.player_id;
438 std::vector<Role> availableRoles;
439 availableRoles.emplace_back(currentGameInfo.role_names[ourRole],
441 for (
int i = 0; i < currentGameInfo.players_num; ++i) {
443 availableRoles.emplace_back(currentGameInfo.role_names[i], i);
446 emit createdAvailableRolesList(availableRoles);
449void CavokeClientController::becomeHost() {
450 hostGuestStatus = HostGuestStatus::HOST;
451 networkManager.startValidationPolling();
454void CavokeClientController::becomeGuest() {
455 hostGuestStatus = HostGuestStatus::GUEST;
456 networkManager.stopValidationPolling();
459void CavokeClientController::becomeRoomHost() {
460 roomHostGuestStatus = HostGuestStatus::HOST;
463void CavokeClientController::becomeRoomGuest() {
464 roomHostGuestStatus = HostGuestStatus::GUEST;
static AuthenticationManager & getInstance()
Singleton wrapper.