mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-08 02:53:25 -08:00
* [DeckList] Apply include-what-you-use to libcockatrice_deck_list * [IWYU] Apply include-what-you-use across the tree Run one whole-tree IWYU pass over the libcockatrice_* libraries, the Cockatrice client, Oracle, Servatrice and the test suite, so each translation unit includes what it uses directly and forward-declares what it does not need. IWYU only analyses real translation units, never the moc code CMake generates from Q_OBJECT headers, so it strips includes that moc output silently relies on. Where a type is passed by value (or nested in a QList/QMap) in a signal or slot signature, the generated moc code needs the complete type; those includes are kept with an explanatory `// IWYU pragma: keep`. The same holds for Q_DECLARE_METATYPE targets such as QSqlDatabase. A handful of includes are added by hand where callers brace-initialize a const-reference parameter, which also requires the complete type. Applied as a single commit on purpose: stripping includes from the libcockatrice_* headers is precisely what breaks the client, test and server translation units, and the fixes for those live in the same translation units. Splitting by component would therefore leave intermediate commits that do not compile. All 426 modified Q_OBJECT/Q_GADGET headers were verified to be moc-self-contained by compiling each generated moc_*.cpp standalone; several built only because of moc include ordering in mocs_compilation.cpp before the keep-includes were added. * [Tooling] Emit public Qt headers and fix IWYU fallout CI found Three problems the local build could not see, all because a green local build only proves the configuration it was run under. Private Qt headers. IWYU emitted private, lowercase Qt headers, and qtypes.h does not exist at all before Qt 6.9, so the Debian 12 and Ubuntu 24.04 jobs on Qt 6.4.2 failed to compile. The generator only emitted a mapping when the capital header name mirrored the private one (QAbstractAnimation for qabstractanimation.h), so qtypes.h, which is wrapped by QtTypes instead, got no mapping and kept the private spelling. Map it explicitly to <QtTypes>, which is public since Qt 5 and provides the same typedefs either way, and rewrite the 100 affected includes. Do not generalize this to "prefer any Qt* wrapper". The table is generated from whichever Qt is installed, and the wrappers are not equally old: QtLogging only appeared in Qt 6.8 and QtMinMax in 6.5, so that heuristic trades one breakage for another. Every other lowercase header still in the tree has shipped publicly since Qt 5, so those are left alone. Platform-guarded includes. IWYU only sees the preprocessor configuration it is handed, so it dropped an include that only the macOS branch of main.cpp needs, and the macOS jobs failed on an undeclared UrlSchemeEventFilter. Restore the include. Force-compiling every file that carries a platform guard under Q_OS_MAC, Q_OS_MACOS and __APPLE__ reports nothing else, so the macOS branches are covered rather than assumed. shellcheck. Drop a leftover variable and stop assigning the include-what-you-use lookup, which was only ever an existence check. * [Tooling] Map Qt 6.5+ split headers to QtGlobal for Qt 6.4 compatibility IWYU emitted QtTypes, QtPreprocessorSupport, QtVersionChecks, QtAssert, QtMinMax, QtClassHelperMacros, QtEnvironmentVariables and QtTranslation. Qt 6.5 split those declarations out of qglobal.h into separate headers, so neither the Qt* wrapper nor the q*.h private name exists on the oldest Qt we support (6.4.2), and Debian 12 / Ubuntu 24.04 fail with "No such file or directory". qtypes.h is worse still: it only appeared in Qt 6.9. QtGlobal is the one spelling that works across the whole supported range. It has been public since Qt 5, defines these declarations itself up to Qt 6.4, and includes the split headers from Qt 6.5 on, so redirect all eight private headers to it and collapse the affected includes in the tree. Curated overrides now take precedence over the name-mirroring rule, which cannot know about Qt version floors because the mapping is generated from whatever Qt is installed. That rule was mapping qtversionchecks.h to <QtVersionChecks> even with an override present, because the override was only consulted as a fallback. A curated key that matches nothing is now a hard error rather than a silent fallback, and the redirected headers are no longer advertised as the home of a symbol. Verified every Qt include in the tree resolves against the real qt6-base-dev 6.4.2 package: 15 distinct includes, 0 missing. * [Tooling] Map QOverload to QtGlobal and drop the QMetaMethodArgument include Two more headers IWYU emitted do not exist on Qt 6.4.2: QOverload was split out of qglobal.h in Qt 6.5, so on 6.4.2 it is declared in qglobal.h directly and the standalone header is a hard error. QtGlobal covers both, so map qoverload.h to it like the other 6.5+ split headers. QMetaMethodArgument is never referenced anywhere in the tree. It is a bare forwarder for qobjectdefs.h that our generator advertised as a class symbol, which is how IWYU came to add the include at all. The symbol mappings are generated from header names, so they cannot tell a forwarder that happens to be capitalised from a real class; removing the unused include is the fix, and the generic case stays a known limitation of the generator. Extending the audit to every Q* header (an earlier pass only matched headers starting with "Qt" or lowercase q*.h, so it missed all of them) against the real qt6-base-dev, qt6-multimedia-dev, qt6-declarative-dev, qt6-svg-dev and qt6-websockets-dev 6.4.2 packages: 341 distinct Qt includes, 0 missing. * [Game] Give moc the complete CardItem type in player_actions.h player_actions.h forward declares CardItem but passes QList<CardItem *> by value in actPlay/actPlayFacedown/actHide. moc instantiates the metatype for those parameters, and on Qt 6.4 that expands to sizeof(CardItem), so Ubuntu 24.04 fails with "invalid application of 'sizeof' to incomplete type 'CardItem'". Newer Qt tolerates the incomplete type, which is why only the 6.4.2 jobs failed. Include the defining header with an IWYU pragma keep, matching how the other MOC-required includes in this area are handled. * [Game] Give moc the complete ServerInfo_Card type player_logic.h, game_scene.h, view_zone.h and view_zone_widget.h forward declare ServerInfo_Card but pass QList<const ServerInfo_Card *> through signal and slot signatures. moc instantiates the metatype for those parameters, which Qt 6.4 expands to sizeof(ServerInfo_Card), so the connect in game_scene.cpp fails on Debian 12 and Ubuntu 24.04 with "invalid application of 'sizeof' to incomplete type 'const ServerInfo_Card'". Newer Qt tolerates it, so only the 6.4.2 jobs failed. Include the generated protobuf header with an IWYU pragma keep, following the pattern already used for ServerInfo_Player and ServerInfo_User in game_event_handler.h. * [Client] Complete the types Qt 6.4 needs for connect and moc Qt 6.4 expands QMetaType::fromType on the normalised argument list of a PMF connect and on moc-generated metatypes, which requires sizeof on every signal and slot parameter. Newer Qt tolerates forward declarations there, so only the Debian 12 and Ubuntu 24.04 jobs failed, with "invalid application of 'sizeof' to incomplete type" for QMouseEvent, QPrinter and GameReplay. Replace the forward declarations with the real includes: - QMouseEvent in the nine widget headers that pass it through signals - QPrinter in abstract_tab_deck_editor.cpp, for the actPrintDeck lambda - game_replay.pb.h in tab_replays.h. GameReplay is a protobuf message, so the project's game/replay.h only forward declares it and cannot supply the complete type. Found by recompiling all 1918 project TUs against the Debian Qt 6.4.2 headers, which reproduces those two CI jobs locally in one pass. * [Client] Complete Release and QMenu for Qt 6.4 moc and connect Continuing the Qt 6.4 complete-type sweep: - window_main.cpp connects ClientUpdateChecker::finishedCheck to MainWindow::checkClientUpdatesFinished, whose Release * parameter made moc 6.4 instantiate the metatype. GameReplay turned out to be a protobuf message, so Release likewise needed release_channel.h in the three headers declaring those signals: window_main.h, dlg_update.h and client_update_checker.h. - moc 6.4 also needs sizeof(QMenu) for the QList<QMenu *> carried by TabSupervisor::setMenu and MainWindow::updateTabMenu. Verified by regenerating moc output with the Debian moc 6.4.2 for all 434 Q_OBJECT/Q_GADGET classes and compiling it, plus recompiling all 1612 source TUs, against the Qt 6.4.2 headers: no remaining failures. --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2059 lines
64 KiB
C++
2059 lines
64 KiB
C++
#include "player_actions.h"
|
|
|
|
#include "../../client/settings/cache_settings.h"
|
|
#include "../../game_graphics/board/card_item.h"
|
|
#include "../../game_graphics/dialogs/dlg_create_token.h"
|
|
#include "../../game_graphics/dialogs/dlg_move_top_cards_until.h"
|
|
#include "../../game_graphics/player/card_menu_action_type.h"
|
|
#include "../../game_graphics/zones/table_zone.h"
|
|
#include "../../interface/deck_loader/loaded_deck.h"
|
|
#include "../abstract_game.h"
|
|
#include "../board/counter_state.h"
|
|
#include "../game_event_handler.h"
|
|
#include "../zones/card_zone_logic.h"
|
|
#include "../zones/hand_zone_logic.h"
|
|
#include "../zones/pile_zone_logic.h"
|
|
#include "../zones/table_zone_logic.h"
|
|
#include "../zones/view_zone_logic.h"
|
|
#include "libcockatrice/card/database/card_database_querier.h"
|
|
#include "libcockatrice/card/printing/exact_card.h"
|
|
#include "libcockatrice/card/printing/printing_info.h"
|
|
#include "libcockatrice/card/relation/card_relation_type.h"
|
|
#include "libcockatrice/deck_list/deck_list.h"
|
|
#include "libcockatrice/filters/filter_string.h"
|
|
#include "libcockatrice/protocol/pb/card_attributes.pb.h"
|
|
#include "libcockatrice/protocol/pb/game_commands.pb.h"
|
|
#include "libcockatrice/settings/cards_display_settings.h"
|
|
#include "player_info.h"
|
|
#include "player_logic.h"
|
|
|
|
#include <QAction>
|
|
#include <QChar>
|
|
#include <QColor>
|
|
#include <QFlags>
|
|
#include <QInputDialog>
|
|
#include <QKeyEvent>
|
|
#include <QList>
|
|
#include <QMap>
|
|
#include <QMapIterator>
|
|
#include <QMetaType>
|
|
#include <QPoint>
|
|
#include <QPointF>
|
|
#include <QSharedPointer>
|
|
#include <QTimer>
|
|
#include <QVariant>
|
|
#include <QVariantList>
|
|
#include <QtGlobal>
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <google/protobuf/descriptor.h>
|
|
#include <google/protobuf/message.h>
|
|
#include <google/protobuf/stubs/port.h>
|
|
#include <libcockatrice/card/database/card_database_manager.h>
|
|
#include <libcockatrice/card/relation/card_relation.h>
|
|
#include <libcockatrice/protocol/pb/command_attach_card.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_change_zone_properties.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_create_token.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_draw_cards.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_flip_card.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_game_say.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_inc_counter.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_mulligan.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_reveal_cards.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_roll_die.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_set_card_attr.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_set_card_counter.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_shuffle.pb.h>
|
|
#include <libcockatrice/protocol/pb/command_undo_draw.pb.h>
|
|
#include <libcockatrice/settings/card_override_settings.h>
|
|
#include <libcockatrice/settings/interface_settings.h>
|
|
#include <libcockatrice/utility/clamped_arithmetic.h>
|
|
#include <libcockatrice/utility/counter_limits.h>
|
|
#include <libcockatrice/utility/expression.h>
|
|
#include <libcockatrice/utility/zone_names.h>
|
|
#include <memory>
|
|
#include <qnamespace.h>
|
|
#include <qnumeric.h>
|
|
#include <string>
|
|
|
|
// milliseconds in between triggers of the move top cards until action
|
|
static constexpr int MOVE_TOP_CARD_UNTIL_INTERVAL = 100;
|
|
|
|
PlayerActions::PlayerActions(PlayerLogic *_player)
|
|
: QObject(_player), player(_player), lastTokenTableRow(0), movingCardsUntil(false)
|
|
{
|
|
connect(this, &PlayerActions::requestZoneViewToggle, player, &PlayerLogic::onRequestZoneViewToggle);
|
|
|
|
moveTopCardTimer = new QTimer(this);
|
|
moveTopCardTimer->setInterval(MOVE_TOP_CARD_UNTIL_INTERVAL);
|
|
moveTopCardTimer->setSingleShot(true);
|
|
connect(moveTopCardTimer, &QTimer::timeout, [this]() { actMoveTopCardToPlay(); });
|
|
}
|
|
|
|
void PlayerActions::playCard(CardItem *card, bool faceDown)
|
|
{
|
|
if (card == nullptr) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_player_id(card->getZone()->getPlayer()->getPlayerInfo()->getId());
|
|
cmd.set_start_zone(card->getZone()->getName().toStdString());
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
CardToMove *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(card->getId());
|
|
|
|
ExactCard exactCard = card->getCard();
|
|
if (!exactCard) {
|
|
return;
|
|
}
|
|
|
|
const CardInfo &info = exactCard.getInfo();
|
|
|
|
int tableRow = info.getUiAttributes().tableRow;
|
|
bool playToStack = SettingsCache::instance().userInterface().getPlayToStack();
|
|
QString currentZone = card->getZone()->getName();
|
|
if (!faceDown && currentZone == ZoneNames::STACK && tableRow == 3) {
|
|
cmd.set_target_zone(ZoneNames::GRAVE);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
} else if (!faceDown && ((!playToStack && tableRow == 3) ||
|
|
((playToStack && tableRow != 0) && currentZone != ZoneNames::STACK))) {
|
|
cmd.set_target_zone(ZoneNames::STACK);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(0);
|
|
} else {
|
|
tableRow = faceDown ? 2 : info.getUiAttributes().tableRow;
|
|
QPoint gridPoint = QPoint(-1, TableZone::tableRowToGridY(tableRow));
|
|
cardToMove->set_face_down(faceDown);
|
|
if (!faceDown) {
|
|
cardToMove->set_pt(info.getPowTough().toStdString());
|
|
}
|
|
cardToMove->set_tapped(!faceDown && info.getUiAttributes().cipt);
|
|
if (tableRow != 3) {
|
|
cmd.set_target_zone(ZoneNames::TABLE);
|
|
}
|
|
cmd.set_x(gridPoint.x());
|
|
cmd.set_y(gridPoint.y());
|
|
}
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
/**
|
|
* Like {@link PlayerActions::playCard}, but forces the card to be played to the table zone.
|
|
* Cards with tablerow 3 (the stack) will be played to tablerow 1 (the noncreatures row).
|
|
*/
|
|
void PlayerActions::playCardToTable(const CardItem *card, bool faceDown)
|
|
{
|
|
if (card == nullptr) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_player_id(card->getZone()->getPlayer()->getPlayerInfo()->getId());
|
|
cmd.set_start_zone(card->getZone()->getName().toStdString());
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
CardToMove *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(card->getId());
|
|
|
|
ExactCard exactCard = card->getCard();
|
|
if (!exactCard) {
|
|
return;
|
|
}
|
|
|
|
const CardInfo &info = exactCard.getInfo();
|
|
|
|
int tableRow = faceDown ? 2 : info.getUiAttributes().tableRow;
|
|
QPoint gridPoint = QPoint(-1, TableZone::tableRowToGridY(tableRow));
|
|
cardToMove->set_face_down(faceDown);
|
|
if (!faceDown) {
|
|
cardToMove->set_pt(info.getPowTough().toStdString());
|
|
}
|
|
cardToMove->set_tapped(!faceDown && info.getUiAttributes().cipt);
|
|
cmd.set_target_zone(ZoneNames::TABLE);
|
|
cmd.set_x(gridPoint.x());
|
|
cmd.set_y(gridPoint.y());
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actViewLibrary()
|
|
{
|
|
emit requestZoneViewToggle(ZoneNames::DECK, -1);
|
|
}
|
|
|
|
void PlayerActions::actViewHand()
|
|
{
|
|
emit requestZoneViewToggle(ZoneNames::HAND, -1);
|
|
}
|
|
|
|
/**
|
|
* @brief The sortHand actions only pass along a single SortOption in its data.
|
|
* This method fills out the rest of the sort priority list given that option.
|
|
* @param option The single sort option
|
|
* @return The sort priority list
|
|
*/
|
|
static QList<CardList::SortOption> expandSortOption(CardList::SortOption option)
|
|
{
|
|
switch (option) {
|
|
case CardList::SortByName:
|
|
return {};
|
|
case CardList::SortByMainType:
|
|
return {CardList::SortByMainType, CardList::SortByManaValue};
|
|
case CardList::SortByManaValue:
|
|
return {CardList::SortByManaValue, CardList::SortByColors};
|
|
default:
|
|
return {option};
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actSortHand()
|
|
{
|
|
auto *action = qobject_cast<QAction *>(sender());
|
|
CardList::SortOption option = static_cast<CardList::SortOption>(action->data().toInt());
|
|
|
|
QList<CardList::SortOption> sortOptions = expandSortOption(option);
|
|
|
|
static QList defaultOptions = {CardList::SortByName, CardList::SortByPrinting};
|
|
|
|
emit requestSortHand(sortOptions + defaultOptions);
|
|
}
|
|
|
|
void PlayerActions::actRequestViewTopCardsDialog()
|
|
{
|
|
emit requestViewTopCardsDialog(defaultNumberTopCards, player->getDeckZone()->getCards().size());
|
|
}
|
|
|
|
void PlayerActions::actViewTopCards(int number)
|
|
{
|
|
defaultNumberTopCards = number;
|
|
emit requestZoneViewToggle(ZoneNames::DECK, number);
|
|
}
|
|
|
|
void PlayerActions::actRequestViewBottomCardsDialog()
|
|
{
|
|
emit requestViewBottomCardsDialog(defaultNumberBottomCards, player->getDeckZone()->getCards().size());
|
|
}
|
|
|
|
void PlayerActions::actViewBottomCards(int number)
|
|
{
|
|
defaultNumberBottomCards = number;
|
|
emit requestZoneViewToggle(ZoneNames::DECK, number, true);
|
|
}
|
|
|
|
void PlayerActions::actAlwaysRevealTopCard(bool alwaysRevealTopCard)
|
|
{
|
|
Command_ChangeZoneProperties cmd;
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_always_reveal_top_card(alwaysRevealTopCard);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actAlwaysLookAtTopCard(bool alwaysRevealTopCard)
|
|
{
|
|
Command_ChangeZoneProperties cmd;
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_always_look_at_top_card(alwaysRevealTopCard);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actOpenDeckInDeckEditor()
|
|
{
|
|
emit player->openDeckEditor({.deckList = player->getDeck()});
|
|
}
|
|
|
|
void PlayerActions::actViewGraveyard()
|
|
{
|
|
emit requestZoneViewToggle(ZoneNames::GRAVE, -1);
|
|
}
|
|
|
|
void PlayerActions::actViewRfg()
|
|
{
|
|
emit requestZoneViewToggle(ZoneNames::EXILE, -1);
|
|
}
|
|
|
|
void PlayerActions::actViewSideboard()
|
|
{
|
|
emit requestZoneViewToggle(ZoneNames::SIDEBOARD, -1);
|
|
}
|
|
|
|
void PlayerActions::actShuffle()
|
|
{
|
|
sendGameCommand(Command_Shuffle());
|
|
}
|
|
|
|
void PlayerActions::actRequestShuffleTopDialog()
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
emit requestShuffleTopDialog(defaultNumberTopCards, maxCards);
|
|
}
|
|
|
|
void PlayerActions::actShuffleTop(int number)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
if (number > maxCards) {
|
|
number = maxCards;
|
|
}
|
|
|
|
defaultNumberTopCards = number;
|
|
|
|
Command_Shuffle cmd;
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_start(0);
|
|
cmd.set_end(number - 1); // inclusive, the indexed card at end will be shuffled
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestShuffleBottomDialog()
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
emit requestShuffleBottomDialog(defaultNumberBottomCards, maxCards);
|
|
}
|
|
|
|
void PlayerActions::actShuffleBottom(int number)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
if (number > maxCards) {
|
|
number = maxCards;
|
|
}
|
|
|
|
defaultNumberBottomCards = number;
|
|
|
|
Command_Shuffle cmd;
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_start(-number);
|
|
cmd.set_end(-1);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actDrawCard()
|
|
{
|
|
Command_DrawCards cmd;
|
|
cmd.set_number(1);
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestMulliganDialog()
|
|
{
|
|
int startSize = SettingsCache::instance().userInterface().getStartingHandSize();
|
|
int handSize = player->getHandZone()->getCards().size();
|
|
int deckSize = player->getDeckZone()->getCards().size() + handSize;
|
|
|
|
emit requestMulliganDialog(startSize, handSize, deckSize);
|
|
}
|
|
|
|
void PlayerActions::actMulligan(int number)
|
|
{
|
|
int handSize = player->getHandZone()->getCards().size();
|
|
|
|
if (number < 1) {
|
|
number = handSize + number;
|
|
}
|
|
|
|
doMulligan(number);
|
|
SettingsCache::instance().userInterface().setStartingHandSize(number);
|
|
}
|
|
|
|
void PlayerActions::actMulliganSameSize()
|
|
{
|
|
int handSize = player->getHandZone()->getCards().size();
|
|
doMulligan(handSize);
|
|
}
|
|
|
|
void PlayerActions::actMulliganMinusOne()
|
|
{
|
|
int handSize = player->getHandZone()->getCards().size();
|
|
int targetSize = qMax(1, handSize - 1);
|
|
doMulligan(targetSize);
|
|
}
|
|
|
|
void PlayerActions::doMulligan(int number)
|
|
{
|
|
if (number < 1) {
|
|
return;
|
|
}
|
|
|
|
Command_Mulligan cmd;
|
|
cmd.set_number(number);
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestDrawCardsDialog()
|
|
{
|
|
int deckSize = player->getDeckZone()->getCards().size();
|
|
|
|
emit requestDrawCardsDialog(defaultNumberTopCards, deckSize);
|
|
}
|
|
|
|
void PlayerActions::actDrawCards(int number)
|
|
{
|
|
defaultNumberTopCards = number;
|
|
Command_DrawCards cmd;
|
|
cmd.set_number(static_cast<google::protobuf::uint32>(number));
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actUndoDraw()
|
|
{
|
|
sendGameCommand(Command_UndoDraw());
|
|
}
|
|
|
|
void PlayerActions::cmdSetTopCard(Command_MoveCard &cmd)
|
|
{
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
auto *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(0);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
}
|
|
|
|
void PlayerActions::cmdSetBottomCard(Command_MoveCard &cmd)
|
|
{
|
|
CardZoneLogic *zone = player->getDeckZone();
|
|
int lastCard = zone->getCards().size() - 1;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
auto *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(lastCard);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardToGrave()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetTopCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::GRAVE);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardToExile()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetTopCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::EXILE);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardsToGrave()
|
|
{
|
|
actRequestMoveTopCardsToDialog(ZoneNames::GRAVE, tr("grave"), false);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardsToGraveFaceDown()
|
|
{
|
|
actRequestMoveTopCardsToDialog(ZoneNames::GRAVE, tr("grave"), true);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardsToExile()
|
|
{
|
|
actRequestMoveTopCardsToDialog(ZoneNames::EXILE, tr("exile"), false);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardsToExileFaceDown()
|
|
{
|
|
actRequestMoveTopCardsToDialog(ZoneNames::EXILE, tr("exile"), true);
|
|
}
|
|
|
|
void PlayerActions::actRequestMoveTopCardsToDialog(const QString &targetZone,
|
|
const QString &zoneDisplayName,
|
|
bool faceDown)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
emit requestMoveTopCardsToDialog(defaultNumberTopCards, maxCards, targetZone, zoneDisplayName, faceDown);
|
|
}
|
|
|
|
void PlayerActions::moveTopCardsTo(int number, const QString &targetZone, bool faceDown)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
if (number > maxCards) {
|
|
number = maxCards;
|
|
}
|
|
defaultNumberTopCards = number;
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd.set_target_zone(targetZone.toStdString());
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
for (int i = number - 1; i >= 0; --i) {
|
|
auto card = cmd.mutable_cards_to_move()->add_card();
|
|
card->set_card_id(i);
|
|
if (faceDown) {
|
|
card->set_face_down(true);
|
|
}
|
|
}
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestMoveTopCardsUntilDialog()
|
|
{
|
|
stopMoveTopCardsUntil();
|
|
|
|
emit requestMoveTopCardsUntilDialog(movingCardsUntilOptions);
|
|
}
|
|
|
|
void PlayerActions::moveTopCardsUntil(const QString &expr, MoveTopCardsUntilOptions options)
|
|
{
|
|
movingCardsUntilOptions = options;
|
|
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
stopMoveTopCardsUntil();
|
|
} else {
|
|
movingCardsUntilFilter = FilterString(expr);
|
|
movingCardsUntilCounter = movingCardsUntilOptions.numberOfHits;
|
|
movingCardsUntil = true;
|
|
actMoveTopCardToPlay();
|
|
}
|
|
}
|
|
|
|
void PlayerActions::moveOneCardUntil(CardItem *card)
|
|
{
|
|
moveTopCardTimer->stop();
|
|
|
|
const bool isMatch = card && movingCardsUntilFilter.check(card->getCard().getCardPtr());
|
|
|
|
if (isMatch && movingCardsUntilOptions.autoPlay) {
|
|
// Directly calling playCard will deadlock, since we are already in the middle of processing an event.
|
|
// Use QTimer::singleShot to queue up the playCard on the event loop.
|
|
QTimer::singleShot(0, this, [card, this] { playCard(card, false); });
|
|
}
|
|
|
|
if (player->getDeckZone()->getCards().empty() || !card) {
|
|
stopMoveTopCardsUntil();
|
|
} else if (isMatch) {
|
|
--movingCardsUntilCounter;
|
|
if (movingCardsUntilCounter > 0) {
|
|
moveTopCardTimer->start();
|
|
} else {
|
|
stopMoveTopCardsUntil();
|
|
}
|
|
} else {
|
|
moveTopCardTimer->start();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Immediately stops any ongoing `play top card to stack until...` process, resetting all variables involved.
|
|
*/
|
|
void PlayerActions::stopMoveTopCardsUntil()
|
|
{
|
|
moveTopCardTimer->stop();
|
|
movingCardsUntilCounter = 0;
|
|
movingCardsUntil = false;
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardToBottom()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetTopCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::DECK);
|
|
cmd.set_x(-1); // bottom of deck
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardToPlay()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetTopCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::STACK);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveTopCardToPlayFaceDown()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
CardToMove *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(0);
|
|
cardToMove->set_face_down(true);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd.set_target_zone(ZoneNames::TABLE);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardToGrave()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetBottomCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::GRAVE);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardToExile()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetBottomCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::EXILE);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardsToGrave()
|
|
{
|
|
actRequestMoveBottomCardsToDialog(ZoneNames::GRAVE, tr("grave"), false);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardsToGraveFaceDown()
|
|
{
|
|
actRequestMoveBottomCardsToDialog(ZoneNames::GRAVE, tr("grave"), true);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardsToExile()
|
|
{
|
|
actRequestMoveBottomCardsToDialog(ZoneNames::EXILE, tr("exile"), false);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardsToExileFaceDown()
|
|
{
|
|
actRequestMoveBottomCardsToDialog(ZoneNames::EXILE, tr("exile"), true);
|
|
}
|
|
|
|
void PlayerActions::actRequestMoveBottomCardsToDialog(const QString &targetZone,
|
|
const QString &zoneDisplayName,
|
|
bool faceDown)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
emit requestMoveBottomCardsToDialog(defaultNumberBottomCards, maxCards, targetZone, zoneDisplayName, faceDown);
|
|
}
|
|
|
|
void PlayerActions::moveBottomCardsTo(int number, const QString &targetZone, bool faceDown)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
if (number > maxCards) {
|
|
number = maxCards;
|
|
}
|
|
defaultNumberBottomCards = number;
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd.set_target_zone(targetZone.toStdString());
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
for (int i = maxCards - number; i < maxCards; ++i) {
|
|
auto card = cmd.mutable_cards_to_move()->add_card();
|
|
card->set_card_id(i);
|
|
if (faceDown) {
|
|
card->set_face_down(true);
|
|
}
|
|
}
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardToTop()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetBottomCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::DECK);
|
|
cmd.set_x(0); // top of deck
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
/**
|
|
* Selects all cards in the given zone.
|
|
*
|
|
* @param zone The zone to select from
|
|
* @param filter A predicate to filter which cards are selected. Defaults to always returning true.
|
|
*/
|
|
static void selectCardsInZone(
|
|
const CardZoneLogic *zone,
|
|
std::function<bool(const CardItem *)> filter = [](const CardItem *) { return true; })
|
|
{
|
|
if (!zone) {
|
|
return;
|
|
}
|
|
|
|
for (auto &cardItem : zone->getCards()) {
|
|
if (cardItem && filter(cardItem)) {
|
|
cardItem->setSelected(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actSelectAll()
|
|
{
|
|
const CardItem *card = player->getGame()->getActiveCard();
|
|
if (!card) {
|
|
return;
|
|
}
|
|
|
|
selectCardsInZone(card->getZone());
|
|
}
|
|
|
|
void PlayerActions::actSelectRow()
|
|
{
|
|
const CardItem *card = player->getGame()->getActiveCard();
|
|
if (!card) {
|
|
return;
|
|
}
|
|
|
|
auto isSameRow = [card](const CardItem *cardItem) {
|
|
return qAbs(card->scenePos().y() - cardItem->scenePos().y()) < 50;
|
|
};
|
|
selectCardsInZone(card->getZone(), isSameRow);
|
|
}
|
|
|
|
void PlayerActions::actSelectColumn()
|
|
{
|
|
const CardItem *card = player->getGame()->getActiveCard();
|
|
if (!card) {
|
|
return;
|
|
}
|
|
|
|
auto isSameColumn = [card](const CardItem *cardItem) { return cardItem->x() == card->x(); };
|
|
selectCardsInZone(card->getZone(), isSameColumn);
|
|
}
|
|
|
|
void PlayerActions::actDrawBottomCard()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetBottomCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::HAND);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestDrawBottomCardsDialog()
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
emit requestDrawBottomCardsDialog(defaultNumberBottomCards, maxCards);
|
|
}
|
|
|
|
void PlayerActions::actDrawBottomCards(int number)
|
|
{
|
|
const int maxCards = player->getDeckZone()->getCards().size();
|
|
if (maxCards == 0) {
|
|
return;
|
|
}
|
|
|
|
if (number > maxCards) {
|
|
number = maxCards;
|
|
}
|
|
defaultNumberBottomCards = number;
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd.set_target_zone(ZoneNames::HAND);
|
|
cmd.set_x(0);
|
|
cmd.set_y(0);
|
|
|
|
for (int i = maxCards - number; i < maxCards; ++i) {
|
|
cmd.mutable_cards_to_move()->add_card()->set_card_id(i);
|
|
}
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardToPlay()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
Command_MoveCard cmd;
|
|
cmdSetBottomCard(cmd);
|
|
cmd.set_target_zone(ZoneNames::STACK);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actMoveBottomCardToPlayFaceDown()
|
|
{
|
|
if (player->getDeckZone()->getCards().empty()) {
|
|
return;
|
|
}
|
|
|
|
CardZoneLogic *zone = player->getDeckZone();
|
|
int lastCard = zone->getCards().size() - 1;
|
|
|
|
Command_MoveCard cmd;
|
|
cmd.set_start_zone(ZoneNames::DECK);
|
|
auto *cardToMove = cmd.mutable_cards_to_move()->add_card();
|
|
cardToMove->set_card_id(lastCard);
|
|
cardToMove->set_face_down(true);
|
|
|
|
cmd.set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd.set_target_zone(ZoneNames::TABLE);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actUntapAll()
|
|
{
|
|
Command_SetCardAttr cmd;
|
|
cmd.set_zone(ZoneNames::TABLE);
|
|
cmd.set_attribute(AttrTapped);
|
|
cmd.set_attr_value("0");
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestRollDieDialog()
|
|
{
|
|
emit requestRollDieDialog();
|
|
}
|
|
|
|
void PlayerActions::actRollDie(int sides, int count)
|
|
{
|
|
Command_RollDie cmd;
|
|
cmd.set_sides(sides);
|
|
cmd.set_count(count);
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actFlipCoin()
|
|
{
|
|
Command_RollDie cmd;
|
|
cmd.set_sides(2);
|
|
cmd.set_count(1);
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestCreateTokenDialog(const QStringList &predefinedTokens)
|
|
{
|
|
emit requestCreateTokenDialog(predefinedTokens);
|
|
}
|
|
|
|
void PlayerActions::actCreateToken(TokenInfo tokenToCreate)
|
|
{
|
|
lastTokenInfo = tokenToCreate;
|
|
|
|
ExactCard correctedCard = CardDatabaseManager::query()->guessCard({lastTokenInfo.name, lastTokenInfo.providerId});
|
|
if (correctedCard) {
|
|
lastTokenInfo.name = correctedCard.getName();
|
|
int tableRow = lastTokenInfo.faceDown ? 2 : correctedCard.getInfo().getUiAttributes().tableRow;
|
|
lastTokenTableRow = TableZone::tableRowToGridY(tableRow);
|
|
if (lastTokenInfo.pt.isEmpty()) {
|
|
lastTokenInfo.pt = correctedCard.getInfo().getPowTough();
|
|
}
|
|
}
|
|
|
|
emit requestEnableAndSetCreateAnotherTokenAction(lastTokenInfo.name);
|
|
actCreateAnotherToken();
|
|
}
|
|
|
|
void PlayerActions::actCreateAnotherToken()
|
|
{
|
|
if (lastTokenInfo.name.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
Command_CreateToken cmd;
|
|
cmd.set_zone(ZoneNames::TABLE);
|
|
cmd.set_card_name(lastTokenInfo.name.toStdString());
|
|
cmd.set_card_provider_id(lastTokenInfo.providerId.toStdString());
|
|
cmd.set_color(lastTokenInfo.color.toStdString());
|
|
cmd.set_pt(lastTokenInfo.pt.toStdString());
|
|
cmd.set_annotation(lastTokenInfo.annotation.toStdString());
|
|
cmd.set_destroy_on_zone_change(lastTokenInfo.destroy);
|
|
cmd.set_face_down(lastTokenInfo.faceDown);
|
|
cmd.set_x(-1);
|
|
cmd.set_y(lastTokenTableRow);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::setLastToken(CardInfoPtr cardInfo)
|
|
{
|
|
if (cardInfo == nullptr) {
|
|
return;
|
|
}
|
|
|
|
emit requestSetLastToken(cardInfo);
|
|
}
|
|
|
|
void PlayerActions::setLastTokenInfo(CardInfoPtr cardInfo)
|
|
{
|
|
if (cardInfo == nullptr) {
|
|
return;
|
|
}
|
|
|
|
lastTokenInfo = {
|
|
.name = cardInfo->getName(),
|
|
.color = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().left(1).toLower(),
|
|
.pt = cardInfo->getPowTough(),
|
|
.annotation = SettingsCache::instance().userInterface().getAnnotateTokens() ? cardInfo->getText() : "",
|
|
.destroy = true,
|
|
.providerId = SettingsCache::instance().cardOverrides().getCardPreferenceOverride(cardInfo->getName())};
|
|
|
|
lastTokenTableRow = TableZone::tableRowToGridY(cardInfo->getUiAttributes().tableRow);
|
|
|
|
emit requestEnableAndSetCreateAnotherTokenAction(lastTokenInfo.name);
|
|
}
|
|
|
|
void PlayerActions::actCreatePredefinedToken()
|
|
{
|
|
auto *action = static_cast<QAction *>(sender());
|
|
CardInfoPtr cardInfo = CardDatabaseManager::query()->getCardInfo(action->text());
|
|
if (!cardInfo) {
|
|
return;
|
|
}
|
|
|
|
setLastToken(cardInfo);
|
|
|
|
actCreateAnotherToken();
|
|
}
|
|
|
|
void PlayerActions::actCreateRelatedCard()
|
|
{
|
|
const CardItem *sourceCard = player->getGame()->getActiveCard();
|
|
|
|
if (!sourceCard) {
|
|
return;
|
|
}
|
|
|
|
auto *action = static_cast<QAction *>(sender());
|
|
// If there is a better way of passing a CardRelation through a QAction, please add it here.
|
|
auto relatedCards = sourceCard->getCardInfo().getAllRelatedCards();
|
|
|
|
CardRelation *cardRelation = relatedCards.at(action->data().toInt());
|
|
actRequestCreateRelatedFromRelationDialog(sourceCard, cardRelation);
|
|
}
|
|
|
|
void PlayerActions::actCreateAllRelatedCards()
|
|
{
|
|
const CardItem *sourceCard = player->getGame()->getActiveCard();
|
|
if (!sourceCard) {
|
|
return;
|
|
}
|
|
|
|
auto relatedCards = sourceCard->getCardInfo().getAllRelatedCards();
|
|
if (relatedCards.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
CardRelation *cardRelation = nullptr;
|
|
int tokensTypesCreated = 0;
|
|
|
|
if (relatedCards.length() == 1) {
|
|
cardRelation = relatedCards.at(0);
|
|
lastRelatedCreationSucceeded = false; // reset before emit
|
|
actRequestCreateRelatedFromRelationDialog(sourceCard, cardRelation);
|
|
if (lastRelatedCreationSucceeded) {
|
|
++tokensTypesCreated;
|
|
}
|
|
} else {
|
|
QList<CardRelation *> nonExcludedRelatedCards;
|
|
QString dbName;
|
|
for (CardRelation *cardRelationTemp : relatedCards) {
|
|
if (!cardRelationTemp->getIsCreateAllExclusion() && !cardRelationTemp->getDoesAttach()) {
|
|
nonExcludedRelatedCards.append(cardRelationTemp);
|
|
}
|
|
}
|
|
switch (nonExcludedRelatedCards.length()) {
|
|
case 1:
|
|
cardRelation = nonExcludedRelatedCards.at(0);
|
|
lastRelatedCreationSucceeded = false; // reset before emit
|
|
actRequestCreateRelatedFromRelationDialog(sourceCard, cardRelation);
|
|
if (lastRelatedCreationSucceeded) {
|
|
++tokensTypesCreated;
|
|
}
|
|
break;
|
|
|
|
// If all are marked "Exclude", then treat the situation as if none of them are.
|
|
// We won't accept "garbage in, garbage out", here.
|
|
case 0:
|
|
for (CardRelation *cardRelationAll : relatedCards) {
|
|
if (!cardRelationAll->getDoesAttach() && !cardRelationAll->getIsVariable()) {
|
|
dbName = cardRelationAll->getName();
|
|
bool persistent = cardRelationAll->getIsPersistent();
|
|
bool faceDown = cardRelationAll->getIsFaceDown();
|
|
for (int i = 0; i < cardRelationAll->getDefaultCount(); ++i) {
|
|
createCard(sourceCard, dbName, CardRelationType::DoesNotAttach, persistent, faceDown);
|
|
}
|
|
++tokensTypesCreated;
|
|
if (tokensTypesCreated == 1) {
|
|
cardRelation = cardRelationAll;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
default:
|
|
for (CardRelation *cardRelationNotExcluded : nonExcludedRelatedCards) {
|
|
if (!cardRelationNotExcluded->getDoesAttach() && !cardRelationNotExcluded->getIsVariable()) {
|
|
dbName = cardRelationNotExcluded->getName();
|
|
bool persistent = cardRelationNotExcluded->getIsPersistent();
|
|
bool faceDown = cardRelationNotExcluded->getIsFaceDown();
|
|
for (int i = 0; i < cardRelationNotExcluded->getDefaultCount(); ++i) {
|
|
createCard(sourceCard, dbName, CardRelationType::DoesNotAttach, persistent, faceDown);
|
|
}
|
|
++tokensTypesCreated;
|
|
if (tokensTypesCreated == 1) {
|
|
cardRelation = cardRelationNotExcluded;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* If we made at least one token via "Create All Tokens"
|
|
* then assign the first to the "Create another" shortcut.
|
|
*/
|
|
if (cardRelation != nullptr && cardRelation->getCanCreateAnother()) {
|
|
CardInfoPtr cardInfo = CardDatabaseManager::query()->getCardInfo(cardRelation->getName());
|
|
setLastToken(cardInfo);
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actRequestCreateRelatedFromRelationDialog(const CardItem *sourceCard,
|
|
const CardRelation *cardRelation)
|
|
{
|
|
emit requestCreateRelatedFromRelationDialog(sourceCard, cardRelation);
|
|
}
|
|
|
|
bool PlayerActions::createRelatedFromRelation(const CardItem *sourceCard,
|
|
const CardRelation *cardRelation,
|
|
int variableCount)
|
|
{
|
|
if (sourceCard == nullptr || cardRelation == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
const QString dbName = cardRelation->getName();
|
|
const bool persistent = cardRelation->getIsPersistent();
|
|
const bool faceDown = cardRelation->getIsFaceDown();
|
|
|
|
// Variable relations always use DoesNotAttach, regardless of the count the user
|
|
// entered.
|
|
if (cardRelation->getIsVariable()) {
|
|
if (variableCount <= 0) {
|
|
return false;
|
|
}
|
|
for (int i = 0; i < variableCount; ++i) {
|
|
createCard(sourceCard, dbName, CardRelationType::DoesNotAttach, persistent, faceDown);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const int count = cardRelation->getDefaultCount();
|
|
|
|
if (count > 1) {
|
|
for (int i = 0; i < count; ++i) {
|
|
createCard(sourceCard, dbName, CardRelationType::DoesNotAttach, persistent, faceDown);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
CardRelationType attachType;
|
|
// do not attempt to attach to another player's cards, this causes the card to attempt to attach to the same
|
|
// cardid on the local player's field instead, which is an entirely different card!
|
|
if (player->getPlayerInfo()->getLocalOrJudge()) {
|
|
attachType = cardRelation->getAttachType();
|
|
} else {
|
|
attachType = CardRelationType::DoesNotAttach;
|
|
}
|
|
|
|
// move card onto table first if attaching from some other zone
|
|
// we only do this for AttachTo because cross-zone TransformInto is already handled server-side
|
|
if (attachType == CardRelationType::AttachTo && sourceCard->getZone()->getName() != ZoneNames::TABLE) {
|
|
playCardToTable(sourceCard, false);
|
|
}
|
|
|
|
createCard(sourceCard, dbName, attachType, persistent, faceDown);
|
|
return true;
|
|
}
|
|
|
|
void PlayerActions::onRelatedCardCreated(const CardItem *sourceCard, const CardRelation *cardRelation)
|
|
{
|
|
if (sourceCard == nullptr || cardRelation == nullptr) {
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* If we make a token via "Token: TokenName"
|
|
* then let's allow it to be created via "create another token"
|
|
*/
|
|
if (!cardRelation->getCanCreateAnother()) {
|
|
return;
|
|
}
|
|
|
|
ExactCard relatedCard =
|
|
CardDatabaseManager::query()->getCardFromSameSet(cardRelation->getName(), sourceCard->getCard().getPrinting());
|
|
|
|
setLastToken(relatedCard.getCardPtr());
|
|
}
|
|
|
|
void PlayerActions::createCard(const CardItem *sourceCard,
|
|
const QString &dbCardName,
|
|
CardRelationType attachType,
|
|
bool persistent,
|
|
bool faceDown)
|
|
{
|
|
CardInfoPtr cardInfo = CardDatabaseManager::query()->getCardInfo(dbCardName);
|
|
|
|
if (cardInfo == nullptr || sourceCard == nullptr) {
|
|
return;
|
|
}
|
|
|
|
QPoint gridPoint = QPoint(-1, TableZone::tableRowToGridY(cardInfo->getUiAttributes().tableRow));
|
|
|
|
// create the token for the related card
|
|
Command_CreateToken cmd;
|
|
cmd.set_zone(ZoneNames::TABLE);
|
|
cmd.set_card_name(cardInfo->getName().toStdString());
|
|
switch (cardInfo->getColors().size()) {
|
|
case 0:
|
|
cmd.set_color("");
|
|
break;
|
|
case 1:
|
|
cmd.set_color("m");
|
|
break;
|
|
default:
|
|
cmd.set_color(cardInfo->getColors().left(1).toLower().toStdString());
|
|
break;
|
|
}
|
|
|
|
cmd.set_pt(cardInfo->getPowTough().toStdString());
|
|
if (SettingsCache::instance().userInterface().getAnnotateTokens()) {
|
|
cmd.set_annotation(cardInfo->getText().toStdString());
|
|
} else {
|
|
cmd.set_annotation("");
|
|
}
|
|
cmd.set_destroy_on_zone_change(!persistent);
|
|
cmd.set_x(gridPoint.x());
|
|
cmd.set_y(gridPoint.y());
|
|
cmd.set_face_down(faceDown);
|
|
|
|
ExactCard relatedCard =
|
|
CardDatabaseManager::query()->getCardFromSameSet(cardInfo->getName(), sourceCard->getCard().getPrinting());
|
|
|
|
switch (attachType) {
|
|
case CardRelationType::DoesNotAttach:
|
|
cmd.set_target_zone(ZoneNames::TABLE);
|
|
cmd.set_card_provider_id(relatedCard.getPrinting().getUuid().toStdString());
|
|
break;
|
|
|
|
case CardRelationType::AttachTo:
|
|
cmd.set_target_zone(ZoneNames::TABLE); // We currently only support creating tokens on the table
|
|
cmd.set_card_provider_id(relatedCard.getPrinting().getUuid().toStdString());
|
|
cmd.set_target_card_id(sourceCard->getId());
|
|
cmd.set_target_mode(Command_CreateToken::ATTACH_TO);
|
|
break;
|
|
|
|
case CardRelationType::TransformInto:
|
|
// allow cards to directly transform on stack
|
|
cmd.set_zone(sourceCard->getZone()->getName() == ZoneNames::STACK ? ZoneNames::STACK : ZoneNames::TABLE);
|
|
// Transform card zone changes are handled server-side
|
|
cmd.set_target_zone(sourceCard->getZone()->getName().toStdString());
|
|
cmd.set_target_card_id(sourceCard->getId());
|
|
cmd.set_target_mode(Command_CreateToken::TRANSFORM_INTO);
|
|
cmd.set_card_provider_id(sourceCard->getProviderId().toStdString());
|
|
break;
|
|
}
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actSayMessage()
|
|
{
|
|
auto *a = qobject_cast<QAction *>(sender());
|
|
Command_GameSay cmd;
|
|
cmd.set_message(a->text().toStdString());
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRequestMoveCardXCardsFromTopDialog()
|
|
{
|
|
int deckSize = player->getDeckZone()->getCards().size() + 1; // add the card to move to the deck
|
|
|
|
emit requestMoveCardXCardsFromTopDialog(defaultNumberTopCardsToPlaceBelow, deckSize);
|
|
}
|
|
|
|
void PlayerActions::actMoveCardXCardsFromTop(QList<CardItem *> selectedCards, int number)
|
|
{
|
|
defaultNumberTopCardsToPlaceBelow = number;
|
|
|
|
if (selectedCards.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
ListOfCardsToMove idList;
|
|
for (const auto &i : selectedCards) {
|
|
idList.add_card()->set_card_id(i->getId());
|
|
}
|
|
|
|
int startPlayerId = selectedCards[0]->getZone()->getPlayer()->getPlayerInfo()->getId();
|
|
QString startZone = selectedCards[0]->getZone()->getName();
|
|
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::DECK);
|
|
cmd->set_x(number);
|
|
cmd->set_y(0);
|
|
commandList.append(cmd);
|
|
|
|
if (player->getPlayerInfo()->local) {
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
} else {
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actIncPT(QList<CardItem *> selectedCards, int deltaP, int deltaT)
|
|
{
|
|
int playerid = player->getPlayerInfo()->getId();
|
|
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
QString pt = card->getPT();
|
|
const auto ptList = CardItem::parsePT(pt);
|
|
QString newpt;
|
|
if (ptList.isEmpty()) {
|
|
newpt = QString::number(deltaP) + (deltaT ? "/" + QString::number(deltaT) : "");
|
|
} else if (ptList.size() == 1) {
|
|
newpt = QString::number(ptList.at(0).toInt() + deltaP) + (deltaT ? "/" + QString::number(deltaT) : "");
|
|
} else {
|
|
newpt =
|
|
QString::number(ptList.at(0).toInt() + deltaP) + "/" + QString::number(ptList.at(1).toInt() + deltaT);
|
|
}
|
|
|
|
auto *cmd = new Command_SetCardAttr;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrPT);
|
|
cmd->set_attr_value(newpt.toStdString());
|
|
commandList.append(cmd);
|
|
|
|
if (player->getPlayerInfo()->getLocal()) {
|
|
playerid = card->getZone()->getPlayer()->getPlayerInfo()->getId();
|
|
}
|
|
}
|
|
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(prepareGameCommand(commandList), playerid);
|
|
}
|
|
|
|
void PlayerActions::actResetPT(QList<CardItem *> selectedCards)
|
|
{
|
|
int playerid = player->getPlayerInfo()->getId();
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
QString ptString;
|
|
if (!card->getFaceDown()) { // leave the pt empty if the card is face down
|
|
ExactCard ec = card->getCard();
|
|
if (ec) {
|
|
ptString = ec.getInfo().getPowTough();
|
|
}
|
|
}
|
|
if (ptString == card->getPT()) {
|
|
continue;
|
|
}
|
|
QString zoneName = card->getZone()->getName();
|
|
auto *cmd = new Command_SetCardAttr;
|
|
cmd->set_zone(zoneName.toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrPT);
|
|
cmd->set_attr_value(ptString.toStdString());
|
|
commandList.append(cmd);
|
|
|
|
if (player->getPlayerInfo()->getLocal()) {
|
|
playerid = card->getZone()->getPlayer()->getPlayerInfo()->getId();
|
|
}
|
|
}
|
|
|
|
if (!commandList.empty()) {
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(prepareGameCommand(commandList), playerid);
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actRequestSetPTDialog(QList<CardItem *> selectedCards)
|
|
{
|
|
QString oldPT;
|
|
|
|
for (auto card : selectedCards) {
|
|
if (!card->getPT().isEmpty()) {
|
|
oldPT = card->getPT();
|
|
}
|
|
}
|
|
|
|
emit requestSetPTDialog(oldPT);
|
|
}
|
|
|
|
void PlayerActions::actSetPT(QList<CardItem *> selectedCards, const QString &pt)
|
|
{
|
|
int playerid = player->getPlayerInfo()->getId();
|
|
|
|
const auto ptList = CardItem::parsePT(pt);
|
|
bool empty = ptList.isEmpty();
|
|
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
auto *cmd = new Command_SetCardAttr;
|
|
QString newpt = QString();
|
|
if (!empty) {
|
|
const auto oldpt = CardItem::parsePT(card->getPT());
|
|
int ptIter = 0;
|
|
for (const auto &_item : ptList) {
|
|
if (_item.typeId() == QMetaType::Type::Int) {
|
|
int oldItem = ptIter < oldpt.size() ? oldpt.at(ptIter).toInt() : 0;
|
|
newpt += '/' + QString::number(oldItem + _item.toInt());
|
|
} else {
|
|
newpt += '/' + _item.toString();
|
|
}
|
|
++ptIter;
|
|
}
|
|
newpt = newpt.mid(1);
|
|
}
|
|
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrPT);
|
|
cmd->set_attr_value(newpt.toStdString());
|
|
commandList.append(cmd);
|
|
|
|
if (player->getPlayerInfo()->local) {
|
|
playerid = card->getZone()->getPlayer()->getPlayerInfo()->getId();
|
|
}
|
|
}
|
|
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(prepareGameCommand(commandList), playerid);
|
|
}
|
|
|
|
void PlayerActions::actDrawArrow()
|
|
{
|
|
auto *card = player->getGame()->getActiveCard();
|
|
if (card) {
|
|
card->drawArrow(Qt::red);
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actIncP(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, 1, 0);
|
|
}
|
|
|
|
void PlayerActions::actDecP(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, -1, 0);
|
|
}
|
|
|
|
void PlayerActions::actIncT(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, 0, 1);
|
|
}
|
|
|
|
void PlayerActions::actDecT(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, 0, -1);
|
|
}
|
|
|
|
void PlayerActions::actIncPT(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, 1, 1);
|
|
}
|
|
|
|
void PlayerActions::actDecPT(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, -1, -1);
|
|
}
|
|
|
|
void PlayerActions::actFlowP(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, 1, -1);
|
|
}
|
|
|
|
void PlayerActions::actFlowT(QList<CardItem *> selectedCards)
|
|
{
|
|
actIncPT(selectedCards, -1, 1);
|
|
}
|
|
|
|
void PlayerActions::actReduceLifeByPower(QList<CardItem *> selectedCards)
|
|
{
|
|
// find life counter
|
|
auto lifeCounter = player->getLifeCounter();
|
|
if (!lifeCounter) {
|
|
return;
|
|
}
|
|
|
|
// calculate total power;
|
|
int total = 0;
|
|
for (auto card : selectedCards) {
|
|
QVariantList parsed = CardItem::parsePT(card->getPT());
|
|
if (!parsed.isEmpty()) {
|
|
int power = parsed.first().toInt(); // toInt will default to 0 if it's not an int
|
|
total += qMax(power, 0);
|
|
}
|
|
}
|
|
|
|
// send cmd
|
|
Command_IncCounter cmd;
|
|
cmd.set_counter_id(lifeCounter->getId());
|
|
cmd.set_delta(-total);
|
|
sendGameCommand(prepareGameCommand(cmd));
|
|
}
|
|
|
|
void AnnotationDialog::keyPressEvent(QKeyEvent *event)
|
|
{
|
|
if (event->key() == Qt::Key_Return && event->modifiers() & Qt::ControlModifier) {
|
|
event->accept();
|
|
accept();
|
|
return;
|
|
}
|
|
QInputDialog::keyPressEvent(event);
|
|
}
|
|
|
|
void PlayerActions::actRequestSetAnnotationDialog(QList<CardItem *> selectedCards)
|
|
{
|
|
QString oldAnnotation;
|
|
for (auto card : selectedCards) {
|
|
if (!card->getAnnotation().isEmpty()) {
|
|
oldAnnotation = card->getAnnotation();
|
|
}
|
|
}
|
|
|
|
emit requestSetAnnotationDialog(oldAnnotation);
|
|
}
|
|
|
|
void PlayerActions::actSetAnnotation(QList<CardItem *> selectedCards, const QString &annotation)
|
|
{
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
auto *cmd = new Command_SetCardAttr;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrAnnotation);
|
|
cmd->set_attr_value(annotation.toStdString());
|
|
commandList.append(cmd);
|
|
}
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
|
|
void PlayerActions::actAttach()
|
|
{
|
|
auto *card = player->getGame()->getActiveCard();
|
|
if (!card) {
|
|
return;
|
|
}
|
|
|
|
card->drawAttachArrow();
|
|
}
|
|
|
|
void PlayerActions::actUnattach(QList<CardItem *> selectedCards)
|
|
{
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
if (!card->getAttachedTo()) {
|
|
continue;
|
|
}
|
|
|
|
auto *cmd = new Command_AttachCard;
|
|
cmd->set_start_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
commandList.append(cmd);
|
|
}
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
|
|
void PlayerActions::actAddCardCounter(QList<CardItem *> selectedCards, int counterId)
|
|
{
|
|
offsetCardCounter(selectedCards, counterId, 1);
|
|
}
|
|
|
|
void PlayerActions::actRemoveCardCounter(QList<CardItem *> selectedCards, int counterId)
|
|
{
|
|
offsetCardCounter(selectedCards, counterId, -1);
|
|
}
|
|
|
|
void PlayerActions::offsetCardCounter(QList<CardItem *> selectedCards, int counterId, int offset)
|
|
{
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
int oldValue = card->getCounters().value(counterId, 0);
|
|
|
|
// Overflow-safe clamp to the server-enforced range [0, MAX_COUNTER_VALUE];
|
|
// a result differing from oldValue also corrects an out-of-range cached value.
|
|
// Callers only ever pass offset == ±1 (actAddCardCounter / actRemoveCardCounter).
|
|
// This client-side clamp is a defense-in-depth UX check, consistent with
|
|
// actSetCardCounter and actIncrementAllCardCounters; the server remains the
|
|
// authoritative enforcer of the bounds.
|
|
int newValue = addClamped(oldValue, offset, 0, MAX_COUNTER_VALUE);
|
|
if (newValue != oldValue) {
|
|
auto *cmd = new Command_SetCardCounter;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_counter_id(counterId);
|
|
cmd->set_counter_value(newValue);
|
|
commandList.append(cmd);
|
|
}
|
|
}
|
|
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
|
|
void PlayerActions::actRequestSetCardCounterDialog(QList<CardItem *> selectedCards, int counterId)
|
|
{
|
|
// If a single card is selected, we show the old value in the dialog. Otherwise, we show "x"
|
|
QString oldValueForDlg = "x";
|
|
if (selectedCards.size() == 1) {
|
|
auto *card = selectedCards.first();
|
|
oldValueForDlg = QString::number(card->getCounters().value(counterId, 0));
|
|
}
|
|
|
|
emit requestSetCardCounterDialog(counterId, oldValueForDlg);
|
|
}
|
|
|
|
void PlayerActions::actSetCardCounter(QList<CardItem *> selectedCards, int counterId, const QString &counterValue)
|
|
{
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
for (auto card : selectedCards) {
|
|
int oldValue = card->getCounters().value(counterId, 0);
|
|
Expression exp(oldValue);
|
|
double parsed = exp.parse(counterValue);
|
|
// Clamp in double precision first to avoid UB, then cast
|
|
int number = static_cast<int>(qBound(0.0, parsed, static_cast<double>(MAX_COUNTER_VALUE)));
|
|
|
|
auto *cmd = new Command_SetCardCounter;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_counter_id(counterId);
|
|
cmd->set_counter_value(number);
|
|
commandList.append(cmd);
|
|
}
|
|
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
|
|
void PlayerActions::actIncrementAllCardCounters(QList<CardItem *> cardsToUpdate)
|
|
{
|
|
if (cardsToUpdate.isEmpty()) {
|
|
// If no cards selected, update all cards on table
|
|
cardsToUpdate = static_cast<QList<CardItem *>>(player->getTableZone()->getCards());
|
|
}
|
|
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
|
|
for (const auto *card : cardsToUpdate) {
|
|
const auto &cardCounters = card->getCounters();
|
|
|
|
QMapIterator<int, int> counterIterator(cardCounters);
|
|
while (counterIterator.hasNext()) {
|
|
counterIterator.next();
|
|
int counterId = counterIterator.key();
|
|
int currentValue = counterIterator.value();
|
|
if (currentValue >= MAX_COUNTER_VALUE) {
|
|
continue;
|
|
}
|
|
|
|
auto cmd = std::make_unique<Command_SetCardCounter>();
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_counter_id(counterId);
|
|
cmd->set_counter_value(currentValue + 1);
|
|
commandList.append(cmd.release());
|
|
}
|
|
}
|
|
|
|
if (!commandList.isEmpty()) {
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief returns true if the zone is a unwritable reveal zone view (eg a card reveal window). Will return false if zone
|
|
* is nullptr.
|
|
*/
|
|
static bool isUnwritableRevealZone(CardZoneLogic *zone)
|
|
{
|
|
if (auto *view = qobject_cast<ZoneViewZoneLogic *>(zone)) {
|
|
return view->getRevealZone() && !view->getWriteableRevealZone();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void PlayerActions::playSelectedCards(QList<CardItem *> selectedCards, const bool faceDown)
|
|
{
|
|
// CardIds will get shuffled downwards when cards leave the deck.
|
|
// We need to iterate through the cards in reverse order so cardIds don't get changed out from under us as we play
|
|
// out the cards one-by-one.
|
|
std::sort(selectedCards.begin(), selectedCards.end(),
|
|
[](const auto &card1, const auto &card2) { return card1->getId() > card2->getId(); });
|
|
|
|
for (auto &card : selectedCards) {
|
|
if (card && !isUnwritableRevealZone(card->getZone()) && card->getZone()->getName() != ZoneNames::TABLE) {
|
|
playCard(card, faceDown);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actPlay(QList<CardItem *> selectedCards)
|
|
{
|
|
playSelectedCards(selectedCards, false);
|
|
}
|
|
|
|
void PlayerActions::actPlayFacedown(QList<CardItem *> selectedCards)
|
|
{
|
|
playSelectedCards(selectedCards, true);
|
|
}
|
|
|
|
void PlayerActions::actHide(QList<CardItem *> selectedCards)
|
|
{
|
|
for (const auto &item : selectedCards) {
|
|
auto *card = static_cast<CardItem *>(item);
|
|
if (card && isUnwritableRevealZone(card->getZone())) {
|
|
card->getZone()->removeCard(card);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlayerActions::actReveal(QList<CardItem *> selectedCards, QAction *action)
|
|
{
|
|
const int otherPlayerId = action->data().toInt();
|
|
|
|
Command_RevealCards cmd;
|
|
if (otherPlayerId != -1) {
|
|
cmd.set_player_id(otherPlayerId);
|
|
}
|
|
|
|
for (auto card : selectedCards) {
|
|
if (!cmd.has_zone_name()) {
|
|
cmd.set_zone_name(card->getZone()->getName().toStdString());
|
|
}
|
|
cmd.add_card_id(card->getId());
|
|
}
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRevealHand(int revealToPlayerId)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (revealToPlayerId != -1) {
|
|
cmd.set_player_id(revealToPlayerId);
|
|
}
|
|
cmd.set_zone_name(ZoneNames::HAND);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRevealRandomHandCard(int revealToPlayerId)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (revealToPlayerId != -1) {
|
|
cmd.set_player_id(revealToPlayerId);
|
|
}
|
|
cmd.set_zone_name(ZoneNames::HAND);
|
|
cmd.add_card_id(RANDOM_CARD_FROM_ZONE);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRevealLibrary(int revealToPlayerId)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (revealToPlayerId != -1) {
|
|
cmd.set_player_id(revealToPlayerId);
|
|
}
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actLendLibrary(int lendToPlayerId)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (lendToPlayerId != -1) {
|
|
cmd.set_player_id(lendToPlayerId);
|
|
}
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_grant_write_access(true);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRevealTopCards(int revealToPlayerId, int amount)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (revealToPlayerId != -1) {
|
|
cmd.set_player_id(revealToPlayerId);
|
|
}
|
|
|
|
cmd.set_zone_name(ZoneNames::DECK);
|
|
cmd.set_top_cards(amount);
|
|
// backward compatibility: servers before #1051 only permits to reveal the first card
|
|
cmd.add_card_id(0);
|
|
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::actRevealRandomGraveyardCard(int revealToPlayerId)
|
|
{
|
|
Command_RevealCards cmd;
|
|
if (revealToPlayerId != -1) {
|
|
cmd.set_player_id(revealToPlayerId);
|
|
}
|
|
cmd.set_zone_name(ZoneNames::GRAVE);
|
|
cmd.add_card_id(RANDOM_CARD_FROM_ZONE);
|
|
sendGameCommand(cmd);
|
|
}
|
|
|
|
void PlayerActions::cardMenuAction(QList<CardItem *> selectedCards, CardMenuActionType type)
|
|
{
|
|
QList<CardItem *> cardList = selectedCards;
|
|
|
|
QList<const ::google::protobuf::Message *> commandList;
|
|
if (type <= cmClone) {
|
|
for (const auto &card : cardList) {
|
|
switch (type) {
|
|
// Leaving both for compatibility with server
|
|
case cmUntap:
|
|
// fallthrough
|
|
case cmTap: {
|
|
auto *cmd = new Command_SetCardAttr;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrTapped);
|
|
cmd->set_attr_value(std::to_string(1 - static_cast<int>(card->getTapped())));
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmDoesntUntap: {
|
|
auto *cmd = new Command_SetCardAttr;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_attribute(AttrDoesntUntap);
|
|
cmd->set_attr_value(card->getDoesntUntap() ? "0" : "1");
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmFlip: {
|
|
auto *cmd = new Command_FlipCard;
|
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
cmd->set_card_id(card->getId());
|
|
cmd->set_face_down(!card->getFaceDown());
|
|
if (card->getFaceDown()) {
|
|
ExactCard ec = card->getCard();
|
|
if (ec) {
|
|
cmd->set_pt(ec.getInfo().getPowTough().toStdString());
|
|
}
|
|
}
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmPeek: {
|
|
auto *cmd = new Command_RevealCards;
|
|
cmd->set_zone_name(card->getZone()->getName().toStdString());
|
|
cmd->add_card_id(card->getId());
|
|
cmd->set_player_id(player->getPlayerInfo()->getId());
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmClone: {
|
|
auto *cmd = new Command_CreateToken;
|
|
cmd->set_zone(ZoneNames::TABLE);
|
|
cmd->set_card_name(card->getName().toStdString());
|
|
cmd->set_card_provider_id(card->getProviderId().toStdString());
|
|
cmd->set_color(card->getColor().toStdString());
|
|
cmd->set_pt(card->getPT().toStdString());
|
|
cmd->set_annotation(card->getAnnotation().toStdString());
|
|
cmd->set_destroy_on_zone_change(true);
|
|
cmd->set_x(-1);
|
|
cmd->set_y(card->getGridPoint().y());
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
CardZoneLogic *zone = cardList[0]->getZone();
|
|
if (!zone) {
|
|
return;
|
|
}
|
|
|
|
PlayerLogic *startPlayer = zone->getPlayer();
|
|
if (!startPlayer) {
|
|
return;
|
|
}
|
|
|
|
int startPlayerId = startPlayer->getPlayerInfo()->getId();
|
|
QString startZone = zone->getName();
|
|
|
|
ListOfCardsToMove idList;
|
|
for (const auto &i : cardList) {
|
|
idList.add_card()->set_card_id(i->getId());
|
|
}
|
|
|
|
switch (type) {
|
|
case cmMoveToTopLibrary: {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::DECK);
|
|
cmd->set_x(0);
|
|
cmd->set_y(0);
|
|
|
|
if (idList.card_size() > 1) {
|
|
auto *scmd = new Command_Shuffle;
|
|
scmd->set_zone_name(ZoneNames::DECK);
|
|
scmd->set_start(0);
|
|
scmd->set_end(idList.card_size() - 1); // inclusive, the indexed card at end will be shuffled
|
|
// Server process events backwards, so...
|
|
commandList.append(scmd);
|
|
}
|
|
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmMoveToBottomLibrary: {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::DECK);
|
|
cmd->set_x(-1);
|
|
cmd->set_y(0);
|
|
|
|
if (idList.card_size() > 1) {
|
|
auto *scmd = new Command_Shuffle;
|
|
scmd->set_zone_name(ZoneNames::DECK);
|
|
scmd->set_start(-idList.card_size());
|
|
scmd->set_end(-1);
|
|
// Server process events backwards, so...
|
|
commandList.append(scmd);
|
|
}
|
|
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmMoveToHand: {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::HAND);
|
|
cmd->set_x(0);
|
|
cmd->set_y(0);
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmMoveToGraveyard: {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::GRAVE);
|
|
cmd->set_x(0);
|
|
cmd->set_y(0);
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmMoveToExile: {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->mutable_cards_to_move()->CopyFrom(idList);
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::EXILE);
|
|
cmd->set_x(0);
|
|
cmd->set_y(0);
|
|
commandList.append(cmd);
|
|
break;
|
|
}
|
|
case cmMoveToTable: {
|
|
// Each card needs its own command because table row, pt, and cipt vary per card
|
|
for (const auto &card : cardList) {
|
|
auto *cmd = new Command_MoveCard;
|
|
cmd->set_start_player_id(startPlayerId);
|
|
cmd->set_start_zone(startZone.toStdString());
|
|
cmd->set_target_player_id(player->getPlayerInfo()->getId());
|
|
cmd->set_target_zone(ZoneNames::TABLE);
|
|
cmd->set_x(-1);
|
|
|
|
CardToMove *ctm = cmd->mutable_cards_to_move()->add_card();
|
|
ctm->set_card_id(card->getId());
|
|
ctm->set_face_down(false);
|
|
|
|
int tableRow = 0;
|
|
ExactCard exactCard = card->getCard();
|
|
if (exactCard) {
|
|
const CardInfo &info = exactCard.getInfo();
|
|
tableRow = info.getUiAttributes().tableRow;
|
|
ctm->set_pt(info.getPowTough().toStdString());
|
|
ctm->set_tapped(info.getUiAttributes().cipt);
|
|
}
|
|
|
|
cmd->set_y(TableZone::tableRowToGridY(tableRow));
|
|
commandList.append(cmd);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (player->getPlayerInfo()->getLocal()) {
|
|
sendGameCommand(prepareGameCommand(commandList));
|
|
} else {
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(prepareGameCommand(commandList));
|
|
}
|
|
}
|
|
|
|
PendingCommand *PlayerActions::prepareGameCommand(const google::protobuf::Message &cmd)
|
|
{
|
|
|
|
if (player->getPlayerInfo()->getJudge() && !player->getPlayerInfo()->getLocal()) {
|
|
Command_Judge base;
|
|
GameCommand *c = base.add_game_command();
|
|
base.set_target_id(player->getPlayerInfo()->getId());
|
|
c->GetReflection()->MutableMessage(c, cmd.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(cmd);
|
|
return player->getGame()->getGameEventHandler()->prepareGameCommand(base);
|
|
} else {
|
|
return player->getGame()->getGameEventHandler()->prepareGameCommand(cmd);
|
|
}
|
|
}
|
|
|
|
PendingCommand *PlayerActions::prepareGameCommand(const QList<const ::google::protobuf::Message *> &cmdList)
|
|
{
|
|
if (player->getPlayerInfo()->getJudge() && !player->getPlayerInfo()->getLocal()) {
|
|
Command_Judge base;
|
|
base.set_target_id(player->getPlayerInfo()->getId());
|
|
for (int i = 0; i < cmdList.size(); ++i) {
|
|
GameCommand *c = base.add_game_command();
|
|
c->GetReflection()
|
|
->MutableMessage(c, cmdList[i]->GetDescriptor()->FindExtensionByName("ext"))
|
|
->CopyFrom(*cmdList[i]);
|
|
delete cmdList[i];
|
|
}
|
|
return player->getGame()->getGameEventHandler()->prepareGameCommand(base);
|
|
} else {
|
|
return player->getGame()->getGameEventHandler()->prepareGameCommand(cmdList);
|
|
}
|
|
}
|
|
|
|
void PlayerActions::sendGameCommand(const google::protobuf::Message &command)
|
|
{
|
|
if (player->getPlayerInfo()->getJudge() && !player->getPlayerInfo()->getLocal()) {
|
|
Command_Judge base;
|
|
GameCommand *c = base.add_game_command();
|
|
base.set_target_id(player->getPlayerInfo()->getId());
|
|
c->GetReflection()->MutableMessage(c, command.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(command);
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(base, player->getPlayerInfo()->getId());
|
|
} else {
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(command, player->getPlayerInfo()->getId());
|
|
}
|
|
}
|
|
|
|
void PlayerActions::sendGameCommand(PendingCommand *pend)
|
|
{
|
|
player->getGame()->getGameEventHandler()->sendGameCommand(pend, player->getPlayerInfo()->getId());
|
|
}
|