Refactor server counter API to own overflow protection and filter no-op events

Counter modifications now clamp to int bounds server-side and return change
  status, allowing command handlers to skip network broadcasts when values
  don't actually change.
This commit is contained in:
DawnFire42 2026-05-11 19:56:51 -04:00
parent 33e0f8699b
commit 5f92bff741
No known key found for this signature in database
GPG key ID: 24BB855EE2911B33
8 changed files with 379 additions and 29 deletions

View file

@ -49,6 +49,7 @@
#include <libcockatrice/rng/rng_abstract.h>
#include <libcockatrice/utility/trice_limits.h>
#include <libcockatrice/utility/zone_names.h>
#include <limits>
#include <ranges>
Server_AbstractPlayer::Server_AbstractPlayer(Server_Game *_game,
@ -1091,8 +1092,9 @@ Server_AbstractPlayer::cmdCreateToken(const Command_CreateToken &cmd, ResponseCo
_event.set_zone_name(card->getZone()->getName().toStdString());
_event.set_card_id(card->getId());
card->setCounter(i.key(), i.value(), &_event);
ges.enqueueGameEvent(_event, playerId);
if (card->setCounter(i.key(), i.value(), &_event)) {
ges.enqueueGameEvent(_event, playerId);
}
}
// Copy parent card
@ -1338,8 +1340,9 @@ Response::ResponseCode Server_AbstractPlayer::cmdSetCardCounter(const Command_Se
Event_SetCardCounter event;
event.set_zone_name(zone->getName().toStdString());
event.set_card_id(card->getId());
card->setCounter(cmd.counter_id(), cmd.counter_value(), &event);
ges.enqueueGameEvent(event, playerId);
if (card->setCounter(cmd.counter_id(), cmd.counter_value(), &event)) {
ges.enqueueGameEvent(event, playerId);
}
return Response::RespOk;
}
@ -1368,14 +1371,13 @@ Response::ResponseCode Server_AbstractPlayer::cmdIncCardCounter(const Command_In
return Response::RespNameNotFound;
}
int newValue = card->getCounter(cmd.counter_id()) + cmd.counter_delta();
card->setCounter(cmd.counter_id(), newValue);
Event_SetCardCounter event;
event.set_zone_name(zone->getName().toStdString());
event.set_card_id(card->getId());
event.set_counter_id(cmd.counter_id());
event.set_counter_value(newValue);
if (!card->incrementCounter(cmd.counter_id(), cmd.counter_delta(), &event)) {
return Response::RespOk;
}
ges.enqueueGameEvent(event, playerId);
return Response::RespOk;

View file

@ -26,6 +26,7 @@
#include <libcockatrice/protocol/pb/event_set_card_attr.pb.h>
#include <libcockatrice/protocol/pb/event_set_card_counter.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_card.pb.h>
#include <limits>
Server_Card::Server_Card(const CardRef &cardRef, int _id, int _coord_x, int _coord_y, Server_CardZone *_zone)
: zone(_zone), id(_id), coord_x(_coord_x), coord_y(_coord_y), cardRef(cardRef), tapped(false), attacking(false),
@ -110,8 +111,13 @@ QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue
return avalue;
}
void Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
bool Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
{
const int oldValue = counters.value(_id, 0);
if (value == oldValue) {
return false;
}
if (value) {
counters.insert(_id, value);
} else {
@ -122,6 +128,35 @@ void Server_Card::setCounter(int _id, int value, Event_SetCardCounter *event)
event->set_counter_id(_id);
event->set_counter_value(value);
}
return true;
}
bool Server_Card::incrementCounter(int counterId, int delta, Event_SetCardCounter *event)
{
// TODO: Extract overflow-safe arithmetic into shared helper.
// Duplicated in Server_Counter::incrementCount() - keep in sync if modified.
const int oldValue = counters.value(counterId, 0);
const auto result = static_cast<int64_t>(oldValue) + static_cast<int64_t>(delta);
const int newValue = static_cast<int>(qBound(static_cast<int64_t>(std::numeric_limits<int>::min()), result,
static_cast<int64_t>(std::numeric_limits<int>::max())));
if (newValue == oldValue) {
return false;
}
if (newValue) {
counters.insert(counterId, newValue);
} else {
counters.remove(counterId);
}
if (event) {
event->set_counter_id(counterId);
event->set_counter_value(newValue);
}
return true;
}
void Server_Card::setParentCard(Server_Card *_parentCard)

View file

