2022-07-25 10:18:30 -05:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 Citra Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-12-25 13:27:52 -06:00
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include "common/announce_multiplayer_room.h"
|
|
|
|
#include "common/logging/log.h"
|
2022-08-28 12:31:16 -05:00
|
|
|
#include "core/core.h"
|
2021-12-25 13:27:52 -06:00
|
|
|
#include "yuzu/game_list.h"
|
|
|
|
#include "yuzu/multiplayer/client_room.h"
|
|
|
|
#include "yuzu/multiplayer/direct_connect.h"
|
|
|
|
#include "yuzu/multiplayer/host_room.h"
|
|
|
|
#include "yuzu/multiplayer/lobby.h"
|
|
|
|
#include "yuzu/multiplayer/message.h"
|
|
|
|
#include "yuzu/multiplayer/state.h"
|
|
|
|
#include "yuzu/uisettings.h"
|
|
|
|
#include "yuzu/util/clickable_label.h"
|
|
|
|
|
2022-07-11 13:40:57 -05:00
|
|
|
MultiplayerState::MultiplayerState(QWidget* parent, QStandardItemModel* game_list_model_,
|
2022-08-26 20:41:19 -05:00
|
|
|
QAction* leave_room_, QAction* show_room_, Core::System& system_)
|
2022-07-11 13:40:57 -05:00
|
|
|
: QWidget(parent), game_list_model(game_list_model_), leave_room(leave_room_),
|
2022-08-26 20:41:19 -05:00
|
|
|
show_room(show_room_), system{system_}, room_network{system.GetRoomNetwork()} {
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
// register the network structs to use in slots and signals
|
|
|
|
state_callback_handle = member->BindOnStateChanged(
|
|
|
|
[this](const Network::RoomMember::State& state) { emit NetworkStateChanged(state); });
|
|
|
|
connect(this, &MultiplayerState::NetworkStateChanged, this,
|
|
|
|
&MultiplayerState::OnNetworkStateChanged);
|
|
|
|
error_callback_handle = member->BindOnError(
|
|
|
|
[this](const Network::RoomMember::Error& error) { emit NetworkError(error); });
|
|
|
|
connect(this, &MultiplayerState::NetworkError, this, &MultiplayerState::OnNetworkError);
|
|
|
|
}
|
|
|
|
|
|
|
|
qRegisterMetaType<Network::RoomMember::State>();
|
|
|
|
qRegisterMetaType<Network::RoomMember::Error>();
|
|
|
|
qRegisterMetaType<WebService::WebResult>();
|
2022-07-22 09:31:13 -05:00
|
|
|
announce_multiplayer_session = std::make_shared<Core::AnnounceMultiplayerSession>(room_network);
|
2021-12-25 13:27:52 -06:00
|
|
|
announce_multiplayer_session->BindErrorCallback(
|
|
|
|
[this](const WebService::WebResult& result) { emit AnnounceFailed(result); });
|
|
|
|
connect(this, &MultiplayerState::AnnounceFailed, this, &MultiplayerState::OnAnnounceFailed);
|
|
|
|
|
|
|
|
status_text = new ClickableLabel(this);
|
|
|
|
status_icon = new ClickableLabel(this);
|
|
|
|
|
|
|
|
connect(status_text, &ClickableLabel::clicked, this, &MultiplayerState::OnOpenNetworkRoom);
|
|
|
|
connect(status_icon, &ClickableLabel::clicked, this, &MultiplayerState::OnOpenNetworkRoom);
|
|
|
|
|
|
|
|
connect(static_cast<QApplication*>(QApplication::instance()), &QApplication::focusChanged, this,
|
|
|
|
[this](QWidget* /*old*/, QWidget* now) {
|
|
|
|
if (client_room && client_room->isAncestorOf(now)) {
|
|
|
|
HideNotification();
|
|
|
|
}
|
|
|
|
});
|
2022-09-09 15:29:22 -05:00
|
|
|
|
|
|
|
retranslateUi();
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
2022-08-03 12:45:13 -05:00
|
|
|
MultiplayerState::~MultiplayerState() = default;
|
|
|
|
|
|
|
|
void MultiplayerState::Close() {
|
2021-12-25 13:27:52 -06:00
|
|
|
if (state_callback_handle) {
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
member->Unbind(state_callback_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_callback_handle) {
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
member->Unbind(error_callback_handle);
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 12:16:59 -05:00
|
|
|
if (host_room) {
|
2021-12-25 13:27:52 -06:00
|
|
|
host_room->close();
|
2022-07-25 12:16:59 -05:00
|
|
|
}
|
|
|
|
if (direct_connect) {
|
2021-12-25 13:27:52 -06:00
|
|
|
direct_connect->close();
|
2022-07-25 12:16:59 -05:00
|
|
|
}
|
|
|
|
if (client_room) {
|
2021-12-25 13:27:52 -06:00
|
|
|
client_room->close();
|
2022-07-25 12:16:59 -05:00
|
|
|
}
|
|
|
|
if (lobby) {
|
2021-12-25 13:27:52 -06:00
|
|
|
lobby->close();
|
2022-07-25 12:16:59 -05:00
|
|
|
}
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::retranslateUi() {
|
|
|
|
status_text->setToolTip(tr("Current connection status"));
|
|
|
|
|
2022-09-09 15:29:22 -05:00
|
|
|
UpdateNotificationStatus();
|
2021-12-25 13:27:52 -06:00
|
|
|
|
2022-07-25 10:08:20 -05:00
|
|
|
if (lobby) {
|
2021-12-25 13:27:52 -06:00
|
|
|
lobby->RetranslateUi();
|
2022-07-25 10:08:20 -05:00
|
|
|
}
|
|
|
|
if (host_room) {
|
2021-12-25 13:27:52 -06:00
|
|
|
host_room->RetranslateUi();
|
2022-07-25 10:08:20 -05:00
|
|
|
}
|
|
|
|
if (client_room) {
|
2021-12-25 13:27:52 -06:00
|
|
|
client_room->RetranslateUi();
|
2022-07-25 10:08:20 -05:00
|
|
|
}
|
|
|
|
if (direct_connect) {
|
2021-12-25 13:27:52 -06:00
|
|
|
direct_connect->RetranslateUi();
|
2022-07-25 10:08:20 -05:00
|
|
|
}
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
2022-09-09 15:29:22 -05:00
|
|
|
void MultiplayerState::SetNotificationStatus(NotificationStatus status) {
|
|
|
|
notification_status = status;
|
|
|
|
UpdateNotificationStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::UpdateNotificationStatus() {
|
|
|
|
switch (notification_status) {
|
2023-03-11 21:10:38 -06:00
|
|
|
case NotificationStatus::Uninitialized:
|
2022-09-09 15:29:22 -05:00
|
|
|
status_icon->setPixmap(QIcon::fromTheme(QStringLiteral("disconnected")).pixmap(16));
|
|
|
|
status_text->setText(tr("Not Connected. Click here to find a room!"));
|
|
|
|
leave_room->setEnabled(false);
|
|
|
|
show_room->setEnabled(false);
|
|
|
|
break;
|
|
|
|
case NotificationStatus::Disconnected:
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme(QStringLiteral("disconnected")).pixmap(16));
|
|
|
|
status_text->setText(tr("Not Connected"));
|
|
|
|
leave_room->setEnabled(false);
|
|
|
|
show_room->setEnabled(false);
|
|
|
|
break;
|
|
|
|
case NotificationStatus::Connected:
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme(QStringLiteral("connected")).pixmap(16));
|
|
|
|
status_text->setText(tr("Connected"));
|
|
|
|
leave_room->setEnabled(true);
|
|
|
|
show_room->setEnabled(true);
|
|
|
|
break;
|
|
|
|
case NotificationStatus::Notification:
|
|
|
|
status_icon->setPixmap(
|
|
|
|
QIcon::fromTheme(QStringLiteral("connected_notification")).pixmap(16));
|
|
|
|
status_text->setText(tr("New Messages Received"));
|
|
|
|
leave_room->setEnabled(true);
|
|
|
|
show_room->setEnabled(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up status bar if game is running
|
|
|
|
if (system.IsPoweredOn()) {
|
|
|
|
status_text->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 13:27:52 -06:00
|
|
|
void MultiplayerState::OnNetworkStateChanged(const Network::RoomMember::State& state) {
|
|
|
|
LOG_DEBUG(Frontend, "Network State: {}", Network::GetStateStr(state));
|
|
|
|
if (state == Network::RoomMember::State::Joined ||
|
|
|
|
state == Network::RoomMember::State::Moderator) {
|
|
|
|
|
|
|
|
OnOpenNetworkRoom();
|
2022-09-09 15:29:22 -05:00
|
|
|
SetNotificationStatus(NotificationStatus::Connected);
|
2021-12-25 13:27:52 -06:00
|
|
|
} else {
|
2022-09-09 15:29:22 -05:00
|
|
|
SetNotificationStatus(NotificationStatus::Disconnected);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
current_state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnNetworkError(const Network::RoomMember::Error& error) {
|
|
|
|
LOG_DEBUG(Frontend, "Network Error: {}", Network::GetErrorStr(error));
|
|
|
|
switch (error) {
|
|
|
|
case Network::RoomMember::Error::LostConnection:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::LOST_CONNECTION);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::HostKicked:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::HOST_KICKED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::CouldNotConnect:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::UNABLE_TO_CONNECT);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::NameCollision:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(
|
|
|
|
NetworkMessage::ErrorManager::USERNAME_NOT_VALID_SERVER);
|
|
|
|
break;
|
2022-07-29 22:58:23 -05:00
|
|
|
case Network::RoomMember::Error::IpCollision:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::IP_COLLISION);
|
2021-12-25 13:27:52 -06:00
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::RoomIsFull:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::ROOM_IS_FULL);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::WrongPassword:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::WRONG_PASSWORD);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::WrongVersion:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::WRONG_VERSION);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::HostBanned:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::HOST_BANNED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::UnknownError:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::UNABLE_TO_CONNECT);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::PermissionDenied:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::PERMISSION_DENIED);
|
|
|
|
break;
|
|
|
|
case Network::RoomMember::Error::NoSuchUser:
|
|
|
|
NetworkMessage::ErrorManager::ShowError(NetworkMessage::ErrorManager::NO_SUCH_USER);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnAnnounceFailed(const WebService::WebResult& result) {
|
|
|
|
announce_multiplayer_session->Stop();
|
|
|
|
QMessageBox::warning(this, tr("Error"),
|
|
|
|
tr("Failed to update the room information. Please check your Internet "
|
|
|
|
"connection and try hosting the room again.\nDebug Message: ") +
|
|
|
|
QString::fromStdString(result.result_string),
|
|
|
|
QMessageBox::Ok);
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:29:22 -05:00
|
|
|
void MultiplayerState::OnSaveConfig() {
|
|
|
|
emit SaveConfig();
|
|
|
|
}
|
|
|
|
|
2021-12-25 13:27:52 -06:00
|
|
|
void MultiplayerState::UpdateThemedIcons() {
|
|
|
|
if (show_notification) {
|
|
|
|
status_icon->setPixmap(
|
|
|
|
QIcon::fromTheme(QStringLiteral("connected_notification")).pixmap(16));
|
|
|
|
} else if (current_state == Network::RoomMember::State::Joined ||
|
|
|
|
current_state == Network::RoomMember::State::Moderator) {
|
|
|
|
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme(QStringLiteral("connected")).pixmap(16));
|
|
|
|
} else {
|
|
|
|
status_icon->setPixmap(QIcon::fromTheme(QStringLiteral("disconnected")).pixmap(16));
|
|
|
|
}
|
|
|
|
if (client_room)
|
|
|
|
client_room->UpdateIconDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BringWidgetToFront(QWidget* widget) {
|
|
|
|
widget->show();
|
|
|
|
widget->activateWindow();
|
|
|
|
widget->raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnViewLobby() {
|
|
|
|
if (lobby == nullptr) {
|
2022-08-26 20:41:19 -05:00
|
|
|
lobby = new Lobby(this, game_list_model, announce_multiplayer_session, system);
|
2022-09-09 15:29:22 -05:00
|
|
|
connect(lobby, &Lobby::SaveConfig, this, &MultiplayerState::OnSaveConfig);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
2022-09-09 15:29:22 -05:00
|
|
|
lobby->RefreshLobby();
|
2021-12-25 13:27:52 -06:00
|
|
|
BringWidgetToFront(lobby);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnCreateRoom() {
|
|
|
|
if (host_room == nullptr) {
|
2022-08-26 20:41:19 -05:00
|
|
|
host_room = new HostRoomWindow(this, game_list_model, announce_multiplayer_session, system);
|
2022-09-09 15:29:22 -05:00
|
|
|
connect(host_room, &HostRoomWindow::SaveConfig, this, &MultiplayerState::OnSaveConfig);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
BringWidgetToFront(host_room);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiplayerState::OnCloseRoom() {
|
|
|
|
if (!NetworkMessage::WarnCloseRoom())
|
|
|
|
return false;
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto room = room_network.GetRoom().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
// if you are in a room, leave it
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
member->Leave();
|
|
|
|
LOG_DEBUG(Frontend, "Left the room (as a client)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// if you are hosting a room, also stop hosting
|
|
|
|
if (room->GetState() != Network::Room::State::Open) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Save ban list
|
2022-10-21 01:34:08 -05:00
|
|
|
UISettings::values.multiplayer_ban_list = room->GetBanList();
|
2021-12-25 13:27:52 -06:00
|
|
|
|
|
|
|
room->Destroy();
|
|
|
|
announce_multiplayer_session->Stop();
|
|
|
|
LOG_DEBUG(Frontend, "Closed the room (as a server)");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::ShowNotification() {
|
|
|
|
if (client_room && client_room->isAncestorOf(QApplication::focusWidget()))
|
|
|
|
return; // Do not show notification if the chat window currently has focus
|
|
|
|
show_notification = true;
|
|
|
|
QApplication::alert(nullptr);
|
2022-07-30 21:46:26 -05:00
|
|
|
QApplication::beep();
|
2022-09-09 15:29:22 -05:00
|
|
|
SetNotificationStatus(NotificationStatus::Notification);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::HideNotification() {
|
|
|
|
show_notification = false;
|
2022-09-09 15:29:22 -05:00
|
|
|
SetNotificationStatus(NotificationStatus::Connected);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnOpenNetworkRoom() {
|
2022-07-22 09:31:13 -05:00
|
|
|
if (auto member = room_network.GetRoomMember().lock()) {
|
2021-12-25 13:27:52 -06:00
|
|
|
if (member->IsConnected()) {
|
|
|
|
if (client_room == nullptr) {
|
2022-07-22 09:31:13 -05:00
|
|
|
client_room = new ClientRoomWindow(this, room_network);
|
2021-12-25 13:27:52 -06:00
|
|
|
connect(client_room, &ClientRoomWindow::ShowNotification, this,
|
|
|
|
&MultiplayerState::ShowNotification);
|
|
|
|
}
|
|
|
|
BringWidgetToFront(client_room);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the user is not a member of a room, show the lobby instead.
|
|
|
|
// This is currently only used on the clickable label in the status bar
|
|
|
|
OnViewLobby();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::OnDirectConnectToRoom() {
|
|
|
|
if (direct_connect == nullptr) {
|
2022-08-26 20:41:19 -05:00
|
|
|
direct_connect = new DirectConnectWindow(system, this);
|
2022-09-09 15:29:22 -05:00
|
|
|
connect(direct_connect, &DirectConnectWindow::SaveConfig, this,
|
|
|
|
&MultiplayerState::OnSaveConfig);
|
2021-12-25 13:27:52 -06:00
|
|
|
}
|
|
|
|
BringWidgetToFront(direct_connect);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MultiplayerState::IsHostingPublicRoom() const {
|
|
|
|
return announce_multiplayer_session->IsRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::UpdateCredentials() {
|
|
|
|
announce_multiplayer_session->UpdateCredentials();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiplayerState::UpdateGameList(QStandardItemModel* game_list) {
|
|
|
|
game_list_model = game_list;
|
|
|
|
if (lobby) {
|
|
|
|
lobby->UpdateGameList(game_list);
|
|
|
|
}
|
|
|
|
if (host_room) {
|
|
|
|
host_room->UpdateGameList(game_list);
|
|
|
|
}
|
|
|
|
}
|