[Refactor] Build Command_MoveCard in game/ instead of the zone views (#7417)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-10-31 18:44:09 +01:00 • committed by GitHub
parent 4972ebcb97
commit 563833a88a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 98 additions and 78 deletions

View file

@ -0,0 +1,22 @@
/**
* @file card_move_request.h
* @ingroup GameLogicActions
*/
//! \todo Document this file.
#ifndef COCKATRICE_CARD_MOVE_REQUEST_H
#define COCKATRICE_CARD_MOVE_REQUEST_H
#include <QString>
/**
* @brief Describes one card a view wants moved, in logic terms rather than wire terms.
*/
struct CardMoveRequest
{
int cardId = -1;
bool faceDown = false;
QString pt = {};
};
#endif // COCKATRICE_CARD_MOVE_REQUEST_H

View file

@ -1999,6 +1999,42 @@ PendingCommand *PlayerActions::prepareGameCommand(const QList<const ::google::pr
}
}
void PlayerActions::moveCards(CardZoneLogic *startZone,
CardZoneLogic *targetZone,
int x,
int y,
const QList<CardMoveRequest> &cards)
{
if (!startZone || !targetZone) {
return;
}
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(targetZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(targetZone->getName().toStdString());
cmd.set_x(x);
cmd.set_y(y);
if (auto *viewLogic = qobject_cast<ZoneViewZoneLogic *>(targetZone)) {
cmd.set_is_reversed(viewLogic->getIsReversed());
}
for (const CardMoveRequest &card : cards) {
auto *cardToMove = cmd.mutable_cards_to_move()->add_card();
cardToMove->set_card_id(card.cardId);
if (card.faceDown) {
cardToMove->set_face_down(true);
}
if (!card.pt.isEmpty()) {
cardToMove->set_pt(card.pt.toStdString());
}
}
sendGameCommand(cmd);
}
void PlayerActions::sendGameCommand(const google::protobuf::Message &command)
{
if (player->getPlayerInfo()->getJudge() && !player->getPlayerInfo()->getLocal()) {

View file

@ -11,6 +11,7 @@
#include "../../game_graphics/board/card_item.h" // IWYU pragma: keep - moc instantiates the metatype for QList<CardItem *>, which Qt 6.4 needs complete
#include "../board/card_list.h"
#include "card_menu_action_type.h"
#include "card_move_request.h"
#include "libcockatrice/card/card_info.h"
#include "move_top_cards_until_options.h"
#include "token_info.h"
@ -36,6 +37,7 @@ class Message;
}
} // namespace google
class CardZoneLogic;
class Command_MoveCard;
class PendingCommand;
class PlayerLogic;
@ -58,6 +60,17 @@ public:
PendingCommand *prepareGameCommand(const ::google::protobuf::Message &cmd);
PendingCommand *prepareGameCommand(const QList<const ::google::protobuf::Message *> &cmdList);
/**
* @brief Moves the requested cards between zones on behalf of a view.
* @param startZone Zone the cards are dragged out of.
* @param targetZone Zone the cards are dropped onto.
* @param x Horizontal target index within the zone.
* @param y Vertical target index within the zone.
* @param cards The cards to move, as chosen by the view.
*/
void
moveCards(CardZoneLogic *startZone, CardZoneLogic *targetZone, int x, int y, const QList<CardMoveRequest> &cards);
void moveOneCardUntil(CardItem *card);
void stopMoveTopCardsUntil();

View file

@ -18,7 +18,6 @@
#include <QPointF>
#include <QRectF>
#include <QString>
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
#include <libcockatrice/settings/interface_settings.h>
#include <string>
@ -58,19 +57,13 @@ void HandZone::handleDropEvent(const QList<CardDragItem *> &dragItems,
x = calcDropIndexFromY(dropPoint.y(), !sameZone);
}
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(getLogic()->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(getLogic()->getName().toStdString());
cmd.set_x(x);
cmd.set_y(-1);
for (int i = 0; i < dragItems.size(); ++i) {
cmd.mutable_cards_to_move()->add_card()->set_card_id(dragItems[i]->getId());
QList<CardMoveRequest> cards;
cards.reserve(dragItems.size());
for (const CardDragItem *item : dragItems) {
cards.append(CardMoveRequest{item->getId()});
}
getLogic()->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
getLogic()->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), x, -1, cards);
}
QRectF HandZone::boundingRect() const

View file

@ -24,7 +24,6 @@
#include <QString>
#include <QTransform>
#include <QtGlobal>
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
#include <libcockatrice/settings/cards_display_settings.h>
#include <qnamespace.h>
#include <string>
@ -80,23 +79,13 @@ void PileZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*optio
void PileZone::handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &)
{
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(getLogic()->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(getLogic()->getName().toStdString());
cmd.set_x(0);
cmd.set_y(0);
for (int i = 0; i < dragItems.size(); ++i) {
auto cardToMove = cmd.mutable_cards_to_move()->add_card();
cardToMove->set_card_id(dragItems[i]->getId());
if (dragItems[i]->isForceFaceDown()) {
cardToMove->set_face_down(true);
}
QList<CardMoveRequest> cards;
cards.reserve(dragItems.size());
for (const CardDragItem *item : dragItems) {
cards.append(CardMoveRequest{item->getId(), item->isForceFaceDown()});
}
getLogic()->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
getLogic()->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), 0, 0, cards);
}
void PileZone::reorganizeCards()

