mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-10 20:13:24 -08:00
Add instant quick host to the game lobby
This commit is contained in:
parent
39220a2ea9
commit
8301c20efd
7 changed files with 216 additions and 13 deletions
|
|
@ -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)},
|
||||
|
|
|
|||
|
|
@ -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<int, QString> &gameTypes)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -49,6 +49,14 @@ struct GameCreationOptions
|
|||
[[nodiscard]] static GameCreationOptions
|
||||
fromSettings(const IGameSettingsProvider &settings, const QMap<int, QString> &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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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 <QHBoxLayout>
|
||||
#include <QHeaderView>
|
||||
#include <QItemSelectionModel>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMap>
|
||||
#include <QMenu>
|
||||
|
|
@ -31,6 +34,7 @@
|
|||
#include <QSize>
|
||||
#include <QString>
|
||||
#include <QStringLiteral>
|
||||
#include <QToolButton>
|
||||
#include <QTreeView>
|
||||
#include <QVBoxLayout>
|
||||
#include <QVariant>
|
||||
|
|
@ -40,6 +44,7 @@
|
|||
#include <libcockatrice/protocol/pb/room_commands.pb.h>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
#include <libcockatrice/settings/game_settings.h>
|
||||
#include <libcockatrice/settings/interface_settings.h>
|
||||
#include <libcockatrice/utility/qt_utils.h>
|
||||
#include <qlogging.h>
|
||||
|
|
@ -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<TabRoom *>(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);
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor,
|
|||
|
||||
QMap<int, GameTypeMap> tempMap;
|
||||
tempMap.insert(info.room_id(), gameTypes);
|
||||
gameSelector = new GameSelector(client, tabSupervisor, this, QMap<int, QString>(), tempMap, true, true);
|
||||
gameSelector = new GameSelector(client, tabSupervisor, this, QMap<int, QString>(), 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)
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue