[Game][Counters] Split counters into AbstractCounter (graphics) and CounterState (logic) (#6917)

* [Counters] Split counters into graphics and logic states

Took 22 minutes

* Don't have widget hold pointer to state -> Copy what we need and subscribe to changes.

Took 12 minutes

Took 5 seconds

* Sync value too.

Took 3 minutes

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-05-21 20:16:28 +02:00 committed by GitHub
parent 0549892092
commit bddf9bd818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 273 additions and 199 deletions

View file

@ -258,57 +258,52 @@ void PlayerLogic::setDeck(const DeckList &_deck)
emit deckChanged();
}
AbstractCounter *PlayerLogic::addCounter(const ServerInfo_Counter &counter)
CounterState *PlayerLogic::addCounter(const ServerInfo_Counter &counter)
{
return addCounter(counter.id(), QString::fromStdString(counter.name()),
convertColorToQColor(counter.counter_color()), counter.radius(), counter.count());
}
AbstractCounter *PlayerLogic::addCounter(int counterId, const QString &name, QColor color, int radius, int value)
CounterState *PlayerLogic::addCounter(int id, const QString &name, const QColor &color, int radius, int value)
{
if (counters.contains(counterId)) {
if (counters.contains(id)) {
return nullptr;
}
AbstractCounter *ctr;
if (name == "life") {
ctr = getGraphicsItem()->getPlayerTarget()->addCounter(counterId, name, value);
} else {
ctr = new GeneralCounter(this, counterId, name, color, radius, value, true, graphicsItem);
}
counters.insert(counterId, ctr);
if (playerMenu->getCountersMenu() && ctr->getMenu()) {
playerMenu->getCountersMenu()->addMenu(ctr->getMenu());
}
if (playerMenu->getShortcutsActive()) {
ctr->setShortcutsActive();
}
emit rearrangeCounters();
return ctr;
auto *state = new CounterState(id, name, color, radius, value, this);
counters.insert(id, state);
emit counterAdded(state);
return state;
}
void PlayerLogic::delCounter(int counterId)
void PlayerLogic::delCounter(int id)
{
AbstractCounter *ctr = counters.value(counterId, 0);
if (!ctr) {
auto *state = counters.take(id);
if (!state) {
return;
}
ctr->delCounter();
counters.remove(counterId);
emit rearrangeCounters();
emit counterRemoved(id);
state->deleteLater();
}
void PlayerLogic::clearCounters()
{
QMapIterator<int, AbstractCounter *> counterIterator(counters);
while (counterIterator.hasNext()) {
counterIterator.next().value()->delCounter();
for (int id : counters.keys()) {
emit counterRemoved(id);
}
qDeleteAll(counters);
counters.clear();
}
CounterState *PlayerLogic::getLifeCounter() const
{
for (auto *s : counters.values()) {
if (s->getName() == "life") {
return s;
}
}
return nullptr;
}
void PlayerLogic::incrementAllCardCounters()
{
auto cardsToUpdate = getGameScene()->selectedCards();
@ -345,16 +340,6 @@ void PlayerLogic::incrementAllCardCounters()
}
}
AbstractCounter *PlayerLogic::getLifeCounter() const
{
for (auto counter : counters.values()) {
if (counter->getName() == "life") {
return counter;
}
}
return nullptr;
}
ArrowItem *PlayerLogic::addArrow(const ServerInfo_Arrow &arrow)
{
const QMap<int, PlayerLogic *> &playerList = game->getPlayerManager()->getPlayers();