diff --git a/cockatrice/src/game_graphics/player/menu/hand_menu.cpp b/cockatrice/src/game_graphics/player/menu/hand_menu.cpp index 4d27fd689..cdb30007d 100644 --- a/cockatrice/src/game_graphics/player/menu/hand_menu.cpp +++ b/cockatrice/src/game_graphics/player/menu/hand_menu.cpp @@ -6,6 +6,8 @@ #include "../player_graphics_item.h" #include +#include +#include #include #include #include @@ -24,8 +26,6 @@ #include #include #include -#include -#include #include class QWidget; @@ -33,6 +33,7 @@ class QWidget; HandMenu::HandMenu(PlayerGraphicsItem *_player, QWidget *parent) : TearOffMenu(parent), player(_player) { auto *actions = player->getLogic()->getPlayerActions(); + connect(actions, &PlayerActions::randomHandRevealFailed, this, &HandMenu::onRandomHandRevealFailed); if (player->getLogic()->getPlayerInfo()->local || player->getLogic()->getPlayerInfo()->judge) { aViewHand = new QAction(this); @@ -161,7 +162,7 @@ void HandMenu::retranslateUi() mRevealRandomHandCard->setTitle(tr("Reveal r&andom card to...")); aRevealRandomHandCardToAll->setText(tr("All players")); - mRevealRandomHandCards->setTitle(tr("Reveal random cards to...")); + mRevealRandomHandCards->setTitle(tr("Reveal a chosen number of random cards to...")); aRevealRandomHandCardsToAll->setText(tr("All players")); } } @@ -253,15 +254,19 @@ void HandMenu::onRevealRandomHandCardTriggered() void HandMenu::populateRevealRandomHandCardsMenuWithActivePlayers() { + const bool hasCards = !player->getLogic()->getHandZone()->getCards().isEmpty(); mRevealRandomHandCards->clear(); + aRevealRandomHandCardsToAll->setEnabled(hasCards); mRevealRandomHandCards->addAction(aRevealRandomHandCardsToAll); mRevealRandomHandCards->addSeparator(); - for (auto *other : player->getLogic()->getGame()->getPlayerManager()->getPlayers()) { + const auto &players = player->getLogic()->getGame()->getPlayerManager()->getPlayers().values(); + for (auto *other : players) { if (other == player->getLogic()) { continue; } auto *action = mRevealRandomHandCards->addAction(other->getPlayerInfo()->getName()); action->setData(other->getPlayerInfo()->getId()); + action->setEnabled(hasCards); connect(action, &QAction::triggered, this, &HandMenu::onRevealRandomHandCardsTriggered); } } @@ -280,36 +285,45 @@ void HandMenu::onRevealRandomHandCardsTriggered() if (!client) { return; } - auto *dialog = new QInputDialog(this); + auto *dialog = new QInputDialog(dialogParent()); dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setWindowTitle(tr("Reveal Random Hand Cards")); - dialog->setLabelText(tr("Number of distinct random cards to reveal:")); + dialog->setLabelText(tr("Number of distinct random cards to reveal to %1:").arg(action->text())); dialog->setInputMode(QInputDialog::IntInput); dialog->setIntRange(1, handSize); dialog->setIntValue(qMin(2, handSize)); + connect(this, &QObject::destroyed, dialog, &QDialog::reject); connect(client, &QObject::destroyed, dialog, &QDialog::reject); connect(actions, &QObject::destroyed, dialog, &QDialog::reject); - connect(dialog, &QDialog::accepted, this, [this, dialog, actions, client, targetId] { + connect(dialog, &QDialog::accepted, this, [dialog, actions, client, targetId] { if (!actions || !client) { return; } - auto *pending = actions->prepareRandomHandReveal(targetId, dialog->intValue()); - connect(pending, &PendingCommand::finished, this, [this, actions, client](const Response &response) { - if (!actions || !client || response.response_code() == Response::RespOk) { - return; - } - const QString text = - response.response_code() == Response::RespInvalidCommand - ? tr("This server does not support revealing multiple random cards. No cards were revealed.") - : tr("The random reveal could not be completed. The hand or recipient may have changed."); - auto *message = - new QMessageBox(QMessageBox::Warning, tr("Reveal Random Hand Cards"), text, QMessageBox::Ok, this); - message->setAttribute(Qt::WA_DeleteOnClose); - connect(actions, &QObject::destroyed, message, &QDialog::reject); - connect(client, &QObject::destroyed, message, &QDialog::reject); - message->open(); - }); - actions->sendGameCommand(pending); + actions->actRevealRandomHandCards(targetId, dialog->intValue()); }); dialog->open(); } + +QWidget *HandMenu::dialogParent() const +{ + if (auto *view = player->scene() ? player->scene()->views().value(0) : nullptr) { + return view->window(); + } + return nullptr; +} + +void HandMenu::onRandomHandRevealFailed(bool unsupported) +{ + const QString text = + unsupported ? tr("This server does not support revealing multiple random cards. No cards were revealed.") + : tr("The random reveal could not be completed. The hand or recipient may have changed."); + auto *message = new QMessageBox(QMessageBox::Warning, tr("Reveal Random Hand Cards"), text, QMessageBox::Ok, + dialogParent()); + message->setAttribute(Qt::WA_DeleteOnClose); + connect(this, &QObject::destroyed, message, &QDialog::reject); + connect(player->getLogic()->getPlayerActions(), &QObject::destroyed, message, &QDialog::reject); + if (auto *client = player->getLogic()->getGame()->getClientForPlayer(player->getLogic()->getPlayerInfo()->getId())) { + connect(client, &QObject::destroyed, message, &QDialog::reject); + } + message->open(); +} diff --git a/cockatrice/src/game_graphics/player/menu/hand_menu.h b/cockatrice/src/game_graphics/player/menu/hand_menu.h index ed4df28c5..c2ca110de 100644 --- a/cockatrice/src/game_graphics/player/menu/hand_menu.h +++ b/cockatrice/src/game_graphics/player/menu/hand_menu.h @@ -44,8 +44,10 @@ private slots: void onRevealRandomHandCardTriggered(); void populateRevealRandomHandCardsMenuWithActivePlayers(); void onRevealRandomHandCardsTriggered(); + void onRandomHandRevealFailed(bool unsupported); private: + QWidget *dialogParent() const; PlayerGraphicsItem *player; QAction *aViewHand = nullptr; diff --git a/doc/doxygen/pages/user_documentation/gameplay/revealing_cards.md b/doc/doxygen/pages/user_documentation/gameplay/revealing_cards.md index 4c6956878..bac74cff7 100644 --- a/doc/doxygen/pages/user_documentation/gameplay/revealing_cards.md +++ b/doc/doxygen/pages/user_documentation/gameplay/revealing_cards.md @@ -1,6 +1,6 @@ @page revealing_cards Revealing Cards -The **Hand** menu offers **Reveal random cards to...**. Choose **All players** or a specific player, then choose how many cards to reveal. +The **Hand** menu offers **Reveal a chosen number of random cards to...**. Choose **All players** or a specific player, then choose how many cards to reveal. The count dialog displays the chosen recipient. The server chooses distinct cards without replacement. Two copies of the same card are separate physical cards and can both be chosen. The operation does not reorder, move, or otherwise change the hand. diff --git a/libcockatrice_game/libcockatrice/game/player/player_actions.cpp b/libcockatrice_game/libcockatrice/game/player/player_actions.cpp index c506b1774..fe152ec88 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_actions.cpp +++ b/libcockatrice_game/libcockatrice/game/player/player_actions.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +68,8 @@ #include #include #include +#include +#include #include #include #include @@ -1756,15 +1760,25 @@ void PlayerActions::actRevealRandomHandCard(int revealToPlayerId) sendGameCommand(cmd); } -PendingCommand *PlayerActions::prepareRandomHandReveal(int revealToPlayerId, int amount) +void PlayerActions::actRevealRandomHandCards(int revealToPlayerId, int amount) { + QPointer client = player->getGame()->getClientForPlayer(player->getPlayerInfo()->getId()); + if (!client) { + return; + } Command_RevealRandomCards cmd; if (revealToPlayerId != -1) { cmd.set_player_id(revealToPlayerId); } cmd.set_zone_name(ZoneNames::HAND); cmd.set_number_of_cards(amount); - return prepareGameCommand(cmd); + auto *pending = prepareGameCommand(cmd); + connect(pending, &PendingCommand::finished, this, [this, client](const Response &response) { + if (client && response.response_code() != Response::RespOk) { + emit randomHandRevealFailed(response.response_code() == Response::RespInvalidCommand); + } + }); + sendGameCommand(pending); } void PlayerActions::actRevealLibrary(int revealToPlayerId) diff --git a/libcockatrice_game/libcockatrice/game/player/player_actions.h b/libcockatrice_game/libcockatrice/game/player/player_actions.h index 13991bea7..8baac760e 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_actions.h +++ b/libcockatrice_game/libcockatrice/game/player/player_actions.h @@ -83,7 +83,6 @@ public: * @param numberCards How many cards to dump, -1 for all. */ PendingCommand *prepareZoneDump(CardZoneLogic *zone, int numberCards); - PendingCommand *prepareRandomHandReveal(int revealToPlayerId, int amount); /** * @brief Sets the tapped state of the cards that do not already have it. @@ -121,6 +120,7 @@ public: } signals: + void randomHandRevealFailed(bool unsupported); void requestViewTopCardsDialog(int defaultNumberTopCards, int deckSize); void requestViewBottomCardsDialog(int defaultNumberBottomCards, int deckSize); void requestShuffleTopDialog(int defaultNumberTopCards, int maxCards); @@ -280,6 +280,7 @@ public slots: void actReveal(QList selectedCards, QAction *action); void actRevealHand(int revealToPlayerId); void actRevealRandomHandCard(int revealToPlayerId); + void actRevealRandomHandCards(int revealToPlayerId, int amount); void actRevealLibrary(int revealToPlayerId); void actSortHand();