mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-28 11:53:11 -07:00
refactor: extract shared card insertion algorithm from hand/stack zones (#6701)
Hand and stack zones had near-identical addCardImpl() implementations, differing only in whether resetState() preserves annotations. Extract the shared pattern into a template function (CardZoneAlgorithms::addCardToList) to eliminate duplication and enable isolated testing without Qt dependencies. Pile, table, and zone-view logic are intentionally excluded — their post-add behavior (signals, coordinate placement, hidden cards) is materially different.
This commit is contained in:
45
cockatrice/src/game/zones/logic/card_zone_algorithms.h
Normal file
45
cockatrice/src/game/zones/logic/card_zone_algorithms.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef COCKATRICE_CARD_ZONE_ALGORITHMS_H
|
||||
#define COCKATRICE_CARD_ZONE_ALGORITHMS_H
|
||||
|
||||
namespace CardZoneAlgorithms
|
||||
{
|
||||
|
||||
/**
|
||||
* Shared insertion logic for zones where cards become visible on add and follow
|
||||
* the standard pattern: clamp index, insert, clear identity if contents unknown,
|
||||
* reset state, show card.
|
||||
*
|
||||
* Zones with different post-add behavior (signal connections, positional resets,
|
||||
* hidden cards, or coordinate-based placement) should NOT use this — implement
|
||||
* addCardImpl directly instead.
|
||||
*
|
||||
* Template parameters allow testing with lightweight mocks that avoid Qt graphics
|
||||
* dependencies.
|
||||
*
|
||||
* @tparam CardList Must provide: size() -> int, insert(int, CardType*),
|
||||
* getContentsKnown() -> bool
|
||||
* @tparam CardType Must provide: setId(int), setCardRef(CardRefType),
|
||||
* resetState(bool), setVisible(bool)
|
||||
* @param keepAnnotations Forwarded to card->resetState(). Stack-like zones preserve
|
||||
* annotations across zone transitions; hand-like zones clear them.
|
||||
*/
|
||||
template <typename CardList, typename CardType>
|
||||
void addCardToList(CardList &cards, CardType *card, int x, bool keepAnnotations)
|
||||
{
|
||||
if (x < 0 || x >= cards.size()) {
|
||||
x = static_cast<int>(cards.size());
|
||||
}
|
||||
cards.insert(x, card);
|
||||
|
||||
if (!cards.getContentsKnown()) {
|
||||
card->setId(-1);
|
||||
card->setCardRef({});
|
||||
}
|
||||
|
||||
card->resetState(keepAnnotations);
|
||||
card->setVisible(true);
|
||||
}
|
||||
|
||||
} // namespace CardZoneAlgorithms
|
||||
|
||||
#endif // COCKATRICE_CARD_ZONE_ALGORITHMS_H
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "hand_zone_logic.h"
|
||||
|
||||
#include "../../board/card_item.h"
|
||||
#include "card_zone_algorithms.h"
|
||||
|
||||
HandZoneLogic::HandZoneLogic(Player *_player,
|
||||
const QString &_name,
|
||||
@@ -14,16 +15,5 @@ HandZoneLogic::HandZoneLogic(Player *_player,
|
||||
|
||||
void HandZoneLogic::addCardImpl(CardItem *card, int x, int /*y*/)
|
||||
{
|
||||
// if x is negative set it to add at end
|
||||
if (x < 0 || x >= cards.size()) {
|
||||
x = cards.size();
|
||||
}
|
||||
cards.insert(x, card);
|
||||
|
||||
if (!cards.getContentsKnown()) {
|
||||
card->setId(-1);
|
||||
card->setCardRef({});
|
||||
}
|
||||
card->resetState();
|
||||
card->setVisible(true);
|
||||
}
|
||||
CardZoneAlgorithms::addCardToList(cards, card, x, false);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "stack_zone_logic.h"
|
||||
|
||||
#include "../../board/card_item.h"
|
||||
#include "card_zone_algorithms.h"
|
||||
|
||||
StackZoneLogic::StackZoneLogic(Player *_player,
|
||||
const QString &_name,
|
||||
@@ -14,16 +15,5 @@ StackZoneLogic::StackZoneLogic(Player *_player,
|
||||
|
||||
void StackZoneLogic::addCardImpl(CardItem *card, int x, int /*y*/)
|
||||
{
|
||||
// if x is negative set it to add at end
|
||||
if (x < 0 || x >= cards.size()) {
|
||||
x = static_cast<int>(cards.size());
|
||||
}
|
||||
cards.insert(x, card);
|
||||
|
||||
if (!cards.getContentsKnown()) {
|
||||
card->setId(-1);
|
||||
card->setCardRef({});
|
||||
}
|
||||
card->resetState(true);
|
||||
card->setVisible(true);
|
||||
}
|
||||
CardZoneAlgorithms::addCardToList(cards, card, x, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user