diff --git a/cockatrice/src/arrowitem.cpp b/cockatrice/src/arrowitem.cpp index fb4a66a0..f8b70eb7 100644 --- a/cockatrice/src/arrowitem.cpp +++ b/cockatrice/src/arrowitem.cpp @@ -3,6 +3,7 @@ #include "cardzone.h" #include "player.h" #include "math.h" +#include "protocol_items.h" #include #include #include @@ -76,7 +77,7 @@ void ArrowItem::mousePressEvent(QGraphicsSceneMouseEvent *event) event->accept(); if (event->button() == Qt::RightButton) - player->client->deleteArrow(id); + player->sendGameCommand(new Command_DeleteArrow(-1, id)); } ArrowDragItem::ArrowDragItem(CardItem *_startItem, const QColor &_color) @@ -110,7 +111,8 @@ void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/) if (targetItem && (targetItem != startItem)) { CardZone *startZone = static_cast(startItem->parentItem()); CardZone *targetZone = static_cast(targetItem->parentItem()); - player->client->createArrow( + player->sendGameCommand(new Command_CreateArrow( + -1, startZone->getPlayer()->getId(), startZone->getName(), startItem->getId(), @@ -118,7 +120,7 @@ void ArrowDragItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/) targetZone->getName(), targetItem->getId(), color - ); + )); } deleteLater(); } diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index 91a34dfa..6f1fcb1b 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -71,6 +71,18 @@ void CardItem::resetState() update(); } +void CardItem::processCardInfo(ServerInfo_Card *info) +{ + id = info->getId(); + name = info->getName(); + attacking = info->getAttacking(); + counters = info->getCounters(); + annotation = info->getAnnotation(); + tapped = info->getTapped(); + + update(); +} + CardDragItem *CardItem::createDragItem(int _id, const QPointF &_pos, const QPointF &_scenePos, bool faceDown) { deleteDragItem(); diff --git a/cockatrice/src/carditem.h b/cockatrice/src/carditem.h index 93fb1a42..a1f66abc 100644 --- a/cockatrice/src/carditem.h +++ b/cockatrice/src/carditem.h @@ -6,6 +6,7 @@ class CardDatabase; class CardDragItem; class CardZone; +class ServerInfo_Card; const int MAX_COUNTERS_ON_CARD = 999; @@ -41,6 +42,7 @@ public: bool getDoesntUntap() const { return doesntUntap; } void setDoesntUntap(bool _doesntUntap); void resetState(); + void processCardInfo(ServerInfo_Card *info); 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 946b8951..915864e1 100644 --- a/cockatrice/src/cardzone.cpp +++ b/cockatrice/src/cardzone.cpp @@ -128,9 +128,9 @@ CardItem *CardZone::takeCard(int position, int cardId, const QString &cardName, void CardZone::setCardAttr(int cardId, const QString &aname, const QString &avalue) { - if (hasCardAttr) +/* if (hasCardAttr) player->client->setCardAttr(name, cardId, aname, avalue); -} +*/} void CardZone::moveAllToZone() { @@ -140,8 +140,8 @@ 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->client->moveCard(cards.at(i)->getId(), getName(), targetZone, targetX); +// for (int i = cards.size() - 1; i >= 0; i--) +// player->client->moveCard(cards.at(i)->getId(), getName(), targetZone, targetX); } QPointF CardZone::closestGridPoint(const QPointF &point) diff --git a/cockatrice/src/client.h b/cockatrice/src/client.h index 3e81cdeb..9bdff6f9 100644 --- a/cockatrice/src/client.h +++ b/cockatrice/src/client.h @@ -33,7 +33,6 @@ class Client : public QObject { Q_OBJECT signals: void statusChanged(ClientStatus _status); -// void playerIdReceived(int id, QString name); void maxPingTime(int seconds, int maxSeconds); void serverTimeout(); void logSocketError(const QString &errorString); @@ -79,40 +78,6 @@ public: void connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password); void disconnectFromServer(); void sendCommand(Command *cmd); -public slots: - - void chatListChannels() { } - void chatJoinChannel(const QString &name) { } - void chatLeaveChannel(const QString &name) { } - void chatSay(const QString &name, const QString &s) { } - void listGames() { } - void listPlayers() { } - void createGame(const QString &description, const QString &password, unsigned int maxPlayers, bool spectatorsAllowed) { } - void joinGame(int gameId, const QString &password, bool spectator) { } - void leaveGame() { } - void login(const QString &name, const QString &pass) { } - void say(const QString &s) { } - void shuffle() { } - void rollDie(unsigned int sides) { } - void drawCard() { return drawCards(1); } - void drawCards(unsigned int number) { } - void moveCard(int cardid, const QString &startzone, const QString &targetzone, int x, int y = 0, bool faceDown = false) { } - void createToken(const QString &zone, const QString &name, const QString &powtough, int x, int y) { } - void createArrow(int startPlayerId, const QString &startZone, int startCardId, int targetPlayerId, const QString &targetPlayerZone, int targetCardId, const QColor &color) { } - void deleteArrow(int arrowId) { } - void setCardAttr(const QString &zone, int cardid, const QString &aname, const QString &avalue) { } - void readyStart() { } - void incCounter(int counterId, int delta) { } - void addCounter(const QString &counterName, QColor color, int radius, int value) { } - void setCounter(int counterId, int value) { } - void delCounter(int counterId) { } - void nextTurn() { } - void setActivePhase(int phase) { } - void dumpZone(int player, const QString &zone, int numberCards) { } - void stopDumpZone(int player, const QString &zone) { } - void dumpAll() { } - void submitDeck(const QStringList &deck) { } - }; #endif diff --git a/cockatrice/src/counter.cpp b/cockatrice/src/counter.cpp index f747dbd0..b47e297b 100644 --- a/cockatrice/src/counter.cpp +++ b/cockatrice/src/counter.cpp @@ -1,6 +1,7 @@ #include "counter.h" #include "player.h" #include "client.h" +#include "protocol_items.h" #include Counter::Counter(Player *_player, int _id, const QString &_name, QColor _color, int _radius, int _value, QGraphicsItem *parent) @@ -77,10 +78,10 @@ void Counter::setValue(int _value) void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event) { if (event->button() == Qt::LeftButton) { - player->client->incCounter(id, 1); + player->sendGameCommand(new Command_IncCounter(-1, id, 1)); event->accept(); } else if (event->button() == Qt::RightButton) { - player->client->incCounter(id, -1); + player->sendGameCommand(new Command_IncCounter(-1, id, -1)); event->accept(); } else if (event->button() == Qt::MidButton) { if (menu) @@ -93,7 +94,7 @@ void Counter::mousePressEvent(QGraphicsSceneMouseEvent *event) void Counter::incrementCounter() { int delta = static_cast(sender())->data().toInt(); - player->client->incCounter(id, delta); + player->sendGameCommand(new Command_IncCounter(-1, id, delta)); } void Counter::setCounter() @@ -101,5 +102,5 @@ void Counter::setCounter() bool ok; int newValue = QInputDialog::getInteger(0, tr("Set counter"), tr("New value for counter '%1':").arg(name), value, 0, 2000000000, 1, &ok); if (ok) - player->client->setCounter(id, newValue); + player->sendGameCommand(new Command_SetCounter(-1, id, newValue)); } diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index 9a0ca6cd..890a862f 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -129,49 +129,6 @@ void Game::retranslateUi() } /* -void Game::cardListReceived(QList list) -{ - for (int i = 0; i < list.size(); ++i) { - Player *p = players.value(list[i].getPlayerId(), 0); - if (!p) - continue; - - CardZone *zone = p->getZones().value(list[i].getZoneName(), 0); - if (!zone) - continue; - - CardItem *card = new CardItem(db, list[i].getName(), list[i].getId()); - zone->addCard(card, false, list[i].getX(), list[i].getY()); - - card->setCounters(list[i].getCounters()); - card->setTapped(list[i].getTapped()); - card->setAttacking(list[i].getAttacking()); - card->setAnnotation(list[i].getAnnotation()); - } -} - -void Game::zoneListReceived(QList list) -{ - for (int i = 0; i < list.size(); ++i) { - Player *p = players.value(list[i].getPlayerId(), 0); - if (!p) - continue; - - CardZone *zone = p->getZones().value(list[i].getName(), 0); - if (!zone) - continue; - - zone->clearContents(); - if ( - (list[i].getType() != ServerZone::PublicZone) - && !((list[i].getType() == ServerZone::PrivateZone) && p->getLocal()) - ) { - for (int j = 0; j < list[i].getCardCount(); ++j) - zone->addCard(new CardItem(db), false, -1); - zone->reorganizeCards(); - } - } -} void Game::counterListReceived(QList list) { @@ -228,11 +185,6 @@ void Game::playerListReceived(QList playerList) restartGameDialog(); } */ -void Game::readyStart() -{ - client->readyStart(); -} - void Game::restartGameDialog() { // dlgStartGame->show(); @@ -393,12 +345,12 @@ void Game::actNextPhase() int phase = currentPhase; if (++phase >= phaseCount) phase = 0; - client->setActivePhase(phase); +// client->setActivePhase(phase); } void Game::actNextTurn() { - client->nextTurn(); +// client->nextTurn(); } void Game::actRemoveLocalArrows() @@ -409,7 +361,7 @@ void Game::actRemoveLocalArrows() QMapIterator arrowIterator(players[i]->getArrows()); while (arrowIterator.hasNext()) { ArrowItem *a = arrowIterator.next().value(); - players[i]->client->deleteArrow(a->getId()); +// players[i]->client->deleteArrow(a->getId()); } } } @@ -436,37 +388,37 @@ void Game::cardMenuAction() void Game::actTap(CardItem *card) { - if (!card->getTapped()) - client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "tapped", "1"); +// if (!card->getTapped()) +// client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "tapped", "1"); } void Game::actUntap(CardItem *card) { - if (card->getTapped()) - client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "tapped", "0"); +// if (card->getTapped()) +// client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "tapped", "0"); } void Game::actDoesntUntap(CardItem *card) { - client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "doesnt_untap", QString::number(!card->getDoesntUntap())); +// client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "doesnt_untap", QString::number(!card->getDoesntUntap())); } void Game::actFlip(CardItem *card) { - QString zone = qgraphicsitem_cast(card->parentItem())->getName(); - client->moveCard(card->getId(), zone, zone, card->getGridPoint().x(), card->getGridPoint().y(), !card->getFaceDown()); +// QString zone = qgraphicsitem_cast(card->parentItem())->getName(); +// client->moveCard(card->getId(), zone, zone, card->getGridPoint().x(), card->getGridPoint().y(), !card->getFaceDown()); } void Game::actAddCounter(CardItem *card) { - if (card->getCounters() < MAX_COUNTERS_ON_CARD) - client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "counters", QString::number(card->getCounters() + 1)); +// if (card->getCounters() < MAX_COUNTERS_ON_CARD) +// client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "counters", QString::number(card->getCounters() + 1)); } void Game::actRemoveCounter(CardItem *card) { - if (card->getCounters()) - client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "counters", QString::number(card->getCounters() - 1)); +// if (card->getCounters()) +// client->setCardAttr(qgraphicsitem_cast(card->parentItem())->getName(), card->getId(), "counters", QString::number(card->getCounters() - 1)); } void Game::actSetCounters() @@ -479,32 +431,32 @@ void Game::actSetCounters() QListIterator i(scene->selectedItems()); while (i.hasNext()) { CardItem *temp = (CardItem *) i.next(); - client->setCardAttr(qgraphicsitem_cast(temp->parentItem())->getName(), temp->getId(), "counters", QString::number(number)); +// client->setCardAttr(qgraphicsitem_cast(temp->parentItem())->getName(), temp->getId(), "counters", QString::number(number)); } } void Game::actMoveToTopLibrary(CardItem *card) { - CardZone *startZone = qgraphicsitem_cast(card->parentItem()); - client->moveCard(card->getId(), startZone->getName(), "deck", 0, 0, false); +// CardZone *startZone = qgraphicsitem_cast(card->parentItem()); +// client->moveCard(card->getId(), startZone->getName(), "deck", 0, 0, false); } void Game::actMoveToBottomLibrary(CardItem *card) { - CardZone *startZone = qgraphicsitem_cast(card->parentItem()); - client->moveCard(card->getId(), startZone->getName(), "deck", -1, 0, false); +// CardZone *startZone = qgraphicsitem_cast(card->parentItem()); +// client->moveCard(card->getId(), startZone->getName(), "deck", -1, 0, false); } void Game::actMoveToGraveyard(CardItem *card) { - CardZone *startZone = qgraphicsitem_cast(card->parentItem()); - client->moveCard(card->getId(), startZone->getName(), "grave", 0, 0, false); +// CardZone *startZone = qgraphicsitem_cast(card->parentItem()); +// client->moveCard(card->getId(), startZone->getName(), "grave", 0, 0, false); } void Game::actMoveToExile(CardItem *card) { - CardZone *startZone = qgraphicsitem_cast(card->parentItem()); - client->moveCard(card->getId(), startZone->getName(), "rfg", 0, 0, false); +// CardZone *startZone = qgraphicsitem_cast(card->parentItem()); +// client->moveCard(card->getId(), startZone->getName(), "rfg", 0, 0, false); } void Game::hoverCardEvent(CardItem *card) diff --git a/cockatrice/src/game.h b/cockatrice/src/game.h index 140b9a8b..c8e5fbde 100644 --- a/cockatrice/src/game.h +++ b/cockatrice/src/game.h @@ -66,7 +66,6 @@ private slots: void counterListReceived(QList list); void arrowListReceived(QList list); */ - void readyStart(); signals: void submitDecklist(); void hoverCard(QString name); diff --git a/cockatrice/src/handzone.cpp b/cockatrice/src/handzone.cpp index 66bb5032..eb98e387 100644 --- a/cockatrice/src/handzone.cpp +++ b/cockatrice/src/handzone.cpp @@ -2,6 +2,7 @@ #include "handzone.h" #include "player.h" #include "client.h" +#include "protocol_items.h" HandZone::HandZone(Player *_p, int _zoneHeight, QGraphicsItem *parent) : CardZone(_p, "hand", false, false, _p->getLocal(), parent), zoneHeight(_zoneHeight) @@ -73,5 +74,5 @@ void HandZone::addCardImpl(CardItem *card, int x, int /*y*/) void HandZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { - player->client->moveCard(cardId, startZone->getName(), getName(), cards.size(), 0); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), cards.size(), -1, false)); } diff --git a/cockatrice/src/pilezone.cpp b/cockatrice/src/pilezone.cpp index 13e3cff7..589ce211 100644 --- a/cockatrice/src/pilezone.cpp +++ b/cockatrice/src/pilezone.cpp @@ -5,6 +5,7 @@ #include "client.h" #include "carddragitem.h" #include "zoneviewzone.h" +#include "protocol_items.h" PileZone::PileZone(Player *_p, const QString &_name, bool _isShufflable, bool _contentsKnown, QGraphicsItem *parent) : CardZone(_p, _name, false, _isShufflable, _contentsKnown, parent) @@ -47,7 +48,7 @@ void PileZone::addCardImpl(CardItem *card, int x, int /*y*/) void PileZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { - player->client->moveCard(cardId, startZone->getName(), getName(), 0, 0); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), 0, 0, false)); } void PileZone::reorganizeCards() diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 34957766..c184e008 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -10,6 +10,7 @@ #include "handzone.h" #include "cardlist.h" #include "tab_game.h" +#include "protocol_items.h" #include #include #include @@ -327,12 +328,12 @@ void Player::actViewSideboard() void Player::actShuffle() { - client->shuffle(); + sendGameCommand(new Command_Shuffle); } void Player::actDrawCard() { - client->drawCards(1); + sendGameCommand(new Command_DrawCards(-1, 1)); } void Player::actMulligan() @@ -342,21 +343,21 @@ void Player::actMulligan() const CardList &handCards = hand->getCards(); for (int i = 0; i < handCards.size(); i++) - client->moveCard(handCards.at(i)->getId(), "hand", "deck", 0); - client->shuffle(); - client->drawCards(mulliganCards--); + sendGameCommand(new Command_MoveCard(-1, "hand", handCards.at(i)->getId(), "deck", 0, -1, false)); + sendGameCommand(new Command_Shuffle); + sendGameCommand(new Command_DrawCards(-1, mulliganCards--)); } void Player::actDrawCards() { int number = QInputDialog::getInteger(0, tr("Draw cards"), tr("Number:")); if (number) - client->drawCards(number); + sendGameCommand(new Command_DrawCards(-1, number)); } void Player::actUntapAll() { - client->setCardAttr("table", -1, "tapped", "false"); +// client->setCardAttr("table", -1, "tapped", "false"); } void Player::actRollDie() @@ -364,25 +365,20 @@ void Player::actRollDie() bool ok; int sides = QInputDialog::getInteger(0, tr("Roll die"), tr("Number of sides:"), 20, 2, 1000, 1, &ok); if (ok) - client->rollDie(sides); + sendGameCommand(new Command_RollDie(-1, sides)); } void Player::actCreateToken() { QString cardname = QInputDialog::getText(0, tr("Create token"), tr("Name:")); if (!cardname.isEmpty()) - client->createToken("table", cardname, QString(), 0, 0); + sendGameCommand(new Command_CreateToken(-1, "table", cardname, QString(), 0, 0)); } void Player::actSayMessage() { QAction *a = qobject_cast(sender()); - client->say(a->text()); -} - -void Player::addZone(CardZone *z) -{ - zones.insert(z->getName(), z); + sendGameCommand(new Command_Say(-1, a->text())); } void Player::setCardAttrHelper(CardItem *card, const QString &aname, const QString &avalue, bool allCards) @@ -699,6 +695,56 @@ void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/ void Player::processPlayerInfo(ServerInfo_Player *info) { + for (int i = 0; i < info->getZoneList().size(); ++i) { + ServerInfo_Zone *zoneInfo = info->getZoneList()[i]; + CardZone *zone = zones.value(zoneInfo->getName(), 0); + if (!zone) + continue; + + zone->clearContents(); + const QList &cardList = zoneInfo->getCardList(); + if (cardList.isEmpty()) { + for (int j = 0; j < zoneInfo->getCardCount(); ++j) + zone->addCard(new CardItem, false, -1); + } else { + for (int j = 0; j < cardList.size(); ++j) { + CardItem *card = new CardItem; + card->processCardInfo(cardList[i]); + zone->addCard(card, false, cardList[i]->getX(), cardList[i]->getY()); + } + } + zone->reorganizeCards(); + } + + clearCounters(); + for (int i = 0; i < info->getCounterList().size(); ++i) { + ServerInfo_Counter *counterInfo = info->getCounterList().at(i); + addCounter(counterInfo->getId(), counterInfo->getName(), counterInfo->getColor(), counterInfo->getRadius(), counterInfo->getCount()); + } + + clearArrows(); + for (int i = 0; i < info->getArrowList().size(); ++i) { + ServerInfo_Arrow *arrowInfo = info->getArrowList().at(i); + const QMap &playerList = static_cast(parent())->getPlayers(); + Player *startPlayer = playerList.value(arrowInfo->getStartPlayerId(), 0); + Player *targetPlayer = playerList.value(arrowInfo->getTargetPlayerId(), 0); + if (!startPlayer || !targetPlayer) + continue; + CardZone *startZone = startPlayer->getZones().value(arrowInfo->getStartZone(), 0); + CardZone *targetZone = targetPlayer->getZones().value(arrowInfo->getTargetZone(), 0); + if (!startZone || !targetZone) + continue; + CardItem *startCard = startZone->getCard(arrowInfo->getStartCardId(), QString()); + CardItem *targetCard = targetZone->getCard(arrowInfo->getTargetCardId(), QString()); + if (!startCard || !targetCard) + continue; + addArrow(arrowInfo->getId(), startCard, targetCard, arrowInfo->getColor()); + } +} + +void Player::addZone(CardZone *z) +{ + zones.insert(z->getName(), z); } void Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value) @@ -783,3 +829,8 @@ void Player::rearrangeCounters() y += br.height() + padding; } } + +void Player::sendGameCommand(GameCommand *command) +{ + static_cast(parent())->sendGameCommand(command); +} diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 066421e4..a051a9e2 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -19,6 +19,7 @@ class CardZone; class TableZone; class HandZone; class ServerInfo_Player; +class GameCommand; class Player : public QObject, public QGraphicsItem { Q_OBJECT @@ -94,6 +95,8 @@ public: QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + void addZone(CardZone *z); + void addCounter(int counterId, const QString &name, QColor color, int radius, int value); void delCounter(int counterId); void clearCounters(); @@ -103,7 +106,6 @@ public: void clearArrows(); Client *client; - void addZone(CardZone *z); Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent); ~Player(); void retranslateUi(); @@ -120,6 +122,7 @@ public: void setActive(bool _active); void processPlayerInfo(ServerInfo_Player *info); + void sendGameCommand(GameCommand *command); }; #endif diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index bafdf565..497a5524 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -17,8 +17,8 @@ #include "dlg_load_remote_deck.h" #include "main.h" -TabGame::TabGame(Client *_client, int _gameId) - : client(_client), gameId(_gameId), localPlayerId(-1) +TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator) + : client(_client), gameId(_gameId), localPlayerId(_localPlayerId), spectator(_spectator) { zoneLayout = new ZoneViewLayout; scene = new GameScene(zoneLayout, this); @@ -124,20 +124,26 @@ void TabGame::processGameEvent(GameEvent *event) { switch (event->getItemId()) { case ItemId_Event_GameStart: eventGameStart(qobject_cast(event)); break; + case ItemId_Event_GameStateChanged: eventGameStateChanged(qobject_cast(event)); break; default: qDebug() << "unhandled game event"; } } -void TabGame::processGameJoinedEvent(Event_GameJoined *event) +void TabGame::sendGameCommand(GameCommand *command) { - localPlayerId = event->getPlayerId(); - spectator = event->getSpectator(); + command->setGameId(gameId); + client->sendCommand(command); +} +void TabGame::eventGameStateChanged(Event_GameStateChanged *event) +{ const QList &plList = event->getPlayerList(); for (int i = 0; i < plList.size(); ++i) { ServerInfo_Player *pl = plList[i]; - Player *newPlayer = addPlayer(pl->getPlayerId(), pl->getName()); - newPlayer->processPlayerInfo(pl); + Player *player = players.value(pl->getPlayerId(), 0); + if (!player) + player = addPlayer(pl->getPlayerId(), pl->getName()); + player->processPlayerInfo(pl); } } diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index cf333249..4e8220d4 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -19,7 +19,8 @@ class ZoneViewWidget; class PhasesToolbar; class ProtocolResponse; class GameEvent; -class Event_GameJoined; +class GameCommand; +class Event_GameStateChanged; class Event_GameStart; class Player; class CardZone; @@ -51,6 +52,7 @@ private: Player *addPlayer(int playerId, const QString &playerName); + void eventGameStateChanged(Event_GameStateChanged *event); void eventGameStart(Event_GameStart *event); signals: void playerAdded(Player *player); @@ -86,11 +88,12 @@ private slots: void readyStart(); void deckSelectFinished(ProtocolResponse *r); public: - TabGame(Client *_client, int _gameId); + TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator); void retranslateUi(); + const QMap &getPlayers() const { return players; } void processGameEvent(GameEvent *event); - void processGameJoinedEvent(Event_GameJoined *event); + void sendGameCommand(GameCommand *command); }; #endif diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 81855492..9f9c5109 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -101,10 +101,9 @@ void TabSupervisor::updatePingTime(int value, int max) void TabSupervisor::gameJoined(Event_GameJoined *event) { - TabGame *tab = new TabGame(client, event->getGameId()); + TabGame *tab = new TabGame(client, event->getGameId(), event->getPlayerId(), event->getSpectator()); addTab(tab, tr("Game %1").arg(event->getGameId())); gameTabs.insert(event->getGameId(), tab); - tab->processGameJoinedEvent(event); setCurrentWidget(tab); } diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 619b0a45..7a76016a 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -2,6 +2,7 @@ #include "tablezone.h" #include "player.h" #include "client.h" +#include "protocol_items.h" TableZone::TableZone(Player *_p, QGraphicsItem *parent) : CardZone(_p, "table", true, false, true, parent) @@ -67,7 +68,7 @@ void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &d void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown) { - player->client->moveCard(cardId, startZone->getName(), getName(), gridPoint.x(), gridPoint.y(), faceDown); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), gridPoint.x(), gridPoint.y(), faceDown)); } void TableZone::reorganizeCards() diff --git a/cockatrice/src/zoneviewwidget.cpp b/cockatrice/src/zoneviewwidget.cpp index c92fdde1..1597260d 100644 --- a/cockatrice/src/zoneviewwidget.cpp +++ b/cockatrice/src/zoneviewwidget.cpp @@ -1,11 +1,11 @@ #include - #include "zoneviewwidget.h" #include "carditem.h" #include "zoneviewzone.h" #include "player.h" #include "client.h" #include "gamescene.h" +#include "protocol_items.h" ZoneViewWidget::ZoneViewWidget(Player *_player, CardZone *_origZone, int numberCards, QGraphicsItem *parent) : QGraphicsWidget(parent, Qt::Tool | Qt::CustomizeWindowHint | Qt::WindowSystemMenuHint | Qt::WindowTitleHint/* | Qt::WindowCloseButtonHint*/), player(_player) @@ -91,10 +91,10 @@ void ZoneViewWidget::resizeToZoneContents() void ZoneViewWidget::closeEvent(QCloseEvent *event) { - player->client->stopDumpZone(player->getId(), zone->getName()); + player->sendGameCommand(new Command_StopDumpZone(-1, player->getId(), zone->getName())); if (shuffleCheckBox) if (shuffleCheckBox->isChecked()) - player->client->shuffle(); + player->sendGameCommand(new Command_Shuffle); emit closePressed(this); deleteLater(); event->accept(); diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index aab05631..fb708f77 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -2,6 +2,7 @@ #include "zoneviewzone.h" #include "player.h" #include "client.h" +#include "protocol_items.h" ZoneViewZone::ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards, QGraphicsItem *parent) : CardZone(_p, _origZone->getName(), false, false, true, parent, true), height(0), numberCards(_numberCards), origZone(_origZone), sortingEnabled(false) @@ -105,7 +106,7 @@ void ZoneViewZone::addCardImpl(CardItem *card, int x, int /*y*/) void ZoneViewZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &/*dropPoint*/, bool /*faceDown*/) { qDebug(QString("handleDropEvent id=%1").arg(cardId).toLatin1()); - player->client->moveCard(cardId, startZone->getName(), getName(), 0, 0); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), 0, 0, false)); } void ZoneViewZone::removeCard(int position) diff --git a/common/protocol.cpp b/common/protocol.cpp index 4dfe145c..4a4fb350 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -76,7 +76,7 @@ void ProtocolItem::initializeHash() itemNameHash.insert("generic_eventlist_games", Event_ListGames::newItem); itemNameHash.insert("generic_eventlist_chat_channels", Event_ListChatChannels::newItem); - itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem); + itemNameHash.insert("game_eventgame_state_changed", Event_GameStateChanged::newItem); itemNameHash.insert("chat_eventchat_list_players", Event_ChatListPlayers::newItem); } @@ -421,29 +421,18 @@ void Event_ListGames::writeElement(QXmlStreamWriter *xml) } } -Event_GameJoined::Event_GameJoined(int _gameId, int _playerId, bool _spectator, const QList &_playerList) - : GenericEvent("game_joined"), currentItem(0), readFinished(false), gameId(_gameId), playerId(_playerId), spectator(_spectator), playerList(_playerList) +Event_GameStateChanged::Event_GameStateChanged(int _gameId, const QList &_playerList) + : GameEvent("game_state_changed", _gameId, -1), currentItem(0), readFinished(false), playerList(_playerList) { - setParameter("game_id", gameId); - setParameter("player_id", playerId); - setParameter("spectator", spectator); } -Event_GameJoined::~Event_GameJoined() +Event_GameStateChanged::~Event_GameStateChanged() { for (int i = 0; i < playerList.size(); ++i) delete playerList[i]; } -void Event_GameJoined::extractParameters() -{ - GenericEvent::extractParameters(); - gameId = parameters["game_id"].toInt(); - playerId = parameters["player_id"].toInt(); - spectator = (parameters["spectator"] == "1"); -} - -bool Event_GameJoined::readElement(QXmlStreamReader *xml) +bool Event_GameStateChanged::readElement(QXmlStreamReader *xml) { if (currentItem) { if (currentItem->readElement(xml)) @@ -462,7 +451,7 @@ bool Event_GameJoined::readElement(QXmlStreamReader *xml) return true; } -void Event_GameJoined::writeElement(QXmlStreamWriter *xml) +void Event_GameStateChanged::writeElement(QXmlStreamWriter *xml) { for (int i = 0; i < playerList.size(); ++i) playerList[i]->writeElement(xml); diff --git a/common/protocol.h b/common/protocol.h index 8f822abb..ae6cb2be 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -22,7 +22,7 @@ enum ItemId { ItemId_Event_ListChatChannels = ItemId_Other + 200, ItemId_Event_ChatListPlayers = ItemId_Other + 201, ItemId_Event_ListGames = ItemId_Other + 202, - ItemId_Event_GameJoined = ItemId_Other + 203, + ItemId_Event_GameStateChanged = ItemId_Other + 203, ItemId_Response_DeckList = ItemId_Other + 300, ItemId_Response_DeckDownload = ItemId_Other + 301, ItemId_Response_DeckUpload = ItemId_Other + 302 @@ -41,6 +41,7 @@ protected: void setParameter(const QString &name, const QString &value) { parameters[name] = value; } void setParameter(const QString &name, bool value) { parameters[name] = (value ? "1" : "0"); } void setParameter(const QString &name, int value) { parameters[name] = QString::number(value); } + void setParameter(const QString &name, const QColor &value) { parameters[name] = QString::number(ColorConverter::colorToInt(value)); } virtual void extractParameters() { } virtual QString getItemType() const = 0; @@ -127,6 +128,11 @@ public: setParameter("game_id", gameId); } int getGameId() const { return gameId; } + void setGameId(int _gameId) + { + gameId = _gameId; + setParameter("game_id", gameId); + } }; class Command_DeckUpload : public Command { @@ -331,26 +337,18 @@ public: void writeElement(QXmlStreamWriter *xml); }; -class Event_GameJoined : public GenericEvent { +class Event_GameStateChanged : public GameEvent { Q_OBJECT private: SerializableItem *currentItem; bool readFinished; - int gameId; - int playerId; - bool spectator; QList playerList; -protected: - void extractParameters(); public: - Event_GameJoined(int _gameId = -1, int _playerId = -1, bool _spectator = false, const QList &_playerList = QList()); - ~Event_GameJoined(); - int getGameId() const { return gameId; } - int getPlayerId() const { return playerId; } - bool getSpectator() const { return spectator; } + Event_GameStateChanged(int _gameId = -1, const QList &_playerList = QList()); + ~Event_GameStateChanged(); const QList &getPlayerList() const { return playerList; } - static ProtocolItem *newItem() { return new Event_GameJoined; } - int getItemId() const { return ItemId_Event_GameJoined; } + static ProtocolItem *newItem() { return new Event_GameStateChanged; } + int getItemId() const { return ItemId_Event_GameStateChanged; } bool readElement(QXmlStreamReader *xml); void writeElement(QXmlStreamWriter *xml); diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp index f16f0883..cf7451fa 100644 --- a/common/protocol_datastructures.cpp +++ b/common/protocol_datastructures.cpp @@ -2,18 +2,6 @@ #include #include -class ColorConverter { -public: - static int colorToInt(const QColor &color) - { - return color.red() * 65536 + color.green() * 256 + color.blue(); - } - static QColor colorFromInt(int colorValue) - { - return QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256); - } -}; - ServerInfo_Player::~ServerInfo_Player() { for (int i = 0; i < zoneList.size(); ++i) diff --git a/common/protocol_datastructures.h b/common/protocol_datastructures.h index 251278fd..32fda8b4 100644 --- a/common/protocol_datastructures.h +++ b/common/protocol_datastructures.h @@ -20,6 +20,18 @@ 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 ColorConverter { +public: + static int colorToInt(const QColor &color) + { + return color.red() * 65536 + color.green() * 256 + color.blue(); + } + static QColor colorFromInt(int colorValue) + { + return QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256); + } +}; + class SerializableItem { protected: SerializableItem *currentItem; diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h index 5b9a6764..981368aa 100644 --- a/common/protocol_item_ids.h +++ b/common/protocol_item_ids.h @@ -39,23 +39,23 @@ ItemId_Event_Leave = 1037, ItemId_Event_DeckSelect = 1038, ItemId_Event_GameClosed = 1039, ItemId_Event_ReadyStart = 1040, -ItemId_Event_SetupZones = 1041, -ItemId_Event_GameStart = 1042, -ItemId_Event_Shuffle = 1043, -ItemId_Event_RollDie = 1044, -ItemId_Event_MoveCard = 1045, -ItemId_Event_CreateToken = 1046, -ItemId_Event_CreateArrow = 1047, -ItemId_Event_DeleteArrow = 1048, -ItemId_Event_SetCardAttr = 1049, -ItemId_Event_AddCounter = 1050, -ItemId_Event_SetCounter = 1051, -ItemId_Event_DelCounter = 1052, -ItemId_Event_SetActivePlayer = 1053, -ItemId_Event_SetActivePhase = 1054, -ItemId_Event_DumpZone = 1055, -ItemId_Event_StopDumpZone = 1056, -ItemId_Event_ServerMessage = 1057, +ItemId_Event_GameStart = 1041, +ItemId_Event_Shuffle = 1042, +ItemId_Event_RollDie = 1043, +ItemId_Event_MoveCard = 1044, +ItemId_Event_CreateToken = 1045, +ItemId_Event_CreateArrow = 1046, +ItemId_Event_DeleteArrow = 1047, +ItemId_Event_SetCardAttr = 1048, +ItemId_Event_AddCounter = 1049, +ItemId_Event_SetCounter = 1050, +ItemId_Event_DelCounter = 1051, +ItemId_Event_SetActivePlayer = 1052, +ItemId_Event_SetActivePhase = 1053, +ItemId_Event_DumpZone = 1054, +ItemId_Event_StopDumpZone = 1055, +ItemId_Event_ServerMessage = 1056, +ItemId_Event_GameJoined = 1057, ItemId_Event_ChatJoinChannel = 1058, ItemId_Event_ChatLeaveChannel = 1059, ItemId_Event_ChatSay = 1060, diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 64218fac..b76209ab 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -201,7 +201,7 @@ void Command_CreateToken::extractParameters() x = parameters["x"].toInt(); y = parameters["y"].toInt(); } -Command_CreateArrow::Command_CreateArrow(int _gameId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, int _color) +Command_CreateArrow::Command_CreateArrow(int _gameId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, const QColor &_color) : GameCommand("create_arrow", _gameId), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { setParameter("start_player_id", startPlayerId); @@ -221,7 +221,7 @@ void Command_CreateArrow::extractParameters() targetPlayerId = parameters["target_player_id"].toInt(); targetZone = parameters["target_zone"]; targetCardId = parameters["target_card_id"].toInt(); - color = parameters["color"].toInt(); + color = ColorConverter::colorFromInt(parameters["color"].toInt()); } Command_DeleteArrow::Command_DeleteArrow(int _gameId, int _arrowId) : GameCommand("delete_arrow", _gameId), arrowId(_arrowId) @@ -265,7 +265,7 @@ void Command_IncCounter::extractParameters() counterId = parameters["counter_id"].toInt(); delta = parameters["delta"].toInt(); } -Command_AddCounter::Command_AddCounter(int _gameId, const QString &_counterName, int _color, int _radius, int _value) +Command_AddCounter::Command_AddCounter(int _gameId, const QString &_counterName, const QColor &_color, int _radius, int _value) : GameCommand("add_counter", _gameId), counterName(_counterName), color(_color), radius(_radius), value(_value) { setParameter("counter_name", counterName); @@ -277,7 +277,7 @@ void Command_AddCounter::extractParameters() { GameCommand::extractParameters(); counterName = parameters["counter_name"]; - color = parameters["color"].toInt(); + color = ColorConverter::colorFromInt(parameters["color"].toInt()); radius = parameters["radius"].toInt(); value = parameters["value"].toInt(); } @@ -391,18 +391,6 @@ Event_ReadyStart::Event_ReadyStart(int _gameId, int _playerId) : GameEvent("ready_start", _gameId, _playerId) { } -Event_SetupZones::Event_SetupZones(int _gameId, int _playerId, int _deckSize, int _sbSize) - : GameEvent("setup_zones", _gameId, _playerId), deckSize(_deckSize), sbSize(_sbSize) -{ - setParameter("deck_size", deckSize); - setParameter("sb_size", sbSize); -} -void Event_SetupZones::extractParameters() -{ - GameEvent::extractParameters(); - deckSize = parameters["deck_size"].toInt(); - sbSize = parameters["sb_size"].toInt(); -} Event_GameStart::Event_GameStart(int _gameId, int _playerId) : GameEvent("game_start", _gameId, _playerId) { @@ -467,7 +455,7 @@ void Event_CreateToken::extractParameters() x = parameters["x"].toInt(); y = parameters["y"].toInt(); } -Event_CreateArrow::Event_CreateArrow(int _gameId, int _playerId, int _arrowId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, int _color) +Event_CreateArrow::Event_CreateArrow(int _gameId, int _playerId, int _arrowId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, const QColor &_color) : GameEvent("create_arrow", _gameId, _playerId), arrowId(_arrowId), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { setParameter("arrow_id", arrowId); @@ -489,7 +477,7 @@ void Event_CreateArrow::extractParameters() targetPlayerId = parameters["target_player_id"].toInt(); targetZone = parameters["target_zone"]; targetCardId = parameters["target_card_id"].toInt(); - color = parameters["color"].toInt(); + color = ColorConverter::colorFromInt(parameters["color"].toInt()); } Event_DeleteArrow::Event_DeleteArrow(int _gameId, int _playerId, int _arrowId) : GameEvent("delete_arrow", _gameId, _playerId), arrowId(_arrowId) @@ -517,7 +505,7 @@ void Event_SetCardAttr::extractParameters() attrName = parameters["attr_name"]; attrValue = parameters["attr_value"]; } -Event_AddCounter::Event_AddCounter(int _gameId, int _playerId, int _counterId, const QString &_counterName, int _color, int _radius, int _value) +Event_AddCounter::Event_AddCounter(int _gameId, int _playerId, int _counterId, const QString &_counterName, const QColor &_color, int _radius, int _value) : GameEvent("add_counter", _gameId, _playerId), counterId(_counterId), counterName(_counterName), color(_color), radius(_radius), value(_value) { setParameter("counter_id", counterId); @@ -531,7 +519,7 @@ void Event_AddCounter::extractParameters() GameEvent::extractParameters(); counterId = parameters["counter_id"].toInt(); counterName = parameters["counter_name"]; - color = parameters["color"].toInt(); + color = ColorConverter::colorFromInt(parameters["color"].toInt()); radius = parameters["radius"].toInt(); value = parameters["value"].toInt(); } @@ -613,6 +601,20 @@ void Event_ServerMessage::extractParameters() GenericEvent::extractParameters(); message = parameters["message"]; } +Event_GameJoined::Event_GameJoined(int _gameId, int _playerId, bool _spectator) + : GenericEvent("game_joined"), gameId(_gameId), playerId(_playerId), spectator(_spectator) +{ + setParameter("game_id", gameId); + setParameter("player_id", playerId); + setParameter("spectator", spectator); +} +void Event_GameJoined::extractParameters() +{ + GenericEvent::extractParameters(); + gameId = parameters["game_id"].toInt(); + playerId = parameters["player_id"].toInt(); + spectator = (parameters["spectator"] == "1"); +} Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, const QString &_playerName) : ChatEvent("chat_join_channel", _channel), playerName(_playerName) { @@ -687,7 +689,6 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("game_eventdeck_select", Event_DeckSelect::newItem); itemNameHash.insert("game_eventgame_closed", Event_GameClosed::newItem); itemNameHash.insert("game_eventready_start", Event_ReadyStart::newItem); - itemNameHash.insert("game_eventsetup_zones", Event_SetupZones::newItem); itemNameHash.insert("game_eventgame_start", Event_GameStart::newItem); itemNameHash.insert("game_eventshuffle", Event_Shuffle::newItem); itemNameHash.insert("game_eventroll_die", Event_RollDie::newItem); @@ -704,6 +705,7 @@ void ProtocolItem::initializeHashAuto() itemNameHash.insert("game_eventdump_zone", Event_DumpZone::newItem); itemNameHash.insert("game_eventstop_dump_zone", Event_StopDumpZone::newItem); itemNameHash.insert("generic_eventserver_message", Event_ServerMessage::newItem); + itemNameHash.insert("generic_eventgame_joined", Event_GameJoined::newItem); itemNameHash.insert("chat_eventchat_join_channel", Event_ChatJoinChannel::newItem); itemNameHash.insert("chat_eventchat_leave_channel", Event_ChatLeaveChannel::newItem); itemNameHash.insert("chat_eventchat_say", Event_ChatSay::newItem); diff --git a/common/protocol_items.dat b/common/protocol_items.dat index db1f45ca..643e8c5c 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -19,12 +19,12 @@ 2:draw_cards:i,number 2:move_card:s,start_zone:i,card_id:s,target_zone:i,x:i,y:b,face_down 2:create_token:s,zone:s,card_name:s,pt:i,x:i,y -2:create_arrow:i,start_player_id:s,start_zone:i,start_card_id:i,target_player_id:s,target_zone:i,target_card_id:i,color +2:create_arrow:i,start_player_id:s,start_zone:i,start_card_id:i,target_player_id:s,target_zone:i,target_card_id:c,color 2:delete_arrow:i,arrow_id 2:set_card_attr:s,zone:i,card_id:s,attr_name:s,attr_value 2:ready_start 2:inc_counter:i,counter_id:i,delta -2:add_counter:s,counter_name:i,color:i,radius:i,value +2:add_counter:s,counter_name:c,color:i,radius:i,value 2:set_counter:i,counter_id:i,value 2:del_counter:i,counter_id 2:next_turn @@ -38,16 +38,15 @@ 3:deck_select:i,deck_id 3:game_closed 3:ready_start -3:setup_zones:i,deck_size:i,sb_size 3:game_start 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:b,face_down 3:create_token:s,zone:i,card_id:s,card_name:s,pt:i,x:i,y -3:create_arrow:i,arrow_id:i,start_player_id:s,start_zone:i,start_card_id:i,target_player_id:s,target_zone:i,target_card_id:i,color +3:create_arrow:i,arrow_id:i,start_player_id:s,start_zone:i,start_card_id:i,target_player_id:s,target_zone:i,target_card_id:c,color 3:delete_arrow:i,arrow_id 3:set_card_attr:s,zone:i,card_id:s,attr_name:s,attr_value -3:add_counter:i,counter_id:s,counter_name:i,color:i,radius:i,value +3:add_counter:i,counter_id:s,counter_name:c,color:i,radius:i,value 3:set_counter:i,counter_id:i,value 3:del_counter:i,counter_id 3:set_active_player:i,active_player_id @@ -55,6 +54,7 @@ 3:dump_zone:i,zone_owner_id:s,zone:i,number_cards 3:stop_dump_zone:i,zone_owner_id:s,zone 4:server_message:s,message +4:game_joined:i,game_id:i,player_id:b,spectator 5:chat_join_channel:s,player_name 5:chat_leave_channel:s,player_name 5:chat_say:s,player_name:s,message \ No newline at end of file diff --git a/common/protocol_items.h b/common/protocol_items.h index ec839ac2..c5e3cfa4 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -268,16 +268,16 @@ private: int targetPlayerId; QString targetZone; int targetCardId; - int color; + QColor color; public: - Command_CreateArrow(int _gameId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, int _color = -1); + Command_CreateArrow(int _gameId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, const QColor &_color = QColor()); int getStartPlayerId() const { return startPlayerId; } QString getStartZone() const { return startZone; } int getStartCardId() const { return startCardId; } int getTargetPlayerId() const { return targetPlayerId; } QString getTargetZone() const { return targetZone; } int getTargetCardId() const { return targetCardId; } - int getColor() const { return color; } + QColor getColor() const { return color; } static ProtocolItem *newItem() { return new Command_CreateArrow; } int getItemId() const { return ItemId_Command_CreateArrow; } protected: @@ -339,13 +339,13 @@ class Command_AddCounter : public GameCommand { Q_OBJECT private: QString counterName; - int color; + QColor color; int radius; int value; public: - Command_AddCounter(int _gameId = -1, const QString &_counterName = QString(), int _color = -1, int _radius = -1, int _value = -1); + Command_AddCounter(int _gameId = -1, const QString &_counterName = QString(), const QColor &_color = QColor(), int _radius = -1, int _value = -1); QString getCounterName() const { return counterName; } - int getColor() const { return color; } + QColor getColor() const { return color; } int getRadius() const { return radius; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Command_AddCounter; } @@ -499,20 +499,6 @@ public: static ProtocolItem *newItem() { return new Event_ReadyStart; } int getItemId() const { return ItemId_Event_ReadyStart; } }; -class Event_SetupZones : public GameEvent { - Q_OBJECT -private: - int deckSize; - int sbSize; -public: - Event_SetupZones(int _gameId = -1, int _playerId = -1, int _deckSize = -1, int _sbSize = -1); - int getDeckSize() const { return deckSize; } - int getSbSize() const { return sbSize; } - static ProtocolItem *newItem() { return new Event_SetupZones; } - int getItemId() const { return ItemId_Event_SetupZones; } -protected: - void extractParameters(); -}; class Event_GameStart : public GameEvent { Q_OBJECT private: @@ -601,9 +587,9 @@ private: int targetPlayerId; QString targetZone; int targetCardId; - int color; + QColor color; public: - Event_CreateArrow(int _gameId = -1, int _playerId = -1, int _arrowId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, int _color = -1); + Event_CreateArrow(int _gameId = -1, int _playerId = -1, int _arrowId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, const QColor &_color = QColor()); int getArrowId() const { return arrowId; } int getStartPlayerId() const { return startPlayerId; } QString getStartZone() const { return startZone; } @@ -611,7 +597,7 @@ public: int getTargetPlayerId() const { return targetPlayerId; } QString getTargetZone() const { return targetZone; } int getTargetCardId() const { return targetCardId; } - int getColor() const { return color; } + QColor getColor() const { return color; } static ProtocolItem *newItem() { return new Event_CreateArrow; } int getItemId() const { return ItemId_Event_CreateArrow; } protected: @@ -652,14 +638,14 @@ class Event_AddCounter : public GameEvent { private: int counterId; QString counterName; - int color; + QColor color; int radius; int value; public: - Event_AddCounter(int _gameId = -1, int _playerId = -1, int _counterId = -1, const QString &_counterName = QString(), int _color = -1, int _radius = -1, int _value = -1); + Event_AddCounter(int _gameId = -1, int _playerId = -1, int _counterId = -1, const QString &_counterName = QString(), const QColor &_color = QColor(), int _radius = -1, int _value = -1); int getCounterId() const { return counterId; } QString getCounterName() const { return counterName; } - int getColor() const { return color; } + QColor getColor() const { return color; } int getRadius() const { return radius; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Event_AddCounter; } @@ -759,6 +745,22 @@ public: protected: void extractParameters(); }; +class Event_GameJoined : public GenericEvent { + Q_OBJECT +private: + int gameId; + int playerId; + bool spectator; +public: + Event_GameJoined(int _gameId = -1, int _playerId = -1, bool _spectator = false); + int getGameId() const { return gameId; } + int getPlayerId() const { return playerId; } + bool getSpectator() const { return spectator; } + static ProtocolItem *newItem() { return new Event_GameJoined; } + int getItemId() const { return ItemId_Event_GameJoined; } +protected: + void extractParameters(); +}; class Event_ChatJoinChannel : public ChatEvent { Q_OBJECT private: diff --git a/common/protocol_mc.pl b/common/protocol_mc.pl index 17a70b7a..b4869222 100755 --- a/common/protocol_mc.pl +++ b/common/protocol_mc.pl @@ -102,6 +102,11 @@ while () { $constructorParamsH .= "int _$prettyVarName = -1"; $constructorParamsCpp .= "int _$prettyVarName"; $paramStr5 .= "\t$prettyVarName = parameters[\"$value\"].toInt();\n"; + } elsif ($key eq 'c') { + $dataType = 'QColor'; + $constructorParamsH .= "const QColor &_$prettyVarName = QColor()"; + $constructorParamsCpp .= "const QColor &_$prettyVarName"; + $paramStr5 .= "\t$prettyVarName = ColorConverter::colorFromInt(parameters[\"$value\"].toInt());\n"; } ($prettyVarName2 = $prettyVarName) =~ s/^(.)/\U$1\E/; $paramStr4 .= "\t$dataType get$prettyVarName2() const { return $prettyVarName; }\n"; diff --git a/common/server_arrow.h b/common/server_arrow.h index 216d6246..67b5b1cd 100644 --- a/common/server_arrow.h +++ b/common/server_arrow.h @@ -1,20 +1,22 @@ #ifndef SERVER_ARROW_H #define SERVER_ARROW_H +#include + class Server_Card; class Server_Arrow { private: int id; Server_Card *startCard, *targetCard; - int color; + QColor color; public: - Server_Arrow(int _id, Server_Card *_startCard, Server_Card *_targetCard, int _color) + Server_Arrow(int _id, Server_Card *_startCard, Server_Card *_targetCard, const QColor &_color) : id(_id), startCard(_startCard), targetCard(_targetCard), color(_color) { } int getId() const { return id; } Server_Card *getStartCard() const { return startCard; } Server_Card *getTargetCard() const { return targetCard; } - int getColor() const { return color; } + QColor getColor() const { return color; } }; -#endif \ No newline at end of file +#endif diff --git a/common/server_counter.h b/common/server_counter.h index 30568ada..aa3fe071 100644 --- a/common/server_counter.h +++ b/common/server_counter.h @@ -21,20 +21,21 @@ #define SERVER_COUNTER_H #include +#include class Server_Counter { protected: int id; QString name; - int color; + QColor color; int radius; int count; public: - Server_Counter(int _id, const QString &_name, int _color, int _radius, int _count = 0) : id(_id), name(_name), color(_color), radius(_radius), count(_count) { } + Server_Counter(int _id, const QString &_name, const QColor &_color, int _radius, int _count = 0) : id(_id), name(_name), color(_color), radius(_radius), count(_count) { } ~Server_Counter() { } int getId() const { return id; } QString getName() const { return name; } - int getColor() const { return color; } + QColor getColor() const { return color; } int getRadius() const { return radius; } int getCount() const { return count; } void setCount(int _count) { count = _count; } diff --git a/common/server_game.cpp b/common/server_game.cpp index 0c89a429..f5e0b910 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -54,8 +54,16 @@ void Server_Game::startGameIfReady() return; QMapIterator playerIterator(players); while (playerIterator.hasNext()) - if (playerIterator.next().value()->getStatus() != StatusReadyStart) + if (!playerIterator.next().value()->getReadyStart()) return; + playerIterator.toFront(); + while (playerIterator.hasNext()) + playerIterator.next().value()->setupZones(); + playerIterator.toFront(); + while (playerIterator.hasNext()) { + Server_Player *player = playerIterator.next().value(); + player->sendProtocolItem(new Event_GameStateChanged(gameId, getGameState(player))); + } /* QSqlQuery query; query.prepare("insert into games (id, descr, password, time_started) values(:id, :descr, :password, now())"); @@ -71,8 +79,6 @@ void Server_Game::startGameIfReady() query.bindValue(":id", gameId); query.bindValue(":player", player->getPlayerName()); query.exec(); - - player->setupZones(); } */ gameStarted = true; @@ -160,7 +166,7 @@ void Server_Game::setActivePhase(int _activePhase) sendGameEvent(new Event_SetActivePhase(-1, -1, activePhase)); } -QList Server_Game::getGameState() const +QList Server_Game::getGameState(Server_Player *playerWhosAsking) const { QList result; QMapIterator playerIterator(players); @@ -195,10 +201,15 @@ QList Server_Game::getGameState() const while (zoneIterator.hasNext()) { Server_CardZone *zone = zoneIterator.next().value(); QList cardList; - QListIterator cardIterator(zone->cards); - while (cardIterator.hasNext()) { - Server_Card *card = cardIterator.next(); - cardList.append(new ServerInfo_Card(card->getId(), card->getName(), card->getX(), card->getY(), card->getCounters(), card->getTapped(), card->getAttacking(), card->getAnnotation())); + if ( + ((playerWhosAsking == player) && (zone->getType() != HiddenZone)) + || ((playerWhosAsking != player) && (zone->getType() == PublicZone)) + ) { + QListIterator cardIterator(zone->cards); + while (cardIterator.hasNext()) { + Server_Card *card = cardIterator.next(); + cardList.append(new ServerInfo_Card(card->getId(), card->getName(), card->getX(), card->getY(), card->getCounters(), card->getTapped(), card->getAttacking(), card->getAnnotation())); + } } zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList)); } diff --git a/common/server_game.h b/common/server_game.h index ace0a0c5..74ef0bf6 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -65,7 +65,7 @@ public: void setActivePlayer(int _activePlayer); void setActivePhase(int _activePhase); - QList getGameState() const; + QList getGameState(Server_Player *playerWhosAsking) const; void sendGameEvent(GameEvent *event); }; diff --git a/common/server_player.cpp b/common/server_player.cpp index c9af122a..9b523799 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -10,7 +10,7 @@ #include "decklist.h" Server_Player::Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler) - : game(_game), handler(_handler), deck(0), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0), PlayerStatus(StatusNormal) + : game(_game), handler(_handler), deck(0), playerId(_playerId), playerName(_playerName), spectator(_spectator), nextCardId(0) { } @@ -85,9 +85,6 @@ void Server_Player::setupZones() } } deckZone->shuffle(); - - PlayerStatus = StatusPlaying; - game->sendGameEvent(new Event_SetupZones(-1, playerId, deckZone->cards.size(), sbZone->cards.size())); } void Server_Player::clearZones() diff --git a/common/server_player.h b/common/server_player.h index d74a239e..df14c131 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -14,8 +14,6 @@ class Server_Arrow; class Server_ProtocolHandler; class ProtocolItem; -enum PlayerStatusEnum { StatusNormal, StatusSubmitDeck, StatusReadyStart, StatusPlaying }; - class Server_Player : public QObject { Q_OBJECT private: @@ -30,14 +28,14 @@ private: bool spectator; int nextCardId; void clearZones(); - PlayerStatusEnum PlayerStatus; + bool readyStart; public: Server_Player(Server_Game *_game, int _playerId, const QString &_playerName, bool _spectator, Server_ProtocolHandler *_handler); void setProtocolHandler(Server_ProtocolHandler *_handler) { handler = _handler; } - void setStatus(PlayerStatusEnum _status) { PlayerStatus = _status; } void setPlayerId(int _id) { playerId = _id; } - PlayerStatusEnum getStatus() { return PlayerStatus; } + bool getReadyStart() const { return readyStart; } + void setReadyStart(bool _readyStart) { readyStart = _readyStart; } int getPlayerId() const { return playerId; } bool getSpectator() const { return spectator; } QString getPlayerName() const { return playerName; } diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 63165edf..ba802831 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -218,9 +218,11 @@ ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames * /*cmd*/) ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd) { Server_Game *game = server->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), this); - games.insert(game->getGameId(), QPair(game, game->getCreator())); + Server_Player *creator = game->getCreator(); + games.insert(game->getGameId(), QPair(game, creator)); - enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getCreator()->getPlayerId(), false, game->getGameState())); + enqueueProtocolItem(new Event_GameJoined(game->getGameId(), creator->getPlayerId(), false)); + enqueueProtocolItem(new Event_GameStateChanged(game->getGameId(), game->getGameState(creator))); return RespOk; } @@ -234,7 +236,8 @@ ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd) if (result == RespOk) { Server_Player *player = g->addPlayer(this, cmd->getSpectator()); games.insert(cmd->getGameId(), QPair(g, player)); - enqueueProtocolItem(new Event_GameJoined(cmd->getGameId(), player->getPlayerId(), cmd->getSpectator(), g->getGameState())); + enqueueProtocolItem(new Event_GameJoined(cmd->getGameId(), player->getPlayerId(), cmd->getSpectator())); + enqueueProtocolItem(new Event_GameStateChanged(cmd->getGameId(), g->getGameState(player))); } return result; } @@ -346,20 +349,13 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, Server_G // The player does not get to see which card he moved if it moves between two parts of hidden zones which // are not being looked at. - QString privateCardId = QString::number(card->getId()); + int privateCardId = card->getId(); if (!targetBeingLookedAt && !sourceBeingLookedAt) { - privateCardId = QString(); + privateCardId = -1; privateCardName = QString(); } -/* player->privateEvent(QString("move_card|%1|%2|%3|%4|%5|%6|%7|%8").arg(privateCardId) - .arg(privateCardName) - .arg(startzone->getName()) - .arg(position) - .arg(targetzone->getName()) - .arg(x) - .arg(y) - .arg(facedown ? 1 : 0)); -*/ + player->sendProtocolItem(new Event_MoveCard(game->getGameId(), player->getPlayerId(), privateCardId, privateCardName, startzone->getName(), position, targetzone->getName(), x, y, facedown)); + // 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, // all cards are equal. @@ -368,22 +364,11 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, Server_G if ((targetzone->getType() == HiddenZone) && ((targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1))) x = -1; -/* if ((startzone->getType() == Server_CardZone::PublicZone) || (targetzone->getType() == Server_CardZone::PublicZone)) - game->broadcastEvent(QString("move_card|%1|%2|%3|%4|%5|%6|%7|%8").arg(card->getId()) - .arg(publicCardName) - .arg(startzone->getName()) - .arg(position) - .arg(targetzone->getName()) - .arg(x) - .arg(y) - .arg(facedown ? 1 : 0), player); + if ((startzone->getType() == PublicZone) || (targetzone->getType() == PublicZone)) + game->sendGameEvent(new Event_MoveCard(-1, player->getPlayerId(), card->getId(), publicCardName, startzone->getName(), position, targetzone->getName(), x, y, facedown)); else - game->broadcastEvent(QString("move_card|||%1|%2|%3|%4|%5|0").arg(startzone->getName()) - .arg(position) - .arg(targetzone->getName()) - .arg(x) - .arg(y), player); -*/ + game->sendGameEvent(new Event_MoveCard(-1, player->getPlayerId(), -1, QString(), startzone->getName(), position, targetzone->getName(), x, y, false)); + // If the card was moved to another zone, delete all arrows from and to the card if (startzone != targetzone) { const QList &players = game->getPlayers(); @@ -493,7 +478,7 @@ ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart * /*cmd*/, if (!player->getDeck()) return RespContextError; - player->setStatus(StatusReadyStart); + player->setReadyStart(true); game->sendGameEvent(new Event_ReadyStart(-1, player->getPlayerId())); game->startGameIfReady(); return RespOk;