Centralize counter API with server-side bounds and no-op filtering (#6879)

* 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.

* Centralize MAX_COUNTERS_ON_CARD and enforce [0, 999] bounds on server

  - Move MAX_COUNTERS_ON_CARD to trice_limits.h
  - Server clamps values in setCounter() and incrementCounter()
  - Client uses clamped comparison to allow recovery from invalid states
  - Add tests for clamping behavior

* move incrementCount() implementation from header to cpp
This commit is contained in:
DawnFire42 2026-05-21 23:39:35 -04:00 committed by GitHub
parent 74102aa1ec
commit 8dca14933c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 432 additions and 32 deletions

View file

@ -0,0 +1,86 @@
/** @file server_counter_test.cpp
* @brief Tests for Server_Counter operations.
* @ingroup Tests
*/
#include <gtest/gtest.h>
#include <libcockatrice/network/server/remote/game/server_counter.h>
#include <limits>
TEST(ServerCounter, IncrementDoesNotOverflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max());
bool changed = c.incrementCount(1);
EXPECT_FALSE(changed);
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::max());
}
TEST(ServerCounter, DecrementDoesNotUnderflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::min());
bool changed = c.incrementCount(-1);
EXPECT_FALSE(changed);
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::min());
}
TEST(ServerCounter, SetCountReturnsFalseWhenUnchanged)
{
Server_Counter c(1, "test", color(), 10, 50);
bool changed = c.setCount(50);
EXPECT_FALSE(changed);
}
TEST(ServerCounter, IncrementReturnsChangeStatus)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.incrementCount(10));
EXPECT_EQ(c.getCount(), 60);
EXPECT_FALSE(c.incrementCount(0));
EXPECT_EQ(c.getCount(), 60);
}
TEST(ServerCounter, LargePositiveDeltaDoesNotOverflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max() - 10);
bool changed = c.incrementCount(std::numeric_limits<int>::max());
EXPECT_TRUE(changed); // Value changes from INT_MAX-10 to INT_MAX (clamped)
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::max());
}
TEST(ServerCounter, LargeNegativeDeltaDoesNotUnderflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::min() + 10);
bool changed = c.incrementCount(std::numeric_limits<int>::min());
EXPECT_TRUE(changed); // Value changes from INT_MIN+10 to INT_MIN (clamped)
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::min());
}
TEST(ServerCounter, SetCountReturnsTrueWhenChanged)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.setCount(100));
EXPECT_EQ(c.getCount(), 100);
}
TEST(ServerCounter, BasicIncrementWorks)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.incrementCount(10));
EXPECT_EQ(c.getCount(), 60);
EXPECT_TRUE(c.incrementCount(-20));
EXPECT_EQ(c.getCount(), 40);
}
TEST(ServerCounter, MixedExtremesDoNotClamp)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max());
bool changed = c.incrementCount(std::numeric_limits<int>::min());
EXPECT_TRUE(changed);
EXPECT_EQ(c.getCount(), -1);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}