@ -153,7 +153,24 @@ public:
{
cardRef = _cardRef;
}
void setCounter(int _id, int value, Event_SetCardCounter *event = nullptr);
/**
* @brief Sets a card counter to an exact value.
* @param _id The counter ID.
* @param value The new value (0 removes the counter).
* @param event Optional event to populate with counter state.
* @return true if the value changed, false otherwise.
*/
[[nodiscard]] bool setCounter(int _id, int value, Event_SetCardCounter *event = nullptr);
/**
* @brief Increments a card counter with overflow-safe arithmetic.
* @param counterId The counter ID to modify.
* @param delta The amount to add (may be negative for decrement).
* @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.
* @warning For direct assignment without clamping, use setCounter().
*/
[[nodiscard]] bool incrementCounter(int counterId, int delta, Event_SetCardCounter *event = nullptr);
void setTapped(bool _tapped)
{
tapped = _tapped;

View file

@ -22,9 +22,22 @@
#include <QString>
#include <libcockatrice/protocol/pb/color.pb.h>
#include <limits>
class ServerInfo_Counter;
/**
* @class Server_Counter
* @brief Represents a player counter with overflow-safe increment arithmetic.
*
* All value modifications return whether the value actually changed,
* enabling callers to skip unnecessary network events.
*
* @note Direct assignment via setCount() does not clamp; only
* incrementCount() enforces int boundary saturation.
* @note Unlike card counters, player counters are never auto-removed
* when they reach zero - they persist with value 0.
*/
class Server_Counter
{
protected:
@ -59,11 +72,42 @@ public:
{
return count;
}
void setCount(int _count)
/**
* @brief Sets the counter to an exact value.
* @param _count The new value (assigned directly without clamping).
* @return true if the value changed, false otherwise.
* @warning This performs raw assignment. For overflow-safe incrementing,
* use incrementCount().
*/
[[nodiscard]] bool setCount(int _count)
{
const int oldCount = count;
count = _count;
return count != oldCount;
}
/**
* @brief Increments the counter by delta with overflow-safe arithmetic.
* @param delta The amount to add (may be negative for decrement).
* @return true if the value changed, false otherwise.
* @note Clamps result to [INT_MIN, INT_MAX] to prevent overflow.
*/
[[nodiscard]] bool incrementCount(int delta)
{
// TODO: Extract overflow-safe arithmetic into shared helper.
// Duplicated in Server_Card::incrementCounter() - keep in sync if modified.
const int oldCount = count;
const auto result = static_cast<int64_t>(count) + static_cast<int64_t>(delta);
count = static_cast<int>(qBound(static_cast<int64_t>(std::numeric_limits<int>::min()), result,
static_cast<int64_t>(std::numeric_limits<int>::max())));
return count != oldCount;
}
/**
* @brief Populates info with this counter's current state for network serialization.
* @param info The protobuf message to populate.
*/
void getInfo(ServerInfo_Counter *info);
};

View file

@ -436,17 +436,19 @@ Server_Player::cmdIncCounter(const Command_IncCounter &cmd, ResponseContainer &
return Response::RespContextError;
}
Server_Counter *c = counters.value(cmd.counter_id(), 0);
const int counterId = cmd.counter_id();
Server_Counter *c = counters.value(counterId, nullptr);
if (!c) {
return Response::RespNameNotFound;
}
c->setCount(c->getCount() + cmd.delta());
Event_SetCounter event;
event.set_counter_id(c->getId());
event.set_value(c->getCount());
ges.enqueueGameEvent(event, playerId);
bool didChange = c->incrementCount(cmd.delta());
if (didChange) {
Event_SetCounter event;
event.set_counter_id(c->getId());
event.set_value(c->getCount());
ges.enqueueGameEvent(event, playerId);
}
return Response::RespOk;
}
@ -487,17 +489,19 @@ Server_Player::cmdSetCounter(const Command_SetCounter &cmd, ResponseContainer &
return Response::RespContextError;
}
Server_Counter *c = counters.value(cmd.counter_id(), 0);
const int counterId = cmd.counter_id();
Server_Counter *c = counters.value(counterId, nullptr);
if (!c) {
return Response::RespNameNotFound;
}
c->setCount(cmd.value());
Event_SetCounter event;
event.set_counter_id(c->getId());
event.set_value(c->getCount());
ges.enqueueGameEvent(event, playerId);
bool didChange = c->setCount(cmd.value());
if (didChange) {
Event_SetCounter event;
event.set_counter_id(c->getId());
event.set_value(c->getCount());
ges.enqueueGameEvent(event, playerId);
}
return Response::RespOk;
}
@ -512,15 +516,16 @@ Server_Player::cmdDelCounter(const Command_DelCounter &cmd, ResponseContainer &
return Response::RespContextError;
}
Server_Counter *counter = counters.value(cmd.counter_id(), 0);
const int counterId = cmd.counter_id();
Server_Counter *counter = counters.value(counterId, nullptr);
if (!counter) {
return Response::RespNameNotFound;
}
counters.remove(cmd.counter_id());
counters.remove(counterId);
delete counter;
Event_DelCounter event;
event.set_counter_id(cmd.counter_id());
event.set_counter_id(counterId);
ges.enqueueGameEvent(event, playerId);
return Response::RespOk;