From 8dcf81654e35aca32b824b4bf9d20909e816c250 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sun, 22 Nov 2009 00:34:31 +0100 Subject: [PATCH] decklist transfer code --- cockatrice/cockatrice.pro | 4 + cockatrice/src/abstractcarditem.cpp | 148 ++++++++++++++++++++ cockatrice/src/abstractcarditem.h | 43 ++++++ cockatrice/src/carditem.cpp | 132 +----------------- cockatrice/src/carditem.h | 27 +--- cockatrice/src/deckview.cpp | 168 +++++++++++++++++++++++ cockatrice/src/deckview.h | 62 +++++++++ cockatrice/src/player.cpp | 1 + cockatrice/src/tab_chatchannel.cpp | 4 + cockatrice/src/tab_chatchannel.h | 1 + cockatrice/src/tab_deck_storage.cpp | 8 +- cockatrice/src/tab_game.cpp | 38 ++++- cockatrice/src/tab_game.h | 6 +- cockatrice/src/tab_server.cpp | 7 + cockatrice/src/tab_server.h | 1 + cockatrice/src/tab_supervisor.cpp | 17 ++- cockatrice/src/window_main.cpp | 17 +-- cockatrice/src/window_main.h | 1 - common/decklist.cpp | 13 +- common/decklist.h | 1 + common/protocol.cpp | 49 ++++++- common/protocol.h | 36 +++-- common/protocol_item_ids.h | 8 +- common/protocol_items.cpp | 16 ++- common/protocol_items.dat | 2 +- common/protocol_items.h | 20 +-- common/server_player.cpp | 51 ++++--- common/server_player.h | 9 +- common/server_protocolhandler.cpp | 31 ++++- common/server_protocolhandler.h | 4 +- servatrice/src/serversocketinterface.cpp | 27 ++-- servatrice/src/serversocketinterface.h | 2 + 32 files changed, 694 insertions(+), 260 deletions(-) create mode 100644 cockatrice/src/abstractcarditem.cpp create mode 100644 cockatrice/src/abstractcarditem.h create mode 100644 cockatrice/src/deckview.cpp create mode 100644 cockatrice/src/deckview.h diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 96694aec..6685d58e 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -16,6 +16,7 @@ HEADERS += src/counter.h \ src/cardzone.h \ src/player.h \ src/cardlist.h \ + src/abstractcarditem.h \ src/carditem.h \ src/tablezone.h \ src/handzone.h \ @@ -45,6 +46,7 @@ HEADERS += src/counter.h \ src/tab_game.h \ src/tab_deck_storage.h \ src/tab_supervisor.h \ + src/deckview.h \ ../common/decklist.h \ ../common/protocol.h \ ../common/protocol_items.h \ @@ -60,6 +62,7 @@ SOURCES += src/counter.cpp \ src/player.cpp \ src/cardzone.cpp \ src/cardlist.cpp \ + src/abstractcarditem.cpp \ src/carditem.cpp \ src/tablezone.cpp \ src/handzone.cpp \ @@ -89,6 +92,7 @@ SOURCES += src/counter.cpp \ src/tab_game.cpp \ src/tab_deck_storage.cpp \ src/tab_supervisor.cpp \ + src/deckview.cpp \ ../common/decklist.cpp \ ../common/protocol.cpp \ ../common/protocol_items.cpp diff --git a/cockatrice/src/abstractcarditem.cpp b/cockatrice/src/abstractcarditem.cpp new file mode 100644 index 00000000..71f4ae91 --- /dev/null +++ b/cockatrice/src/abstractcarditem.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include "carddatabase.h" +#include "abstractcarditem.h" +#include "main.h" +#include + +AbstractCardItem::AbstractCardItem(const QString &_name, QGraphicsItem *parent) + : AbstractGraphicsItem(parent), info(db->getCard(_name)), name(_name), tapped(false) +{ + setCursor(Qt::OpenHandCursor); + setFlag(ItemIsSelectable); + setAcceptsHoverEvents(true); + setCacheMode(DeviceCoordinateCache); + + connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated())); +} + +AbstractCardItem::~AbstractCardItem() +{ + qDebug(QString("AbstractCardItem destructor: %1").arg(name).toLatin1()); +} + +QRectF AbstractCardItem::boundingRect() const +{ + return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); +} + +void AbstractCardItem::pixmapUpdated() +{ + update(); +} + +void AbstractCardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/) +{ + painter->save(); + + QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); + if (tapped) + translatedSize.transpose(); + QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize()); + painter->save(); + if (translatedPixmap) { + painter->resetTransform(); + if (tapped) { + painter->translate(((qreal) translatedSize.height()) / 2, ((qreal) translatedSize.width()) / 2); + painter->rotate(90); + painter->translate(-((qreal) translatedSize.width()) / 2, -((qreal) translatedSize.height()) / 2); + } + painter->drawPixmap(translatedPixmap->rect(), *translatedPixmap, translatedPixmap->rect()); + } else { + QFont f("Serif"); + f.setStyleHint(QFont::Serif); + f.setPixelSize(11); + painter->setFont(f); + painter->setBrush(QColor(230, 230, 230)); + qDebug() <<"COLORS:::::" << info->getColors(); + QString color; + QPen pen; + if(!info->getColors().empty()) + { + color = info->getColors().first(); + if(color == "B") + painter->setBrush(QColor(0,0,0)); + if(color == "U") + painter->setBrush(QColor(0,140,180)); + if(color == "W") + painter->setBrush(QColor(255,250,140)); + if(color == "R") + painter->setBrush(QColor(230,0,0)); + if(color == "G") + painter->setBrush(QColor(0,160,0)); + if(info->getColors().size() > 1) + { + painter->setBrush(QColor(250,190,30)); + color = "M"; // Multicolor + } + + } + + painter->setPen(Qt::black); + + painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); + + pen.setWidth(3); + painter->setPen(pen); + painter->drawRect(QRectF(3, 3, CARD_WIDTH - 6, CARD_HEIGHT - 6)); + painter->setPen(Qt::white); + if(color == "W" || color == "" || color == "M") + painter->setPen(Qt::black); + painter->drawText(QRectF(5, 5, CARD_WIDTH - 15, CARD_HEIGHT - 15), Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap, name); + if(info->getCardType().contains("Creature")) + { + painter->drawText(QRectF(CARD_WIDTH - 40, CARD_HEIGHT - 25, 30, 30), Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap, info->getPowTough()); + } + } + painter->restore(); + + if (isSelected()) { + painter->setPen(Qt::red); + painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); + } + + painter->restore(); +} + +void AbstractCardItem::setName(const QString &_name) +{ + disconnect(info, 0, this, 0); + name = _name; + info = db->getCard(name); + connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated())); + update(); +} + +void AbstractCardItem::setTapped(bool _tapped) +{ + tapped = _tapped; + if (tapped) + setTransform(QTransform().translate((float) CARD_WIDTH / 2, (float) CARD_HEIGHT / 2).rotate(90).translate((float) -CARD_WIDTH / 2, (float) -CARD_HEIGHT / 2)); + else + setTransform(QTransform()); + update(); +} + +void AbstractCardItem::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (!isSelected()) { + scene()->clearSelection(); + setSelected(true); + } + if (event->button() == Qt::LeftButton) + setCursor(Qt::ClosedHandCursor); + event->accept(); +} + +QVariant AbstractCardItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +{ + if (change == ItemSelectedHasChanged) { + update(); + return value; + } else + return QGraphicsItem::itemChange(change, value); +} + diff --git a/cockatrice/src/abstractcarditem.h b/cockatrice/src/abstractcarditem.h new file mode 100644 index 00000000..6ed76b62 --- /dev/null +++ b/cockatrice/src/abstractcarditem.h @@ -0,0 +1,43 @@ +#ifndef ABSTRACTCARDITEM_H +#define ABSTRACTCARDITEM_H + +#include "abstractgraphicsitem.h" + +class CardInfo; + +const int CARD_WIDTH = 72; +const int CARD_HEIGHT = 102; + +enum CardItemType { + typeCard = QGraphicsItem::UserType + 1, + typeCardDrag = QGraphicsItem::UserType + 2, + typeZone = QGraphicsItem::UserType + 3, + typeOther = QGraphicsItem::UserType + 4 +}; + +class AbstractCardItem : public QObject, public AbstractGraphicsItem { + Q_OBJECT +protected: + CardInfo *info; + QString name; + bool tapped; +private slots: + void pixmapUpdated(); +public: + enum { Type = typeCard }; + int type() const { return Type; } + AbstractCardItem(const QString &_name = QString(), QGraphicsItem *parent = 0); + ~AbstractCardItem(); + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + CardInfo *getInfo() const { return info; } + QString getName() const { return name; } + void setName(const QString &_name = QString()); + bool getTapped() const { return tapped; } + void setTapped(bool _tapped); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); +}; + +#endif diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 8efac478..91a34dfa 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -11,126 +11,24 @@ #include "main.h" CardItem::CardItem(const QString &_name, int _cardid, QGraphicsItem *parent) - : AbstractGraphicsItem(parent), info(db->getCard(_name)), name(_name), id(_cardid), tapped(false), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL) + : AbstractCardItem(_name, parent), id(_cardid), attacking(false), facedown(false), counters(0), doesntUntap(false), dragItem(NULL) { - setCursor(Qt::OpenHandCursor); - setFlag(ItemIsSelectable); - setAcceptsHoverEvents(true); - setCacheMode(DeviceCoordinateCache); - - connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated())); } CardItem::~CardItem() { deleteDragItem(); - qDebug(QString("CardItem destructor: %1").arg(name).toLatin1()); } -QRectF CardItem::boundingRect() const -{ - return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); -} - -void CardItem::pixmapUpdated() -{ - update(); -} - -void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*widget*/) +void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->save(); - - QSizeF translatedSize = option->matrix.mapRect(boundingRect()).size(); - if (tapped) - translatedSize.transpose(); - QPixmap *translatedPixmap = info->getPixmap(translatedSize.toSize()); - painter->save(); - if (translatedPixmap) { - painter->resetTransform(); - if (tapped) { - painter->translate(((qreal) translatedSize.height()) / 2, ((qreal) translatedSize.width()) / 2); - painter->rotate(90); - painter->translate(-((qreal) translatedSize.width()) / 2, -((qreal) translatedSize.height()) / 2); - } - painter->drawPixmap(translatedPixmap->rect(), *translatedPixmap, translatedPixmap->rect()); - } else { - QFont f("Serif"); - f.setStyleHint(QFont::Serif); - f.setPixelSize(11); - painter->setFont(f); - painter->setBrush(QColor(230, 230, 230)); - qDebug() <<"COLORS:::::" << info->getColors(); - QString color; - QPen pen; - if(!info->getColors().empty()) - { - color = info->getColors().first(); - if(color == "B") - painter->setBrush(QColor(0,0,0)); - if(color == "U") - painter->setBrush(QColor(0,140,180)); - if(color == "W") - painter->setBrush(QColor(255,250,140)); - if(color == "R") - painter->setBrush(QColor(230,0,0)); - if(color == "G") - painter->setBrush(QColor(0,160,0)); - if(info->getColors().size() > 1) - { - painter->setBrush(QColor(250,190,30)); - color = "M"; // Multicolor - } - - } - - painter->setPen(Qt::black); - - painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); - - pen.setWidth(3); - painter->setPen(pen); - painter->drawRect(QRectF(3, 3, CARD_WIDTH - 6, CARD_HEIGHT - 6)); - painter->setPen(Qt::white); - if(color == "W" || color == "" || color == "M") - painter->setPen(Qt::black); - painter->drawText(QRectF(5, 5, CARD_WIDTH - 15, CARD_HEIGHT - 15), Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap, name); - if(info->getCardType().contains("Creature")) - { - painter->drawText(QRectF(CARD_WIDTH - 40, CARD_HEIGHT - 25, 30, 30), Qt::AlignTop | Qt::AlignRight | Qt::TextWordWrap, info->getPowTough()); - } - } - painter->restore(); - - if (isSelected()) { - painter->setPen(Qt::red); - painter->drawRect(QRectF(0.5, 0.5, CARD_WIDTH - 1, CARD_HEIGHT - 1)); - } + AbstractCardItem::paint(painter, option, widget); if (counters) paintNumberEllipse(counters, painter); - painter->restore(); } -void CardItem::setName(const QString &_name) -{ - disconnect(info, 0, this, 0); - name = _name; - info = db->getCard(name); - connect(info, SIGNAL(pixmapUpdated()), this, SLOT(pixmapUpdated())); - update(); -} - -void CardItem::setTapped(bool _tapped) -{ - tapped = _tapped; - if (tapped) - setTransform(QTransform().translate((float) CARD_WIDTH / 2, (float) CARD_HEIGHT / 2).rotate(90).translate((float) -CARD_WIDTH / 2, (float) -CARD_HEIGHT / 2)); - else - setTransform(QTransform()); - update(); -} - void CardItem::setAttacking(bool _attacking) { attacking = _attacking; @@ -189,17 +87,6 @@ void CardItem::deleteDragItem() dragItem = NULL; } -void CardItem::mousePressEvent(QGraphicsSceneMouseEvent *event) -{ - if (!isSelected()) { - scene()->clearSelection(); - setSelected(true); - } - if (event->button() == Qt::LeftButton) - setCursor(Qt::ClosedHandCursor); - event->accept(); -} - void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (event->buttons().testFlag(Qt::RightButton)) { @@ -278,16 +165,3 @@ void CardItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) ((Game *) ((CardZone *) parentItem())->getPlayer()->parent())->hoverCardEvent(this); QGraphicsItem::hoverEnterEvent(event); } - -QVariant CardItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) -{ - if (change == ItemSelectedChange) { - // XXX - return value; - } else if (change == ItemSelectedHasChanged) { - qDebug("selection changed"); - update(); - return value; - } else - return QGraphicsItem::itemChange(change, value); -} diff --git a/cockatrice/src/carditem.h b/cockatrice/src/carditem.h index e1945829..93fb1a42 100644 --- a/cockatrice/src/carditem.h +++ b/cockatrice/src/carditem.h @@ -1,32 +1,18 @@ #ifndef CARDITEM_H #define CARDITEM_H -#include "abstractgraphicsitem.h" +#include "abstractcarditem.h" class CardDatabase; class CardDragItem; class CardZone; -class CardInfo; - -const int CARD_WIDTH = 72; -const int CARD_HEIGHT = 102; const int MAX_COUNTERS_ON_CARD = 999; -enum CardItemType { - typeCard = QGraphicsItem::UserType + 1, - typeCardDrag = QGraphicsItem::UserType + 2, - typeZone = QGraphicsItem::UserType + 3, - typeOther = QGraphicsItem::UserType + 4 -}; - -class CardItem : public QObject, public AbstractGraphicsItem { +class CardItem : public AbstractCardItem { Q_OBJECT private: - CardInfo *info; - QString name; int id; - bool tapped; bool attacking; bool facedown; int counters; @@ -34,23 +20,16 @@ private: bool doesntUntap; QPoint gridPoint; CardDragItem *dragItem; -private slots: - void pixmapUpdated(); public: enum { Type = typeCard }; int type() const { return Type; } CardItem(const QString &_name = QString(), int _cardid = -1, QGraphicsItem *parent = 0); ~CardItem(); - QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPoint getGridPoint() const { return gridPoint; } void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; } int getId() const { return id; } void setId(int _id) { id = _id; } - QString getName() const { return name; } - void setName(const QString &_name = QString()); - bool getTapped() const { return tapped; } - void setTapped(bool _tapped); bool getAttacking() const { return attacking; } void setAttacking(bool _attacking); bool getFaceDown() const { return facedown; } @@ -66,12 +45,10 @@ public: CardDragItem *createDragItem(int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown); void deleteDragItem(); protected: - void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event); - QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); }; #endif diff --git a/cockatrice/src/deckview.cpp b/cockatrice/src/deckview.cpp new file mode 100644 index 00000000..c528237a --- /dev/null +++ b/cockatrice/src/deckview.cpp @@ -0,0 +1,168 @@ +#include +#include "deckview.h" +#include "decklist.h" +#include "carddatabase.h" +#include "main.h" +#include + +DeckViewCard::DeckViewCard(const QString &_name, QGraphicsItem *parent) + : AbstractCardItem(_name, parent) +{ +} + +DeckViewCardContainer::DeckViewCardContainer(const QString &_name) + : QGraphicsItem(), name(_name), width(0), height(0) +{ + QSettings settings; + QString bgPath = settings.value("zonebg/table").toString(); + if (!bgPath.isEmpty()) + bgPixmap.load(bgPath); + + setCacheMode(DeviceCoordinateCache); +} + +QRectF DeckViewCardContainer::boundingRect() const +{ + return QRectF(0, 0, width, height); +} + +void DeckViewCardContainer::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/) +{ + if (bgPixmap.isNull()) + painter->fillRect(boundingRect(), QColor(0, 0, 100)); + else + painter->fillRect(boundingRect(), QBrush(bgPixmap)); + painter->setPen(QColor(255, 255, 255, 100)); + painter->drawLine(QPointF(0, separatorY), QPointF(width, separatorY)); + + painter->setPen(QColor(Qt::white)); + QFont f("Serif"); + f.setStyleHint(QFont::Serif); + f.setPixelSize(24); + f.setWeight(QFont::Bold); + painter->setFont(f); + painter->drawText(10, 0, width - 20, separatorY, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, InnerDecklistNode::visibleNameFromName(name)); +} + +void DeckViewCardContainer::addCard(DeckViewCard *card) +{ + cards.insertMulti(card->getInfo()->getMainCardType(), card); +} + +void DeckViewCardContainer::rearrangeItems() +{ + separatorY = 30; + + QList cardTypeList = cards.uniqueKeys(); + int rows = cardTypeList.size(); + int cols = 0; + for (int i = 0; i < rows; ++i) { + QList row = cards.values(cardTypeList[i]); + if (row.size() > cols) + cols = row.size(); + for (int j = 0; j < row.size(); ++j) { + DeckViewCard *card = row[j]; + card->setPos(j * CARD_WIDTH, separatorY + 10 + i * (CARD_HEIGHT + rowSpacing)); + } + } + + prepareGeometryChange(); + width = cols * CARD_WIDTH; + height = separatorY + 10 + rows * CARD_HEIGHT + rowSpacing * (rows - 1); +} + +void DeckViewCardContainer::setWidth(qreal _width) +{ + prepareGeometryChange(); + width = _width; + update(); +} + +DeckViewScene::DeckViewScene(QObject *parent) + : QGraphicsScene(parent), deck(0) +{ +} + +DeckViewScene::~DeckViewScene() +{ +} + +void DeckViewScene::setDeck(DeckList *_deck) +{ + deck = _deck; + rebuildTree(); + rearrangeItems(); +} + +void DeckViewScene::rebuildTree() +{ + InnerDecklistNode *listRoot = deck->getRoot(); + for (int i = 0; i < listRoot->size(); i++) { + InnerDecklistNode *currentZone = dynamic_cast(listRoot->at(i)); + + DeckViewCardContainer *container = cardContainers.value(currentZone->getName(), 0); + if (!container) { + container = new DeckViewCardContainer(currentZone->getName()); + cardContainers.insert(currentZone->getName(), container); + addItem(container); + } + + for (int j = 0; j < currentZone->size(); j++) { + DecklistCardNode *currentCard = dynamic_cast(currentZone->at(j)); + if (!currentCard) + continue; + + for (int k = 0; k < currentCard->getNumber(); ++k) + container->addCard(new DeckViewCard(currentCard->getName(), container)); + } + } +} + +void DeckViewScene::rearrangeItems() +{ + const int spacing = CARD_HEIGHT / 3; + QList contList = cardContainers.values(); + qreal totalHeight = -spacing; + qreal totalWidth = 0; + for (int i = 0; i < contList.size(); ++i) { + DeckViewCardContainer *c = contList[i]; + c->rearrangeItems(); + c->setPos(0, totalHeight + spacing); + totalHeight += c->boundingRect().height() + spacing; + if (c->boundingRect().width() > totalWidth) + totalWidth = c->boundingRect().width(); + } + for (int i = 0; i < contList.size(); ++i) + contList[i]->setWidth(totalWidth); + + setSceneRect(QRectF(0, 0, totalWidth, totalHeight)); +} + +DeckView::DeckView(QWidget *parent) + : QGraphicsView(parent) +{ + deckViewScene = new DeckViewScene(this); + + setBackgroundBrush(QBrush(QColor(0, 0, 0))); + setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing/* | QPainter::SmoothPixmapTransform*/); + setScene(deckViewScene); + + connect(deckViewScene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(updateSceneRect(const QRectF &))); +} + +void DeckView::resizeEvent(QResizeEvent *event) +{ + QGraphicsView::resizeEvent(event); + updateSceneRect(scene()->sceneRect()); +} + +void DeckView::updateSceneRect(const QRectF &rect) +{ + qDebug(QString("deckView::updateSceneRect = %1,%2").arg(rect.width()).arg(rect.height()).toLatin1()); + fitInView(rect, Qt::KeepAspectRatio); +} + +void DeckView::setDeck(DeckList *_deck) +{ + deckViewScene->setDeck(_deck); +} diff --git a/cockatrice/src/deckview.h b/cockatrice/src/deckview.h new file mode 100644 index 00000000..7f800eba --- /dev/null +++ b/cockatrice/src/deckview.h @@ -0,0 +1,62 @@ +#ifndef DECKVIEW_H +#define DECKVIEW_H + +#include +#include +#include +#include +#include "carditem.h" + +class DeckList; +class InnerDecklistNode; +class CardInfo; + +class DeckViewCard : public AbstractCardItem { +public: + DeckViewCard(const QString &_name = QString(), QGraphicsItem *parent = 0); +}; + +class DeckViewCardContainer : public QGraphicsItem { +private: + QString name; + QMap cards; + qreal width, height; + qreal separatorY; + QPixmap bgPixmap; + static const int rowSpacing = 5; +public: + DeckViewCardContainer(const QString &_name); + QRectF boundingRect() const; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + void addCard(DeckViewCard *card); + void rearrangeItems(); + void setWidth(qreal _width); +}; + +class DeckViewScene : public QGraphicsScene { + Q_OBJECT +private: + DeckList *deck; + QMap cardContainers; + void rebuildTree(); + void rearrangeItems(); +public: + DeckViewScene(QObject *parent = 0); + ~DeckViewScene(); + void setDeck(DeckList *_deck); +}; + +class DeckView : public QGraphicsView { + Q_OBJECT +private: + DeckViewScene *deckViewScene; +protected: + void resizeEvent(QResizeEvent *event); +public slots: + void updateSceneRect(const QRectF &rect); +public: + DeckView(QWidget *parent = 0); + void setDeck(DeckList *_deck); +}; + +#endif diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 2e76b5a2..77451fdf 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -212,6 +212,7 @@ Player::~Player() void Player::updateBoundingRect() { + // XXX! PREPARE GEOMETRY CHANGE bRect = QRectF(0, 0, CARD_WIDTH + 5 + counterAreaWidth + hand->boundingRect().width() + table->boundingRect().width(), table->boundingRect().height()); emit sizeChanged(); } diff --git a/cockatrice/src/tab_chatchannel.cpp b/cockatrice/src/tab_chatchannel.cpp index 0bebdd56..0513d8a4 100644 --- a/cockatrice/src/tab_chatchannel.cpp +++ b/cockatrice/src/tab_chatchannel.cpp @@ -25,6 +25,10 @@ TabChatChannel::TabChatChannel(Client *_client, const QString &_channelName) setLayout(hbox); } +void TabChatChannel::retranslateUi() +{ +} + void TabChatChannel::sendMessage() { if (sayEdit->text().isEmpty()) diff --git a/cockatrice/src/tab_chatchannel.h b/cockatrice/src/tab_chatchannel.h index 2c663281..52d7d3fd 100644 --- a/cockatrice/src/tab_chatchannel.h +++ b/cockatrice/src/tab_chatchannel.h @@ -31,6 +31,7 @@ private slots: void processSayEvent(Event_ChatSay *event); public: TabChatChannel(Client *_client, const QString &_channelName); + void retranslateUi(); void processChatEvent(ChatEvent *event); }; diff --git a/cockatrice/src/tab_deck_storage.cpp b/cockatrice/src/tab_deck_storage.cpp index bbd69844..ebb3289b 100644 --- a/cockatrice/src/tab_deck_storage.cpp +++ b/cockatrice/src/tab_deck_storage.cpp @@ -177,9 +177,8 @@ void TabDeckStorage::actUpload() curRight = curRight->parent(); if (curRight) targetPath = curRight->data(0, Qt::UserRole).toString(); - qDebug() << "targetPath:" << targetPath; - Command_DeckUpload *command = new Command_DeckUpload(-1, deck, targetPath); + Command_DeckUpload *command = new Command_DeckUpload(deck, targetPath); connect(command, SIGNAL(finished(ProtocolResponse *)), this, SLOT(uploadFinished(ProtocolResponse *))); client->sendCommand(command); } @@ -190,6 +189,7 @@ void TabDeckStorage::uploadFinished(ProtocolResponse *r) if (!resp) return; Command_DeckUpload *cmd = static_cast(sender()); + delete cmd->getDeck(); QTreeWidgetItemIterator it(serverDirView); while (*it) { @@ -264,7 +264,6 @@ void TabDeckStorage::newFolderFinished(ResponseCode resp) QTreeWidgetItemIterator it(serverDirView); while (*it) { if ((*it)->data(0, Qt::UserRole) == cmd->getPath()) { - qDebug() << "gefunden"; QFileIconProvider fip; QTreeWidgetItem *newItem = new QTreeWidgetItem(TWIFolderType); newItem->setIcon(0, fip.icon(QFileIconProvider::Folder)); @@ -272,8 +271,7 @@ void TabDeckStorage::newFolderFinished(ResponseCode resp) newItem->setData(0, Qt::UserRole, cmd->getPath() + "/" + cmd->getDirName()); (*it)->addChild(newItem); break; - } else - qDebug() << "path = " << (*it)->data(0, Qt::UserRole) << " nicht gefunden"; + } ++it; } } diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index d33795d7..35dd95bc 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -10,6 +10,8 @@ #include "zoneviewzone.h" #include "zoneviewwidget.h" #include "zoneviewlayout.h" +#include "deckview.h" +#include "decklist.h" #include "main.h" TabGame::TabGame(Client *_client, int _gameId) @@ -17,7 +19,15 @@ TabGame::TabGame(Client *_client, int _gameId) { zoneLayout = new ZoneViewLayout; scene = new GameScene(zoneLayout, this); - view = new GameView(scene); + gameView = new GameView(scene); + gameView->hide(); + + deckView = new DeckView; + + DeckList *foo = new DeckList; + foo->loadFromFile("/home/brukie/cockatrice/decks/adfb.cod", DeckList::CockatriceFormat); + deckView->setDeck(foo); +// deckView->hide(); cardInfo = new CardInfoWidget(db); messageLog = new MessageLogWidget; @@ -30,6 +40,7 @@ TabGame::TabGame(Client *_client, int _gameId) hLayout->addWidget(sayEdit); phasesToolbar = new PhasesToolbar; + phasesToolbar->hide(); QVBoxLayout *verticalLayout = new QVBoxLayout; verticalLayout->addWidget(cardInfo); @@ -38,11 +49,14 @@ TabGame::TabGame(Client *_client, int _gameId) QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(phasesToolbar); - mainLayout->addWidget(view, 10); + mainLayout->addWidget(gameView, 10); + mainLayout->addWidget(deckView, 10); mainLayout->addLayout(verticalLayout); - setLayout(mainLayout); - + aCloseMostRecentZoneView = new QAction(this); + connect(aCloseMostRecentZoneView, SIGNAL(triggered()), zoneLayout, SLOT(closeMostRecentZoneView())); + addAction(aCloseMostRecentZoneView); + connect(sayEdit, SIGNAL(returnPressed()), this, SLOT(actSay())); // connect(client, SIGNAL(maxPingTime(int, int)), pingWidget, SLOT(setPercentage(int, int))); @@ -60,7 +74,21 @@ TabGame::TabGame(Client *_client, int _gameId) messageLog->connectToGame(game); game->queryGameState(); -*/} +*/ + retranslateUi(); + setLayout(mainLayout); +} + +void TabGame::retranslateUi() +{ + sayLabel->setText(tr("&Say:")); + cardInfo->retranslateUi(); +// if (game) +// game->retranslateUi(); + zoneLayout->retranslateUi(); + aCloseMostRecentZoneView->setText(tr("Close most recent zone view")); + aCloseMostRecentZoneView->setShortcut(tr("Esc")); +} void TabGame::processGameEvent(GameEvent *event) { diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index 048dde04..a66b03c1 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -7,6 +7,7 @@ class Client; class CardDatabase; class GameEvent; class GameView; +class DeckView; class GameScene; class Game; class CardInfoWidget; @@ -30,12 +31,15 @@ private: QLineEdit *sayEdit; PhasesToolbar *phasesToolbar; GameScene *scene; - GameView *view; + GameView *gameView; + DeckView *deckView; Game *game; ZoneViewLayout *zoneLayout; + QAction *aCloseMostRecentZoneView; private slots: public: TabGame(Client *_client, int _gameId); + void retranslateUi(); void processGameEvent(GameEvent *event); }; diff --git a/cockatrice/src/tab_server.cpp b/cockatrice/src/tab_server.cpp index 7c0c52b1..d3e13eb3 100644 --- a/cockatrice/src/tab_server.cpp +++ b/cockatrice/src/tab_server.cpp @@ -244,3 +244,10 @@ TabServer::TabServer(Client *_client, QWidget *parent) setLayout(mainLayout); } + +void TabServer::retranslateUi() +{ + gameSelector->retranslateUi(); + chatChannelSelector->retranslateUi(); + serverMessageLog->retranslateUi(); +} diff --git a/cockatrice/src/tab_server.h b/cockatrice/src/tab_server.h index 04399979..446a45bc 100644 --- a/cockatrice/src/tab_server.h +++ b/cockatrice/src/tab_server.h @@ -83,6 +83,7 @@ private: ServerMessageLog *serverMessageLog; public: TabServer(Client *_client, QWidget *parent = 0); + void retranslateUi(); }; #endif diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 94985898..bdb1c667 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -14,10 +14,22 @@ TabSupervisor:: TabSupervisor(QWidget *parent) void TabSupervisor::retranslateUi() { - if (tabServer) + if (tabServer) { setTabText(0, tr("Server")); - if (tabDeckStorage) + tabServer->retranslateUi(); + } + if (tabDeckStorage) { setTabText(1, tr("Deck storage")); + tabDeckStorage->retranslateUi(); + } + + QMapIterator chatChannelIterator(chatChannelTabs); + while (chatChannelIterator.hasNext()) + chatChannelIterator.next().value()->retranslateUi(); + + QMapIterator gameIterator(gameTabs); + while (gameIterator.hasNext()) + gameIterator.next().value()->retranslateUi(); } void TabSupervisor::start(Client *_client) @@ -28,7 +40,6 @@ void TabSupervisor::start(Client *_client) connect(client, SIGNAL(gameJoinedEventReceived(Event_GameJoined *)), this, SLOT(gameJoined(Event_GameJoined *))); tabServer = new TabServer(client); - connect(tabServer, SIGNAL(gameJoined(int)), this, SLOT(addGameTab(int))); connect(tabServer, SIGNAL(chatChannelJoined(const QString &)), this, SLOT(addChatChannelTab(const QString &))); addTab(tabServer, QString()); diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 354442f7..3e98ef59 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -201,20 +201,9 @@ void MainWindow::retranslateUi() aFullScreen->setShortcut(tr("Ctrl+F")); aSettings->setText(tr("&Settings...")); aExit->setText(tr("&Exit")); - aCloseMostRecentZoneView->setText(tr("Close most recent zone view")); - aCloseMostRecentZoneView->setShortcut(tr("Esc")); cockatriceMenu->setTitle(tr("&Cockatrice")); -/* - sayLabel->setText(tr("&Say:")); - - cardInfo->retranslateUi(); - chatWidget->retranslateUi(); - gameSelector->retranslateUi(); - if (game) - game->retranslateUi(); - zoneLayout->retranslateUi(); -*/} +} void MainWindow::createActions() { @@ -238,10 +227,6 @@ void MainWindow::createActions() connect(aSettings, SIGNAL(triggered()), this, SLOT(actSettings())); aExit = new QAction(this); connect(aExit, SIGNAL(triggered()), this, SLOT(actExit())); - - aCloseMostRecentZoneView = new QAction(this); -// connect(aCloseMostRecentZoneView, SIGNAL(triggered()), zoneLayout, SLOT(closeMostRecentZoneView())); - addAction(aCloseMostRecentZoneView); } void MainWindow::createMenus() diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 31f049f9..5d29df2d 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -61,7 +61,6 @@ private: void createMenus(); QMenu *cockatriceMenu; QAction *aConnect, *aDisconnect, *aDeckEditor, *aFullScreen, *aSettings, *aExit; - QAction *aCloseMostRecentZoneView; TabSupervisor *tabSupervisor; PingWidget *pingWidget; diff --git a/common/decklist.cpp b/common/decklist.cpp index a4fe437b..1482fab2 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -26,14 +26,19 @@ InnerDecklistNode::~InnerDecklistNode() clearTree(); } -QString InnerDecklistNode::getVisibleName() const +QString InnerDecklistNode::visibleNameFromName(const QString &_name) { - if (name == "main") + if (_name == "main") return QObject::tr("Maindeck"); - else if (name == "side") + else if (_name == "side") return QObject::tr("Sideboard"); else - return getName(); + return _name; +} + +QString InnerDecklistNode::getVisibleName() const +{ + return visibleNameFromName(name); } void InnerDecklistNode::clearTree() diff --git a/common/decklist.h b/common/decklist.h index cabade83..2010f7b5 100644 --- a/common/decklist.h +++ b/common/decklist.h @@ -39,6 +39,7 @@ public: virtual ~InnerDecklistNode(); QString getName() const { return name; } void setName(const QString &_name) { name = _name; } + static QString visibleNameFromName(const QString &_name); virtual QString getVisibleName() const; void clearTree(); AbstractDecklistNode *findChild(const QString &name); diff --git a/common/protocol.cpp b/common/protocol.cpp index 38458be8..bf8d40c1 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -66,6 +66,7 @@ void ProtocolItem::initializeHash() initializeHashAuto(); itemNameHash.insert("cmddeck_upload", Command_DeckUpload::newItem); + itemNameHash.insert("cmddeck_select", Command_DeckSelect::newItem); itemNameHash.insert("resp", ProtocolResponse::newItem); ProtocolResponse::initializeHash(); @@ -102,17 +103,12 @@ void Command::processResponse(ProtocolResponse *response) emit finished(response->getResponseCode()); } -Command_DeckUpload::Command_DeckUpload(int _cmdId, DeckList *_deck, const QString &_path) - : Command("deck_upload", _cmdId), deck(_deck), path(_path), readFinished(false) +Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path) + : Command("deck_upload"), deck(_deck), path(_path), readFinished(false) { setParameter("path", path); } -Command_DeckUpload::~Command_DeckUpload() -{ - delete deck; -} - void Command_DeckUpload::extractParameters() { Command::extractParameters(); @@ -144,6 +140,45 @@ void Command_DeckUpload::writeElement(QXmlStreamWriter *xml) deck->writeElement(xml); } +Command_DeckSelect::Command_DeckSelect(int _gameId, DeckList *_deck, int _deckId) + : GameCommand("deck_upload", _gameId), deck(_deck), deckId(_deckId), readFinished(false) +{ + setParameter("deck_id", _deckId); +} + +void Command_DeckSelect::extractParameters() +{ + GameCommand::extractParameters(); + + bool ok; + deckId = parameters["deck_id"].toInt(&ok); + if (!ok) + deckId = -1; +} + +bool Command_DeckSelect::readElement(QXmlStreamReader *xml) +{ + if (readFinished) + return false; + + if (!deck) { + if (xml->isStartElement() && (xml->name() == "cockatrice_deck")) { + deck = new DeckList; + return true; + } + return false; + } + + if (deck->readElement(xml)) + readFinished = true; + return true; +} + +void Command_DeckSelect::writeElement(QXmlStreamWriter *xml) +{ + if (deck) + deck->writeElement(xml); +} QHash ProtocolResponse::responseHash; diff --git a/common/protocol.h b/common/protocol.h index d0c261d5..01a96d68 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -17,13 +17,14 @@ class ProtocolResponse; class DeckList; enum ItemId { - ItemId_Command_DeckUpload = ItemId_Other + 1, - ItemId_Event_ListChatChannels = ItemId_Other + 2, - ItemId_Event_ChatListPlayers = ItemId_Other + 3, - ItemId_Event_ListGames = ItemId_Other + 4, - ItemId_Response_DeckList = ItemId_Other + 5, - ItemId_Response_DeckDownload = ItemId_Other + 6, - ItemId_Response_DeckUpload = ItemId_Other + 7 + ItemId_Command_DeckUpload = ItemId_Other + 100, + ItemId_Command_DeckSelect = ItemId_Other + 101, + ItemId_Event_ListChatChannels = ItemId_Other + 200, + ItemId_Event_ChatListPlayers = ItemId_Other + 201, + ItemId_Event_ListGames = ItemId_Other + 202, + ItemId_Response_DeckList = ItemId_Other + 300, + ItemId_Response_DeckDownload = ItemId_Other + 301, + ItemId_Response_DeckUpload = ItemId_Other + 302 }; class ProtocolItem : public QObject { @@ -138,14 +139,31 @@ protected: bool readElement(QXmlStreamReader *xml); void writeElement(QXmlStreamWriter *xml); public: - Command_DeckUpload(int _cmdId = -1, DeckList *_deck = 0, const QString &_path = QString()); - ~Command_DeckUpload(); + Command_DeckUpload(DeckList *_deck = 0, const QString &_path = QString()); static ProtocolItem *newItem() { return new Command_DeckUpload; } int getItemId() const { return ItemId_Command_DeckUpload; } DeckList *getDeck() const { return deck; } QString getPath() const { return path; } }; +class Command_DeckSelect : public GameCommand { + Q_OBJECT +private: + DeckList *deck; + int deckId; + bool readFinished; +protected: + void extractParameters(); + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); +public: + Command_DeckSelect(int _gameId = -1, DeckList *_deck = 0, int _deckId = -1); + static ProtocolItem *newItem() { return new Command_DeckSelect; } + int getItemId() const { return ItemId_Command_DeckSelect; } + DeckList *getDeck() const { return deck; } + int getDeckId() const { return deckId; } +}; + // ----------------- // --- RESPONSES --- // ----------------- diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h index 1b37e790..fe7b3e07 100644 --- a/common/protocol_item_ids.h +++ b/common/protocol_item_ids.h @@ -33,10 +33,10 @@ ItemId_Command_SetActivePhase = 1031, ItemId_Command_DumpZone = 1032, ItemId_Command_StopDumpZone = 1033, ItemId_Command_DumpAll = 1034, -ItemId_Command_SubmitDeck = 1035, -ItemId_Event_Say = 1036, -ItemId_Event_Join = 1037, -ItemId_Event_Leave = 1038, +ItemId_Event_Say = 1035, +ItemId_Event_Join = 1036, +ItemId_Event_Leave = 1037, +ItemId_Event_DeckSelect = 1038, ItemId_Event_GameClosed = 1039, ItemId_Event_ReadyStart = 1040, ItemId_Event_SetupZones = 1041, diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 954e534b..9152eb72 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -347,10 +347,6 @@ Command_DumpAll::Command_DumpAll(int _gameId) : GameCommand("dump_all", _gameId) { } -Command_SubmitDeck::Command_SubmitDeck(int _gameId) - : GameCommand("submit_deck", _gameId) -{ -} Event_Say::Event_Say(int _gameId, int _playerId, const QString &_message) : GameEvent("say", _gameId, _playerId), message(_message) { @@ -377,6 +373,16 @@ Event_Leave::Event_Leave(int _gameId, int _playerId) : GameEvent("leave", _gameId, _playerId) { } +Event_DeckSelect::Event_DeckSelect(int _gameId, int _playerId, int _deckId) + : GameEvent("deck_select", _gameId, _playerId), deckId(_deckId) +{ + setParameter("deck_id", deckId); +} +void Event_DeckSelect::extractParameters() +{ + GameEvent::extractParameters(); + deckId = parameters["deck_id"].toInt(); +} Event_GameClosed::Event_GameClosed(int _gameId, int _playerId) : GameEvent("game_closed", _gameId, _playerId) { @@ -687,10 +693,10 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem); itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem); itemNameHash.insert("cmddump_all", Command_DumpAll::newItem); - itemNameHash.insert("cmdsubmit_deck", Command_SubmitDeck::newItem); itemNameHash.insert("game_eventsay", Event_Say::newItem); itemNameHash.insert("game_eventjoin", Event_Join::newItem); itemNameHash.insert("game_eventleave", Event_Leave::newItem); + itemNameHash.insert("game_eventdeck_select", Event_DeckSelect::newItem); itemNameHash.insert("game_eventgame_closed", Event_GameClosed::newItem); itemNameHash.insert("game_eventready_start", Event_ReadyStart::newItem); itemNameHash.insert("game_eventsetup_zones", Event_SetupZones::newItem); diff --git a/common/protocol_items.dat b/common/protocol_items.dat index 43a62eaf..65316aff 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -32,10 +32,10 @@ 2:dump_zone:i,player_id:s,zone_name:i,number_cards 2:stop_dump_zone:i,player_id:s,zone_name 2:dump_all -2:submit_deck 3:say:s,message 3:join:s,player_name:b,spectator 3:leave +3:deck_select:i,deck_id 3:game_closed 3:ready_start 3:setup_zones:i,deck_size:i,sb_size diff --git a/common/protocol_items.h b/common/protocol_items.h index ea34a7e4..46e3e6cb 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -437,14 +437,6 @@ public: static ProtocolItem *newItem() { return new Command_DumpAll; } int getItemId() const { return ItemId_Command_DumpAll; } }; -class Command_SubmitDeck : public GameCommand { - Q_OBJECT -private: -public: - Command_SubmitDeck(int _gameId = -1); - static ProtocolItem *newItem() { return new Command_SubmitDeck; } - int getItemId() const { return ItemId_Command_SubmitDeck; } -}; class Event_Say : public GameEvent { Q_OBJECT private: @@ -479,6 +471,18 @@ public: static ProtocolItem *newItem() { return new Event_Leave; } int getItemId() const { return ItemId_Event_Leave; } }; +class Event_DeckSelect : public GameEvent { + Q_OBJECT +private: + int deckId; +public: + Event_DeckSelect(int _gameId = -1, int _playerId = -1, int _deckId = -1); + int getDeckId() const { return deckId; } + static ProtocolItem *newItem() { return new Event_DeckSelect; } + int getItemId() const { return ItemId_Event_DeckSelect; } +protected: + void extractParameters(); +}; class Event_GameClosed : public GameEvent { Q_OBJECT private: diff --git a/common/server_player.cpp b/common/server_player.cpp index dc12cc6e..c71e716e 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -7,9 +7,10 @@ #include "server_protocolhandler.h" #include "protocol.h" #include "protocol_items.h" +#include "decklist.h" Server_Player::Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler) - : game(_game), handler(_handler), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), PlayerStatus(StatusNormal) + : game(_game), handler(_handler), deck(0), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), PlayerStatus(StatusNormal) { } @@ -51,10 +52,10 @@ void Server_Player::setupZones() // ------------------------------------------------------------------ // Create zones - Server_CardZone *deck = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone); - addZone(deck); - Server_CardZone *sb = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone); - addZone(sb); + Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone); + addZone(deckZone); + Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone); + addZone(sbZone); addZone(new Server_CardZone(this, "table", true, Server_CardZone::PublicZone)); addZone(new Server_CardZone(this, "hand", false, Server_CardZone::PrivateZone)); addZone(new Server_CardZone(this, "grave", false, Server_CardZone::PublicZone)); @@ -63,20 +64,30 @@ void Server_Player::setupZones() // ------------------------------------------------------------------ // Assign card ids and create deck from decklist - QListIterator DeckIterator(DeckList); - int i = 0; - while (DeckIterator.hasNext()) - deck->cards.append(new Server_Card(DeckIterator.next(), i++, 0, 0)); - deck->shuffle(); + InnerDecklistNode *listRoot = deck->getRoot(); + nextCardId = 0; + for (int i = 0; i < listRoot->size(); ++i) { + InnerDecklistNode *currentZone = dynamic_cast(listRoot->at(i)); + Server_CardZone *z; + if (currentZone->getName() == "main") + z = deckZone; + else if (currentZone->getName() == "side") + z = sbZone; + else + continue; + + for (int j = 0; j < currentZone->size(); ++j) { + DecklistCardNode *currentCard = dynamic_cast(currentZone->at(j)); + if (!currentCard) + continue; + for (int k = 0; k < currentCard->getNumber(); ++k) + z->cards.append(new Server_Card(currentCard->getName(), nextCardId++, 0, 0)); + } + } + deckZone->shuffle(); - QListIterator SBIterator(SideboardList); - while (SBIterator.hasNext()) - sb->cards.append(new Server_Card(SBIterator.next(), i++, 0, 0)); - - nextCardId = i; - PlayerStatus = StatusPlaying; - game->sendGameEvent(new Event_SetupZones(-1, playerId, deck->cards.size(), sb->cards.size())); + game->sendGameEvent(new Event_SetupZones(-1, playerId, deckZone->cards.size(), sbZone->cards.size())); } void Server_Player::clearZones() @@ -97,6 +108,12 @@ void Server_Player::clearZones() arrows.clear(); } +void Server_Player::setDeck(DeckList *_deck) +{ + delete deck; + deck = _deck; +} + void Server_Player::addZone(Server_CardZone *zone) { zones.insert(zone->getName(), zone); diff --git a/common/server_player.h b/common/server_player.h index 0053d50e..d74a239e 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -6,6 +6,7 @@ #include #include +class DeckList; class Server_Game; class Server_CardZone; class Server_Counter; @@ -20,6 +21,7 @@ class Server_Player : public QObject { private: Server_Game *game; Server_ProtocolHandler *handler; + DeckList *deck; QMap zones; QMap counters; QMap arrows; @@ -30,11 +32,6 @@ private: void clearZones(); PlayerStatusEnum PlayerStatus; public: - // Pfusch - QList DeckList; - QList SideboardList; - // Pfusch Ende - Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler); void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; } @@ -44,6 +41,8 @@ public: int getPlayerId() const { return playerId; } bool getSpectator() const { return spectator; } QString getPlayerName() const { return playerName; } + void setDeck(DeckList *_deck); + DeckList *getDeck() const { return deck; } const QMap &getZones() const { return zones; } const QMap &getCounters() const { return counters; } const QMap &getArrows() const { return arrows; } diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 57f4016b..38ec6ecb 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -67,6 +67,7 @@ void Server_ProtocolHandler::processCommand(Command *command) Server_Player *player = gamePair.second; switch (command->getItemId()) { + case ItemId_Command_DeckSelect: response = cmdDeckSelect(qobject_cast(command), game, player); break; case ItemId_Command_LeaveGame: response = cmdLeaveGame(qobject_cast(command), game, player); break; case ItemId_Command_Say: response = cmdSay(qobject_cast(command), game, player); break; case ItemId_Command_Shuffle: response = cmdShuffle(qobject_cast(command), game, player); break; @@ -87,7 +88,6 @@ void Server_ProtocolHandler::processCommand(Command *command) case ItemId_Command_DumpZone: response = cmdDumpZone(qobject_cast(command), game, player); break; case ItemId_Command_StopDumpZone: response = cmdStopDumpZone(qobject_cast(command), game, player); break; case ItemId_Command_DumpAll: response = cmdDumpAll(qobject_cast(command), game, player); break; - case ItemId_Command_SubmitDeck: response = cmdSubmitDeck(qobject_cast(command), game, player); break; } } else { qDebug() << "received generic Command"; @@ -245,6 +245,27 @@ ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame * /*cmd*/, S return RespOk; } +ResponseCode Server_ProtocolHandler::cmdDeckSelect(Command_DeckSelect *cmd, Server_Game *game, Server_Player *player) +{ + DeckList *deck; + if (cmd->getDeckId() == -1) { + if (!cmd->getDeck()) + return RespInvalidData; + deck = cmd->getDeck(); + } else { + try { + deck = getDeckFromDatabase(cmd->getDeckId()); + } catch(ResponseCode r) { + return r; + } + } + player->setDeck(deck); + + game->sendGameEvent(new Event_DeckSelect(-1, player->getPlayerId(), cmd->getDeckId())); + + return RespOk; +} + ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player) { game->sendGameEvent(new Event_Say(-1, player->getPlayerId(), cmd->getMessage())); @@ -468,6 +489,9 @@ ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, Se ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart * /*cmd*/, Server_Game *game, Server_Player *player) { + if (!player->getDeck()) + return RespContextError; + player->setStatus(StatusReadyStart); game->sendGameEvent(new Event_ReadyStart(-1, player->getPlayerId())); game->startGameIfReady(); @@ -570,8 +594,3 @@ ResponseCode Server_ProtocolHandler::cmdDumpAll(Command_DumpAll *cmd, Server_Gam { return RespOk; } - -ResponseCode Server_ProtocolHandler::cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player) -{ - return RespOk; -} diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index d0f325ca..18c66335 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -27,6 +27,8 @@ protected: private: QList itemQueue; + virtual DeckList *getDeckFromDatabase(int deckId) = 0; + ResponseCode cmdPing(Command_Ping *cmd); ResponseCode cmdLogin(Command_Login *cmd); virtual ResponseCode cmdDeckList(Command_DeckList *cmd) = 0; @@ -43,6 +45,7 @@ private: ResponseCode cmdCreateGame(Command_CreateGame *cmd); ResponseCode cmdJoinGame(Command_JoinGame *cmd); ResponseCode cmdLeaveGame(Command_LeaveGame *cmd, Server_Game *game, Server_Player *player); + ResponseCode cmdDeckSelect(Command_DeckSelect *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdShuffle(Command_Shuffle *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdRollDie(Command_RollDie *cmd, Server_Game *game, Server_Player *player); @@ -62,7 +65,6 @@ private: ResponseCode cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, Server_Game *game, Server_Player *player); ResponseCode cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player); - ResponseCode cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player); public: Server_ProtocolHandler(Server *_server, QObject *parent = 0); ~Server_ProtocolHandler(); diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index 526ffc51..c3149fc1 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -26,6 +26,7 @@ #include "protocol.h" #include "protocol_items.h" #include "decklist.h" +#include "server_player.h" ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_socket, QObject *parent) : Server_ProtocolHandler(_server, parent), servatrice(_server), socket(_socket), currentItem(0) @@ -279,31 +280,41 @@ ResponseCode ServerSocketInterface::cmdDeckUpload(Command_DeckUpload *cmd) query.bindValue(":content", deckContents); servatrice->execSqlQuery(query); + delete cmd->getDeck(); + sendProtocolItem(new Response_DeckUpload(cmd->getCmdId(), RespOk, new DeckList_File(deckName, query.lastInsertId().toInt(), QDateTime::currentDateTime()))); return RespNothing; } -ResponseCode ServerSocketInterface::cmdDeckDownload(Command_DeckDownload *cmd) +DeckList *ServerSocketInterface::getDeckFromDatabase(int deckId) { servatrice->checkSql(); QSqlQuery query; query.prepare("select content from decklist_files where id = :id and user = :user"); - query.bindValue(":id", cmd->getDeckId()); + query.bindValue(":id", deckId); query.bindValue(":user", playerName); servatrice->execSqlQuery(query); if (!query.next()) - return RespNameNotFound; + throw RespNameNotFound; QXmlStreamReader deckReader(query.value(0).toString()); DeckList *deck = new DeckList; - if (!deck->loadFromXml(&deckReader)) { - delete deck; - deck = 0; - return RespInvalidData; - } + if (!deck->loadFromXml(&deckReader)) + throw RespInvalidData; + return deck; +} + +ResponseCode ServerSocketInterface::cmdDeckDownload(Command_DeckDownload *cmd) +{ + DeckList *deck; + try { + deck = getDeckFromDatabase(cmd->getDeckId()); + } catch(ResponseCode r) { + return r; + } sendProtocolItem(new Response_DeckDownload(cmd->getCmdId(), RespOk, deck)); return RespNothing; } diff --git a/servatrice/src/serversocketinterface.h b/servatrice/src/serversocketinterface.h index 3fffbe1d..2dae2f2b 100644 --- a/servatrice/src/serversocketinterface.h +++ b/servatrice/src/serversocketinterface.h @@ -27,6 +27,7 @@ class QTcpSocket; class Servatrice; class QXmlStreamReader; class QXmlStreamWriter; +class DeckList; class ServerSocketInterface : public Server_ProtocolHandler { @@ -50,6 +51,7 @@ private: ResponseCode cmdDeckDelDir(Command_DeckDelDir *cmd); ResponseCode cmdDeckDel(Command_DeckDel *cmd); ResponseCode cmdDeckUpload(Command_DeckUpload *cmd); + DeckList *getDeckFromDatabase(int deckId); ResponseCode cmdDeckDownload(Command_DeckDownload *cmd); void itemFinishedReading();