From 8301c20efd2f59eb4eec8d5ea1f2adb529b85906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= <44814898+BruebachL@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:30:47 +0100 Subject: [PATCH] Add instant quick host to the game lobby --- .../src/client/settings/shortcuts_settings.h | 6 + .../game_hosting/game_creation_options.cpp | 7 + .../game_hosting/game_creation_options.h | 8 + .../widgets/server/game_selector.cpp | 150 ++++++++++++++++-- .../interface/widgets/server/game_selector.h | 35 +++- .../src/interface/widgets/tabs/tab_room.cpp | 3 +- tests/game_creation_options_test.cpp | 20 +++ 7 files changed, 216 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/client/settings/shortcuts_settings.h b/cockatrice/src/client/settings/shortcuts_settings.h index 24790a407..1ebf3e7ef 100644 --- a/cockatrice/src/client/settings/shortcuts_settings.h +++ b/cockatrice/src/client/settings/shortcuts_settings.h @@ -313,6 +313,12 @@ private: {"DeckViewContainer/forceStartGameButton", ShortcutKey(QT_TRANSLATE_NOOP("shortcutsTab", "Force Start"), parseSequenceString(""), ShortcutGroup::Game_Lobby)}, + {"GameSelector/aQuickHostGame", ShortcutKey(QT_TRANSLATE_NOOP("shortcutsTab", "Host Game"), + parseSequenceString("Ctrl+N"), + ShortcutGroup::Game_Lobby)}, + {"GameSelector/aReviewGame", ShortcutKey(QT_TRANSLATE_NOOP("shortcutsTab", "Review Game Settings"), + parseSequenceString("Ctrl+Shift+N"), + ShortcutGroup::Game_Lobby)}, {"Player/aCCMagenta", ShortcutKey(QT_TRANSLATE_NOOP("shortcutsTab", "Add Card Counter (F)"), parseSequenceString(""), ShortcutGroup::Card_Counters)}, diff --git a/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp b/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp index 048899266..acef35ebc 100644 --- a/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp +++ b/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp @@ -36,6 +36,13 @@ GameCreationOptions GameCreationOptions::fromSettings(const IGameSettingsProvide return options; } +GameCreationOptions GameCreationOptions::defaultsFor(bool registeredUser) +{ + GameCreationOptions options; + options.onlyRegistered = registeredUser; + return options; +} + GameCreationOptions GameCreationOptions::fromGameInfo(const ServerInfo_Game &gameInfo, const QMap &gameTypes) { diff --git a/cockatrice/src/interface/widgets/game_hosting/game_creation_options.h b/cockatrice/src/interface/widgets/game_hosting/game_creation_options.h index cbc941815..d9a2bc0cc 100644 --- a/cockatrice/src/interface/widgets/game_hosting/game_creation_options.h +++ b/cockatrice/src/interface/widgets/game_hosting/game_creation_options.h @@ -49,6 +49,14 @@ struct GameCreationOptions [[nodiscard]] static GameCreationOptions fromSettings(const IGameSettingsProvider &settings, const QMap &gameTypes, bool registeredUser); + /** + * @brief The options the host form shows when nothing is remembered. + * + * A registered host defaults to a registered-only game, matching the form's + * ``Clear`` action. + */ + [[nodiscard]] static GameCreationOptions defaultsFor(bool registeredUser); + /** * @brief Reads the read-only fields shown in the game information view. */ diff --git a/cockatrice/src/interface/widgets/server/game_selector.cpp b/cockatrice/src/interface/widgets/server/game_selector.cpp index 9dc7bf1e8..0e5ce725c 100644 --- a/cockatrice/src/interface/widgets/server/game_selector.cpp +++ b/cockatrice/src/interface/widgets/server/game_selector.cpp @@ -1,9 +1,11 @@ #include "game_selector.h" #include "../../../client/settings/cache_settings.h" +#include "../../../client/settings/shortcuts_settings.h" #include "../../theme_icon.h" #include "../interface/widgets/dialogs/dlg_create_game.h" #include "../interface/widgets/dialogs/dlg_filter_games.h" +#include "../interface/widgets/game_hosting/game_creation_options.h" #include "../interface/widgets/tabs/tab_room.h" #include "../interface/widgets/tabs/tab_supervisor.h" #include "../interface/widgets/utility/get_text_with_max.h" @@ -22,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -139,11 +144,39 @@ GameSelector::GameSelector(AbstractClient *_client, connect(clearFilterButton, &QPushButton::clicked, this, &GameSelector::actClearFilter); connect(gameListProxyModel, &GamesProxyModel::filtersChanged, this, &GameSelector::checkClearFilterButtonState); + hostGameButton = nullptr; + hostGameAction = nullptr; + reviewGameAction = nullptr; if (room) { - createButton = new QPushButton; - connect(createButton, &QPushButton::clicked, this, &GameSelector::actCreate); - } else { - createButton = nullptr; + hostGameAction = new QAction(this); + hostGameAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); + connect(hostGameAction, &QAction::triggered, this, &GameSelector::actQuickHost); + + hostGameButton = new QToolButton(this); + hostGameButton->setObjectName("hostGameButton"); + hostGameButton->setDefaultAction(hostGameAction); + hostGameButton->setPopupMode(QToolButton::MenuButtonPopup); + hostGameButton->setToolButtonStyle(Qt::ToolButtonTextOnly); + + auto *hostGameMenu = new QMenu(hostGameButton); + hostGameMenu->setObjectName("hostGameMenu"); + reviewGameAction = hostGameMenu->addAction(QString()); + reviewGameAction->setObjectName("reviewGameAction"); + reviewGameAction->setShortcutContext(Qt::WidgetWithChildrenShortcut); + connect(reviewGameAction, &QAction::triggered, this, &GameSelector::actCreate); + hostGameButton->setMenu(hostGameMenu); + + // The shortcuts belong to the whole room tab, not just the game list, so they also + // fire while the chat or the user list has focus. A QToolButton menu is mouse-only, + // so the review action needs its own shortcut for keyboard parity. + if (auto *roomTab = qobject_cast(parentWidget())) { + roomTab->addAction(hostGameAction); + roomTab->addAction(reviewGameAction); + } else { + addAction(hostGameAction); + addAction(reviewGameAction); + } + refreshShortcuts(); } joinButton = new QPushButton; joinAsJudgeButton = new QPushButton; @@ -157,7 +190,7 @@ GameSelector::GameSelector(AbstractClient *_client, } buttonLayout->addStretch(); if (room) { - buttonLayout->addWidget(createButton); + buttonLayout->addWidget(hostGameButton); } buttonLayout->addWidget(joinButton); if (tabSupervisor->getUserInfo()->user_level() & ServerInfo_User::IsJudge) { @@ -173,10 +206,16 @@ GameSelector::GameSelector(AbstractClient *_client, } buttonLayout->setAlignment(Qt::AlignTop); + hostErrorLabel = new QLabel(this); + hostErrorLabel->setObjectName("hostGameErrorLabel"); + hostErrorLabel->setWordWrap(true); + hostErrorLabel->setVisible(false); + QVBoxLayout *mainLayout = new QVBoxLayout; if (showFilters && restoresettings) { mainLayout->addWidget(quickFilterToolBar); } + mainLayout->addWidget(hostErrorLabel); mainLayout->addWidget(gameListView); mainLayout->addLayout(buttonLayout); @@ -266,6 +305,8 @@ void GameSelector::actCreate() return; } + clearHostError(); + // Heap-allocate the dialog: if this selector (or its room tab) is destroyed while the // dialog is open, e.g. on server disconnect, Qt deletes its children. A stack-allocated // child would then be deleted by Qt and crash. @@ -275,6 +316,69 @@ void GameSelector::actCreate() dlg->exec(); } +void GameSelector::actQuickHost() +{ + if (room == nullptr) { + qWarning() << "Attempted to create a game, but the room was null"; + return; + } + + clearHostError(); + + auto &gameSettings = SettingsCache::instance().game(); + const bool registered = tabSupervisor->getUserInfo()->user_level() & ServerInfo_User::IsRegistered; + const GameCreationOptions options = + gameSettings.getRememberGameSettings() + ? GameCreationOptions::fromSettings(gameSettings, room->getGameTypes(), registered) + : GameCreationOptions::defaultsFor(registered); + + PendingCommand *pend = room->prepareRoomCommand(options.toCreateGameCommand()); + connect(pend, &PendingCommand::finished, this, &GameSelector::checkQuickHostResponse); + room->sendRoomCommand(pend); + + setHostCommandPending(true); +} + +void GameSelector::checkQuickHostResponse(const Response &response) +{ + setHostCommandPending(false); + + switch (response.response_code()) { + case Response::RespOk: + clearHostError(); + break; + case Response::RespContextError: + showHostError(tr("You already host the maximum number of games.")); + break; + case Response::RespInternalError: + showHostError(tr("The server could not create the game.")); + break; + default: + showHostError(tr("Could not create the game.")); + break; + } +} + +void GameSelector::showHostError(const QString &message) +{ + hostErrorLabel->setText(message); + hostErrorLabel->setVisible(true); +} + +void GameSelector::clearHostError() +{ + hostErrorLabel->clear(); + hostErrorLabel->setVisible(false); +} + +void GameSelector::setHostCommandPending(bool pending) +{ + hostCommandPending = pending; + if (hostGameAction) { + hostGameAction->setEnabled(!pending); + } +} + void GameSelector::checkResponse(const Response &response) { // NB: We re-enable buttons for the currently selected game, which may not @@ -486,8 +590,8 @@ bool GameSelector::joinGameById(const int gameId, const bool asSpectator) void GameSelector::disableButtons() { - if (createButton) { - createButton->setEnabled(false); + if (hostGameAction) { + hostGameAction->setEnabled(false); } joinButton->setEnabled(false); @@ -496,8 +600,8 @@ void GameSelector::disableButtons() void GameSelector::enableButtons() { - if (createButton) { - createButton->setEnabled(true); + if (hostGameAction) { + hostGameAction->setEnabled(!hostCommandPending); } // Enable buttons for the currently selected game @@ -521,8 +625,14 @@ void GameSelector::retranslateUi() { filterButton->setText(tr("&Filter games")); clearFilterButton->setText(tr("C&lear filter")); - if (createButton) { - createButton->setText(tr("C&reate")); + if (hostGameAction) { + // A QToolButton cannot draw a mnemonic from a default action; the shortcut is the + // keyboard path here. + hostGameAction->setText(tr("Host game")); + updateHostGameToolTip(); + } + if (reviewGameAction) { + reviewGameAction->setText(tr("Review first…")); } joinButton->setText(tr("&Join")); joinAsJudgeButton->setText(tr("Join as judge")); @@ -532,6 +642,24 @@ void GameSelector::retranslateUi() updateTitle(); } +void GameSelector::refreshShortcuts() +{ + if (hostGameAction) { + hostGameAction->setShortcuts(SettingsCache::instance().shortcuts().getShortcut("GameSelector/aQuickHostGame")); + updateHostGameToolTip(); + } + if (reviewGameAction) { + reviewGameAction->setShortcuts(SettingsCache::instance().shortcuts().getShortcut("GameSelector/aReviewGame")); + } +} + +void GameSelector::updateHostGameToolTip() +{ + const QString shortcut = SettingsCache::instance().shortcuts().getShortcutString("GameSelector/aQuickHostGame"); + hostGameAction->setToolTip(shortcut.isEmpty() ? tr("Host a game with your last-used settings") + : tr("Host a game with your last-used settings (%1)").arg(shortcut)); +} + void GameSelector::processGameInfo(const ServerInfo_Game &info) { gameListModel->updateGameList(info); diff --git a/cockatrice/src/interface/widgets/server/game_selector.h b/cockatrice/src/interface/widgets/server/game_selector.h index e3407abea..7e2ab24f0 100644 --- a/cockatrice/src/interface/widgets/server/game_selector.h +++ b/cockatrice/src/interface/widgets/server/game_selector.h @@ -14,7 +14,10 @@ class QTreeView; class GamesModel; class GamesProxyModel; +class QLabel; class QPushButton; +class QToolButton; +class QAction; class AbstractClient; class TabSupervisor; class TabRoom; @@ -64,6 +67,17 @@ private slots: */ void actCreate(); + /** + * @brief Creates a game immediately from the remembered settings, without a dialog. + */ + void actQuickHost(); + + /** + * @brief Reports a failed quick-host request inline instead of as a modal box. + * @param response The server response to the create command. + */ + void checkQuickHostResponse(const Response &response); + /** * @brief Joins the currently selected game as a player. */ @@ -140,7 +154,10 @@ private: QPushButton *filterButton; /**< Button to open the filter dialog. */ QPushButton *clearFilterButton; /**< Button to clear active filters. */ - QPushButton *createButton; /**< Button to create a new game (only if room is set). */ + QToolButton *hostGameButton; /**< Split button: quick host, with the full form in its menu. */ + QAction *hostGameAction; /**< Quick-host action, carrying the keyboard shortcut. */ + QAction *reviewGameAction; /**< Menu action that opens the full create dialog. */ + QLabel *hostErrorLabel; /**< Inline failure message for a quick-host request. */ QPushButton *joinButton; /**< Button to join the selected game. */ QPushButton *joinAsJudgeButton; /**< Button to join the selected game as a judge. */ QPushButton *spectateButton; /**< Button to spectate the selected game. */ @@ -152,6 +169,17 @@ private: QTimer filterSaveDebounceTimer; /**< Delays persisting quick-filter edits so typing does not rewrite the settings file on every keystroke. */ + bool hostCommandPending = false; /**< True while a quick-host command is in flight. */ + + /** @brief Shows a quick-host failure above the game list. */ + void showHostError(const QString &message); + /** @brief Hides the quick-host failure message. */ + void clearHostError(); + /** @brief Enables or disables the quick-host action while a command is in flight. */ + void setHostCommandPending(bool pending); + /** @brief Keeps the quick-host button tooltip in sync with the current shortcut. */ + void updateHostGameToolTip(); + /** * @brief Updates the widget title to reflect the current number of displayed games. * @@ -222,6 +250,11 @@ public: */ void retranslateUi(); + /** + * @brief Re-reads the quick-host and review shortcuts after the user rebinds them. + */ + void refreshShortcuts(); + /** * @brief Updates or adds a game entry in the list. * @param info The ServerInfo_Game object containing information about the game to update. diff --git a/cockatrice/src/interface/widgets/tabs/tab_room.cpp b/cockatrice/src/interface/widgets/tabs/tab_room.cpp index 329732c64..1973a0747 100644 --- a/cockatrice/src/interface/widgets/tabs/tab_room.cpp +++ b/cockatrice/src/interface/widgets/tabs/tab_room.cpp @@ -113,7 +113,7 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor, QMap tempMap; tempMap.insert(info.room_id(), gameTypes); - gameSelector = new GameSelector(client, tabSupervisor, this, QMap(), tempMap, true, true); + gameSelector = new GameSelector(client, tabSupervisor, this, QMap(), tempMap, true, true, this); userListPanel = new UserListPanelWidget(tabSupervisor, client, this); userListPanel->bind(tabSupervisor->getUserListManager()); @@ -720,6 +720,7 @@ void TabRoom::processRemoveMessagesEvent(const Event_RemoveMessages &event) void TabRoom::refreshShortcuts() { aClearChat->setShortcuts(SettingsCache::instance().shortcuts().getShortcut("tab_room/aClearChat")); + gameSelector->refreshShortcuts(); } void TabRoom::addMentionTag(QString mentionTag) diff --git a/tests/game_creation_options_test.cpp b/tests/game_creation_options_test.cpp index e4a7996cb..cd8344ba9 100644 --- a/tests/game_creation_options_test.cpp +++ b/tests/game_creation_options_test.cpp @@ -185,6 +185,26 @@ TEST(GameCreationOptions, DescriptionWhitespaceIsSimplified) EXPECT_EQ(cmd.description(), "a b"); } +TEST(GameCreationOptions, DefaultsForRegisteredHostEnforceRegistration) +{ + const GameCreationOptions registered = GameCreationOptions::defaultsFor(true); + + EXPECT_TRUE(registered.onlyRegistered); + EXPECT_FALSE(registered.onlyBuddies); + EXPECT_EQ(registered.maxPlayers, 2); + EXPECT_TRUE(registered.spectatorsAllowed); + EXPECT_EQ(registered.startingLifeTotal, 20); + EXPECT_TRUE(registered.gameTypeIds.isEmpty()); +} + +TEST(GameCreationOptions, DefaultsForUnregisteredHostAllowOpenJoining) +{ + const GameCreationOptions unregistered = GameCreationOptions::defaultsFor(false); + + EXPECT_FALSE(unregistered.onlyRegistered); + EXPECT_FALSE(unregistered.onlyBuddies); +} + TEST(GameCreationOptions, FromSettingsCopiesEveryRememberedField) { const FakeGameSettings settings{GameCreationOptions{.description = "Remembered game",