From f1746144964cacc425b4a6820e4beea012f43af1 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Wed, 27 Mar 2024 14:47:00 +0100 Subject: [PATCH] assign new arrow id when arrow is moved to transformed card (#5012) * add timeout to settingscache * assign new arrow id when arrow is moved to transformed card fixes bug introduced in #4907 fixes #5008 --- cockatrice/src/remoteclient.cpp | 1 + cockatrice/src/remoteclient.h | 2 +- cockatrice/src/settingscache.cpp | 1 + cockatrice/src/settingscache.h | 5 +++++ common/server_arrow.h | 4 ++++ common/server_player.cpp | 27 +++++++++++++++++---------- common/server_player.h | 1 + 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/remoteclient.cpp b/cockatrice/src/remoteclient.cpp index 78dc02db..cbb9a5d9 100644 --- a/cockatrice/src/remoteclient.cpp +++ b/cockatrice/src/remoteclient.cpp @@ -32,6 +32,7 @@ RemoteClient::RemoteClient(QObject *parent) { clearNewClientFeatures(); + maxTimeout = SettingsCache::instance().getTimeOut(); int keepalive = SettingsCache::instance().getKeepAlive(); timer = new QTimer(this); timer->setInterval(keepalive * 1000); diff --git a/cockatrice/src/remoteclient.h b/cockatrice/src/remoteclient.h index 1e0464ed..3ce89314 100644 --- a/cockatrice/src/remoteclient.h +++ b/cockatrice/src/remoteclient.h @@ -89,7 +89,7 @@ private slots: void submitForgotPasswordChallengeResponse(const Response &response); private: - static const int maxTimeout = 5; + int maxTimeout; int timeRunning, lastDataReceived; QByteArray inputBuffer; bool messageInProgress; diff --git a/cockatrice/src/settingscache.cpp b/cockatrice/src/settingscache.cpp index ca289fd0..b105fca8 100644 --- a/cockatrice/src/settingscache.cpp +++ b/cockatrice/src/settingscache.cpp @@ -195,6 +195,7 @@ SettingsCache::SettingsCache() lang = settings->value("personal/lang").toString(); keepalive = settings->value("personal/keepalive", 3).toInt(); + timeout = settings->value("personal/timeout", 5).toInt(); // tip of the day settings showTipsOnStartup = settings->value("tipOfDay/showTips", true).toBool(); diff --git a/cockatrice/src/settingscache.h b/cockatrice/src/settingscache.h index b9c4d3fb..d3d53a9d 100644 --- a/cockatrice/src/settingscache.h +++ b/cockatrice/src/settingscache.h @@ -134,6 +134,7 @@ private: bool spectatorsCanSeeEverything; bool createGameAsSpectator; int keepalive; + int timeout; void translateLegacySettings(); QString getSafeConfigPath(QString configEntry, QString defaultPath) const; QString getSafeConfigFilePath(QString configEntry, QString defaultPath) const; @@ -434,6 +435,10 @@ public: { return keepalive; } + int getTimeOut() const + { + return timeout; + } int getMaxFontSize() const { return maxFontSize; diff --git a/common/server_arrow.h b/common/server_arrow.h index e87ce3fe..13a6ea2d 100644 --- a/common/server_arrow.h +++ b/common/server_arrow.h @@ -21,6 +21,10 @@ public: { return id; } + void setId(int _id) + { + id = _id; + } Server_Card *getStartCard() const { return startCard; diff --git a/common/server_player.cpp b/common/server_player.cpp index 69087826..d2a3ec59 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -296,6 +296,12 @@ void Server_Player::addArrow(Server_Arrow *arrow) arrows.insert(arrow->getId(), arrow); } +void Server_Player::updateArrowId(int id) +{ + auto *arrow = arrows.take(id); + arrows.insert(arrow->getId(), arrow); +} + bool Server_Player::deleteArrow(int arrowId) { Server_Arrow *arrow = arrows.value(arrowId, 0); @@ -497,9 +503,7 @@ Response::ResponseCode Server_Player::moveCard(GameEventStorage &ges, const QList &players = game->getPlayers().values(); for (auto player : players) { QList arrowsToDelete; - QMapIterator arrowIterator(player->getArrows()); - while (arrowIterator.hasNext()) { - Server_Arrow *arrow = arrowIterator.next().value(); + for (Server_Arrow *arrow : player->getArrows()) { if ((arrow->getStartCard() == card) || (arrow->getTargetItem() == card)) arrowsToDelete.append(arrow->getId()); } @@ -1478,9 +1482,8 @@ Server_Player::cmdCreateToken(const Command_CreateToken &cmd, ResponseContainer // Copy Arrows const QList &players = game->getPlayers().values(); for (auto player : players) { - QMapIterator arrowIterator(player->getArrows()); - while (arrowIterator.hasNext()) { - Server_Arrow *arrow = arrowIterator.next().value(); + QList changedArrowIds; + for (Server_Arrow *arrow : player->getArrows()) { bool sendGameEvent = false; const auto *startCard = arrow->getStartCard(); if (startCard == targetCard) { @@ -1497,7 +1500,10 @@ Server_Player::cmdCreateToken(const Command_CreateToken &cmd, ResponseContainer if (sendGameEvent) { Event_CreateArrow _event; ServerInfo_Arrow *arrowInfo = _event.mutable_arrow_info(); - arrowInfo->set_id(arrow->getId()); + changedArrowIds.append(arrow->getId()); + int id = player->newArrowId(); + arrow->setId(id); + arrowInfo->set_id(id); arrowInfo->set_start_player_id(player->getPlayerId()); arrowInfo->set_start_zone(startCard->getZone()->getName().toStdString()); arrowInfo->set_start_card_id(startCard->getId()); @@ -1514,6 +1520,9 @@ Server_Player::cmdCreateToken(const Command_CreateToken &cmd, ResponseContainer ges.enqueueGameEvent(_event, player->getPlayerId()); } } + for (int id : changedArrowIds) { + player->updateArrowId(id); + } } targetCard->resetState(); @@ -1579,9 +1588,7 @@ Server_Player::cmdCreateArrow(const Command_CreateArrow &cmd, ResponseContainer return Response::RespNameNotFound; } - QMapIterator arrowIterator(arrows); - while (arrowIterator.hasNext()) { - Server_Arrow *temp = arrowIterator.next().value(); + for (Server_Arrow *temp : arrows) { if ((temp->getStartCard() == startCard) && (temp->getTargetItem() == targetItem)) { return Response::RespContextError; } diff --git a/common/server_player.h b/common/server_player.h index 4fd4318c..27257d06 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -164,6 +164,7 @@ public: void addZone(Server_CardZone *zone); void addArrow(Server_Arrow *arrow); + void updateArrowId(int id); bool deleteArrow(int arrowId); void addCounter(Server_Counter *counter);