diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 6bf1d4ff..8d32ed68 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -78,7 +78,7 @@ HEADERS += src/counter.h \ ../common/server_arrow.h \ ../common/server_card.h \ ../common/server_cardzone.h \ - ../common/server_chatchannel.h \ + ../common/server_room.h \ ../common/server_counter.h \ ../common/server_game.h \ ../common/server_player.h \ @@ -154,7 +154,7 @@ SOURCES += src/counter.cpp \ ../common/server.cpp \ ../common/server_card.cpp \ ../common/server_cardzone.cpp \ - ../common/server_chatchannel.cpp \ + ../common/server_room.cpp \ ../common/server_game.cpp \ ../common/server_player.cpp \ ../common/server_protocolhandler.cpp diff --git a/cockatrice/src/abstractcarddragitem.h b/cockatrice/src/abstractcarddragitem.h index 62f36436..047c4f55 100644 --- a/cockatrice/src/abstractcarddragitem.h +++ b/cockatrice/src/abstractcarddragitem.h @@ -20,6 +20,7 @@ public: ~AbstractCardDragItem(); QRectF boundingRect() const { return QRectF(0, 0, CARD_WIDTH, CARD_HEIGHT); } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + AbstractCardItem *getItem() const { return item; } QPointF getHotSpot() const { return hotSpot; } void addChildDrag(AbstractCardDragItem *child); virtual void updatePosition(const QPointF &cursorScenePos) = 0; diff --git a/cockatrice/src/carddragitem.cpp b/cockatrice/src/carddragitem.cpp index 5375bae0..f3ab1a54 100644 --- a/cockatrice/src/carddragitem.cpp +++ b/cockatrice/src/carddragitem.cpp @@ -75,11 +75,11 @@ void CardDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) CardZone *startZone = static_cast(item)->getZone(); if (currentZone && !(static_cast(item)->getAttachedTo() && (startZone == currentZone))) { if (!occupied) - currentZone->handleDropEvent(id, startZone, (sp - currentZone->scenePos()).toPoint(), faceDown); + currentZone->handleDropEvent(this, startZone, (sp - currentZone->scenePos()).toPoint(), faceDown); for (int i = 0; i < childDrags.size(); i++) { CardDragItem *c = static_cast(childDrags[i]); if (!(static_cast(c->item)->getAttachedTo() && (startZone == currentZone)) && !c->occupied) - currentZone->handleDropEvent(c->id, startZone, (sp - currentZone->scenePos() + c->getHotSpot()).toPoint(), faceDown); + currentZone->handleDropEvent(static_cast(c), startZone, (sp - currentZone->scenePos() + c->getHotSpot()).toPoint(), faceDown); sc->removeItem(c); } } diff --git a/cockatrice/src/carddragitem.h b/cockatrice/src/carddragitem.h index 82f1d66a..95cc71ce 100644 --- a/cockatrice/src/carddragitem.h +++ b/cockatrice/src/carddragitem.h @@ -13,6 +13,7 @@ private: CardZone *currentZone; public: CardDragItem(CardItem *_item, int _id, const QPointF &_hotSpot, bool _faceDown, AbstractCardDragItem *parentDrag = 0); + int getId() const { return id; } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void updatePosition(const QPointF &cursorScenePos); protected: diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 1a03db31..7e76cfa7 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -17,91 +17,68 @@ #include "tab_game.h" CardItem::CardItem(Player *_owner, const QString &_name, int _cardid, QGraphicsItem *parent) - : AbstractCardItem(_name, _owner, parent), id(_cardid), attacking(false), facedown(false), destroyOnZoneChange(false), doesntUntap(false), dragItem(0), attachedTo(0) + : AbstractCardItem(_name, _owner, parent), zone(0), id(_cardid), attacking(false), facedown(false), destroyOnZoneChange(false), doesntUntap(false), dragItem(0), attachedTo(0) { owner->addCard(this); - if (owner->getLocal()) { - aTap = new QAction(this); - aTap->setData(0); - connect(aTap, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - aUntap = new QAction(this); - aUntap->setData(1); - connect(aUntap, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - aDoesntUntap = new QAction(this); - aDoesntUntap->setData(2); - connect(aDoesntUntap, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - aAttach = new QAction(this); - connect(aAttach, SIGNAL(triggered()), owner, SLOT(actAttach())); - aUnattach = new QAction(this); - connect(aUnattach, SIGNAL(triggered()), owner, SLOT(actUnattach())); - aSetPT = new QAction(this); - connect(aSetPT, SIGNAL(triggered()), owner, SLOT(actSetPT())); - aSetAnnotation = new QAction(this); - connect(aSetAnnotation, SIGNAL(triggered()), owner, SLOT(actSetAnnotation())); - aFlip = new QAction(this); - aFlip->setData(3); - connect(aFlip, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - aClone = new QAction(this); - aClone->setData(4); - connect(aClone, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - aMoveToTopLibrary = new QAction(this); - aMoveToTopLibrary->setData(5); - aMoveToBottomLibrary = new QAction(this); - aMoveToBottomLibrary->setData(6); - aMoveToGraveyard = new QAction(this); - aMoveToGraveyard->setData(7); - aMoveToExile = new QAction(this); - aMoveToExile->setData(8); - connect(aMoveToTopLibrary, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - connect(aMoveToBottomLibrary, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - connect(aMoveToGraveyard, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); - connect(aMoveToExile, SIGNAL(triggered()), owner, SLOT(cardMenuAction())); + aTap = new QAction(this); + aTap->setData(0); + connect(aTap, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + aUntap = new QAction(this); + aUntap->setData(1); + connect(aUntap, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + aDoesntUntap = new QAction(this); + aDoesntUntap->setData(2); + connect(aDoesntUntap, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + aAttach = new QAction(this); + connect(aAttach, SIGNAL(triggered()), this, SLOT(actAttach())); + aUnattach = new QAction(this); + connect(aUnattach, SIGNAL(triggered()), this, SLOT(actUnattach())); + aSetPT = new QAction(this); + connect(aSetPT, SIGNAL(triggered()), this, SLOT(actSetPT())); + aSetAnnotation = new QAction(this); + connect(aSetAnnotation, SIGNAL(triggered()), this, SLOT(actSetAnnotation())); + aFlip = new QAction(this); + aFlip->setData(3); + connect(aFlip, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + aClone = new QAction(this); + aClone->setData(4); + connect(aClone, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + aMoveToTopLibrary = new QAction(this); + aMoveToTopLibrary->setData(5); + aMoveToBottomLibrary = new QAction(this); + aMoveToBottomLibrary->setData(6); + aMoveToGraveyard = new QAction(this); + aMoveToGraveyard->setData(7); + aMoveToExile = new QAction(this); + aMoveToExile->setData(8); + connect(aMoveToTopLibrary, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + connect(aMoveToBottomLibrary, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + connect(aMoveToGraveyard, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + connect(aMoveToExile, SIGNAL(triggered()), this, SLOT(cardMenuAction())); + + aPlay = new QAction(this); + connect(aPlay, SIGNAL(triggered()), this, SLOT(actPlay())); - cardMenu = new QMenu; - cardMenu->addAction(aTap); - cardMenu->addAction(aUntap); - cardMenu->addAction(aDoesntUntap); - cardMenu->addAction(aFlip); - cardMenu->addSeparator(); - cardMenu->addAction(aAttach); - cardMenu->addAction(aUnattach); - cardMenu->addSeparator(); - cardMenu->addAction(aSetPT); - cardMenu->addAction(aSetAnnotation); - cardMenu->addSeparator(); - cardMenu->addAction(aClone); - for (int i = 0; i < 3; ++i) { - QAction *tempAddCounter = new QAction(this); - tempAddCounter->setData(9 + i * 1000); - QAction *tempRemoveCounter = new QAction(this); - tempRemoveCounter->setData(10 + i * 1000); - QAction *tempSetCounter = new QAction(this); - tempSetCounter->setData(11 + i * 1000); - aAddCounter.append(tempAddCounter); - aRemoveCounter.append(tempRemoveCounter); - aSetCounter.append(tempSetCounter); - connect(tempAddCounter, SIGNAL(triggered()), owner, SLOT(actCardCounterTrigger())); - connect(tempRemoveCounter, SIGNAL(triggered()), owner, SLOT(actCardCounterTrigger())); - connect(tempSetCounter, SIGNAL(triggered()), owner, SLOT(actCardCounterTrigger())); - - cardMenu->addSeparator(); - cardMenu->addAction(tempAddCounter); - cardMenu->addAction(tempRemoveCounter); - cardMenu->addAction(tempSetCounter); - } - cardMenu->addSeparator(); - - moveMenu = cardMenu->addMenu(QString()); - moveMenu->addAction(aMoveToTopLibrary); - moveMenu->addAction(aMoveToBottomLibrary); - moveMenu->addAction(aMoveToGraveyard); - moveMenu->addAction(aMoveToExile); - - - retranslateUi(); - } else - cardMenu = 0; + for (int i = 0; i < 3; ++i) { + QAction *tempAddCounter = new QAction(this); + tempAddCounter->setData(9 + i * 1000); + QAction *tempRemoveCounter = new QAction(this); + tempRemoveCounter->setData(10 + i * 1000); + QAction *tempSetCounter = new QAction(this); + tempSetCounter->setData(11 + i * 1000); + aAddCounter.append(tempAddCounter); + aRemoveCounter.append(tempRemoveCounter); + aSetCounter.append(tempSetCounter); + connect(tempAddCounter, SIGNAL(triggered()), this, SLOT(actCardCounterTrigger())); + connect(tempRemoveCounter, SIGNAL(triggered()), this, SLOT(actCardCounterTrigger())); + connect(tempSetCounter, SIGNAL(triggered()), this, SLOT(actCardCounterTrigger())); + } + cardMenu = new QMenu; + moveMenu = new QMenu; + + retranslateUi(); + updateCardMenu(); } CardItem::~CardItem() @@ -110,6 +87,8 @@ CardItem::~CardItem() delete cardMenu; cardMenu = 0; + delete moveMenu; + moveMenu = 0; deleteDragItem(); } @@ -139,37 +118,85 @@ void CardItem::deleteLater() AbstractCardItem::deleteLater(); } +void CardItem::setZone(CardZone *_zone) +{ + zone = _zone; + updateCardMenu(); +} + +void CardItem::updateCardMenu() +{ + cardMenu->clear(); + + if (owner->getLocal()) { + if (zone) { + if (zone->getName() == "table") { + cardMenu->addAction(aTap); + cardMenu->addAction(aUntap); + cardMenu->addAction(aDoesntUntap); + cardMenu->addAction(aFlip); + cardMenu->addSeparator(); + cardMenu->addAction(aAttach); + if (attachedTo) + cardMenu->addAction(aUnattach); + cardMenu->addSeparator(); + cardMenu->addAction(aSetPT); + cardMenu->addAction(aSetAnnotation); + cardMenu->addSeparator(); + cardMenu->addAction(aClone); + + for (int i = 0; i < aAddCounter.size(); ++i) { + cardMenu->addSeparator(); + cardMenu->addAction(aAddCounter[i]); + cardMenu->addAction(aRemoveCounter[i]); + cardMenu->addAction(aSetCounter[i]); + } + cardMenu->addSeparator(); + } else { + cardMenu->addAction(aPlay); + } + } + + moveMenu->clear(); + moveMenu->addAction(aMoveToTopLibrary); + moveMenu->addAction(aMoveToBottomLibrary); + moveMenu->addAction(aMoveToGraveyard); + moveMenu->addAction(aMoveToExile); + cardMenu->addMenu(moveMenu); + } +} + void CardItem::retranslateUi() { - if (owner->getLocal()) { - aTap->setText(tr("&Tap")); - aUntap->setText(tr("&Untap")); - aDoesntUntap->setText(tr("Toggle &normal untapping")); - aFlip->setText(tr("&Flip")); - aClone->setText(tr("&Clone")); - aAttach->setText(tr("&Attach to card...")); - aAttach->setShortcut(tr("Ctrl+A")); - aUnattach->setText(tr("Unattac&h")); - aSetPT->setText(tr("Set &P/T...")); - aSetAnnotation->setText(tr("&Set annotation...")); - QStringList counterColors; - counterColors.append(tr("red")); - counterColors.append(tr("yellow")); - counterColors.append(tr("green")); - for (int i = 0; i < aAddCounter.size(); ++i) - aAddCounter[i]->setText(tr("&Add counter (%1)").arg(counterColors[i])); - for (int i = 0; i < aRemoveCounter.size(); ++i) - aRemoveCounter[i]->setText(tr("&Remove counter (%1)").arg(counterColors[i])); - for (int i = 0; i < aSetCounter.size(); ++i) - aSetCounter[i]->setText(tr("&Set counters (%1)...").arg(counterColors[i])); - aMoveToTopLibrary->setText(tr("&top of library")); - aMoveToBottomLibrary->setText(tr("&bottom of library")); - aMoveToGraveyard->setText(tr("&graveyard")); - aMoveToGraveyard->setShortcut(tr("Ctrl+Del")); - aMoveToExile->setText(tr("&exile")); - - moveMenu->setTitle(tr("&Move to")); - } + aPlay->setText(tr("&Play")); + + aTap->setText(tr("&Tap")); + aUntap->setText(tr("&Untap")); + aDoesntUntap->setText(tr("Toggle &normal untapping")); + aFlip->setText(tr("&Flip")); + aClone->setText(tr("&Clone")); + aAttach->setText(tr("&Attach to card...")); + aAttach->setShortcut(tr("Ctrl+A")); + aUnattach->setText(tr("Unattac&h")); + aSetPT->setText(tr("Set &P/T...")); + aSetAnnotation->setText(tr("&Set annotation...")); + QStringList counterColors; + counterColors.append(tr("red")); + counterColors.append(tr("yellow")); + counterColors.append(tr("green")); + for (int i = 0; i < aAddCounter.size(); ++i) + aAddCounter[i]->setText(tr("&Add counter (%1)").arg(counterColors[i])); + for (int i = 0; i < aRemoveCounter.size(); ++i) + aRemoveCounter[i]->setText(tr("&Remove counter (%1)").arg(counterColors[i])); + for (int i = 0; i < aSetCounter.size(); ++i) + aSetCounter[i]->setText(tr("&Set counters (%1)...").arg(counterColors[i])); + aMoveToTopLibrary->setText(tr("&top of library")); + aMoveToBottomLibrary->setText(tr("&bottom of library")); + aMoveToGraveyard->setText(tr("&graveyard")); + aMoveToGraveyard->setShortcut(tr("Ctrl+Del")); + aMoveToExile->setText(tr("&exile")); + + moveMenu->setTitle(tr("&Move to")); } void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) @@ -263,6 +290,8 @@ void CardItem::setAttachedTo(CardItem *_attachedTo) if (zone) zone->reorganizeCards(); + + updateCardMenu(); } void CardItem::resetState() @@ -376,7 +405,7 @@ void CardItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) setCursor(Qt::OpenHandCursor); } -void CardItem::playCard(QGraphicsSceneMouseEvent *event) +void CardItem::playCard(bool faceDown) { // Do nothing if the card belongs to another player if (!owner->getLocal()) @@ -385,22 +414,19 @@ void CardItem::playCard(QGraphicsSceneMouseEvent *event) TableZone *tz = qobject_cast(zone); if (tz) tz->toggleTapped(); - else { - bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); - bool tapped = info->getCipt(); - - zone->getPlayer()->playCard(this, faceDown, tapped); - } + else + zone->getPlayer()->playCard(this, faceDown, info->getCipt()); } void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::RightButton) { if (cardMenu) - cardMenu->exec(event->screenPos()); + if (!cardMenu->isEmpty()) + cardMenu->exec(event->screenPos()); } else if ((event->button() == Qt::LeftButton) && !settingsCache->getDoubleClickToPlay()) { setCursor(Qt::OpenHandCursor); - playCard(event); + playCard(event->modifiers().testFlag(Qt::ShiftModifier)); } AbstractCardItem::mouseReleaseEvent(event); @@ -409,7 +435,7 @@ void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { if (settingsCache->getDoubleClickToPlay()) - playCard(event); + playCard(event->modifiers().testFlag(Qt::ShiftModifier)); event->accept(); } @@ -423,3 +449,38 @@ QVariant CardItem::itemChange(GraphicsItemChange change, const QVariant &value) } return QGraphicsItem::itemChange(change, value); } + +void CardItem::cardMenuAction() +{ + owner->cardMenuAction(static_cast(sender())); +} + +void CardItem::actAttach() +{ + owner->actAttach(static_cast(sender())); +} + +void CardItem::actUnattach() +{ + owner->actUnattach(static_cast(sender())); +} + +void CardItem::actSetPT() +{ + owner->actSetPT(static_cast(sender())); +} + +void CardItem::actSetAnnotation() +{ + owner->actSetAnnotation(static_cast(sender())); +} + +void CardItem::actCardCounterTrigger() +{ + owner->actCardCounterTrigger(static_cast(sender())); +} + +void CardItem::actPlay() +{ + playCard(false); +} \ No newline at end of file diff --git a/cockatrice/src/carditem.h b/cockatrice/src/carditem.h index 52642abb..844b3198 100644 --- a/cockatrice/src/carditem.h +++ b/cockatrice/src/carditem.h @@ -30,12 +30,21 @@ private: QList attachedCards; QList aAddCounter, aSetCounter, aRemoveCounter; - QAction *aTap, *aUntap, *aDoesntUntap, *aAttach, *aUnattach, *aSetPT, *aSetAnnotation, *aFlip, *aClone, + QAction *aPlay, + *aTap, *aUntap, *aDoesntUntap, *aAttach, *aUnattach, *aSetPT, *aSetAnnotation, *aFlip, *aClone, *aMoveToTopLibrary, *aMoveToBottomLibrary, *aMoveToGraveyard, *aMoveToExile; QMenu *cardMenu, *moveMenu; - void playCard(QGraphicsSceneMouseEvent *event); + void playCard(bool faceDown); void prepareDelete(); +private slots: + void cardMenuAction(); + void actCardCounterTrigger(); + void actAttach(); + void actUnattach(); + void actSetPT(); + void actSetAnnotation(); + void actPlay(); public slots: void deleteLater(); public: @@ -45,13 +54,14 @@ public: ~CardItem(); void retranslateUi(); CardZone *getZone() const { return zone; } - void setZone(CardZone *_zone) { zone = _zone; } + void setZone(CardZone *_zone); QMenu *getCardMenu() const { return cardMenu; } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPoint getGridPoint() const { return gridPoint; } void setGridPoint(const QPoint &_gridPoint) { gridPoint = _gridPoint; } QPoint getGridPos() const { return gridPoint; } Player *getOwner() const { return owner; } + void setOwner(Player *_owner) { owner = _owner; } int getId() const { return id; } void setId(int _id) { id = _id; } bool getAttacking() const { return attacking; } @@ -75,6 +85,7 @@ public: const QList &getAttachedCards() const { return attachedCards; } void resetState(); void processCardInfo(ServerInfo_Card *info); + void updateCardMenu(); CardDragItem *createDragItem(int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown); void deleteDragItem(); diff --git a/cockatrice/src/cardzone.cpp b/cockatrice/src/cardzone.cpp index 6052f34e..bae26ceb 100644 --- a/cockatrice/src/cardzone.cpp +++ b/cockatrice/src/cardzone.cpp @@ -166,7 +166,7 @@ void CardZone::moveAllToZone() // Cards need to be moved in reverse order so that the other // cards' list index doesn't change for (int i = cards.size() - 1; i >= 0; i--) - player->sendGameCommand(new Command_MoveCard(-1, getName(), cards.at(i)->getId(), targetZone, targetX)); + player->sendGameCommand(new Command_MoveCard(-1, getName(), cards.at(i)->getId(), player->getId(), targetZone, targetX)); } QPointF CardZone::closestGridPoint(const QPointF &point) diff --git a/cockatrice/src/cardzone.h b/cockatrice/src/cardzone.h index b8e834f6..e9a04c30 100644 --- a/cockatrice/src/cardzone.h +++ b/cockatrice/src/cardzone.h @@ -12,6 +12,7 @@ class ZoneViewZone; class QMenu; class QAction; class QPainter; +class CardDragItem; class CardZone : public QObject, public AbstractGraphicsItem { Q_OBJECT @@ -34,7 +35,7 @@ public slots: public: enum { Type = typeZone }; int type() const { return Type; } - virtual void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown) = 0; + virtual void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown) = 0; CardZone(Player *_player, const QString &_name, bool _hasCardAttr, bool _isShufflable, bool _contentsKnown, QGraphicsItem *parent = 0, bool isView = false); ~CardZone(); void retranslateUi(); diff --git a/cockatrice/src/handzone.cpp b/cockatrice/src/handzone.cpp index d3d26924..1b78eb8e 100644 --- a/cockatrice/src/handzone.cpp +++ b/cockatrice/src/handzone.cpp @@ -3,6 +3,7 @@ #include "settingscache.h" #include "player.h" #include "protocol_items.h" +#include "carddragitem.h" HandZone::HandZone(Player *_p, bool _contentsKnown, int _zoneHeight, QGraphicsItem *parent) : SelectZone(_p, "hand", false, false, _contentsKnown, parent), zoneHeight(_zoneHeight) @@ -36,9 +37,9 @@ void HandZone::addCardImpl(CardItem *card, int x, int /*y*/) card->update(); } -void HandZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) +void HandZone::handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), cards.size(), -1, false)); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), dragItem->getId(), player->getId(), getName(), cards.size(), -1, false)); } QRectF HandZone::boundingRect() const diff --git a/cockatrice/src/handzone.h b/cockatrice/src/handzone.h index 4a68e59c..cde11e83 100644 --- a/cockatrice/src/handzone.h +++ b/cockatrice/src/handzone.h @@ -14,7 +14,7 @@ public slots: void updateOrientation(); public: HandZone(Player *_p, bool _contentsKnown, int _zoneHeight, QGraphicsItem *parent = 0); - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); + void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void reorganizeCards(); diff --git a/cockatrice/src/pilezone.cpp b/cockatrice/src/pilezone.cpp index e3278998..cf57df21 100644 --- a/cockatrice/src/pilezone.cpp +++ b/cockatrice/src/pilezone.cpp @@ -48,9 +48,9 @@ void PileZone::addCardImpl(CardItem *card, int x, int /*y*/) card->setParentItem(this); } -void PileZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) +void PileZone::handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), 0, 0, false)); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), dragItem->getId(), player->getId(), getName(), 0, 0, false)); } void PileZone::reorganizeCards() diff --git a/cockatrice/src/pilezone.h b/cockatrice/src/pilezone.h index 0b786333..7dbb4699 100644 --- a/cockatrice/src/pilezone.h +++ b/cockatrice/src/pilezone.h @@ -9,7 +9,7 @@ public: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void reorganizeCards(); - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); + void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index e265745f..6e0b19e0 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -562,7 +562,7 @@ void Player::actMoveTopCardsToGrave() if (number > maxCards) number = maxCards; for (int i = 0; i < number; ++i) - commandList.append(new Command_MoveCard(-1, "deck", 0, "grave", 0, 0, false)); + commandList.append(new Command_MoveCard(-1, "deck", 0, getId(), "grave", 0, 0, false)); sendCommandContainer(new CommandContainer(commandList)); } @@ -577,13 +577,13 @@ void Player::actMoveTopCardsToExile() if (number > maxCards) number = maxCards; for (int i = 0; i < number; ++i) - commandList.append(new Command_MoveCard(-1, "deck", 0, "rfg", 0, 0, false)); + commandList.append(new Command_MoveCard(-1, "deck", 0, getId(), "rfg", 0, 0, false)); sendCommandContainer(new CommandContainer(commandList)); } void Player::actMoveTopCardToBottom() { - sendGameCommand(new Command_MoveCard(-1, "deck", 0, "deck", -1, 0, false)); + sendGameCommand(new Command_MoveCard(-1, "deck", 0, getId(), "deck", -1, 0, false)); } void Player::actUntapAll() @@ -790,7 +790,10 @@ void Player::eventStopDumpZone(Event_StopDumpZone *event) void Player::eventMoveCard(Event_MoveCard *event) { CardZone *startZone = zones.value(event->getStartZone(), 0); - CardZone *targetZone = zones.value(event->getTargetZone(), 0); + Player *targetPlayer = static_cast(parent())->getPlayers().value(event->getTargetPlayerId()); + if (!targetPlayer) + return; + CardZone *targetZone = targetPlayer->getZones().value(event->getTargetZone(), 0); if (!startZone || !targetZone) return; @@ -820,6 +823,13 @@ void Player::eventMoveCard(Event_MoveCard *event) if (startZone != targetZone) { card->setBeingPointedAt(false); card->setHovered(false); + + const QList &attachedCards = card->getAttachedCards(); + for (int i = 0; i < attachedCards.size(); ++i) + attachedCards[i]->setParentItem(targetZone); + + if (startZone->getPlayer() != targetZone->getPlayer()) + card->setOwner(targetZone->getPlayer()); } // The log event has to be sent before the card is added to the target zone @@ -1076,10 +1086,10 @@ void Player::playCard(CardItem *c, bool faceDown, bool tapped) { CardInfo *ci = c->getInfo(); if (ci->getTableRow() == 3) - stack->handleDropEvent(c->getId(), c->getZone(), QPoint(), false); + sendGameCommand(new Command_MoveCard(-1, c->getZone()->getName(), c->getId(), getId(), "stack", 0, 0, false)); else { QPoint gridPoint = QPoint(-1, 2 - ci->getTableRow()); - table->handleDropEventByGrid(c->getId(), c->getZone(), gridPoint, faceDown, tapped); + sendGameCommand(new Command_MoveCard(-1, c->getZone()->getName(), c->getId(), getId(), "table", gridPoint.x(), gridPoint.y(), faceDown, tapped)); } } @@ -1246,9 +1256,8 @@ bool Player::clearCardsToDelete() return true; } -void Player::cardMenuAction() +void Player::cardMenuAction(QAction *a) { - QAction *a = static_cast(sender()); QList sel = scene()->selectedItems(); QList commandList; while (!sel.isEmpty()) { @@ -1273,19 +1282,19 @@ void Player::cardMenuAction() break; } case 4: - commandList.append(new Command_CreateToken(-1, card->getZone()->getName(), card->getName(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), -1, card->getGridPoint().y())); + commandList.append(new Command_CreateToken(-1, card->getZone()->getName(), card->getName(), card->getColor(), card->getPT(), card->getAnnotation(), true, -1, card->getGridPoint().y())); break; case 5: - commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), "deck", 0, 0, false)); + commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), getId(), "deck", 0, 0, false)); break; case 6: - commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), "deck", -1, 0, false)); + commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), getId(), "deck", -1, 0, false)); break; case 7: - commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), "grave", 0, 0, false)); + commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), getId(), "grave", 0, 0, false)); break; case 8: - commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), "rfg", 0, 0, false)); + commandList.append(new Command_MoveCard(-1, card->getZone()->getName(), card->getId(), getId(), "rfg", 0, 0, false)); break; default: ; } @@ -1293,7 +1302,7 @@ void Player::cardMenuAction() sendCommandContainer(new CommandContainer(commandList)); } -void Player::actSetPT() +void Player::actSetPT(QAction * /*a*/) { QString oldPT; QListIterator i(scene()->selectedItems()); @@ -1318,7 +1327,7 @@ void Player::actSetPT() } } -void Player::actSetAnnotation() +void Player::actSetAnnotation(QAction * /*a*/) { QString oldAnnotation; QListIterator i(scene()->selectedItems()); @@ -1344,24 +1353,22 @@ void Player::actSetAnnotation() } } -void Player::actAttach() +void Player::actAttach(QAction *a) { - CardItem *card = static_cast(sender()->parent()); + CardItem *card = static_cast(a->parent()); ArrowAttachItem *arrow = new ArrowAttachItem(card); scene()->addItem(arrow); arrow->grabMouse(); } -void Player::actUnattach() +void Player::actUnattach(QAction *a) { - CardItem *card = static_cast(sender()->parent()); + CardItem *card = static_cast(a->parent()); sendGameCommand(new Command_AttachCard(-1, card->getZone()->getName(), card->getId(), -1, QString(), -1)); } -void Player::actCardCounterTrigger() +void Player::actCardCounterTrigger(QAction *a) { - QAction *a = static_cast(sender()); - int counterId = a->data().toInt() / 1000; int action = a->data().toInt() % 1000; switch (action) { diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 03974926..904bc549 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -94,12 +94,12 @@ public slots: void actSayMessage(); - void actAttach(); - void actUnattach(); - void actSetPT(); - void actSetAnnotation(); - void cardMenuAction(); - void actCardCounterTrigger(); + void actAttach(QAction *action); + void actUnattach(QAction *action); + void actSetPT(QAction *action); + void actSetAnnotation(QAction *action); + void cardMenuAction(QAction *action); + void actCardCounterTrigger(QAction *action); private slots: void addPlayer(Player *player); diff --git a/cockatrice/src/stackzone.cpp b/cockatrice/src/stackzone.cpp index b21bd77f..fa368d8e 100644 --- a/cockatrice/src/stackzone.cpp +++ b/cockatrice/src/stackzone.cpp @@ -5,6 +5,7 @@ #include "settingscache.h" #include "player.h" #include "protocol_items.h" +#include "carddragitem.h" StackZone::StackZone(Player *_p, int _zoneHeight, QGraphicsItem *parent) : SelectZone(_p, "stack", false, false, true, parent), zoneHeight(_zoneHeight) @@ -51,11 +52,11 @@ void StackZone::paint(QPainter *painter, const QStyleOptionGraphicsItem */*optio painter->fillRect(boundingRect(), QBrush(bgPixmap)); } -void StackZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) +void StackZone::handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { if (startZone == this) return; - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), 0, 0, false)); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), dragItem->getId(), player->getId(), getName(), 0, 0, false)); } void StackZone::reorganizeCards() diff --git a/cockatrice/src/stackzone.h b/cockatrice/src/stackzone.h index 40d67541..5bb11ce6 100644 --- a/cockatrice/src/stackzone.h +++ b/cockatrice/src/stackzone.h @@ -12,7 +12,7 @@ private slots: void updateBgPixmap(); public: StackZone(Player *_p, int _zoneHeight, QGraphicsItem *parent = 0); - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); + void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void reorganizeCards(); diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 5f008262..2551c8cb 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -7,6 +7,7 @@ #include "protocol_items.h" #include "settingscache.h" #include "arrowitem.h" +#include "carddragitem.h" TableZone::TableZone(Player *_p, QGraphicsItem *parent) : SelectZone(_p, "table", true, false, true, parent), active(false) @@ -84,14 +85,14 @@ void TableZone::addCardImpl(CardItem *card, int _x, int _y) card->update(); } -void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown) +void TableZone::handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown) { - handleDropEventByGrid(cardId, startZone, mapToGrid(dropPoint), faceDown); + handleDropEventByGrid(dragItem, startZone, mapToGrid(dropPoint), faceDown); } -void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown, bool tapped) +void TableZone::handleDropEventByGrid(CardDragItem *dragItem, CardZone *startZone, const QPoint &gridPoint, bool faceDown, bool tapped) { - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), gridPoint.x(), gridPoint.y(), faceDown, tapped)); + static_cast(dragItem->getItem())->getZone()->getPlayer()->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), dragItem->getId(), player->getId(), getName(), gridPoint.x(), gridPoint.y(), faceDown, tapped)); } void TableZone::reorganizeCards() @@ -223,18 +224,21 @@ CardItem *TableZone::getCardFromCoords(const QPointF &point) const return getCardFromGrid(gridPoint); } -QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const +QPointF TableZone::mapFromGrid(QPoint gridPoint) const { qreal x, y; x = marginX + (gridPoint.x() % 3) * CARD_WIDTH / 3.0; for (int i = 0; i < gridPoint.x() / 3; ++i) x += gridPointWidth.value(gridPoint.y() * 1000 + i, CARD_WIDTH) + paddingX; - y = boxLineWidth + gridPoint.y() * (CARD_HEIGHT + paddingY + 20) + (gridPoint.x() % 3) * 10; + if (isInverted()) + gridPoint.setY(2 - gridPoint.y()); + y = boxLineWidth + gridPoint.y() * (CARD_HEIGHT + paddingY + 20) + (gridPoint.x() % 3) * 10; +/* if (isInverted()) y = height - CARD_HEIGHT - y; - +*/ return QPointF(x, y); } @@ -242,9 +246,9 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const { qreal x = mapPoint.x() - marginX; qreal y = mapPoint.y(); - if (isInverted()) +/* if (isInverted()) y = height - y; - y -= boxLineWidth; +*/ y -= boxLineWidth; if (x < 0) x = 0; @@ -256,6 +260,8 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const y = height - CARD_HEIGHT; int resultY = round(y / (CARD_HEIGHT + paddingY + 20)); + if (isInverted()) + resultY = 2 - resultY; int baseX = -1; qreal oldTempX = 0, tempX = 0; diff --git a/cockatrice/src/tablezone.h b/cockatrice/src/tablezone.h index 4eb48ceb..9761f8ea 100644 --- a/cockatrice/src/tablezone.h +++ b/cockatrice/src/tablezone.h @@ -29,11 +29,11 @@ public: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void toggleTapped(); - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown = false); - void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown = false, bool tapped = false); + void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown = false); + void handleDropEventByGrid(CardDragItem *dragItem, CardZone *startZone, const QPoint &gridPoint, bool faceDown = false, bool tapped = false); CardItem *getCardFromGrid(const QPoint &gridPoint) const; CardItem *getCardFromCoords(const QPointF &point) const; - QPointF mapFromGrid(const QPoint &gridPoint) const; + QPointF mapFromGrid(QPoint gridPoint) const; QPoint mapToGrid(const QPointF &mapPoint) const; QPointF closestGridPoint(const QPointF &point); CardItem *takeCard(int position, int cardId, bool canResize = true); diff --git a/cockatrice/src/window_deckeditor.cpp b/cockatrice/src/window_deckeditor.cpp index 3b091026..e9f2f44e 100644 --- a/cockatrice/src/window_deckeditor.cpp +++ b/cockatrice/src/window_deckeditor.cpp @@ -308,8 +308,9 @@ bool WndDeckEditor::actSaveDeck() else if (deckModel->getDeckList()->saveToFile(lastFileName, lastFileFormat)) { setWindowModified(false); return true; - } else - return false; + } + QMessageBox::critical(this, tr("Error"), tr("The deck could not be saved.\nPlease check that the directory is writable and try again.")); + return false; } bool WndDeckEditor::actSaveDeckAs() @@ -332,6 +333,7 @@ bool WndDeckEditor::actSaveDeckAs() setWindowModified(false); return true; } + QMessageBox::critical(this, tr("Error"), tr("The deck could not be saved.\nPlease check that the directory is writable and try again.")); return false; } diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index 18166308..bcf59775 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -3,6 +3,7 @@ #include "zoneviewzone.h" #include "player.h" #include "protocol_items.h" +#include "carddragitem.h" ZoneViewZone::ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards, QGraphicsItem *parent) : SelectZone(_p, _origZone->getName(), false, false, true, parent, true), bRect(QRectF()), minRows(0), numberCards(_numberCards), origZone(_origZone), sortByName(false), sortByType(false) @@ -122,9 +123,9 @@ void ZoneViewZone::addCardImpl(CardItem *card, int x, int /*y*/) card->update(); } -void ZoneViewZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) +void ZoneViewZone::handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), 0, 0, false)); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), dragItem->getId(), player->getId(), getName(), 0, 0, false)); } void ZoneViewZone::removeCard(int position) diff --git a/cockatrice/src/zoneviewzone.h b/cockatrice/src/zoneviewzone.h index 52a9aac7..d2208768 100644 --- a/cockatrice/src/zoneviewzone.h +++ b/cockatrice/src/zoneviewzone.h @@ -13,7 +13,7 @@ class ZoneViewZone : public SelectZone, public QGraphicsLayoutItem { private: QRectF bRect, optimumRect; int minRows, numberCards; - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); + void handleDropEvent(CardDragItem *dragItem, CardZone *startZone, const QPoint &dropPoint, bool faceDown); CardZone *origZone; bool sortByName, sortByType; public: diff --git a/common/protocol.cpp b/common/protocol.cpp index ccfc081a..4ac9677f 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -14,7 +14,7 @@ void ProtocolItem::initializeHash() initializeHashAuto(); registerSerializableItem("move_card_to_zone", MoveCardToZone::newItem); - registerSerializableItem("chat_channel", ServerInfo_ChatChannel::newItem); + registerSerializableItem("room", ServerInfo_Room::newItem); registerSerializableItem("user", ServerInfo_User::newItem); registerSerializableItem("game", ServerInfo_Game::newItem); registerSerializableItem("card_counter", ServerInfo_CardCounter::newItem); @@ -44,9 +44,10 @@ void ProtocolItem::initializeHash() registerSerializableItem("respdeck_upload", Response_DeckUpload::newItem); registerSerializableItem("respdump_zone", Response_DumpZone::newItem); - registerSerializableItem("generic_eventlist_games", Event_ListGames::newItem); + registerSerializableItem("room_eventlist_games", Event_ListGames::newItem); + registerSerializableItem("room_eventjoin_room", Event_JoinRoom::newItem); registerSerializableItem("generic_eventuser_joined", Event_UserJoined::newItem); - registerSerializableItem("generic_eventlist_chat_channels", Event_ListChatChannels::newItem); + registerSerializableItem("generic_eventlist_rooms", Event_ListRooms::newItem); registerSerializableItem("game_eventjoin", Event_Join::newItem); registerSerializableItem("game_eventgame_state_changed", Event_GameStateChanged::newItem); registerSerializableItem("game_eventplayer_properties_changed", Event_PlayerPropertiesChanged::newItem); @@ -55,8 +56,6 @@ void ProtocolItem::initializeHash() registerSerializableItem("game_eventdraw_cards", Event_DrawCards::newItem); registerSerializableItem("game_eventreveal_cards", Event_RevealCards::newItem); registerSerializableItem("game_eventping", Event_Ping::newItem); - registerSerializableItem("chat_eventchat_list_players", Event_ChatListPlayers::newItem); - registerSerializableItem("chat_eventchat_join_channel", Event_ChatJoinChannel::newItem); } TopLevelProtocolItem::TopLevelProtocolItem() @@ -284,36 +283,29 @@ GameEventContext::GameEventContext(const QString &_contextName) { } -ChatEvent::ChatEvent(const QString &_eventName, const QString &_channel) - : ProtocolItem("chat_event", _eventName) +RoomEvent::RoomEvent(const QString &_eventName, int _roomId) + : ProtocolItem("room_event", _eventName) { - insertItem(new SerializableItem_String("channel", _channel)); + insertItem(new SerializableItem_Int("room_id", _roomId)); } -Event_ListChatChannels::Event_ListChatChannels(const QList &_channelList) - : GenericEvent("list_chat_channels") +Event_ListRooms::Event_ListRooms(const QList &_roomList) + : GenericEvent("list_rooms") { - for (int i = 0; i < _channelList.size(); ++i) - itemList.append(_channelList[i]); + for (int i = 0; i < _roomList.size(); ++i) + itemList.append(_roomList[i]); } -Event_ChatListPlayers::Event_ChatListPlayers(const QString &_channel, const QList &_playerList) - : ChatEvent("chat_list_players", _channel) -{ - for (int i = 0; i < _playerList.size(); ++i) - itemList.append(_playerList[i]); -} - -Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, ServerInfo_User *_info) - : ChatEvent("chat_join_channel", _channel) +Event_JoinRoom::Event_JoinRoom(int _roomId, ServerInfo_User *_info) + : RoomEvent("join_room", _roomId) { if (!_info) _info = new ServerInfo_User; insertItem(_info); } -Event_ListGames::Event_ListGames(const QList &_gameList) - : GenericEvent("list_games") +Event_ListGames::Event_ListGames(int _roomId, const QList &_gameList) + : RoomEvent("list_games", _roomId) { for (int i = 0; i < _gameList.size(); ++i) itemList.append(_gameList[i]); diff --git a/common/protocol.h b/common/protocol.h index e42d5456..387057a0 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -25,9 +25,8 @@ enum ItemId { ItemId_Command_DeckUpload = ItemId_Other + 100, ItemId_Command_DeckSelect = ItemId_Other + 101, ItemId_Command_SetSideboardPlan = ItemId_Other + 102, - ItemId_Event_ListChatChannels = ItemId_Other + 200, - ItemId_Event_ChatListPlayers = ItemId_Other + 201, - ItemId_Event_ChatJoinChannel = ItemId_Other + 202, + ItemId_Event_ListRooms = ItemId_Other + 200, + ItemId_Event_JoinRoom = ItemId_Other + 201, ItemId_Event_ListGames = ItemId_Other + 203, ItemId_Event_UserJoined = ItemId_Other + 204, ItemId_Event_GameStateChanged = ItemId_Other + 205, @@ -136,15 +135,15 @@ public: int getPrivatePlayerId() const { return privatePlayerId; } }; -class ChatCommand : public Command { +class RoomCommand : public Command { Q_OBJECT public: - ChatCommand(const QString &_cmdName, const QString &_channel) + RoomCommand(const QString &_cmdName, int _roomId) : Command(_cmdName) { - insertItem(new SerializableItem_String("channel", _channel)); + insertItem(new SerializableItem_Int("room_id", _roomId)); } - QString getChannel() const { return static_cast(itemMap.value("channel"))->getData(); } + int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); } }; class GameCommand : public Command { @@ -304,44 +303,35 @@ public: void setGameId(int _gameId) { static_cast(itemMap.value("game_id"))->setData(_gameId); } }; -class ChatEvent : public ProtocolItem { +class RoomEvent : public ProtocolItem { Q_OBJECT public: - ChatEvent(const QString &_eventName, const QString &_channel); - QString getChannel() const { return static_cast(itemMap.value("channel"))->getData(); } + RoomEvent(const QString &_eventName, int _roomId); + int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); } }; -class Event_ListChatChannels : public GenericEvent { +class Event_ListRooms : public GenericEvent { Q_OBJECT public: - Event_ListChatChannels(const QList &_channelList = QList()); - int getItemId() const { return ItemId_Event_ListChatChannels; } - static SerializableItem *newItem() { return new Event_ListChatChannels; } - QList getChannelList() const { return typecastItemList(); } + Event_ListRooms(const QList &_roomList = QList()); + int getItemId() const { return ItemId_Event_ListRooms; } + static SerializableItem *newItem() { return new Event_ListRooms; } + QList getRoomList() const { return typecastItemList(); } }; -class Event_ChatListPlayers : public ChatEvent { +class Event_JoinRoom : public RoomEvent { Q_OBJECT public: - Event_ChatListPlayers(const QString &_channel = QString(), const QList &_playerList = QList()); - int getItemId() const { return ItemId_Event_ChatListPlayers; } - static SerializableItem *newItem() { return new Event_ChatListPlayers; } - QList getPlayerList() const { return typecastItemList(); } -}; - -class Event_ChatJoinChannel : public ChatEvent { - Q_OBJECT -public: - Event_ChatJoinChannel(const QString &_channel = QString(), ServerInfo_User *_info = 0); - int getItemId() const { return ItemId_Event_ChatJoinChannel; } - static SerializableItem *newItem() { return new Event_ChatJoinChannel; } + Event_JoinRoom(int _roomId = -1, ServerInfo_User *_info = 0); + int getItemId() const { return ItemId_Event_JoinRoom; } + static SerializableItem *newItem() { return new Event_JoinRoom; } ServerInfo_User *getUserInfo() const { return static_cast(itemMap.value("user")); } }; -class Event_ListGames : public GenericEvent { +class Event_ListGames : public RoomEvent { Q_OBJECT public: - Event_ListGames(const QList &_gameList = QList()); + Event_ListGames(int _roomId = -1, const QList &_gameList = QList()); int getItemId() const { return ItemId_Event_ListGames; } static SerializableItem *newItem() { return new Event_ListGames; } QList getGameList() const { return typecastItemList(); } diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp index afbf726a..f3ee0b73 100644 --- a/common/protocol_datastructures.cpp +++ b/common/protocol_datastructures.cpp @@ -3,11 +3,13 @@ #include #include -ServerInfo_ChatChannel::ServerInfo_ChatChannel(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin) - : SerializableItem_Map("chat_channel") +ServerInfo_Room::ServerInfo_Room(int _roomId, const QString &_name, const QString &_description, int _gameCount, int _playerCount, bool _autoJoin) + : SerializableItem_Map("room") { + insertItem(new SerializableItem_Int("room_id", _roomId)); insertItem(new SerializableItem_String("name", _name)); insertItem(new SerializableItem_String("description", _description)); + insertItem(new SerializableItem_Int("game_count", _gameCount)); insertItem(new SerializableItem_Int("player_count", _playerCount)); insertItem(new SerializableItem_Bool("auto_join", _autoJoin)); } diff --git a/common/protocol_datastructures.h b/common/protocol_datastructures.h index ff16b667..614dcaf8 100644 --- a/common/protocol_datastructures.h +++ b/common/protocol_datastructures.h @@ -20,12 +20,14 @@ enum ResponseCode { RespNothing, RespOk, RespInvalidCommand, RespInvalidData, Re // list index, whereas cards in any other zone are referenced by their ids. enum ZoneType { PrivateZone, PublicZone, HiddenZone }; -class ServerInfo_ChatChannel : public SerializableItem_Map { +class ServerInfo_Room : public SerializableItem_Map { public: - ServerInfo_ChatChannel(const QString &_name = QString(), const QString &_description = QString(), int _playerCount = -1, bool _autoJoin = false); - static SerializableItem *newItem() { return new ServerInfo_ChatChannel; } + ServerInfo_Room(int _id = -1, const QString &_name = QString(), const QString &_description = QString(), int _gameCount = -1, int _playerCount = -1, bool _autoJoin = false); + static SerializableItem *newItem() { return new ServerInfo_Room; } + int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); } QString getName() const { return static_cast(itemMap.value("name"))->getData(); } QString getDescription() const { return static_cast(itemMap.value("description"))->getData(); } + int getGameCount() const { return static_cast(itemMap.value("game_count"))->getData(); } int getPlayerCount() const { return static_cast(itemMap.value("player_count"))->getData(); } bool getAutoJoin() const { return static_cast(itemMap.value("auto_join"))->getData(); } }; diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h index 8404364b..428a3f17 100644 --- a/common/protocol_item_ids.h +++ b/common/protocol_item_ids.h @@ -2,73 +2,72 @@ enum AutoItemId { ItemId_Command_Ping = 1001, ItemId_Command_Login = 1002, ItemId_Command_Message = 1003, -ItemId_Command_GetUserInfo = 1004, -ItemId_Command_DeckList = 1005, -ItemId_Command_DeckNewDir = 1006, -ItemId_Command_DeckDelDir = 1007, -ItemId_Command_DeckDel = 1008, -ItemId_Command_DeckDownload = 1009, -ItemId_Command_ListChatChannels = 1010, -ItemId_Command_ChatJoinChannel = 1011, -ItemId_Command_ChatLeaveChannel = 1012, -ItemId_Command_ChatSay = 1013, -ItemId_Command_ListGames = 1014, -ItemId_Command_ListUsers = 1015, -ItemId_Command_CreateGame = 1016, -ItemId_Command_JoinGame = 1017, -ItemId_Command_LeaveGame = 1018, -ItemId_Command_Say = 1019, -ItemId_Command_Shuffle = 1020, -ItemId_Command_Mulligan = 1021, -ItemId_Command_RollDie = 1022, -ItemId_Command_DrawCards = 1023, -ItemId_Command_MoveCard = 1024, -ItemId_Command_FlipCard = 1025, -ItemId_Command_AttachCard = 1026, -ItemId_Command_CreateToken = 1027, -ItemId_Command_CreateArrow = 1028, -ItemId_Command_DeleteArrow = 1029, -ItemId_Command_SetCardAttr = 1030, -ItemId_Command_SetCardCounter = 1031, -ItemId_Command_IncCardCounter = 1032, -ItemId_Command_ReadyStart = 1033, -ItemId_Command_Concede = 1034, -ItemId_Command_IncCounter = 1035, -ItemId_Command_CreateCounter = 1036, -ItemId_Command_SetCounter = 1037, -ItemId_Command_DelCounter = 1038, -ItemId_Command_NextTurn = 1039, -ItemId_Command_SetActivePhase = 1040, -ItemId_Command_DumpZone = 1041, -ItemId_Command_StopDumpZone = 1042, -ItemId_Command_RevealCards = 1043, -ItemId_Event_Say = 1044, -ItemId_Event_Leave = 1045, -ItemId_Event_GameClosed = 1046, -ItemId_Event_Shuffle = 1047, -ItemId_Event_RollDie = 1048, -ItemId_Event_MoveCard = 1049, -ItemId_Event_FlipCard = 1050, -ItemId_Event_DestroyCard = 1051, -ItemId_Event_AttachCard = 1052, -ItemId_Event_CreateToken = 1053, -ItemId_Event_DeleteArrow = 1054, -ItemId_Event_SetCardAttr = 1055, -ItemId_Event_SetCardCounter = 1056, -ItemId_Event_SetCounter = 1057, -ItemId_Event_DelCounter = 1058, -ItemId_Event_SetActivePlayer = 1059, -ItemId_Event_SetActivePhase = 1060, -ItemId_Event_DumpZone = 1061, -ItemId_Event_StopDumpZone = 1062, -ItemId_Event_ServerMessage = 1063, -ItemId_Event_Message = 1064, -ItemId_Event_GameJoined = 1065, -ItemId_Event_UserLeft = 1066, -ItemId_Event_ChatLeaveChannel = 1067, -ItemId_Event_ChatSay = 1068, -ItemId_Context_ReadyStart = 1069, -ItemId_Context_Concede = 1070, -ItemId_Context_DeckSelect = 1071, -ItemId_Other = 1072 +ItemId_Command_ListUsers = 1004, +ItemId_Command_GetUserInfo = 1005, +ItemId_Command_DeckList = 1006, +ItemId_Command_DeckNewDir = 1007, +ItemId_Command_DeckDelDir = 1008, +ItemId_Command_DeckDel = 1009, +ItemId_Command_DeckDownload = 1010, +ItemId_Command_ListRooms = 1011, +ItemId_Command_JoinRoom = 1012, +ItemId_Command_LeaveRoom = 1013, +ItemId_Command_RoomSay = 1014, +ItemId_Command_CreateGame = 1015, +ItemId_Command_JoinGame = 1016, +ItemId_Command_LeaveGame = 1017, +ItemId_Command_Say = 1018, +ItemId_Command_Shuffle = 1019, +ItemId_Command_Mulligan = 1020, +ItemId_Command_RollDie = 1021, +ItemId_Command_DrawCards = 1022, +ItemId_Command_MoveCard = 1023, +ItemId_Command_FlipCard = 1024, +ItemId_Command_AttachCard = 1025, +ItemId_Command_CreateToken = 1026, +ItemId_Command_CreateArrow = 1027, +ItemId_Command_DeleteArrow = 1028, +ItemId_Command_SetCardAttr = 1029, +ItemId_Command_SetCardCounter = 1030, +ItemId_Command_IncCardCounter = 1031, +ItemId_Command_ReadyStart = 1032, +ItemId_Command_Concede = 1033, +ItemId_Command_IncCounter = 1034, +ItemId_Command_CreateCounter = 1035, +ItemId_Command_SetCounter = 1036, +ItemId_Command_DelCounter = 1037, +ItemId_Command_NextTurn = 1038, +ItemId_Command_SetActivePhase = 1039, +ItemId_Command_DumpZone = 1040, +ItemId_Command_StopDumpZone = 1041, +ItemId_Command_RevealCards = 1042, +ItemId_Event_Say = 1043, +ItemId_Event_Leave = 1044, +ItemId_Event_GameClosed = 1045, +ItemId_Event_Shuffle = 1046, +ItemId_Event_RollDie = 1047, +ItemId_Event_MoveCard = 1048, +ItemId_Event_FlipCard = 1049, +ItemId_Event_DestroyCard = 1050, +ItemId_Event_AttachCard = 1051, +ItemId_Event_CreateToken = 1052, +ItemId_Event_DeleteArrow = 1053, +ItemId_Event_SetCardAttr = 1054, +ItemId_Event_SetCardCounter = 1055, +ItemId_Event_SetCounter = 1056, +ItemId_Event_DelCounter = 1057, +ItemId_Event_SetActivePlayer = 1058, +ItemId_Event_SetActivePhase = 1059, +ItemId_Event_DumpZone = 1060, +ItemId_Event_StopDumpZone = 1061, +ItemId_Event_ServerMessage = 1062, +ItemId_Event_Message = 1063, +ItemId_Event_GameJoined = 1064, +ItemId_Event_UserLeft = 1065, +ItemId_Event_LeaveRoom = 1066, +ItemId_Event_RoomSay = 1067, +ItemId_Context_ReadyStart = 1068, +ItemId_Context_Concede = 1069, +ItemId_Context_DeckSelect = 1070, +ItemId_Other = 1071 }; diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 57a32f63..652f1a4c 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -17,6 +17,10 @@ Command_Message::Command_Message(const QString &_userName, const QString &_text) insertItem(new SerializableItem_String("user_name", _userName)); insertItem(new SerializableItem_String("text", _text)); } +Command_ListUsers::Command_ListUsers() + : Command("list_users") +{ +} Command_GetUserInfo::Command_GetUserInfo(const QString &_userName) : Command("get_user_info") { @@ -47,34 +51,26 @@ Command_DeckDownload::Command_DeckDownload(int _deckId) { insertItem(new SerializableItem_Int("deck_id", _deckId)); } -Command_ListChatChannels::Command_ListChatChannels() - : Command("list_chat_channels") +Command_ListRooms::Command_ListRooms() + : Command("list_rooms") { } -Command_ChatJoinChannel::Command_ChatJoinChannel(const QString &_channel) - : Command("chat_join_channel") +Command_JoinRoom::Command_JoinRoom(int _roomId) + : Command("join_room") { - insertItem(new SerializableItem_String("channel", _channel)); + insertItem(new SerializableItem_Int("room_id", _roomId)); } -Command_ChatLeaveChannel::Command_ChatLeaveChannel(const QString &_channel) - : ChatCommand("chat_leave_channel", _channel) +Command_LeaveRoom::Command_LeaveRoom(int _roomId) + : RoomCommand("leave_room", _roomId) { } -Command_ChatSay::Command_ChatSay(const QString &_channel, const QString &_message) - : ChatCommand("chat_say", _channel) +Command_RoomSay::Command_RoomSay(int _roomId, const QString &_message) + : RoomCommand("room_say", _roomId) { insertItem(new SerializableItem_String("message", _message)); } -Command_ListGames::Command_ListGames() - : Command("list_games") -{ -} -Command_ListUsers::Command_ListUsers() - : Command("list_users") -{ -} -Command_CreateGame::Command_CreateGame(const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything) - : Command("create_game") +Command_CreateGame::Command_CreateGame(int _roomId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything) + : RoomCommand("create_game", _roomId) { insertItem(new SerializableItem_String("description", _description)); insertItem(new SerializableItem_String("password", _password)); @@ -84,8 +80,8 @@ Command_CreateGame::Command_CreateGame(const QString &_description, const QStrin insertItem(new SerializableItem_Bool("spectators_can_talk", _spectatorsCanTalk)); insertItem(new SerializableItem_Bool("spectators_see_everything", _spectatorsSeeEverything)); } -Command_JoinGame::Command_JoinGame(int _gameId, const QString &_password, bool _spectator) - : Command("join_game") +Command_JoinGame::Command_JoinGame(int _roomId, int _gameId, const QString &_password, bool _spectator) + : RoomCommand("join_game", _roomId) { insertItem(new SerializableItem_Int("game_id", _gameId)); insertItem(new SerializableItem_String("password", _password)); @@ -118,11 +114,12 @@ Command_DrawCards::Command_DrawCards(int _gameId, int _number) { insertItem(new SerializableItem_Int("number", _number)); } -Command_MoveCard::Command_MoveCard(int _gameId, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped) +Command_MoveCard::Command_MoveCard(int _gameId, const QString &_startZone, int _cardId, int _targetPlayerId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped) : GameCommand("move_card", _gameId) { insertItem(new SerializableItem_String("start_zone", _startZone)); insertItem(new SerializableItem_Int("card_id", _cardId)); + insertItem(new SerializableItem_Int("target_player_id", _targetPlayerId)); insertItem(new SerializableItem_String("target_zone", _targetZone)); insertItem(new SerializableItem_Int("x", _x)); insertItem(new SerializableItem_Int("y", _y)); @@ -283,13 +280,14 @@ Event_RollDie::Event_RollDie(int _playerId, int _sides, int _value) insertItem(new SerializableItem_Int("sides", _sides)); insertItem(new SerializableItem_Int("value", _value)); } -Event_MoveCard::Event_MoveCard(int _playerId, int _cardId, const QString &_cardName, const QString &_startZone, int _position, const QString &_targetZone, int _x, int _y, int _newCardId, bool _faceDown) +Event_MoveCard::Event_MoveCard(int _playerId, int _cardId, const QString &_cardName, const QString &_startZone, int _position, int _targetPlayerId, const QString &_targetZone, int _x, int _y, int _newCardId, bool _faceDown) : GameEvent("move_card", _playerId) { insertItem(new SerializableItem_Int("card_id", _cardId)); insertItem(new SerializableItem_String("card_name", _cardName)); insertItem(new SerializableItem_String("start_zone", _startZone)); insertItem(new SerializableItem_Int("position", _position)); + insertItem(new SerializableItem_Int("target_player_id", _targetPlayerId)); insertItem(new SerializableItem_String("target_zone", _targetZone)); insertItem(new SerializableItem_Int("x", _x)); insertItem(new SerializableItem_Int("y", _y)); @@ -415,13 +413,13 @@ Event_UserLeft::Event_UserLeft(const QString &_userName) { insertItem(new SerializableItem_String("user_name", _userName)); } -Event_ChatLeaveChannel::Event_ChatLeaveChannel(const QString &_channel, const QString &_playerName) - : ChatEvent("chat_leave_channel", _channel) +Event_LeaveRoom::Event_LeaveRoom(int _roomId, const QString &_playerName) + : RoomEvent("leave_room", _roomId) { insertItem(new SerializableItem_String("player_name", _playerName)); } -Event_ChatSay::Event_ChatSay(const QString &_channel, const QString &_playerName, const QString &_message) - : ChatEvent("chat_say", _channel) +Event_RoomSay::Event_RoomSay(int _roomId, const QString &_playerName, const QString &_message) + : RoomEvent("room_say", _roomId) { insertItem(new SerializableItem_String("player_name", _playerName)); insertItem(new SerializableItem_String("message", _message)); @@ -444,18 +442,17 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("cmdping", Command_Ping::newItem); itemNameHash.insert("cmdlogin", Command_Login::newItem); itemNameHash.insert("cmdmessage", Command_Message::newItem); + itemNameHash.insert("cmdlist_users", Command_ListUsers::newItem); itemNameHash.insert("cmdget_user_info", Command_GetUserInfo::newItem); itemNameHash.insert("cmddeck_list", Command_DeckList::newItem); itemNameHash.insert("cmddeck_new_dir", Command_DeckNewDir::newItem); itemNameHash.insert("cmddeck_del_dir", Command_DeckDelDir::newItem); itemNameHash.insert("cmddeck_del", Command_DeckDel::newItem); itemNameHash.insert("cmddeck_download", Command_DeckDownload::newItem); - itemNameHash.insert("cmdlist_chat_channels", Command_ListChatChannels::newItem); - itemNameHash.insert("cmdchat_join_channel", Command_ChatJoinChannel::newItem); - itemNameHash.insert("cmdchat_leave_channel", Command_ChatLeaveChannel::newItem); - itemNameHash.insert("cmdchat_say", Command_ChatSay::newItem); - itemNameHash.insert("cmdlist_games", Command_ListGames::newItem); - itemNameHash.insert("cmdlist_users", Command_ListUsers::newItem); + itemNameHash.insert("cmdlist_rooms", Command_ListRooms::newItem); + itemNameHash.insert("cmdjoin_room", Command_JoinRoom::newItem); + itemNameHash.insert("cmdleave_room", Command_LeaveRoom::newItem); + itemNameHash.insert("cmdroom_say", Command_RoomSay::newItem); itemNameHash.insert("cmdcreate_game", Command_CreateGame::newItem); itemNameHash.insert("cmdjoin_game", Command_JoinGame::newItem); itemNameHash.insert("cmdleave_game", Command_LeaveGame::newItem); @@ -507,8 +504,8 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("generic_eventmessage", Event_Message::newItem); itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem); itemNameHash.insert("generic_eventuser_left", Event_UserLeft::newItem); - itemNameHash.insert("chat_eventchat_leave_channel", Event_ChatLeaveChannel::newItem); - itemNameHash.insert("chat_eventchat_say", Event_ChatSay::newItem); + itemNameHash.insert("room_eventleave_room", Event_LeaveRoom::newItem); + itemNameHash.insert("room_eventroom_say", Event_RoomSay::newItem); itemNameHash.insert("game_event_contextready_start", Context_ReadyStart::newItem); itemNameHash.insert("game_event_contextconcede", Context_Concede::newItem); itemNameHash.insert("game_event_contextdeck_select", Context_DeckSelect::newItem); diff --git a/common/protocol_items.dat b/common/protocol_items.dat index bc9a28e5..2819ddec 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -1,27 +1,26 @@ 0:ping 0:login:s,username:s,password 0:message:s,user_name:s,text +0:list_users 0:get_user_info:s,user_name 0:deck_list 0:deck_new_dir:s,path:s,dir_name 0:deck_del_dir:s,path 0:deck_del:i,deck_id 0:deck_download:i,deck_id -0:list_chat_channels -0:chat_join_channel:s,channel -1:chat_leave_channel -1:chat_say:s,message -0:list_games -0:list_users -0:create_game:s,description:s,password:i,max_players:b,spectators_allowed:b,spectators_need_password:b,spectators_can_talk:b,spectators_see_everything -0:join_game:i,game_id:s,password:b,spectator +0:list_rooms +0:join_room:i,room_id +1:leave_room +1:room_say:s,message +1:create_game:s,description:s,password:i,max_players:b,spectators_allowed:b,spectators_need_password:b,spectators_can_talk:b,spectators_see_everything +1:join_game:i,game_id:s,password:b,spectator 2:leave_game 2:say:s,message 2:shuffle 2:mulligan 2:roll_die:i,sides 2:draw_cards:i,number -2:move_card:s,start_zone:i,card_id:s,target_zone:i,x:i,y:b,face_down:b,tapped +2:move_card:s,start_zone:i,card_id:i,target_player_id:s,target_zone:i,x:i,y:b,face_down:b,tapped 2:flip_card:s,zone:i,card_id:b,face_down 2:attach_card:s,start_zone:i,card_id:i,target_player_id:s,target_zone:i,target_card_id 2:create_token:s,zone:s,card_name:s,color:s,pt:s,annotation:b,destroy:i,x:i,y @@ -46,7 +45,7 @@ 3:game_closed 3:shuffle 3:roll_die:i,sides:i,value -3:move_card:i,card_id:s,card_name:s,start_zone:i,position:s,target_zone:i,x:i,y:i,new_card_id:b,face_down +3:move_card:i,card_id:s,card_name:s,start_zone:i,position:i,target_player_id:s,target_zone:i,x:i,y:i,new_card_id:b,face_down 3:flip_card:s,zone:i,card_id:s,card_name:b,face_down 3:destroy_card:s,zone:i,card_id 3:attach_card:s,start_zone:i,card_id:i,target_player_id:s,target_zone:i,target_card_id @@ -64,8 +63,8 @@ 4:message:s,sender_name:s,receiver_name:s,text 4:game_joined:i,game_id:s,game_description:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming 4:user_left:s,user_name -5:chat_leave_channel:s,player_name -5:chat_say:s,player_name:s,message +5:leave_room:s,player_name +5:room_say:s,player_name:s,message 6:ready_start 6:concede 6:deck_select:i,deck_id diff --git a/common/protocol_items.h b/common/protocol_items.h index 69664127..8a32ace1 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -28,6 +28,13 @@ public: static SerializableItem *newItem() { return new Command_Message; } int getItemId() const { return ItemId_Command_Message; } }; +class Command_ListUsers : public Command { + Q_OBJECT +public: + Command_ListUsers(); + static SerializableItem *newItem() { return new Command_ListUsers; } + int getItemId() const { return ItemId_Command_ListUsers; } +}; class Command_GetUserInfo : public Command { Q_OBJECT public: @@ -76,54 +83,40 @@ public: static SerializableItem *newItem() { return new Command_DeckDownload; } int getItemId() const { return ItemId_Command_DeckDownload; } }; -class Command_ListChatChannels : public Command { +class Command_ListRooms : public Command { Q_OBJECT public: - Command_ListChatChannels(); - static SerializableItem *newItem() { return new Command_ListChatChannels; } - int getItemId() const { return ItemId_Command_ListChatChannels; } + Command_ListRooms(); + static SerializableItem *newItem() { return new Command_ListRooms; } + int getItemId() const { return ItemId_Command_ListRooms; } }; -class Command_ChatJoinChannel : public Command { +class Command_JoinRoom : public Command { Q_OBJECT public: - Command_ChatJoinChannel(const QString &_channel = QString()); - QString getChannel() const { return static_cast(itemMap.value("channel"))->getData(); }; - static SerializableItem *newItem() { return new Command_ChatJoinChannel; } - int getItemId() const { return ItemId_Command_ChatJoinChannel; } + Command_JoinRoom(int _roomId = -1); + int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); }; + static SerializableItem *newItem() { return new Command_JoinRoom; } + int getItemId() const { return ItemId_Command_JoinRoom; } }; -class Command_ChatLeaveChannel : public ChatCommand { +class Command_LeaveRoom : public RoomCommand { Q_OBJECT public: - Command_ChatLeaveChannel(const QString &_channel = QString()); - static SerializableItem *newItem() { return new Command_ChatLeaveChannel; } - int getItemId() const { return ItemId_Command_ChatLeaveChannel; } + Command_LeaveRoom(int _roomId = -1); + static SerializableItem *newItem() { return new Command_LeaveRoom; } + int getItemId() const { return ItemId_Command_LeaveRoom; } }; -class Command_ChatSay : public ChatCommand { +class Command_RoomSay : public RoomCommand { Q_OBJECT public: - Command_ChatSay(const QString &_channel = QString(), const QString &_message = QString()); + Command_RoomSay(int _roomId = -1, const QString &_message = QString()); QString getMessage() const { return static_cast(itemMap.value("message"))->getData(); }; - static SerializableItem *newItem() { return new Command_ChatSay; } - int getItemId() const { return ItemId_Command_ChatSay; } + static SerializableItem *newItem() { return new Command_RoomSay; } + int getItemId() const { return ItemId_Command_RoomSay; } }; -class Command_ListGames : public Command { +class Command_CreateGame : public RoomCommand { Q_OBJECT public: - Command_ListGames(); - static SerializableItem *newItem() { return new Command_ListGames; } - int getItemId() const { return ItemId_Command_ListGames; } -}; -class Command_ListUsers : public Command { - Q_OBJECT -public: - Command_ListUsers(); - static SerializableItem *newItem() { return new Command_ListUsers; } - int getItemId() const { return ItemId_Command_ListUsers; } -}; -class Command_CreateGame : public Command { - Q_OBJECT -public: - Command_CreateGame(const QString &_description = QString(), const QString &_password = QString(), int _maxPlayers = -1, bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, bool _spectatorsCanTalk = false, bool _spectatorsSeeEverything = false); + Command_CreateGame(int _roomId = -1, const QString &_description = QString(), const QString &_password = QString(), int _maxPlayers = -1, bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, bool _spectatorsCanTalk = false, bool _spectatorsSeeEverything = false); QString getDescription() const { return static_cast(itemMap.value("description"))->getData(); }; QString getPassword() const { return static_cast(itemMap.value("password"))->getData(); }; int getMaxPlayers() const { return static_cast(itemMap.value("max_players"))->getData(); }; @@ -134,10 +127,10 @@ public: static SerializableItem *newItem() { return new Command_CreateGame; } int getItemId() const { return ItemId_Command_CreateGame; } }; -class Command_JoinGame : public Command { +class Command_JoinGame : public RoomCommand { Q_OBJECT public: - Command_JoinGame(int _gameId = -1, const QString &_password = QString(), bool _spectator = false); + Command_JoinGame(int _roomId = -1, int _gameId = -1, const QString &_password = QString(), bool _spectator = false); int getGameId() const { return static_cast(itemMap.value("game_id"))->getData(); }; QString getPassword() const { return static_cast(itemMap.value("password"))->getData(); }; bool getSpectator() const { return static_cast(itemMap.value("spectator"))->getData(); }; @@ -192,9 +185,10 @@ public: class Command_MoveCard : public GameCommand { Q_OBJECT public: - Command_MoveCard(int _gameId = -1, const QString &_startZone = QString(), int _cardId = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, bool _faceDown = false, bool _tapped = false); + Command_MoveCard(int _gameId = -1, const QString &_startZone = QString(), int _cardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, bool _faceDown = false, bool _tapped = false); QString getStartZone() const { return static_cast(itemMap.value("start_zone"))->getData(); }; int getCardId() const { return static_cast(itemMap.value("card_id"))->getData(); }; + int getTargetPlayerId() const { return static_cast(itemMap.value("target_player_id"))->getData(); }; QString getTargetZone() const { return static_cast(itemMap.value("target_zone"))->getData(); }; int getX() const { return static_cast(itemMap.value("x"))->getData(); }; int getY() const { return static_cast(itemMap.value("y"))->getData(); }; @@ -432,11 +426,12 @@ public: class Event_MoveCard : public GameEvent { Q_OBJECT public: - Event_MoveCard(int _playerId = -1, int _cardId = -1, const QString &_cardName = QString(), const QString &_startZone = QString(), int _position = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, int _newCardId = -1, bool _faceDown = false); + Event_MoveCard(int _playerId = -1, int _cardId = -1, const QString &_cardName = QString(), const QString &_startZone = QString(), int _position = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, int _newCardId = -1, bool _faceDown = false); int getCardId() const { return static_cast(itemMap.value("card_id"))->getData(); }; QString getCardName() const { return static_cast(itemMap.value("card_name"))->getData(); }; QString getStartZone() const { return static_cast(itemMap.value("start_zone"))->getData(); }; int getPosition() const { return static_cast(itemMap.value("position"))->getData(); }; + int getTargetPlayerId() const { return static_cast(itemMap.value("target_player_id"))->getData(); }; QString getTargetZone() const { return static_cast(itemMap.value("target_zone"))->getData(); }; int getX() const { return static_cast(itemMap.value("x"))->getData(); }; int getY() const { return static_cast(itemMap.value("y"))->getData(); }; @@ -615,22 +610,22 @@ public: static SerializableItem *newItem() { return new Event_UserLeft; } int getItemId() const { return ItemId_Event_UserLeft; } }; -class Event_ChatLeaveChannel : public ChatEvent { +class Event_LeaveRoom : public RoomEvent { Q_OBJECT public: - Event_ChatLeaveChannel(const QString &_channel = QString(), const QString &_playerName = QString()); + Event_LeaveRoom(int _roomId = -1, const QString &_playerName = QString()); QString getPlayerName() const { return static_cast(itemMap.value("player_name"))->getData(); }; - static SerializableItem *newItem() { return new Event_ChatLeaveChannel; } - int getItemId() const { return ItemId_Event_ChatLeaveChannel; } + static SerializableItem *newItem() { return new Event_LeaveRoom; } + int getItemId() const { return ItemId_Event_LeaveRoom; } }; -class Event_ChatSay : public ChatEvent { +class Event_RoomSay : public RoomEvent { Q_OBJECT public: - Event_ChatSay(const QString &_channel = QString(), const QString &_playerName = QString(), const QString &_message = QString()); + Event_RoomSay(int _roomId = -1, const QString &_playerName = QString(), const QString &_message = QString()); QString getPlayerName() const { return static_cast(itemMap.value("player_name"))->getData(); }; QString getMessage() const { return static_cast(itemMap.value("message"))->getData(); }; - static SerializableItem *newItem() { return new Event_ChatSay; } - int getItemId() const { return ItemId_Event_ChatSay; } + static SerializableItem *newItem() { return new Event_RoomSay; } + int getItemId() const { return ItemId_Event_RoomSay; } }; class Context_ReadyStart : public GameEventContext { Q_OBJECT diff --git a/common/protocol_mc.pl b/common/protocol_mc.pl index 696915d9..3dae3a16 100755 --- a/common/protocol_mc.pl +++ b/common/protocol_mc.pl @@ -34,10 +34,10 @@ while () { } elsif ($type == 1) { $type = 'cmd'; $namePrefix = 'Command'; - $baseClass = 'ChatCommand'; - $parentConstructorCall = "$baseClass(\"$name1\", _channel)"; - $constructorParamsH = "const QString &_channel = QString()"; - $constructorParamsCpp = "const QString &_channel"; + $baseClass = 'RoomCommand'; + $parentConstructorCall = "$baseClass(\"$name1\", _roomId)"; + $constructorParamsH = "int _roomId = -1"; + $constructorParamsCpp = "int _roomId"; } elsif ($type == 2) { $type = 'cmd'; $namePrefix = 'Command'; @@ -60,12 +60,12 @@ while () { $constructorParamsH = ""; $constructorParamsCpp = ""; } elsif ($type == 5) { - $type = 'chat_event'; + $type = 'room_event'; $namePrefix = 'Event'; - $baseClass = 'ChatEvent'; - $parentConstructorCall = "$baseClass(\"$name1\", _channel)"; - $constructorParamsH = "const QString &_channel = QString()"; - $constructorParamsCpp = "const QString &_channel"; + $baseClass = 'RoomEvent'; + $parentConstructorCall = "$baseClass(\"$name1\", _roomId)"; + $constructorParamsH = "int _roomId = -1"; + $constructorParamsCpp = "int _roomId"; } elsif ($type == 6) { $type = 'game_event_context'; $namePrefix = 'Context'; diff --git a/common/server.cpp b/common/server.cpp index dc4125b2..5e7d98ff 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -20,7 +20,7 @@ #include "server.h" #include "server_game.h" #include "server_counter.h" -#include "server_chatchannel.h" +#include "server_room.h" #include "server_protocolhandler.h" #include "protocol_datastructures.h" #include @@ -69,17 +69,6 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString return authState; } -Server_Game *Server::createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator) -{ - Server_Game *newGame = new Server_Game(creator, nextGameId++, description, password, maxPlayers, spectatorsAllowed, spectatorsNeedPassword, spectatorsCanTalk, spectatorsSeeEverything, this); - games.insert(newGame->getGameId(), newGame); - connect(newGame, SIGNAL(gameClosing()), this, SLOT(gameClosing())); - - broadcastGameListUpdate(newGame); - - return newGame; -} - void Server::addClient(Server_ProtocolHandler *client) { clients << client; @@ -106,56 +95,29 @@ Server_Game *Server::getGame(int gameId) const return games.value(gameId); } -void Server::broadcastGameListUpdate(Server_Game *game) +void Server::broadcastRoomUpdate() { - QList eventGameList; - if (game->getPlayerCount()) - // Game is open - eventGameList.append(new ServerInfo_Game( - game->getGameId(), - game->getDescription(), - !game->getPassword().isEmpty(), - game->getPlayerCount(), - game->getMaxPlayers(), - new ServerInfo_User(game->getCreatorInfo()), - game->getSpectatorsAllowed(), - game->getSpectatorsNeedPassword(), - game->getSpectatorCount() - )); - else - // Game is closing - eventGameList.append(new ServerInfo_Game(game->getGameId(), QString(), false, 0, game->getMaxPlayers(), 0, false, 0)); - Event_ListGames *event = new Event_ListGames(eventGameList); - - for (int i = 0; i < clients.size(); i++) - if (clients[i]->getAcceptsGameListChanges()) - clients[i]->sendProtocolItem(event, false); - delete event; -} - -void Server::broadcastChannelUpdate() -{ - Server_ChatChannel *channel = static_cast(sender()); - QList eventChannelList; - eventChannelList.append(new ServerInfo_ChatChannel(channel->getName(), channel->getDescription(), channel->size(), channel->getAutoJoin())); - Event_ListChatChannels *event = new Event_ListChatChannels(eventChannelList); + Server_Room *room = static_cast(sender()); + QList eventRoomList; + eventRoomList.append(new ServerInfo_Room(room->getId(), room->getName(), room->getDescription(), room->getGames().size(), room->size(), room->getAutoJoin())); + Event_ListRooms *event = new Event_ListRooms(eventRoomList); for (int i = 0; i < clients.size(); ++i) - if (clients[i]->getAcceptsChatChannelListChanges()) + if (clients[i]->getAcceptsRoomListChanges()) clients[i]->sendProtocolItem(event, false); delete event; } -void Server::gameClosing() +void Server::gameClosing(int gameId) { qDebug("Server::gameClosing"); - Server_Game *game = static_cast(sender()); - broadcastGameListUpdate(game); - games.remove(games.key(game)); + games.remove(gameId); } -void Server::addChatChannel(Server_ChatChannel *newChannel) +void Server::addRoom(Server_Room *newRoom) { - chatChannels.insert(newChannel->getName(), newChannel); - connect(newChannel, SIGNAL(channelInfoChanged()), this, SLOT(broadcastChannelUpdate())); + rooms.insert(newRoom->getId(), newRoom); + connect(newRoom, SIGNAL(roomInfoChanged()), this, SLOT(broadcastRoomUpdate())); + connect(newRoom, SIGNAL(gameCreated(Server_Game *)), this, SLOT(gameCreated(Server_Game *))); + connect(newRoom, SIGNAL(gameClosing(int)), this, SLOT(gameClosing(int))); } diff --git a/common/server.h b/common/server.h index 3f4dfd56..b2ed7815 100644 --- a/common/server.h +++ b/common/server.h @@ -6,7 +6,7 @@ #include class Server_Game; -class Server_ChatChannel; +class Server_Room; class Server_ProtocolHandler; class ServerInfo_User; @@ -18,22 +18,21 @@ class Server : public QObject signals: void pingClockTimeout(); private slots: - void gameClosing(); - void broadcastChannelUpdate(); + void gameClosing(int gameId); + void broadcastRoomUpdate(); public: Server(QObject *parent = 0); ~Server(); AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password); QList getGames() const { return games.values(); } Server_Game *getGame(int gameId) const; - const QMap &getChatChannels() { return chatChannels; } - void broadcastGameListUpdate(Server_Game *game); + const QMap &getRooms() { return rooms; } + int getNextGameId() { return nextGameId++; } const QMap &getUsers() const { return users; } void addClient(Server_ProtocolHandler *player); void removeClient(Server_ProtocolHandler *player); virtual QString getLoginMessage() const = 0; - Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator); virtual bool getGameShouldPing() const = 0; virtual int getMaxGameInactivityTime() const = 0; @@ -42,12 +41,12 @@ protected: QMap games; QList clients; QMap users; - QMap chatChannels; + QMap rooms; virtual AuthenticationResult checkUserPassword(const QString &user, const QString &password) = 0; virtual ServerInfo_User *getUserData(const QString &name) = 0; int nextGameId; - void addChatChannel(Server_ChatChannel *newChannel); + void addRoom(Server_Room *newRoom); }; #endif diff --git a/common/server_chatchannel.cpp b/common/server_chatchannel.cpp deleted file mode 100644 index 2bdd8574..00000000 --- a/common/server_chatchannel.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "server_chatchannel.h" -#include "server_protocolhandler.h" - -Server_ChatChannel::Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage) - : name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage) -{ -} - -void Server_ChatChannel::addClient(Server_ProtocolHandler *client) -{ - sendChatEvent(new Event_ChatJoinChannel(name, new ServerInfo_User(client->getUserInfo()))); - append(client); - - QList eventUserList; - for (int i = 0; i < size(); ++i) - eventUserList.append(new ServerInfo_User(at(i)->getUserInfo())); - Event_ChatListPlayers *eventCLP = new Event_ChatListPlayers(name, eventUserList); - client->enqueueProtocolItem(eventCLP); - - client->enqueueProtocolItem(new Event_ChatSay(name, QString(), joinMessage)); - - emit channelInfoChanged(); -} - -void Server_ChatChannel::removeClient(Server_ProtocolHandler *client) -{ - removeAt(indexOf(client)); - sendChatEvent(new Event_ChatLeaveChannel(name, client->getUserInfo()->getName())); - emit channelInfoChanged(); -} - -void Server_ChatChannel::say(Server_ProtocolHandler *client, const QString &s) -{ - sendChatEvent(new Event_ChatSay(name, client->getUserInfo()->getName(), s)); -} - -void Server_ChatChannel::sendChatEvent(ChatEvent *event) -{ - for (int i = 0; i < size(); ++i) - at(i)->sendProtocolItem(event, false); - delete event; -} diff --git a/common/server_chatchannel.h b/common/server_chatchannel.h deleted file mode 100644 index 22b2f360..00000000 --- a/common/server_chatchannel.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef CHATCHANNEL_H -#define CHATCHANNEL_H - -#include -#include -#include - -class Server_ProtocolHandler; -class ChatEvent; - -class Server_ChatChannel : public QObject, public QList { - Q_OBJECT -signals: - void channelInfoChanged(); -private: - QString name; - QString description; - bool autoJoin; - QString joinMessage; -public: - Server_ChatChannel(const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage); - QString getName() const { return name; } - QString getDescription() const { return description; } - bool getAutoJoin() const { return autoJoin; } - void addClient(Server_ProtocolHandler *client); - void removeClient(Server_ProtocolHandler *client); - void say(Server_ProtocolHandler *client, const QString &s); - - void sendChatEvent(ChatEvent *event); -}; - -#endif diff --git a/common/server_game.cpp b/common/server_game.cpp index 2b9add86..84a46cc5 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -18,6 +18,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "server.h" +#include "server_room.h" #include "server_game.h" #include "server_protocolhandler.h" #include "server_arrow.h" @@ -27,12 +28,12 @@ #include #include -Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server *parent) +Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *parent) : QObject(parent), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0) { addPlayer(_creator, false, false); - if (parent->getGameShouldPing()) { + if (parent->getServer()->getGameShouldPing()) { pingClock = new QTimer(this); connect(pingClock, SIGNAL(timeout()), this, SLOT(pingClockTimeout())); pingClock->start(1000); @@ -71,7 +72,7 @@ void Server_Game::pingClockTimeout() } sendGameEvent(new Event_Ping(pingList)); - const int maxTime = static_cast(parent())->getMaxGameInactivityTime(); + const int maxTime = static_cast(parent())->getServer()->getMaxGameInactivityTime(); if (allPlayersInactive) { if ((++inactivityCounter >= maxTime) && (maxTime > 0)) deleteLater(); @@ -193,7 +194,7 @@ Server_Player *Server_Game::addPlayer(Server_ProtocolHandler *handler, bool spec players.insert(playerId, newPlayer); if (broadcastUpdate) - qobject_cast(parent())->broadcastGameListUpdate(this); + qobject_cast(parent())->broadcastGameListUpdate(this); return newPlayer; } @@ -231,7 +232,7 @@ void Server_Game::removePlayer(Server_Player *player) deleteLater(); else if (!spectator) stopGameIfFinished(); - qobject_cast(parent())->broadcastGameListUpdate(this); + qobject_cast(parent())->broadcastGameListUpdate(this); } void Server_Game::setActivePlayer(int _activePlayer) @@ -391,3 +392,23 @@ void Server_Game::sendGameEventToPlayer(Server_Player *player, GameEvent *event) { player->sendProtocolItem(new GameEventContainer(QList() << event, gameId)); } + +ServerInfo_Game *Server_Game::getInfo() const +{ + if (players.isEmpty()) + // Game is open + return new ServerInfo_Game(getGameId(), QString(), false, 0, getMaxPlayers(), 0, false, 0); + else + // Game is closing + return new ServerInfo_Game( + getGameId(), + getDescription(), + !getPassword().isEmpty(), + getPlayerCount(), + getMaxPlayers(), + new ServerInfo_User(getCreatorInfo()), + getSpectatorsAllowed(), + getSpectatorsNeedPassword(), + getSpectatorCount() + ); +} \ No newline at end of file diff --git a/common/server_game.h b/common/server_game.h index 41c52698..420847dd 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -27,7 +27,7 @@ #include "protocol.h" class QTimer; -class Server; +class Server_Room; class ServerInfo_User; class Server_Game : public QObject { @@ -52,8 +52,9 @@ signals: private slots: void pingClockTimeout(); public: - Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server *parent); + Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *parent); ~Server_Game(); + ServerInfo_Game *getInfo() const; ServerInfo_User *getCreatorInfo() const { return creatorInfo; } bool getGameStarted() const { return gameStarted; } int getPlayerCount() const; diff --git a/common/server_player.cpp b/common/server_player.cpp index 92929554..e7da0d31 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -198,10 +198,13 @@ bool Server_Player::deleteCounter(int counterId) return true; } -ResponseCode Server_Player::moveCard(CommandContainer *cont, const QString &_startZone, int _cardId, const QString &_targetZone, int x, int y, bool faceDown, bool tapped) +ResponseCode Server_Player::moveCard(CommandContainer *cont, const QString &_startZone, int _cardId, int targetPlayerId, const QString &_targetZone, int x, int y, bool faceDown, bool tapped) { Server_CardZone *startzone = getZones().value(_startZone); - Server_CardZone *targetzone = getZones().value(_targetZone); + Server_Player *targetPlayer = game->getPlayers().value(targetPlayerId); + if (!targetPlayer) + return RespNameNotFound; + Server_CardZone *targetzone = targetPlayer->getZones().value(_targetZone); if ((!startzone) || (!targetzone)) return RespNameNotFound; @@ -210,12 +213,10 @@ ResponseCode Server_Player::moveCard(CommandContainer *cont, const QString &_sta ResponseCode Server_Player::moveCard(CommandContainer *cont, Server_CardZone *startzone, int _cardId, Server_CardZone *targetzone, int x, int y, bool faceDown, bool tapped) { - // Collision detection -/* if (targetzone->hasCoords()) - for (int i = 0; i < targetzone->cards.size(); ++i) - if ((targetzone->cards[i]->getX() == x) && (targetzone->cards[i]->getY() == y) && (x != -1)) - return RespContextError; -*/ + // Disallow controller change between different zones. + if ((startzone->getName() != targetzone->getName()) && (startzone->getPlayer() != targetzone->getPlayer())) + return RespContextError; + int position = -1; Server_Card *card = startzone->getCard(_cardId, false, &position); if (!card) @@ -226,15 +227,19 @@ ResponseCode Server_Player::moveCard(CommandContainer *cont, Server_CardZone *st int oldX = card->getX(), oldY = card->getY(); - if (startzone != targetzone) { + // Attachment relationships can be retained when moving a card onto the opponent's table + if (startzone->getName() != targetzone->getName()) { // Delete all attachment relationships if (card->getParentCard()) card->setParentCard(0); - const QList &attachedCards = card->getAttachedCards(); + // Make a copy of the list because the original one gets modified during the loop + QList attachedCards = card->getAttachedCards(); for (int i = 0; i < attachedCards.size(); ++i) attachedCards[i]->getZone()->getPlayer()->unattachCard(cont, attachedCards[i]); - + } + + if (startzone != targetzone) { // Delete all arrows from and to the card const QList &players = game->getPlayers().values(); for (int i = 0; i < players.size(); ++i) { @@ -266,7 +271,6 @@ ResponseCode Server_Player::moveCard(CommandContainer *cont, Server_CardZone *st card->resetState(); } else -// if (x == -1) x = targetzone->getFreeGridColumn(x, y, card->getName()); targetzone->insertCard(card, x, y); @@ -302,8 +306,8 @@ ResponseCode Server_Player::moveCard(CommandContainer *cont, Server_CardZone *st int privatePosition = -1; if (startzone->getType() == HiddenZone) privatePosition = position; - cont->enqueueGameEventPrivate(new Event_MoveCard(getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId()); - cont->enqueueGameEventOmniscient(new Event_MoveCard(getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId()); + cont->enqueueGameEventPrivate(new Event_MoveCard(getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getPlayer()->getPlayerId(), targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId()); + cont->enqueueGameEventOmniscient(new Event_MoveCard(getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getPlayer()->getPlayerId(), targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId()); // Other players do not get to see the start and/or target position of the card if the respective // part of the zone is being looked at. The information is not needed anyway because in hidden zones, @@ -317,9 +321,9 @@ ResponseCode Server_Player::moveCard(CommandContainer *cont, Server_CardZone *st x = -1; if ((startzone->getType() == PublicZone) || (targetzone->getType() == PublicZone)) - cont->enqueueGameEventPublic(new Event_MoveCard(getPlayerId(), oldCardId, publicCardName, startzone->getName(), position, targetzone->getName(), x, y, card->getId(), faceDown), game->getGameId()); + cont->enqueueGameEventPublic(new Event_MoveCard(getPlayerId(), oldCardId, publicCardName, startzone->getName(), position, targetzone->getPlayer()->getPlayerId(), targetzone->getName(), x, y, card->getId(), faceDown), game->getGameId()); else - cont->enqueueGameEventPublic(new Event_MoveCard(getPlayerId(), -1, QString(), startzone->getName(), position, targetzone->getName(), x, y, -1, false), game->getGameId()); + cont->enqueueGameEventPublic(new Event_MoveCard(getPlayerId(), -1, QString(), startzone->getName(), position, targetzone->getPlayer()->getPlayerId(), targetzone->getName(), x, y, -1, false), game->getGameId()); if (tapped) setCardAttrHelper(cont, targetzone->getName(), card->getId(), "tapped", "1"); @@ -338,7 +342,7 @@ void Server_Player::unattachCard(CommandContainer *cont, Server_Card *card) cont->enqueueGameEventPrivate(new Event_AttachCard(getPlayerId(), zone->getName(), card->getId(), -1, QString(), -1), game->getGameId()); cont->enqueueGameEventPublic(new Event_AttachCard(getPlayerId(), zone->getName(), card->getId(), -1, QString(), -1), game->getGameId()); - moveCard(cont, zone->getName(), card->getId(), zone->getName(), -1, card->getY(), card->getFaceDown(), card->getTapped()); + moveCard(cont, zone, card->getId(), zone, -1, card->getY(), card->getFaceDown(), card->getTapped()); } ResponseCode Server_Player::setCardAttrHelper(CommandContainer *cont, const QString &zoneName, int cardId, const QString &attrName, const QString &attrValue) diff --git a/common/server_player.h b/common/server_player.h index 6e17b9c6..7a3138c3 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -74,7 +74,7 @@ public: void clearZones(); void setupZones(); - ResponseCode moveCard(CommandContainer *cont, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped); + ResponseCode moveCard(CommandContainer *cont, const QString &_startZone, int _cardId, int _targetPlayer, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped); ResponseCode moveCard(CommandContainer *cont, Server_CardZone *startzone, int _cardId, Server_CardZone *targetzone, int x, int y, bool faceDown, bool tapped); void unattachCard(CommandContainer *cont, Server_Card *card); ResponseCode setCardAttrHelper(CommandContainer *cont, const QString &zone, int cardId, const QString &attrName, const QString &attrValue); diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 1d618952..2e946c24 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -3,7 +3,7 @@ #include "server_protocolhandler.h" #include "protocol.h" #include "protocol_items.h" -#include "server_chatchannel.h" +#include "server_room.h" #include "server_card.h" #include "server_arrow.h" #include "server_cardzone.h" @@ -14,7 +14,7 @@ #include Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent) - : QObject(parent), server(_server), authState(PasswordWrong), acceptsGameListChanges(false), acceptsUserListChanges(false), acceptsChatChannelListChanges(false), userInfo(0), lastCommandTime(QDateTime::currentDateTime()) + : QObject(parent), server(_server), authState(PasswordWrong), acceptsUserListChanges(false), acceptsRoomListChanges(false), userInfo(0), lastCommandTime(QDateTime::currentDateTime()) { connect(server, SIGNAL(pingClockTimeout()), this, SLOT(pingClockTimeout())); } @@ -37,9 +37,9 @@ Server_ProtocolHandler::~Server_ProtocolHandler() p->setProtocolHandler(0); } - QMapIterator chatChannelIterator(chatChannels); - while (chatChannelIterator.hasNext()) - chatChannelIterator.next().value()->removeClient(this); + QMapIterator roomIterator(rooms); + while (roomIterator.hasNext()) + roomIterator.next().value()->removeClient(this); delete userInfo; } @@ -54,20 +54,22 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm { lastCommandTime = QDateTime::currentDateTime(); - ChatCommand *chatCommand = qobject_cast(command); + RoomCommand *roomCommand = qobject_cast(command); GameCommand *gameCommand = qobject_cast(command); - if (chatCommand) { - qDebug() << "received ChatCommand: channel =" << chatCommand->getChannel(); + if (roomCommand) { + qDebug() << "received RoomCommand: roomId =" << roomCommand->getRoomId(); if (authState == PasswordWrong) return RespLoginNeeded; - Server_ChatChannel *channel = chatChannels.value(chatCommand->getChannel(), 0); - if (!channel) + Server_Room *room = rooms.value(roomCommand->getRoomId(), 0); + if (!room) return RespNameNotFound; switch (command->getItemId()) { - case ItemId_Command_ChatLeaveChannel: return cmdChatLeaveChannel(qobject_cast(command), cont, channel); - case ItemId_Command_ChatSay: return cmdChatSay(qobject_cast(command), cont, channel); + case ItemId_Command_LeaveRoom: return cmdLeaveRoom(qobject_cast(command), cont, room); + case ItemId_Command_RoomSay: return cmdRoomSay(qobject_cast(command), cont, room); + case ItemId_Command_CreateGame: return cmdCreateGame(qobject_cast(command), cont, room); + case ItemId_Command_JoinGame: return cmdJoinGame(qobject_cast(command), cont, room); } } else if (gameCommand) { qDebug() << "received GameCommand: game =" << gameCommand->getGameId(); @@ -125,12 +127,9 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm case ItemId_Command_DeckUpload: return cmdDeckUpload(qobject_cast(command), cont); case ItemId_Command_DeckDownload: return cmdDeckDownload(qobject_cast(command), cont); case ItemId_Command_GetUserInfo: return cmdGetUserInfo(qobject_cast(command), cont); - case ItemId_Command_ListChatChannels: return cmdListChatChannels(qobject_cast(command), cont); - case ItemId_Command_ChatJoinChannel: return cmdChatJoinChannel(qobject_cast(command), cont); + case ItemId_Command_ListRooms: return cmdListRooms(qobject_cast(command), cont); + case ItemId_Command_JoinRoom: return cmdJoinRoom(qobject_cast(command), cont); case ItemId_Command_ListUsers: return cmdListUsers(qobject_cast(command), cont); - case ItemId_Command_ListGames: return cmdListGames(qobject_cast(command), cont); - case ItemId_Command_CreateGame: return cmdCreateGame(qobject_cast(command), cont); - case ItemId_Command_JoinGame: return cmdJoinGame(qobject_cast(command), cont); } } return RespInvalidCommand; @@ -273,51 +272,50 @@ ResponseCode Server_ProtocolHandler::cmdGetUserInfo(Command_GetUserInfo *cmd, Co return RespNothing; } -ResponseCode Server_ProtocolHandler::cmdListChatChannels(Command_ListChatChannels * /*cmd*/, CommandContainer *cont) +ResponseCode Server_ProtocolHandler::cmdListRooms(Command_ListRooms * /*cmd*/, CommandContainer *cont) { if (authState == PasswordWrong) return RespLoginNeeded; - QList eventChannelList; - QMapIterator channelIterator(server->getChatChannels()); - while (channelIterator.hasNext()) { - Server_ChatChannel *c = channelIterator.next().value(); - eventChannelList.append(new ServerInfo_ChatChannel(c->getName(), c->getDescription(), c->size(), c->getAutoJoin())); + QList eventRoomList; + QMapIterator roomIterator(server->getRooms()); + while (roomIterator.hasNext()) { + Server_Room *r = roomIterator.next().value(); + eventRoomList.append(new ServerInfo_Room(r->getId(), r->getName(), r->getDescription(), r->getGames().size(), r->size(), r->getAutoJoin())); } - cont->enqueueItem(new Event_ListChatChannels(eventChannelList)); + cont->enqueueItem(new Event_ListRooms(eventRoomList)); - acceptsChatChannelListChanges = true; + acceptsRoomListChanges = true; return RespOk; } -ResponseCode Server_ProtocolHandler::cmdChatJoinChannel(Command_ChatJoinChannel *cmd, CommandContainer *cont) +ResponseCode Server_ProtocolHandler::cmdJoinRoom(Command_JoinRoom *cmd, CommandContainer *cont) { if (authState == PasswordWrong) return RespLoginNeeded; - if (chatChannels.contains(cmd->getChannel())) + if (rooms.contains(cmd->getRoomId())) return RespContextError; - QMap allChannels = server->getChatChannels(); - Server_ChatChannel *c = allChannels.value(cmd->getChannel(), 0); - if (!c) + Server_Room *r = server->getRooms().value(cmd->getRoomId(), 0); + if (!r) return RespNameNotFound; - c->addClient(this); - chatChannels.insert(cmd->getChannel(), c); + r->addClient(this); + rooms.insert(r->getId(), r); return RespOk; } -ResponseCode Server_ProtocolHandler::cmdChatLeaveChannel(Command_ChatLeaveChannel * /*cmd*/, CommandContainer *cont, Server_ChatChannel *channel) +ResponseCode Server_ProtocolHandler::cmdLeaveRoom(Command_LeaveRoom * /*cmd*/, CommandContainer *cont, Server_Room *room) { - chatChannels.remove(channel->getName()); - channel->removeClient(this); + rooms.remove(room->getId()); + room->removeClient(this); return RespOk; } -ResponseCode Server_ProtocolHandler::cmdChatSay(Command_ChatSay *cmd, CommandContainer *cont, Server_ChatChannel *channel) +ResponseCode Server_ProtocolHandler::cmdRoomSay(Command_RoomSay *cmd, CommandContainer *cont, Server_Room *room) { - channel->say(this, cmd->getMessage()); + room->say(this, cmd->getMessage()); return RespOk; } @@ -337,39 +335,12 @@ ResponseCode Server_ProtocolHandler::cmdListUsers(Command_ListUsers * /*cmd*/, C return RespNothing; } -ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames * /*cmd*/, CommandContainer *cont) +ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, CommandContainer *cont, Server_Room *room) { if (authState == PasswordWrong) return RespLoginNeeded; - const QList &gameList = server->getGames(); - QList eventGameList; - for (int i = 0; i < gameList.size(); ++i) { - Server_Game *g = gameList[i]; - eventGameList.append(new ServerInfo_Game( - g->getGameId(), - g->getDescription(), - !g->getPassword().isEmpty(), - g->getPlayerCount(), - g->getMaxPlayers(), - new ServerInfo_User(g->getCreatorInfo()), - g->getSpectatorsAllowed(), - g->getSpectatorsNeedPassword(), - g->getSpectatorCount() - )); - } - cont->enqueueItem(new Event_ListGames(eventGameList)); - - acceptsGameListChanges = true; - return RespOk; -} - -ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, CommandContainer *cont) -{ - if (authState == PasswordWrong) - return RespLoginNeeded; - - Server_Game *game = server->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), cmd->getSpectatorsNeedPassword(), cmd->getSpectatorsCanTalk(), cmd->getSpectatorsSeeEverything(), this); + Server_Game *game = room->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), cmd->getSpectatorsNeedPassword(), cmd->getSpectatorsCanTalk(), cmd->getSpectatorsSeeEverything(), this); Server_Player *creator = game->getPlayers().values().first(); games.insert(game->getGameId(), QPair(game, creator)); @@ -378,7 +349,7 @@ ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, Comm return RespOk; } -ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd, CommandContainer *cont) +ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd, CommandContainer *cont, Server_Room *room) { if (authState == PasswordWrong) return RespLoginNeeded; @@ -386,7 +357,7 @@ ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd, CommandC if (games.contains(cmd->getGameId())) return RespContextError; - Server_Game *g = server->getGame(cmd->getGameId()); + Server_Game *g = room->getGames().value(cmd->getGameId()); if (!g) return RespNameNotFound; @@ -507,10 +478,11 @@ ResponseCode Server_ProtocolHandler::cmdMulligan(Command_Mulligan * /*cmd*/, Com Server_CardZone *hand = player->getZones().value("hand"); int number = (hand->cards.size() <= 1) ? player->getInitialCards() : hand->cards.size() - 1; + Server_CardZone *deck = player->getZones().value("deck"); while (!hand->cards.isEmpty()) - player->moveCard(cont, "hand", hand->cards.first()->getId(), "deck", 0, 0, false, false); + player->moveCard(cont, hand, hand->cards.first()->getId(), deck, 0, 0, false, false); - player->getZones().value("deck")->shuffle(); + deck->shuffle(); cont->enqueueGameEventPrivate(new Event_Shuffle(player->getPlayerId()), game->getGameId()); cont->enqueueGameEventPublic(new Event_Shuffle(player->getPlayerId()), game->getGameId()); @@ -571,7 +543,7 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, CommandC if (!game->getGameStarted()) return RespGameNotStarted; - return player->moveCard(cont, cmd->getStartZone(), cmd->getCardId(), cmd->getTargetZone(), cmd->getX(), cmd->getY(), cmd->getFaceDown(), cmd->getTapped()); + return player->moveCard(cont, cmd->getStartZone(), cmd->getCardId(), cmd->getTargetPlayerId(), cmd->getTargetZone(), cmd->getX(), cmd->getY(), cmd->getFaceDown(), cmd->getTapped()); } ResponseCode Server_ProtocolHandler::cmdFlipCard(Command_FlipCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index aa515760..d9ffa154 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -10,6 +10,7 @@ class Server_Player; class Server_Card; class ServerInfo_User; +class Server_Room; class QTimer; class Server_ProtocolHandler : public QObject { @@ -17,15 +18,14 @@ class Server_ProtocolHandler : public QObject { protected: Server *server; QMap > games; - QMap chatChannels; + QMap rooms; Server *getServer() const { return server; } QPair getGame(int gameId) const; AuthenticationResult authState; - bool acceptsGameListChanges; bool acceptsUserListChanges; - bool acceptsChatChannelListChanges; + bool acceptsRoomListChanges; ServerInfo_User *userInfo; private: @@ -45,14 +45,13 @@ private: virtual ResponseCode cmdDeckUpload(Command_DeckUpload *cmd, CommandContainer *cont) = 0; virtual ResponseCode cmdDeckDownload(Command_DeckDownload *cmd, CommandContainer *cont) = 0; ResponseCode cmdGetUserInfo(Command_GetUserInfo *cmd, CommandContainer *cont); - ResponseCode cmdListChatChannels(Command_ListChatChannels *cmd, CommandContainer *cont); - ResponseCode cmdChatJoinChannel(Command_ChatJoinChannel *cmd, CommandContainer *cont); - ResponseCode cmdChatLeaveChannel(Command_ChatLeaveChannel *cmd, CommandContainer *cont, Server_ChatChannel *channel); - ResponseCode cmdChatSay(Command_ChatSay *cmd, CommandContainer *cont, Server_ChatChannel *channel); + ResponseCode cmdListRooms(Command_ListRooms *cmd, CommandContainer *cont); + ResponseCode cmdJoinRoom(Command_JoinRoom *cmd, CommandContainer *cont); + ResponseCode cmdLeaveRoom(Command_LeaveRoom *cmd, CommandContainer *cont, Server_Room *room); + ResponseCode cmdRoomSay(Command_RoomSay *cmd, CommandContainer *cont, Server_Room *room); ResponseCode cmdListUsers(Command_ListUsers *cmd, CommandContainer *cont); - ResponseCode cmdListGames(Command_ListGames *cmd, CommandContainer *cont); - ResponseCode cmdCreateGame(Command_CreateGame *cmd, CommandContainer *cont); - ResponseCode cmdJoinGame(Command_JoinGame *cmd, CommandContainer *cont); + ResponseCode cmdCreateGame(Command_CreateGame *cmd, CommandContainer *cont, Server_Room *room); + ResponseCode cmdJoinGame(Command_JoinGame *cmd, CommandContainer *cont, Server_Room *room); ResponseCode cmdLeaveGame(Command_LeaveGame *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdConcede(Command_Concede *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdReadyStart(Command_ReadyStart *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); @@ -91,9 +90,8 @@ public: ~Server_ProtocolHandler(); void playerRemovedFromGame(Server_Game *game); - bool getAcceptsGameListChanges() const { return acceptsGameListChanges; } bool getAcceptsUserListChanges() const { return acceptsUserListChanges; } - bool getAcceptsChatChannelListChanges() const { return acceptsChatChannelListChanges; } + bool getAcceptsRoomListChanges() const { return acceptsRoomListChanges; } ServerInfo_User *getUserInfo() const { return userInfo; } void setUserInfo(ServerInfo_User *_userInfo) { userInfo = _userInfo; } const QDateTime &getLastCommandTime() const { return lastCommandTime; } diff --git a/common/server_room.cpp b/common/server_room.cpp new file mode 100644 index 00000000..95ca746a --- /dev/null +++ b/common/server_room.cpp @@ -0,0 +1,76 @@ +#include "server_room.h" +#include "server_protocolhandler.h" +#include "server_game.h" + +Server_Room::Server_Room(int _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, Server *parent) + : QObject(parent), id(_id), name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage) +{ +} + +Server *Server_Room::getServer() const +{ + return static_cast(parent()); +} + +QList Server_Room::addClient(Server_ProtocolHandler *client) +{ + sendRoomEvent(new Event_JoinRoom(id, new ServerInfo_User(client->getUserInfo()))); + append(client); + + QList eventUserList; + for (int i = 0; i < size(); ++i) + eventUserList.append(new ServerInfo_User(at(i)->getUserInfo())); + + emit roomInfoChanged(); + return eventUserList; +} + +void Server_Room::removeClient(Server_ProtocolHandler *client) +{ + removeAt(indexOf(client)); + sendRoomEvent(new Event_LeaveRoom(id, client->getUserInfo()->getName())); + emit roomInfoChanged(); +} + +void Server_Room::say(Server_ProtocolHandler *client, const QString &s) +{ + sendRoomEvent(new Event_RoomSay(id, client->getUserInfo()->getName(), s)); +} + +void Server_Room::sendRoomEvent(RoomEvent *event) +{ + for (int i = 0; i < size(); ++i) + at(i)->sendProtocolItem(event, false); + delete event; +} + +void Server_Room::broadcastGameListUpdate(Server_Game *game) +{ + Event_ListGames *event = new Event_ListGames(id, QList() << game->getInfo()); + + for (int i = 0; i < size(); i++) + at(i)->sendProtocolItem(event, false); + delete event; +} + +Server_Game *Server_Room::createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator) +{ + Server_Game *newGame = new Server_Game(creator, static_cast(parent())->getNextGameId(), description, password, maxPlayers, spectatorsAllowed, spectatorsNeedPassword, spectatorsCanTalk, spectatorsSeeEverything, this); + games.insert(newGame->getGameId(), newGame); + connect(newGame, SIGNAL(gameClosing()), this, SLOT(removeGame())); + + broadcastGameListUpdate(newGame); + + emit gameCreated(newGame); + + return newGame; +} + +void Server_Room::removeGame() +{ + Server_Game *game = static_cast(sender()); + broadcastGameListUpdate(game); + games.remove(game->getGameId()); + + emit gameClosing(game->getGameId()); +} diff --git a/common/server_room.h b/common/server_room.h new file mode 100644 index 00000000..4f6cc46f --- /dev/null +++ b/common/server_room.h @@ -0,0 +1,48 @@ +#ifndef SERVER_ROOM_H +#define SERVER_ROOM_H + +#include +#include +#include +#include + +class Server_ProtocolHandler; +class RoomEvent; +class ServerInfo_User; +class Server_Game; +class Server; + +class Server_Room : public QObject, public QList { + Q_OBJECT +signals: + void roomInfoChanged(); + void gameCreated(Server_Game *game); + void gameClosing(int gameId); +private: + int id; + QString name; + QString description; + bool autoJoin; + QString joinMessage; + QMap games; +private slots: + void removeGame(); +public: + Server_Room(int _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, Server *parent); + int getId() const { return id; } + QString getName() const { return name; } + QString getDescription() const { return description; } + bool getAutoJoin() const { return autoJoin; } + const QMap &getGames() const { return games; } + Server *getServer() const; + + QList addClient(Server_ProtocolHandler *client); + void removeClient(Server_ProtocolHandler *client); + void say(Server_ProtocolHandler *client, const QString &s); + void broadcastGameListUpdate(Server_Game *game); + Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator); + + void sendRoomEvent(RoomEvent *event); +}; + +#endif diff --git a/servatrice/servatrice.pro b/servatrice/servatrice.pro index d89ecf08..9f7fcc70 100755 --- a/servatrice/servatrice.pro +++ b/servatrice/servatrice.pro @@ -27,7 +27,7 @@ HEADERS += src/servatrice.h \ ../common/server_arrow.h \ ../common/server_card.h \ ../common/server_cardzone.h \ - ../common/server_chatchannel.h \ + ../common/server_room.h \ ../common/server_counter.h \ ../common/server_game.h \ ../common/server_player.h \ @@ -48,7 +48,7 @@ SOURCES += src/main.cpp \ ../common/server.cpp \ ../common/server_card.cpp \ ../common/server_cardzone.cpp \ - ../common/server_chatchannel.cpp \ + ../common/server_room.cpp \ ../common/server_game.cpp \ ../common/server_player.cpp \ ../common/server_protocolhandler.cpp diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index 4be6b7f3..b196b84d 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -22,7 +22,7 @@ #include #include #include "servatrice.h" -#include "server_chatchannel.h" +#include "server_room.h" #include "serversocketinterface.h" #include "protocol.h" @@ -49,16 +49,18 @@ Servatrice::Servatrice(QObject *parent) if (dbType == "mysql") openDatabase(); - int size = settings->beginReadArray("chatchannels"); + int size = settings->beginReadArray("rooms"); for (int i = 0; i < size; ++i) { settings->setArrayIndex(i); - Server_ChatChannel *newChannel = new Server_ChatChannel( + Server_Room *newRoom = new Server_Room( + i, settings->value("name").toString(), settings->value("description").toString(), settings->value("autojoin").toBool(), - settings->value("joinmessage").toString() + settings->value("joinmessage").toString(), + this ); - addChatChannel(newChannel); + addRoom(newRoom); } settings->endArray();