View file

@ -19,7 +19,6 @@
#include <QPoint>
#include <QRectF>
#include <QString>
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
#include <qnumeric.h>
#include <string>
@ -79,25 +78,15 @@ void StackZone::handleDropEvent(const QList<CardDragItem *> &dragItems,
}
}
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(getLogic()->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(getLogic()->getName().toStdString());
cmd.set_x(index);
cmd.set_y(0);
QList<CardMoveRequest> cards;
cards.reserve(dragItems.size());
for (const CardDragItem *item : dragItems) {
if (item) {
auto *cardToMove = cmd.mutable_cards_to_move()->add_card();
cardToMove->set_card_id(item->getId());
if (item->isForceFaceDown()) {
cardToMove->set_face_down(true);
}
cards.append(CardMoveRequest{item->getId(), item->isForceFaceDown()});
}
}
getLogic()->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
getLogic()->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), index, 0, cards);
}
void StackZone::setHeight(qreal newHeight)

View file

@ -32,7 +32,6 @@
#include <algorithm>
#include <iterator>
#include <libcockatrice/card/card_info.h>
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
#include <libcockatrice/protocol/pb/command_set_card_attr.pb.h>
#include <libcockatrice/settings/interface_settings.h>
#include <libcockatrice/utility/zone_names.h>
@ -209,29 +208,20 @@ void TableZone::handleDropEventByGrid(const QList<CardDragItem *> &dragItems,
CardZoneLogic *startZone,
const QPoint &gridPoint)
{
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(getLogic()->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(getLogic()->getName().toStdString());
cmd.set_x(gridPoint.x());
cmd.set_y(gridPoint.y());
for (const auto &item : dragItems) {
CardToMove *ctm = cmd.mutable_cards_to_move()->add_card();
ctm->set_card_id(item->getId());
if (item->isForceFaceDown()) {
ctm->set_face_down(true);
}
QList<CardMoveRequest> cards;
cards.reserve(dragItems.size());
for (const CardDragItem *item : dragItems) {
CardMoveRequest card{item->getId(), item->isForceFaceDown()};
if (startZone->getName() != getLogic()->getName() && !item->isForceFaceDown()) {
const auto &card = item->getItem()->getCard();
if (card) {
ctm->set_pt(card.getInfo().getPowTough().toStdString());
const auto &cardInfo = item->getItem()->getCard();
if (cardInfo) {
card.pt = cardInfo.getInfo().getPowTough();
}
}
cards.append(card);
}
startZone->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
startZone->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), gridPoint.x(), gridPoint.y(), cards);
}
void TableZone::reorganizeCards()

View file

@ -27,7 +27,6 @@
#include <functional>
#include <libcockatrice/card/card_localization.h>
#include <libcockatrice/protocol/pb/command_dump_zone.pb.h>
#include <libcockatrice/protocol/pb/command_move_card.pb.h>
#include <libcockatrice/protocol/pb/response_dump_zone.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_card.pb.h>
#include <libcockatrice/protocol/pending_command.h>
@ -301,24 +300,13 @@ void ZoneViewZone::handleDropEvent(const QList<CardDragItem *> &dragItems,
CardZoneLogic *startZone,
const QPoint & /*dropPoint*/)
{
Command_MoveCard cmd;
cmd.set_start_player_id(startZone->getPlayer()->getPlayerInfo()->getId());
cmd.set_start_zone(startZone->getName().toStdString());
cmd.set_target_player_id(getLogic()->getPlayer()->getPlayerInfo()->getId());
cmd.set_target_zone(getLogic()->getName().toStdString());
cmd.set_x(0);
cmd.set_y(0);
cmd.set_is_reversed(qobject_cast<ZoneViewZoneLogic *>(getLogic())->getIsReversed());
QList<CardMoveRequest> cards;
cards.reserve(dragItems.size());
for (int i = 0; i < dragItems.size(); ++i) {
auto cardToMove = cmd.mutable_cards_to_move()->add_card();
cardToMove->set_card_id(dragItems[i]->getId());
if (dragItems[i]->isForceFaceDown()) {
cardToMove->set_face_down(true);
}
cards.append(CardMoveRequest{dragItems[i]->getId(), dragItems[i]->isForceFaceDown()});
}
getLogic()->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
getLogic()->getPlayer()->getPlayerActions()->moveCards(startZone, getLogic(), 0, 0, cards);
}
void ZoneViewZone::setGeometry(const QRectF &rect)