mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-04 12:33:55 -07:00
Rename MAX_COUNTERS_ON_CARD to MAX_COUNTER_VALUE
The constant caps the counter's value, not how many counters can be on the card
This commit is contained in:
parent
7aed233bd8
commit
efdfccc3d2
5 changed files with 20 additions and 20 deletions
|
|
@ -1532,9 +1532,9 @@ void PlayerActions::offsetCardCounter(QList<CardItem *> selectedCards, int count
|
|||
int oldValue = card->getCounters().value(counterId, 0);
|
||||
int newValue = oldValue + offset;
|
||||
|
||||
// Early exit optimization: server enforces [0, MAX_COUNTERS_ON_CARD].
|
||||
// Early exit optimization: server enforces [0, MAX_COUNTER_VALUE].
|
||||
// Compare clamped value to allow recovery from invalid states.
|
||||
int clampedValue = qBound(0, newValue, MAX_COUNTERS_ON_CARD);
|
||||
int clampedValue = qBound(0, newValue, MAX_COUNTER_VALUE);
|
||||
if (clampedValue != oldValue) {
|
||||
auto *cmd = new Command_SetCardCounter;
|
||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||
|
|
@ -1568,7 +1568,7 @@ void PlayerActions::actSetCardCounter(QList<CardItem *> selectedCards, int count
|
|||
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_COUNTERS_ON_CARD)));
|
||||
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());
|
||||
|
|
@ -1598,7 +1598,7 @@ void PlayerActions::actIncrementAllCardCounters(QList<CardItem *> cardsToUpdate)
|
|||
counterIterator.next();
|
||||
int counterId = counterIterator.key();
|
||||
int currentValue = counterIterator.value();
|
||||
if (currentValue >= MAX_COUNTERS_ON_CARD) {
|
||||
if (currentValue >= MAX_COUNTER_VALUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -115,8 +115,8 @@ QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue
|
|||
|
||||
bool Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
|
||||
{
|
||||
// Clamp to valid card counter range [0, MAX_COUNTERS_ON_CARD]
|
||||
value = qBound(0, value, MAX_COUNTERS_ON_CARD);
|
||||
// Clamp to valid card counter range [0, MAX_COUNTER_VALUE]
|
||||
value = qBound(0, value, MAX_COUNTER_VALUE);
|
||||
|
||||
const int oldValue = counters.value(_id, 0);
|
||||
if (value == oldValue) {
|
||||
|
|
@ -140,8 +140,8 @@ bool Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
|
|||
bool Server_Card::incrementCounter(int counterId, int delta, Event_SetCardCounter *event)
|
||||
{
|
||||
const int oldValue = counters.value(counterId, 0);
|
||||
// Clamp to [0, MAX_COUNTERS_ON_CARD] for card counters
|
||||
const int newValue = addClamped(oldValue, delta, 0, MAX_COUNTERS_ON_CARD);
|
||||
// Clamp to [0, MAX_COUNTER_VALUE] for card counters
|
||||
const int newValue = addClamped(oldValue, delta, 0, MAX_COUNTER_VALUE);
|
||||
|
||||
if (newValue == oldValue) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ public:
|
|||
/**
|
||||
* @brief Sets a card counter to an exact value with clamping.
|
||||
* @param _id The counter ID.
|
||||
* @param value The desired value (clamped to [0, MAX_COUNTERS_ON_CARD]; 0 removes the counter).
|
||||
* @param value The desired value (clamped to [0, MAX_COUNTER_VALUE]; 0 removes the counter).
|
||||
* @param event Optional event to populate with counter state.
|
||||
* @return true if the value changed, false otherwise.
|
||||
*/
|
||||
|
|
@ -168,7 +168,7 @@ public:
|
|||
* @param event Optional event to populate with counter state.
|
||||
* @return true if the value changed, false otherwise.
|
||||
* @note If counter does not exist, starts from 0. Counter is removed if result is 0.
|
||||
* @note Clamps result to [0, MAX_COUNTERS_ON_CARD].
|
||||
* @note Clamps result to [0, MAX_COUNTER_VALUE].
|
||||
*/
|
||||
[[nodiscard]] bool incrementCounter(int counterId, int delta, Event_SetCardCounter *event = nullptr);
|
||||
void setTapped(bool _tapped)
|
||||
|
|
|
|||
|
|
@ -18,13 +18,13 @@ constexpr uint MAXIMUM_DIE_SIDES = 1000000;
|
|||
constexpr uint MINIMUM_DICE_TO_ROLL = 1;
|
||||
constexpr uint MAXIMUM_DICE_TO_ROLL = 100;
|
||||
|
||||
// Card counter value bounds [0, MAX_COUNTERS_ON_CARD].
|
||||
// Card counter value bounds [0, MAX_COUNTER_VALUE].
|
||||
// This caps an individual counter's VALUE (e.g. a +1/+1 counter at 999), not how many counters a card holds.
|
||||
// Applies to card counters only; player counters (Server_Counter) are unbounded and may go
|
||||
// negative (e.g. life total), saturating only at the int range.
|
||||
// The max of 999 is a display constraint (3-digit rendering) and reasonable gameplay limit.
|
||||
// Server enforces these bounds; client may also check for UX optimization.
|
||||
constexpr int MAX_COUNTERS_ON_CARD = 999;
|
||||
constexpr int MAX_COUNTER_VALUE = 999;
|
||||
|
||||
// optimized functions to get qstrings that are at most that long
|
||||
static inline QString nameFromStdString(const std::string &_string)
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ TEST(ServerCardCounter, IncrementExistingCounter)
|
|||
TEST(ServerCardCounter, IncrementOverflowProtection)
|
||||
{
|
||||
Server_Card card(CardRef{"TestCard", ""}, 1, 0, 0);
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTERS_ON_CARD));
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTER_VALUE));
|
||||
EXPECT_FALSE(card.incrementCounter(1, 1));
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTERS_ON_CARD);
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTER_VALUE);
|
||||
}
|
||||
|
||||
TEST(ServerCardCounter, DecrementUnderflowProtection)
|
||||
|
|
@ -113,13 +113,13 @@ TEST(ServerCardCounter, IncrementCounterPopulatesEvent)
|
|||
TEST(ServerCardCounter, IncrementCounterEventReflectsClampedValue)
|
||||
{
|
||||
Server_Card card(CardRef{"TestCard", ""}, 1, 0, 0);
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTERS_ON_CARD - 5));
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTER_VALUE - 5));
|
||||
|
||||
Event_SetCardCounter event;
|
||||
EXPECT_TRUE(card.incrementCounter(1, 10, &event));
|
||||
|
||||
EXPECT_EQ(event.counter_id(), 1);
|
||||
EXPECT_EQ(event.counter_value(), MAX_COUNTERS_ON_CARD);
|
||||
EXPECT_EQ(event.counter_value(), MAX_COUNTER_VALUE);
|
||||
}
|
||||
|
||||
TEST(ServerCardCounter, IncrementCounterNoEventWhenNullptr)
|
||||
|
|
@ -133,7 +133,7 @@ TEST(ServerCardCounter, IncrementCounterNoEventWhenNullptr)
|
|||
TEST(ServerCardCounter, IncrementCounterEventNotPopulatedWhenUnchanged)
|
||||
{
|
||||
Server_Card card(CardRef{"TestCard", ""}, 1, 0, 0);
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTERS_ON_CARD));
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTER_VALUE));
|
||||
|
||||
Event_SetCardCounter event;
|
||||
event.set_counter_id(999);
|
||||
|
|
@ -156,7 +156,7 @@ TEST(ServerCardCounter, SetCounterClampsAboveMaxToMax)
|
|||
{
|
||||
Server_Card card(CardRef{"TestCard", ""}, 1, 0, 0);
|
||||
EXPECT_TRUE(card.setCounter(1, 1500));
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTERS_ON_CARD);
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTER_VALUE);
|
||||
}
|
||||
|
||||
TEST(ServerCardCounter, IncrementDoesNotGoBelowZero)
|
||||
|
|
@ -171,9 +171,9 @@ TEST(ServerCardCounter, IncrementDoesNotGoBelowZero)
|
|||
TEST(ServerCardCounter, IncrementDoesNotExceedMax)
|
||||
{
|
||||
Server_Card card(CardRef{"TestCard", ""}, 1, 0, 0);
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTERS_ON_CARD - 5));
|
||||
ASSERT_TRUE(card.setCounter(1, MAX_COUNTER_VALUE - 5));
|
||||
EXPECT_TRUE(card.incrementCounter(1, 10));
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTERS_ON_CARD);
|
||||
EXPECT_EQ(card.getCounter(1), MAX_COUNTER_VALUE);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue