From 6b72b150279a4743f8ebc04ed3b5581903724bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vasco=20Guerreiro=20Vint=C3=A9m=20Morais?= Date: Mon, 6 Apr 2026 09:45:00 +0100 Subject: [PATCH] refactor: change isFromBottom check to static function --- .../remote/game/server_abstract_player.cpp | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_player.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_player.cpp index aa12f1a80..cbbfc676f 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_player.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_player.cpp @@ -229,6 +229,36 @@ shouldBeFaceDown(const MoveCardStruct &cardStruct, const Server_CardZone *startZ return false; } +/** + * @brief Determines whether a set of moved cards is from the bottom of the deck + */ +static bool shouldBeFromTheBottom(const Server_CardZone *startZone, const std::set &cardsToMove) +{ + if (!startZone) { + return false; + } + + if (startZone->getName() != ZoneNames::DECK) { + return false; + } + + int movedCount = static_cast(cardsToMove.size()); + int tailStart = startZone->getCards().size() - movedCount; + if (tailStart <= 0) { // if the move includes the start of the deck, it should not be considered from the bottom + return false; + } + + int expectedPosition = tailStart; + for (const auto &card : cardsToMove) { + if (card.position != expectedPosition) { + return false; + } + ++expectedPosition; + } + + return true; +} + Response::ResponseCode Server_AbstractPlayer::moveCard(GameEventStorage &ges, Server_CardZone *startzone, const QList &_cards, @@ -289,22 +319,7 @@ Response::ResponseCode Server_AbstractPlayer::moveCard(GameEventStorage &ges, bool revealTopStart = false; bool revealTopTarget = false; - bool isFromBottom = false; - if (startzone->getName() == ZoneNames::DECK) { - int movedCount = static_cast(cardsToMove.size()); - int tailStart = startzone->getCards().size() - movedCount; - if (tailStart >= 0) { - isFromBottom = true; - int expectedPosition = tailStart; - for (const auto &card : cardsToMove) { - if (card.position != expectedPosition) { - isFromBottom = false; - break; - } - ++expectedPosition; - } - } - } + bool isFromBottom = shouldBeFromTheBottom(startzone, cardsToMove); if (isFromBottom) { std::ranges::reverse_view reversedCardsToMove{cardsToMove};