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:
DawnFire42 2026-03-15 03:39:44 -04:00 committed by GitHub
parent 8180d2e3b0
commit 9bb399606c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 226 additions and 26 deletions

View 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

View file

@ -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);
}

View file

@ -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);
}