Cavoke  1.1.0
A Platform for creating and hosting multiplayer turn-based board games
Loading...
Searching...
No Matches
roomview.cpp
1#include "roomview.h"
2#include "ui_roomview.h"
3
4RoomView::RoomView(QWidget *parent)
5 : QMainWindow(parent), ui(new Ui::RoomView) {
6 ui->setupUi(this);
7}
8
9RoomView::~RoomView() {
10 delete ui;
11}
12
13void RoomView::updateStatus(CreatingSessionStatus newStatus) {
14 status = newStatus;
15 ui->waitForHostLabel->setText(STATUS.at(status));
16 ui->joinSessionButton->setEnabled(status ==
17 CreatingSessionStatus::NOW_YOU_CAN_JOIN);
18}
19
20void RoomView::updateRoomInfo(const RoomInfo &roomInfo) {
21 sessionId = roomInfo.session_id;
22 is_host = roomInfo.isHost;
23 ui->roomNameLabel->setText(roomInfo.display_name);
24 ui->inviteCodeLabel->setText(roomInfo.invite_code);
25 ui->membersListWidget->clear();
26 for (const auto &member : roomInfo.members) {
27 ui->membersListWidget->addItem(member.display_name);
28 }
29
30 if (!sessionId.isEmpty()) {
31 requestedSessionUpdate(sessionId);
32 } else {
33 updateSessionInfo(SessionInfo());
34 }
35}
36
37void RoomView::updateSessionInfo(const SessionInfo &sessionInfo) {
38 if (!is_host) {
39 ui->sessionGameLabel->hide();
40 ui->sessionGameComboBox->hide();
41 ui->createSessionButton->hide();
42 ui->joinSessionButton->show();
43 ui->joinSessionButton->setEnabled(sessionInfo.status !=
44 SessionInfo::Status::FINISHED);
45 updateStatus(sessionInfo.status != SessionInfo::Status::FINISHED
46 ? CreatingSessionStatus::NOW_YOU_CAN_JOIN
47 : CreatingSessionStatus::WAIT_FOR_THE_HOST);
48 return;
49 }
50 if (sessionInfo.status != SessionInfo::Status::FINISHED) {
51 ui->createSessionButton->hide();
52 ui->joinSessionButton->show();
53 ui->joinSessionButton->setEnabled(true);
54 ui->sessionGameLabel->hide();
55 ui->sessionGameComboBox->hide();
56 updateStatus(CreatingSessionStatus::NOW_YOU_CAN_JOIN);
57 } else {
58 ui->createSessionButton->show();
59 ui->joinSessionButton->hide();
60 ui->createSessionButton->setEnabled(true);
61 ui->sessionGameLabel->show();
62 ui->sessionGameComboBox->show();
63 updateStatus(CreatingSessionStatus::YOU_CAN_CREATE);
64 }
65}
66
67void RoomView::gotGamesListUpdate(const std::vector<GameInfo> &newGamesList) {
68 ui->sessionGameComboBox->clear();
69 for (const auto &gameInfo : newGamesList) {
70 ui->sessionGameComboBox->addItem(gameInfo.display_name, gameInfo.id);
71 }
72 if (ui->sessionGameComboBox->count() > 0) {
73 ui->sessionGameComboBox->setCurrentIndex(0);
74 } else {
75 ui->sessionGameComboBox->setCurrentIndex(-1);
76 }
77}
78
79void RoomView::clear() {
80 updateStatus(CreatingSessionStatus::UNKNOWN);
81 ui->roomNameLabel->setText("<room_name>");
82 ui->inviteCodeLabel->setText("<invite_code>");
83 ui->membersListWidget->clear();
84}
85
86void RoomView::on_backButton_clicked() {
87 this->clear();
88 this->close();
89 emit leftRoom();
90 emit shownStartView();
91}
92
93void RoomView::on_joinSessionButton_clicked() {
94 emit joinedSession(sessionId);
95}
96
97void RoomView::on_createSessionButton_clicked() {
98 if (ui->sessionGameComboBox->currentData().toString().isEmpty()) {
99 return; // Probably no games at all
100 }
101 emit createdSession(ui->sessionGameComboBox->currentData().toString());
102}