From 0d4717f40bcc844a2f9d3183f5190e9f8370349e Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Wed, 25 Nov 2009 22:58:20 +0100 Subject: [PATCH] new code --- cockatrice/cockatrice.pro | 3 +- cockatrice/src/client.cpp | 175 ------------------- cockatrice/src/game.cpp | 21 +-- cockatrice/src/game.h | 1 - cockatrice/src/messagelogwidget.cpp | 6 +- cockatrice/src/messagelogwidget.h | 3 +- cockatrice/src/player.cpp | 8 +- cockatrice/src/player.h | 7 +- cockatrice/src/tab_chatchannel.cpp | 2 +- cockatrice/src/tab_game.cpp | 43 ++++- cockatrice/src/tab_game.h | 50 +++++- cockatrice/src/tab_supervisor.cpp | 3 + cockatrice/src/zoneviewzone.cpp | 2 +- cockatrice/src/zoneviewzone.h | 2 +- common/protocol.cpp | 101 ++++++----- common/protocol.h | 32 +++- common/protocol_datastructures.cpp | 257 ++++++++++++++++++++++++++++ common/protocol_datastructures.h | 115 ++++++++----- common/protocol_item_ids.h | 9 +- common/protocol_items.cpp | 13 -- common/protocol_items.dat | 1 - common/protocol_items.h | 14 -- common/server_cardzone.h | 11 +- common/server_game.cpp | 51 ++++++ common/server_game.h | 1 + common/server_player.cpp | 12 +- common/server_protocolhandler.cpp | 22 +-- servatrice/servatrice.pro | 1 + 28 files changed, 591 insertions(+), 375 deletions(-) create mode 100644 common/protocol_datastructures.cpp diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index c8700a78..44460547 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -99,7 +99,8 @@ SOURCES += src/counter.cpp \ src/deckview.cpp \ ../common/decklist.cpp \ ../common/protocol.cpp \ - ../common/protocol_items.cpp + ../common/protocol_items.cpp \ + ../common/protocol_datastructures.cpp TRANSLATIONS += translations/cockatrice_de.ts translations/cockatrice_en.ts CONFIG += qt debug diff --git a/cockatrice/src/client.cpp b/cockatrice/src/client.cpp index f7dc0b2f..587114fd 100644 --- a/cockatrice/src/client.cpp +++ b/cockatrice/src/client.cpp @@ -99,170 +99,6 @@ void Client::readData() } } } -/* - while (socket->canReadLine()) { - QString line = QString(socket->readLine()).trimmed(); - - if (line.isNull()) - break; - qDebug(QString("<< %1").arg(line).toLatin1()); - QStringList values = line.split("|"); - QString prefix = values.takeFirst(); - // prefix is one of {welcome, private, public, resp, list_games, list_players, list_counters, list_zones, dump_zone} - if ((prefix == "private") || (prefix == "public")) { - ServerEventData event(line); - emit gameEvent(event); - } else if (prefix == "chat") { - emit chatEvent(ChatEventData(line)); - } else if (prefix == "resp") { - if (values.size() != 2) { - qDebug("Client::parseCommand: Invalid response"); - continue; - } - bool ok; - int msgid = values.takeFirst().toInt(&ok); - PendingCommand *pc = pendingCommands.value(msgid, 0); - if (!ok || !pc) { - qDebug("Client::parseCommand: Invalid msgid"); - continue; - } - ServerResponse resp; - if (values[0] == "ok") - resp = RespOk; - else if (values[0] == "name_not_found") - resp = RespNameNotFound; - else if (values[0] == "login_needed") - resp = RespLoginNeeded; - else if (values[0] == "syntax") - resp = RespSyntaxError; - else if (values[0] == "context") - resp = RespContextError; - else if (values[0] == "password") - resp = RespPasswordWrong; - else if (values[0] == "spectators_not_allowed") - resp = RespSpectatorsNotAllowed; - else - resp = RespInvalid; - pc->responseReceived(resp); - } else if (prefix == "list_games") { - if (values.size() != 8) { - emit protocolError(); - continue; - } - emit gameListEvent(ServerGame(values[0].toInt(), values[5], values[1], values[2].toInt(), values[3].toInt(), values[4].toInt(), values[6].toInt(), values[7].toInt())); - } else if (prefix == "welcome") { - if (values.size() != 2) { - emit protocolError(); - disconnectFromServer(); - } else if (values[0].toInt() != protocolVersion) { - emit protocolVersionMismatch(); - disconnectFromServer(); - } else { - emit welcomeMsgReceived(values[1]); - setStatus(StatusLoggingIn); - login(playerName, password); - } - } else if (prefix == "list_players") { - if (values.size() != 4) { - emit protocolError(); - continue; - } - int cmdid = values.takeFirst().toInt(); - PendingCommand *pc = pendingCommands.value(cmdid, 0); - ServerPlayer sp(values[0].toInt(), values[1], values[2].toInt()); - - PendingCommand_ListPlayers *pcLP = qobject_cast(pc); - if (pcLP) - pcLP->addPlayer(sp); - else { - PendingCommand_DumpAll *pcDA = qobject_cast(pc); - if (pcDA) - pcDA->addPlayer(sp); - else - emit protocolError(); - } - } else if (prefix == "dump_zone") { - if (values.size() != 11) { - emit protocolError(); - continue; - } - int cmdid = values.takeFirst().toInt(); - PendingCommand *pc = pendingCommands.value(cmdid, 0); - ServerZoneCard szc(values[0].toInt(), values[1], values[2].toInt(), values[3], values[4].toInt(), values[5].toInt(), values[6].toInt(), values[7] == "1", values[8] == "1", values[9]); - - PendingCommand_DumpZone *pcDZ = qobject_cast(pc); - if (pcDZ) - pcDZ->addCard(szc); - else { - PendingCommand_DumpAll *pcDA = qobject_cast(pc); - if (pcDA) - pcDA->addCard(szc); - else - emit protocolError(); - } - } else if (prefix == "list_zones") { - if (values.size() != 6) { - emit protocolError(); - continue; - } - int cmdid = values.takeFirst().toInt(); - PendingCommand *pc = pendingCommands.value(cmdid, 0); - ServerZone::ZoneType type; - if (values[2] == "private") - type = ServerZone::PrivateZone; - else if (values[2] == "hidden") - type = ServerZone::HiddenZone; - else - type = ServerZone::PublicZone; - ServerZone sz(values[0].toInt(), values[1], type, values[3] == "1", values[4].toInt()); - - PendingCommand_ListZones *pcLZ = qobject_cast(pc); - if (pcLZ) - pcLZ->addZone(sz); - else { - PendingCommand_DumpAll *pcDA = qobject_cast(pc); - if (pcDA) - pcDA->addZone(sz); - else - emit protocolError(); - } - } else if (prefix == "list_counters") { - if (values.size() != 7) { - emit protocolError(); - continue; - } - int cmdid = values.takeFirst().toInt(); - PendingCommand *pc = pendingCommands.value(cmdid, 0); - ServerCounter sc(values[0].toInt(), values[1].toInt(), values[2], numberToColor(values[3].toInt()), values[4].toInt(), values[5].toInt()); - - PendingCommand_ListCounters *pcLC = qobject_cast(pc); - if (pcLC) - pcLC->addCounter(sc); - else { - PendingCommand_DumpAll *pcDA = qobject_cast(pc); - if (pcDA) - pcDA->addCounter(sc); - else - emit protocolError(); - } - } else if (prefix == "list_arrows") { - if (values.size() != 10) { - emit protocolError(); - continue; - } - int cmdid = values.takeFirst().toInt(); - PendingCommand *pc = pendingCommands.value(cmdid, 0); - ServerArrow sa(values[0].toInt(), values[1].toInt(), values[2].toInt(), values[3], values[4].toInt(), values[5].toInt(), values[6], values[7].toInt(), numberToColor(values[8].toInt())); - - PendingCommand_DumpAll *pcDA = qobject_cast(pc); - if (pcDA) - pcDA->addArrow(sa); - else - emit protocolError(); - } else - emit protocolError(); - } -*/ } void Client::processProtocolItem(ProtocolItem *item) @@ -365,14 +201,3 @@ void Client::ping() } else sendCommand(new Command_Ping); } -/* -QColor Client::numberToColor(int colorValue) const -{ - return QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256); -} - -int Client::colorToNumber(const QColor &color) const -{ - return color.red() * 65536 + color.green() * 256 + color.blue(); -} -*/ \ No newline at end of file diff --git a/cockatrice/src/game.cpp b/cockatrice/src/game.cpp index 08b7781a..9a0ca6cd 100644 --- a/cockatrice/src/game.cpp +++ b/cockatrice/src/game.cpp @@ -128,25 +128,6 @@ void Game::retranslateUi() i.next().value()->retranslateUi(); } -Player *Game::addPlayer(int playerId, const QString &playerName, bool local) -{ - Player *newPlayer = new Player(playerName, playerId, local, client, this); - scene->addPlayer(newPlayer); - - connect(newPlayer, SIGNAL(sigShowCardMenu(QPoint)), this, SLOT(showCardMenu(QPoint))); - connect(newPlayer, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)), this, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int))); - connect(newPlayer, SIGNAL(logCreateToken(Player *, QString)), this, SIGNAL(logCreateToken(Player *, QString))); - connect(newPlayer, SIGNAL(logSetCardCounters(Player *, QString, int, int)), this, SIGNAL(logSetCardCounters(Player *, QString, int, int))); - connect(newPlayer, SIGNAL(logSetTapped(Player *, QString, bool)), this, SIGNAL(logSetTapped(Player *, QString, bool))); - connect(newPlayer, SIGNAL(logSetCounter(Player *, QString, int, int)), this, SIGNAL(logSetCounter(Player *, QString, int, int))); - connect(newPlayer, SIGNAL(logCreateArrow(Player *, Player *, QString, Player *, QString)), this, SIGNAL(logCreateArrow(Player *, Player *, QString, Player *, QString))); - connect(newPlayer, SIGNAL(logSetDoesntUntap(Player *, QString, bool)), this, SIGNAL(logSetDoesntUntap(Player *, QString, bool))); - - players.insert(playerId, newPlayer); - emit playerAdded(newPlayer); - - return newPlayer; -} /* void Game::cardListReceived(QList list) { @@ -570,4 +551,4 @@ Player *Game::getActiveLocalPlayer() const return p; } return 0; -} \ No newline at end of file +} diff --git a/cockatrice/src/game.h b/cockatrice/src/game.h index 85f63b6a..140b9a8b 100644 --- a/cockatrice/src/game.h +++ b/cockatrice/src/game.h @@ -103,7 +103,6 @@ public: void retranslateUi(); void restartGameDialog(); void hoverCardEvent(CardItem *card); - Player *addPlayer(int playerId, const QString &playerName, bool local); Player *getActiveLocalPlayer() const; const QMap &getPlayers() const { return players; } void queryGameState(); diff --git a/cockatrice/src/messagelogwidget.cpp b/cockatrice/src/messagelogwidget.cpp index aa255d3e..de1211dd 100644 --- a/cockatrice/src/messagelogwidget.cpp +++ b/cockatrice/src/messagelogwidget.cpp @@ -275,9 +275,9 @@ void MessageLogWidget::logSetActivePhase(int phase) append("" + tr("It is now the %1.").arg(phaseName) + ""); } -void MessageLogWidget::connectToGame(Game *game) +void MessageLogWidget::connectToPlayer(Player *player) { - connect(game, SIGNAL(logPlayerListReceived(QStringList)), this, SLOT(logPlayerListReceived(QStringList))); +/* connect(game, SIGNAL(logPlayerListReceived(QStringList)), this, SLOT(logPlayerListReceived(QStringList))); connect(game, SIGNAL(logJoin(Player *)), this, SLOT(logJoin(Player *))); connect(game, SIGNAL(logLeave(Player *)), this, SLOT(logLeave(Player *))); connect(game, SIGNAL(logGameClosed()), this, SLOT(logGameClosed())); @@ -310,7 +310,7 @@ void MessageLogWidget::connectToGame(Game *game) connect(game, SIGNAL(logDraw(Player *, int)), this, SLOT(msgAlert())); connect(game, SIGNAL(logMoveCard(Player *, QString, CardZone *, int, CardZone *, int)), this, SLOT(msgAlert())); connect(game, SIGNAL(logGameStart()), this, SLOT(msgAlert())); -} +*/} void MessageLogWidget::msgAlert() { diff --git a/cockatrice/src/messagelogwidget.h b/cockatrice/src/messagelogwidget.h index 1175186f..c65459f4 100644 --- a/cockatrice/src/messagelogwidget.h +++ b/cockatrice/src/messagelogwidget.h @@ -6,7 +6,6 @@ #include "translation.h" #include "protocol_datastructures.h" -class Game; class Player; class CardZone; @@ -49,7 +48,7 @@ private slots: void logSetActivePhase(int phase); void msgAlert(); public: - void connectToGame(Game *game); + void connectToPlayer(Player *player); MessageLogWidget(QWidget *parent = 0); }; diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 77451fdf..34957766 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -5,16 +5,16 @@ #include "arrowitem.h" #include "zoneviewzone.h" #include "zoneviewwidget.h" -#include "game.h" #include "pilezone.h" #include "tablezone.h" #include "handzone.h" #include "cardlist.h" +#include "tab_game.h" #include #include #include -Player::Player(const QString &_name, int _id, bool _local, Client *_client, Game *_parent) +Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent) : QObject(_parent), defaultNumberTopCards(3), name(_name), id(_id), active(false), local(_local), client(_client) { QSettings settings; @@ -697,6 +697,10 @@ void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/ painter->drawText(QRectF(0, 0, totalWidth, 40), Qt::AlignCenter, nameStr); } +void Player::processPlayerInfo(ServerInfo_Player *info) +{ +} + void Player::addCounter(int counterId, const QString &name, QColor color, int radius, int value) { Counter *c = new Counter(this, counterId, name, color, radius, value, this); diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index ed619674..066421e4 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -12,12 +12,13 @@ class CardDatabase; class QMenu; class QAction; class ZoneViewZone; -class Game; +class TabGame; class Counter; class ArrowItem; class CardZone; class TableZone; class HandZone; +class ServerInfo_Player; class Player : public QObject, public QGraphicsItem { Q_OBJECT @@ -103,7 +104,7 @@ public: Client *client; void addZone(CardZone *z); - Player(const QString &_name, int _id, bool _local, Client *_client, Game *_parent); + Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent); ~Player(); void retranslateUi(); QMenu *getPlayerMenu() const { return playerMenu; } @@ -117,6 +118,8 @@ public: void showCardMenu(const QPoint &p); bool getActive() const { return active; } void setActive(bool _active); + + void processPlayerInfo(ServerInfo_Player *info); }; #endif diff --git a/cockatrice/src/tab_chatchannel.cpp b/cockatrice/src/tab_chatchannel.cpp index 0513d8a4..c5557677 100644 --- a/cockatrice/src/tab_chatchannel.cpp +++ b/cockatrice/src/tab_chatchannel.cpp @@ -51,7 +51,7 @@ void TabChatChannel::processChatEvent(ChatEvent *event) void TabChatChannel::processListPlayersEvent(Event_ChatListPlayers *event) { - const QList &players = event->getPlayerList(); + const QList &players = event->getPlayerList(); for (int i = 0; i < players.size(); ++i) playerList->addItem(players[i].getName()); } diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index deb22ceb..bafdf565 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -18,7 +18,7 @@ #include "main.h" TabGame::TabGame(Client *_client, int _gameId) - : client(_client), gameId(_gameId) + : client(_client), gameId(_gameId), localPlayerId(-1) { zoneLayout = new ZoneViewLayout; scene = new GameScene(zoneLayout, this); @@ -38,6 +38,8 @@ TabGame::TabGame(Client *_client, int _gameId) QVBoxLayout *deckViewLayout = new QVBoxLayout; deckViewLayout->addLayout(buttonHBox); deckViewLayout->addWidget(deckView); + deckViewContainer = new QWidget; + deckViewContainer->setLayout(deckViewLayout); cardInfo = new CardInfoWidget(db); messageLog = new MessageLogWidget; @@ -60,7 +62,7 @@ TabGame::TabGame(Client *_client, int _gameId) QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(phasesToolbar); mainLayout->addWidget(gameView, 10); - mainLayout->addLayout(deckViewLayout, 10); + mainLayout->addWidget(deckViewContainer, 10); mainLayout->addLayout(verticalLayout); aCloseMostRecentZoneView = new QAction(this); @@ -105,9 +107,44 @@ void TabGame::retranslateUi() aCloseMostRecentZoneView->setShortcut(tr("Esc")); } +Player *TabGame::addPlayer(int playerId, const QString &playerName) +{ + Player *newPlayer = new Player(playerName, playerId, playerId == localPlayerId, client, this); + scene->addPlayer(newPlayer); + + messageLog->connectToPlayer(newPlayer); + + players.insert(playerId, newPlayer); + emit playerAdded(newPlayer); + + return newPlayer; +} + void TabGame::processGameEvent(GameEvent *event) { -// game->processGameEvent(event); + switch (event->getItemId()) { + case ItemId_Event_GameStart: eventGameStart(qobject_cast(event)); break; + default: qDebug() << "unhandled game event"; + } +} + +void TabGame::processGameJoinedEvent(Event_GameJoined *event) +{ + localPlayerId = event->getPlayerId(); + spectator = event->getSpectator(); + + 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); + } +} + +void TabGame::eventGameStart(Event_GameStart *event) +{ + deckViewContainer->hide(); + gameView->show(); } void TabGame::loadLocalDeck() diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index d55a0795..cf333249 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -2,14 +2,13 @@ #define TAB_GAME_H #include +#include class Client; class CardDatabase; -class GameEvent; class GameView; class DeckView; class GameScene; -class Game; class CardInfoWidget; class MessageLogWidget; class QLabel; @@ -19,12 +18,23 @@ class ZoneViewLayout; class ZoneViewWidget; class PhasesToolbar; class ProtocolResponse; +class GameEvent; +class Event_GameJoined; +class Event_GameStart; +class Player; +class CardZone; class TabGame : public QWidget { Q_OBJECT private: Client *client; int gameId; + int localPlayerId; + bool spectator; + QStringList spectatorList; + QMap players; + bool started; + int currentPhase; QPushButton *loadLocalButton, *loadRemoteButton, *readyStartButton; CardInfoWidget *cardInfo; @@ -35,9 +45,41 @@ private: GameScene *scene; GameView *gameView; DeckView *deckView; - Game *game; + QWidget *deckViewContainer; ZoneViewLayout *zoneLayout; QAction *aCloseMostRecentZoneView; + + Player *addPlayer(int playerId, const QString &playerName); + + void eventGameStart(Event_GameStart *event); +signals: + void playerAdded(Player *player); + void playerRemoved(Player *player); + + // Log events + void logPlayerListReceived(QStringList players); + void logJoin(Player *player); + void logLeave(Player *player); + void logGameClosed(); + void logJoinSpectator(QString playerName); + void logLeaveSpectator(QString playerName); + void logReadyStart(Player *player); + void logGameStart(); + void logSay(Player *player, QString text); + void logShuffle(Player *player); + void logRollDie(Player *player, int sides, int roll); + void logDraw(Player *player, int number); + void logMoveCard(Player *player, QString cardName, CardZone *startZone, int oldX, CardZone *targetZone, int newX); + void logCreateToken(Player *player, QString cardName); + void logCreateArrow(Player *player, Player *startPlayer, QString startCard, Player *targetPlayer, QString targetCard); + void logSetCardCounters(Player *player, QString cardName, int value, int oldValue); + void logSetTapped(Player *player, QString cardName, bool tapped); + void logSetCounter(Player *player, QString counterName, int value, int oldValue); + void logSetDoesntUntap(Player *player, QString cardName, bool doesntUntap); + void logDumpZone(Player *player, CardZone *zone, int numberCards); + void logStopDumpZone(Player *player, CardZone *zone); + void logSetActivePlayer(Player *player); + void setActivePhase(int phase); private slots: void loadLocalDeck(); void loadRemoteDeck(); @@ -46,7 +88,9 @@ private slots: public: TabGame(Client *_client, int _gameId); void retranslateUi(); + void processGameEvent(GameEvent *event); + void processGameJoinedEvent(Event_GameJoined *event); }; #endif diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 71c7a2b9..81855492 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -104,6 +104,8 @@ void TabSupervisor::gameJoined(Event_GameJoined *event) TabGame *tab = new TabGame(client, event->getGameId()); addTab(tab, tr("Game %1").arg(event->getGameId())); gameTabs.insert(event->getGameId(), tab); + tab->processGameJoinedEvent(event); + setCurrentWidget(tab); } void TabSupervisor::addChatChannelTab(const QString &channelName) @@ -111,6 +113,7 @@ void TabSupervisor::addChatChannelTab(const QString &channelName) TabChatChannel *tab = new TabChatChannel(client, channelName); addTab(tab, channelName); chatChannelTabs.insert(channelName, tab); + setCurrentWidget(tab); } void TabSupervisor::processChatEvent(ChatEvent *event) diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index 1cd0f87d..aab05631 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -41,7 +41,7 @@ void ZoneViewZone::initializeCards() } } -void ZoneViewZone::zoneDumpReceived(QList cards) +void ZoneViewZone::zoneDumpReceived(QList cards) { for (int i = 0; i < cards.size(); i++) { CardItem *card = new CardItem(cards[i].getName(), i, this); diff --git a/cockatrice/src/zoneviewzone.h b/cockatrice/src/zoneviewzone.h index 28755496..105c18b5 100644 --- a/cockatrice/src/zoneviewzone.h +++ b/cockatrice/src/zoneviewzone.h @@ -29,7 +29,7 @@ public: public slots: void setSortingEnabled(int _sortingEnabled); private slots: - void zoneDumpReceived(QList cards); + void zoneDumpReceived(QList cards); protected: void addCardImpl(CardItem *card, int x, int y); QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; diff --git a/common/protocol.cpp b/common/protocol.cpp index 50a900d9..4dfe145c 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -76,6 +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("chat_eventchat_list_players", Event_ChatListPlayers::newItem); } @@ -210,57 +211,6 @@ void ProtocolResponse::initializeHash() responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed); } -bool DeckList_File::readElement(QXmlStreamReader *xml) -{ - if (xml->isEndElement()) - return true; - else - return false; -} - -void DeckList_File::writeElement(QXmlStreamWriter *xml) -{ - xml->writeStartElement("file"); - xml->writeAttribute("name", name); - xml->writeAttribute("id", QString::number(id)); - xml->writeAttribute("upload_time", QString::number(uploadTime.toTime_t())); - xml->writeEndElement(); -} - -DeckList_Directory::~DeckList_Directory() -{ - for (int i = 0; i < size(); ++i) - delete at(i); -} - -bool DeckList_Directory::readElement(QXmlStreamReader *xml) -{ - if (currentItem) { - if (currentItem->readElement(xml)) - currentItem = 0; - return false; - } - if (xml->isStartElement() && (xml->name() == "directory")) { - currentItem = new DeckList_Directory(xml->attributes().value("name").toString()); - append(currentItem); - } else if (xml->isStartElement() && (xml->name() == "file")) { - currentItem = new DeckList_File(xml->attributes().value("name").toString(), xml->attributes().value("id").toString().toInt(), QDateTime::fromTime_t(xml->attributes().value("upload_time").toString().toUInt())); - append(currentItem); - } else if (xml->isEndElement() && (xml->name() == "directory")) - return true; - - return false; -} - -void DeckList_Directory::writeElement(QXmlStreamWriter *xml) -{ - xml->writeStartElement("directory"); - xml->writeAttribute("name", name); - for (int i = 0; i < size(); ++i) - at(i)->writeElement(xml); - xml->writeEndElement(); -} - Response_DeckList::Response_DeckList(int _cmdId, ResponseCode _responseCode, DeckList_Directory *_root) : ProtocolResponse(_cmdId, _responseCode, "deck_list"), root(_root), readFinished(false) { @@ -420,7 +370,7 @@ void Event_ListChatChannels::writeElement(QXmlStreamWriter *xml) bool Event_ChatListPlayers::readElement(QXmlStreamReader *xml) { if (xml->isStartElement() && ((xml->name() == "player"))) { - playerList.append(ServerPlayerInfo( + playerList.append(ServerChatUserInfo( xml->attributes().value("name").toString() )); return true; @@ -470,3 +420,50 @@ void Event_ListGames::writeElement(QXmlStreamWriter *xml) xml->writeEndElement(); } } + +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) +{ + setParameter("game_id", gameId); + setParameter("player_id", playerId); + setParameter("spectator", spectator); +} + +Event_GameJoined::~Event_GameJoined() +{ + 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) +{ + if (currentItem) { + if (currentItem->readElement(xml)) + currentItem = 0; + return true; + } + if (xml->isStartElement() && (xml->name() == "player")) { + ServerInfo_Player *player = new ServerInfo_Player; + playerList.append(player); + currentItem = player; + } else + return false; + if (currentItem) + if (currentItem->readElement(xml)) + currentItem = 0; + return true; +} + +void Event_GameJoined::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 fa821ade..8f822abb 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -22,6 +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_Response_DeckList = ItemId_Other + 300, ItemId_Response_DeckDownload = ItemId_Other + 301, ItemId_Response_DeckUpload = ItemId_Other + 302 @@ -297,16 +298,16 @@ public: class Event_ChatListPlayers : public ChatEvent { Q_OBJECT private: - QList playerList; + QList playerList; public: Event_ChatListPlayers(const QString &_channel = QString()) : ChatEvent("chat_list_players", _channel) { } int getItemId() const { return ItemId_Event_ChatListPlayers; } static ProtocolItem *newItem() { return new Event_ChatListPlayers; } void addPlayer(const QString &_name) { - playerList.append(ServerPlayerInfo(_name)); + playerList.append(ServerChatUserInfo(_name)); } - const QList &getPlayerList() const { return playerList; } + const QList &getPlayerList() const { return playerList; } bool readElement(QXmlStreamReader *xml); void writeElement(QXmlStreamWriter *xml); @@ -330,4 +331,29 @@ public: void writeElement(QXmlStreamWriter *xml); }; +class Event_GameJoined : public GenericEvent { + 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; } + const QList &getPlayerList() const { return playerList; } + static ProtocolItem *newItem() { return new Event_GameJoined; } + int getItemId() const { return ItemId_Event_GameJoined; } + + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); +}; + #endif diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp new file mode 100644 index 00000000..f16f0883 --- /dev/null +++ b/common/protocol_datastructures.cpp @@ -0,0 +1,257 @@ +#include "protocol_datastructures.h" +#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) + delete zoneList[i]; + for (int i = 0; i < arrowList.size(); ++i) + delete arrowList[i]; + for (int i = 0; i < counterList.size(); ++i) + delete counterList[i]; +} + +ServerInfo_Zone::~ServerInfo_Zone() +{ + for (int i = 0; i < cardList.size(); ++i) + delete cardList[i]; +} + +bool ServerInfo_Arrow::readElement(QXmlStreamReader *xml) +{ + if (xml->isStartElement() && (xml->name() == "arrow")) { + id = xml->attributes().value("id").toString().toInt(); + startPlayerId = xml->attributes().value("start_player_id").toString().toInt(); + startZone = xml->attributes().value("start_zone").toString(); + startCardId = xml->attributes().value("start_card_id").toString().toInt(); + targetPlayerId = xml->attributes().value("target_player_id").toString().toInt(); + targetZone = xml->attributes().value("target_zone").toString(); + targetCardId = xml->attributes().value("target_card_id").toString().toInt(); + color = ColorConverter::colorFromInt(xml->attributes().value("color").toString().toInt()); + } else if (xml->isEndElement() && (xml->name() == "arrow")) + return true; + return false; +} + +void ServerInfo_Arrow::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("arrow"); + xml->writeAttribute("id", QString::number(id)); + xml->writeAttribute("start_player_id", QString::number(startPlayerId)); + xml->writeAttribute("start_zone", startZone); + xml->writeAttribute("start_card_id", QString::number(startCardId)); + xml->writeAttribute("target_player_id", QString::number(targetPlayerId)); + xml->writeAttribute("target_zone", targetZone); + xml->writeAttribute("target_card_id", QString::number(targetCardId)); + xml->writeAttribute("color", QString::number(ColorConverter::colorToInt(color))); + xml->writeEndElement(); +} + +bool ServerInfo_Counter::readElement(QXmlStreamReader *xml) +{ + if (xml->isStartElement() && (xml->name() == "counter")) { + id = xml->attributes().value("id").toString().toInt(); + name = xml->attributes().value("name").toString(); + color = ColorConverter::colorFromInt(xml->attributes().value("color").toString().toInt()); + radius = xml->attributes().value("radius").toString().toInt(); + count = xml->attributes().value("count").toString().toInt(); + } else if (xml->isEndElement() && (xml->name() == "counter")) + return true; + return false; +} + +void ServerInfo_Counter::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("counter"); + xml->writeAttribute("id", QString::number(id)); + xml->writeAttribute("name", name); + xml->writeAttribute("color", QString::number(ColorConverter::colorToInt(color))); + xml->writeAttribute("radius", QString::number(radius)); + xml->writeAttribute("count", QString::number(count)); + xml->writeEndElement(); +} + +bool ServerInfo_Card::readElement(QXmlStreamReader *xml) +{ + if (xml->isStartElement() && (xml->name() == "card")) { + id = xml->attributes().value("id").toString().toInt(); + name = xml->attributes().value("name").toString(); + x = xml->attributes().value("x").toString().toInt(); + y = xml->attributes().value("y").toString().toInt(); + counters = xml->attributes().value("counters").toString().toInt(); + tapped = xml->attributes().value("tapped").toString().toInt(); + attacking = xml->attributes().value("attacking").toString().toInt(); + annotation = xml->attributes().value("annotation").toString(); + } else if (xml->isEndElement() && (xml->name() == "card")) + return true; + return false; +} + +void ServerInfo_Card::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("card"); + xml->writeAttribute("id", QString::number(id)); + xml->writeAttribute("name", name); + xml->writeAttribute("x", QString::number(x)); + xml->writeAttribute("y", QString::number(y)); + xml->writeAttribute("counters", QString::number(counters)); + xml->writeAttribute("tapped", tapped ? "1" : "0"); + xml->writeAttribute("attacking", attacking ? "1" : "0"); + xml->writeAttribute("annotation", annotation); + xml->writeEndElement(); +} + +bool ServerInfo_Zone::readElement(QXmlStreamReader *xml) +{ + if (currentItem) { + if (currentItem->readElement(xml)) + currentItem = 0; + return false; + } + if (xml->isStartElement() && (xml->name() == "zone")) { + name = xml->attributes().value("name").toString(); + type = (ZoneType) xml->attributes().value("type").toString().toInt(); + hasCoords = xml->attributes().value("has_coords").toString().toInt(); + cardCount = xml->attributes().value("card_count").toString().toInt(); + } else if (xml->isStartElement() && (xml->name() == "card")) { + ServerInfo_Card *card = new ServerInfo_Card; + cardList.append(card); + currentItem = card; + } else if (xml->isEndElement() && (xml->name() == "zone")) + return true; + + if (currentItem) + if (currentItem->readElement(xml)) + currentItem = 0; + return false; + +} + +void ServerInfo_Zone::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("zone"); + xml->writeAttribute("name", name); + QString typeStr; + switch (type) { + case PrivateZone: typeStr = "private"; break; + case HiddenZone: typeStr = "hidden"; break; + case PublicZone: typeStr = "public"; break; + } + xml->writeAttribute("type", typeStr); + xml->writeAttribute("has_coords", hasCoords ? "1" : "0"); + xml->writeAttribute("card_count", QString::number(cardCount)); + for (int i = 0; i < cardList.size(); ++i) + cardList[i]->writeElement(xml); + xml->writeEndElement(); +} + +bool ServerInfo_Player::readElement(QXmlStreamReader *xml) +{ + if (currentItem) { + if (currentItem->readElement(xml)) + currentItem = 0; + return false; + } + if (xml->isStartElement() && (xml->name() == "player")) { + playerId = xml->attributes().value("player_id").toString().toInt(); + name = xml->attributes().value("name").toString(); + } else if (xml->isStartElement() && (xml->name() == "zone")) { + ServerInfo_Zone *zone = new ServerInfo_Zone; + zoneList.append(zone); + currentItem = zone; + } else if (xml->isStartElement() && (xml->name() == "counter")) { + ServerInfo_Counter *counter = new ServerInfo_Counter; + counterList.append(counter); + currentItem = counter; + } else if (xml->isStartElement() && (xml->name() == "arrow")) { + ServerInfo_Arrow *arrow = new ServerInfo_Arrow; + arrowList.append(arrow); + currentItem = arrow; + } else if (xml->isEndElement() && (xml->name() == "player")) + return true; + + if (currentItem) + if (currentItem->readElement(xml)) + currentItem = 0; + return false; +} + +void ServerInfo_Player::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("player"); + xml->writeAttribute("player_id", QString::number(playerId)); + xml->writeAttribute("name", name); + for (int i = 0; i < zoneList.size(); ++i) + zoneList[i]->writeElement(xml); + for (int i = 0; i < counterList.size(); ++i) + counterList[i]->writeElement(xml); + for (int i = 0; i < arrowList.size(); ++i) + arrowList[i]->writeElement(xml); + xml->writeEndElement(); +} + +bool DeckList_File::readElement(QXmlStreamReader *xml) +{ + if (xml->isEndElement()) + return true; + else + return false; +} + +void DeckList_File::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("file"); + xml->writeAttribute("name", name); + xml->writeAttribute("id", QString::number(id)); + xml->writeAttribute("upload_time", QString::number(uploadTime.toTime_t())); + xml->writeEndElement(); +} + +DeckList_Directory::~DeckList_Directory() +{ + for (int i = 0; i < size(); ++i) + delete at(i); +} + +bool DeckList_Directory::readElement(QXmlStreamReader *xml) +{ + if (currentItem) { + if (currentItem->readElement(xml)) + currentItem = 0; + return false; + } + if (xml->isStartElement() && (xml->name() == "directory")) { + DeckList_Directory *newItem = new DeckList_Directory(xml->attributes().value("name").toString()); + append(newItem); + currentItem = newItem; + } else if (xml->isStartElement() && (xml->name() == "file")) { + DeckList_File *newItem = new DeckList_File(xml->attributes().value("name").toString(), xml->attributes().value("id").toString().toInt(), QDateTime::fromTime_t(xml->attributes().value("upload_time").toString().toUInt())); + append(newItem); + currentItem = newItem; + } else if (xml->isEndElement() && (xml->name() == "directory")) + return true; + + return false; +} + +void DeckList_Directory::writeElement(QXmlStreamWriter *xml) +{ + xml->writeStartElement("directory"); + xml->writeAttribute("name", name); + for (int i = 0; i < size(); ++i) + at(i)->writeElement(xml); + xml->writeEndElement(); +} diff --git a/common/protocol_datastructures.h b/common/protocol_datastructures.h index 66dc0cb1..251278fd 100644 --- a/common/protocol_datastructures.h +++ b/common/protocol_datastructures.h @@ -10,6 +10,25 @@ class QXmlStreamWriter; enum ResponseCode { RespNothing, RespOk, RespInvalidCommand, RespInvalidData, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed }; +// PrivateZone: Contents of the zone are always visible to the owner, +// but not to anyone else. +// PublicZone: Contents of the zone are always visible to anyone. +// HiddenZone: Contents of the zone are never visible to anyone. +// However, the owner of the zone can issue a dump_zone command, +// setting beingLookedAt to true. +// Cards in a zone with the type HiddenZone are referenced by their +// list index, whereas cards in any other zone are referenced by their ids. +enum ZoneType { PrivateZone, PublicZone, HiddenZone }; + +class SerializableItem { +protected: + SerializableItem *currentItem; +public: + SerializableItem() : currentItem(0) { } + virtual bool readElement(QXmlStreamReader *xml) = 0; + virtual void writeElement(QXmlStreamWriter *xml) = 0; +}; + class ServerChatChannelInfo { private: QString name; @@ -25,11 +44,11 @@ public: bool getAutoJoin() const { return autoJoin; } }; -class ServerPlayerInfo { +class ServerChatUserInfo { private: QString name; public: - ServerPlayerInfo(const QString &_name) + ServerChatUserInfo(const QString &_name) : name(_name) { } QString getName() const { return name; } }; @@ -57,23 +76,8 @@ public: int getSpectatorCount() const { return spectatorCount; } }; -class ServerPlayer { +class ServerInfo_Card : public SerializableItem { private: - int PlayerId; - QString name; - bool local; -public: - ServerPlayer(int _PlayerId, const QString &_name, bool _local) - : PlayerId(_PlayerId), name(_name), local(_local) { } - int getPlayerId() const { return PlayerId; } - QString getName() const { return name; } - bool getLocal() const { return local; } -}; - -class ServerZoneCard { -private: - int playerId; - QString zoneName; int id; QString name; int x, y; @@ -82,10 +86,8 @@ private: bool attacking; QString annotation; public: - ServerZoneCard(int _playerId, const QString &_zoneName, int _id, const QString &_name, int _x, int _y, int _counters, bool _tapped, bool _attacking, const QString &_annotation) - : playerId(_playerId), zoneName(_zoneName), id(_id), name(_name), x(_x), y(_y), counters(_counters), tapped(_tapped), attacking(_attacking), annotation(_annotation) { } - int getPlayerId() const { return playerId; } - QString getZoneName() const { return zoneName; } + ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, int _counters = -1, bool _tapped = false, bool _attacking = false, const QString &_annotation = QString()) + : id(_id), name(_name), x(_x), y(_y), counters(_counters), tapped(_tapped), attacking(_attacking), annotation(_annotation) { } int getId() const { return id; } QString getName() const { return name; } int getX() const { return x; } @@ -94,50 +96,53 @@ public: bool getTapped() const { return tapped; } bool getAttacking() const { return attacking; } QString getAnnotation() const { return annotation; } + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); }; -class ServerZone { -public: - enum ZoneType { PrivateZone, PublicZone, HiddenZone }; +class ServerInfo_Zone : public SerializableItem { private: - int playerId; QString name; ZoneType type; bool hasCoords; int cardCount; + QList cardList; public: - ServerZone(int _playerId, const QString &_name, ZoneType _type, bool _hasCoords, int _cardCount) - : playerId(_playerId), name(_name), type(_type), hasCoords(_hasCoords), cardCount(_cardCount) { } - int getPlayerId() const { return playerId; } + ServerInfo_Zone(const QString &_name = QString(), ZoneType _type = PrivateZone, bool _hasCoords = false, int _cardCount = -1, const QList &_cardList = QList()) + : name(_name), type(_type), hasCoords(_hasCoords), cardCount(_cardCount), cardList(_cardList) { } + ~ServerInfo_Zone(); QString getName() const { return name; } ZoneType getType() const { return type; } bool getHasCoords() const { return hasCoords; } int getCardCount() const { return cardCount; } + const QList &getCardList() const { return cardList; } + void addCard(ServerInfo_Card *card) { cardList.append(card); ++cardCount; } + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); }; -class ServerCounter { +class ServerInfo_Counter : public SerializableItem { private: - int playerId; int id; QString name; QColor color; int radius; int count; public: - ServerCounter(int _playerId, int _id, const QString &_name, QColor _color, int _radius, int _count) - : playerId(_playerId), id(_id), name(_name), color(_color), radius(_radius), count(_count) { } - int getPlayerId() const { return playerId; } + ServerInfo_Counter(int _id = -1, const QString &_name = QString(), const QColor &_color = QColor(), int _radius = -1, int _count = -1) + : id(_id), name(_name), color(_color), radius(_radius), count(_count) { } int getId() const { return id; } QString getName() const { return name; } QColor getColor() const { return color; } int getRadius() const { return radius; } int getCount() const { return count; } + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); }; -class ServerArrow { +class ServerInfo_Arrow : public SerializableItem { private: int id; - int playerId; int startPlayerId; QString startZone; int startCardId; @@ -146,10 +151,9 @@ private: int targetCardId; QColor color; public: - ServerArrow(int _playerId, int _id, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, const QColor &_color) - : id(_id), playerId(_playerId), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { } + ServerInfo_Arrow(int _id = -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()) + : id(_id), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { } int getId() const { return id; } - int getPlayerId() const { return playerId; } int getStartPlayerId() const { return startPlayerId; } QString getStartZone() const { return startZone; } int getStartCardId() const { return startCardId; } @@ -157,9 +161,34 @@ public: QString getTargetZone() const { return targetZone; } int getTargetCardId() const { return targetCardId; } QColor getColor() const { return color; } + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); }; -class DeckList_TreeItem { +class ServerInfo_Player : public SerializableItem { +private: + int playerId; + QString name; + QList zoneList; + QList counterList; + QList arrowList; +public: + ServerInfo_Player(int _playerId = -1, const QString &_name = QString(), const QList &_zoneList = QList(), const QList &_counterList = QList(), const QList &_arrowList = QList()) + : playerId(_playerId), name(_name), zoneList(_zoneList), counterList(_counterList), arrowList(_arrowList) { } + ~ServerInfo_Player(); + int getPlayerId() const { return playerId; } + QString getName() const { return name; } + const QList &getZoneList() const { return zoneList; } + const QList &getCounterList() const { return counterList; } + const QList &getArrowList() const { return arrowList; } + void addZone(ServerInfo_Zone *zone) { zoneList.append(zone); } + void addCounter(ServerInfo_Counter *counter) { counterList.append(counter); } + void addArrow(ServerInfo_Arrow *arrow) { arrowList.append(arrow); } + bool readElement(QXmlStreamReader *xml); + void writeElement(QXmlStreamWriter *xml); +}; + +class DeckList_TreeItem : public SerializableItem { protected: QString name; int id; @@ -167,8 +196,6 @@ public: DeckList_TreeItem(const QString &_name, int _id) : name(_name), id(_id) { } QString getName() const { return name; } int getId() const { return id; } - virtual bool readElement(QXmlStreamReader *xml) = 0; - virtual void writeElement(QXmlStreamWriter *xml) = 0; }; class DeckList_File : public DeckList_TreeItem { private: @@ -180,10 +207,8 @@ public: QDateTime getUploadTime() const { return uploadTime; } }; class DeckList_Directory : public DeckList_TreeItem, public QList { -private: - DeckList_TreeItem *currentItem; public: - DeckList_Directory(const QString &_name = QString(), int _id = 0) : DeckList_TreeItem(_name, _id), currentItem(0) { } + DeckList_Directory(const QString &_name = QString(), int _id = 0) : DeckList_TreeItem(_name, _id) { } ~DeckList_Directory(); bool readElement(QXmlStreamReader *xml); void writeElement(QXmlStreamWriter *xml); diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h index fe7b3e07..5b9a6764 100644 --- a/common/protocol_item_ids.h +++ b/common/protocol_item_ids.h @@ -56,9 +56,8 @@ ItemId_Event_SetActivePhase = 1054, ItemId_Event_DumpZone = 1055, ItemId_Event_StopDumpZone = 1056, ItemId_Event_ServerMessage = 1057, -ItemId_Event_GameJoined = 1058, -ItemId_Event_ChatJoinChannel = 1059, -ItemId_Event_ChatLeaveChannel = 1060, -ItemId_Event_ChatSay = 1061, -ItemId_Other = 1062 +ItemId_Event_ChatJoinChannel = 1058, +ItemId_Event_ChatLeaveChannel = 1059, +ItemId_Event_ChatSay = 1060, +ItemId_Other = 1061 }; diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 9152eb72..64218fac 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -613,18 +613,6 @@ void Event_ServerMessage::extractParameters() GenericEvent::extractParameters(); message = parameters["message"]; } -Event_GameJoined::Event_GameJoined(int _gameId, bool _spectator) - : GenericEvent("game_joined"), gameId(_gameId), spectator(_spectator) -{ - setParameter("game_id", gameId); - setParameter("spectator", spectator); -} -void Event_GameJoined::extractParameters() -{ - GenericEvent::extractParameters(); - gameId = parameters["game_id"].toInt(); - spectator = (parameters["spectator"] == "1"); -} Event_ChatJoinChannel::Event_ChatJoinChannel(const QString &_channel, const QString &_playerName) : ChatEvent("chat_join_channel", _channel), playerName(_playerName) { @@ -716,7 +704,6 @@ 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 65316aff..db1f45ca 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -55,7 +55,6 @@ 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: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 46e3e6cb..ec839ac2 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -759,20 +759,6 @@ public: protected: void extractParameters(); }; -class Event_GameJoined : public GenericEvent { - Q_OBJECT -private: - int gameId; - bool spectator; -public: - Event_GameJoined(int _gameId = -1, bool _spectator = false); - int getGameId() const { return gameId; } - 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/server_cardzone.h b/common/server_cardzone.h index 03d97b24..cda335d7 100644 --- a/common/server_cardzone.h +++ b/common/server_cardzone.h @@ -22,21 +22,12 @@ #include #include +#include "protocol_datastructures.h" class Server_Card; class Server_Player; class Server_CardZone { -public: - // PrivateZone: Contents of the zone are always visible to the owner, - // but not to anyone else. - // PublicZone: Contents of the zone are always visible to anyone. - // HiddenZone: Contents of the zone are never visible to anyone. - // However, the owner of the zone can issue a dump_zone command, - // setting beingLookedAt to true. - // Cards in a zone with the type HiddenZone are referenced by their - // list index, whereas cards in any other zone are referenced by their ids. - enum ZoneType { PrivateZone, PublicZone, HiddenZone }; private: Server_Player *player; QString name; diff --git a/common/server_game.cpp b/common/server_game.cpp index 2430d7bb..0c89a429 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -21,6 +21,9 @@ #include "server_game.h" #include "server_protocolhandler.h" #include "server_arrow.h" +#include "server_card.h" +#include "server_cardzone.h" +#include "server_counter.h" #include Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, QObject *parent) @@ -157,6 +160,54 @@ void Server_Game::setActivePhase(int _activePhase) sendGameEvent(new Event_SetActivePhase(-1, -1, activePhase)); } +QList Server_Game::getGameState() const +{ + QList result; + QMapIterator playerIterator(players); + while (playerIterator.hasNext()) { + Server_Player *player = playerIterator.next().value(); + + QList arrowList; + QMapIterator arrowIterator(player->getArrows()); + while (arrowIterator.hasNext()) { + Server_Arrow *arrow = arrowIterator.next().value(); + arrowList.append(new ServerInfo_Arrow( + arrow->getId(), + arrow->getStartCard()->getZone()->getPlayer()->getPlayerId(), + arrow->getStartCard()->getZone()->getName(), + arrow->getStartCard()->getId(), + arrow->getTargetCard()->getZone()->getPlayer()->getPlayerId(), + arrow->getTargetCard()->getZone()->getName(), + arrow->getTargetCard()->getId(), + arrow->getColor() + )); + } + + QList counterList; + QMapIterator counterIterator(player->getCounters()); + while (counterIterator.hasNext()) { + Server_Counter *counter = counterIterator.next().value(); + counterList.append(new ServerInfo_Counter(counter->getId(), counter->getName(), counter->getColor(), counter->getRadius(), counter->getCount())); + } + + QList zoneList; + QMapIterator zoneIterator(player->getZones()); + 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())); + } + zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList)); + } + + result.append(new ServerInfo_Player(player->getPlayerId(), player->getPlayerName(), zoneList, counterList, arrowList)); + } + return result; +} + void Server_Game::sendGameEvent(GameEvent *event) { event->setGameId(gameId); diff --git a/common/server_game.h b/common/server_game.h index c703660e..ace0a0c5 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -65,6 +65,7 @@ public: void setActivePlayer(int _activePlayer); void setActivePhase(int _activePhase); + QList getGameState() const; void sendGameEvent(GameEvent *event); }; diff --git a/common/server_player.cpp b/common/server_player.cpp index c71e716e..c9af122a 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -52,14 +52,14 @@ void Server_Player::setupZones() // ------------------------------------------------------------------ // Create zones - Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, Server_CardZone::HiddenZone); + Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, HiddenZone); addZone(deckZone); - Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, Server_CardZone::HiddenZone); + Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, HiddenZone); addZone(sbZone); - addZone(new Server_CardZone(this, "table", true, Server_CardZone::PublicZone)); - addZone(new Server_CardZone(this, "hand", false, Server_CardZone::PrivateZone)); - addZone(new Server_CardZone(this, "grave", false, Server_CardZone::PublicZone)); - addZone(new Server_CardZone(this, "rfg", false, Server_CardZone::PublicZone)); + addZone(new Server_CardZone(this, "table", true, PublicZone)); + addZone(new Server_CardZone(this, "hand", false, PrivateZone)); + addZone(new Server_CardZone(this, "grave", false, PublicZone)); + addZone(new Server_CardZone(this, "rfg", false, PublicZone)); // ------------------------------------------------------------------ diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 98792a7a..63165edf 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -220,7 +220,7 @@ 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())); - enqueueProtocolItem(new Event_GameJoined(game->getGameId(), false)); + enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getCreator()->getPlayerId(), false, game->getGameState())); return RespOk; } @@ -234,8 +234,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(), cmd->getSpectator())); return result; } @@ -326,13 +326,13 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, Server_G targetzone->insertCard(card, x, y); - bool targetBeingLookedAt = (targetzone->getType() != Server_CardZone::HiddenZone) || (targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1); - bool sourceBeingLookedAt = (startzone->getType() != Server_CardZone::HiddenZone) || (startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1); + bool targetBeingLookedAt = (targetzone->getType() != HiddenZone) || (targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1); + bool sourceBeingLookedAt = (startzone->getType() != HiddenZone) || (startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1); bool targetHiddenToPlayer = facedown || !targetBeingLookedAt; - bool targetHiddenToOthers = facedown || (targetzone->getType() != Server_CardZone::PublicZone); + bool targetHiddenToOthers = facedown || (targetzone->getType() != PublicZone); bool sourceHiddenToPlayer = card->getFaceDown() || !sourceBeingLookedAt; - bool sourceHiddenToOthers = card->getFaceDown() || (startzone->getType() != Server_CardZone::PublicZone); + bool sourceHiddenToOthers = card->getFaceDown() || (startzone->getType() != PublicZone); QString privateCardName, publicCardName; if (!(sourceHiddenToPlayer && targetHiddenToPlayer)) @@ -363,9 +363,9 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, Server_G // 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. - if ((startzone->getType() == Server_CardZone::HiddenZone) && ((startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1))) + if ((startzone->getType() == HiddenZone) && ((startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1))) position = -1; - if ((targetzone->getType() == Server_CardZone::HiddenZone) && ((targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1))) + if ((targetzone->getType() == HiddenZone) && ((targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1))) x = -1; /* if ((startzone->getType() == Server_CardZone::PublicZone) || (targetzone->getType() == Server_CardZone::PublicZone)) @@ -565,10 +565,10 @@ ResponseCode Server_ProtocolHandler::cmdDumpZone(Command_DumpZone *cmd, Server_G Server_CardZone *zone = otherPlayer->getZones().value(cmd->getZoneName()); if (!zone) return RespNameNotFound; - if (!((zone->getType() == Server_CardZone::PublicZone) || (player == otherPlayer))) + if (!((zone->getType() == PublicZone) || (player == otherPlayer))) return RespContextError; - if (zone->getType() == Server_CardZone::HiddenZone) { + if (zone->getType() == HiddenZone) { // game->broadcastEvent(QString("dump_zone|%1|%2|%3").arg(player_id).arg(zone->getName()).arg(number_cards), player); } // remsg->sendList(dumpZoneHelper(otherPlayer, zone, number_cards)); @@ -584,7 +584,7 @@ ResponseCode Server_ProtocolHandler::cmdStopDumpZone(Command_StopDumpZone *cmd, if (!zone) return RespNameNotFound; - if (zone->getType() == Server_CardZone::HiddenZone) { + if (zone->getType() == HiddenZone) { zone->setCardsBeingLookedAt(0); game->sendGameEvent(new Event_StopDumpZone(-1, player->getPlayerId(), cmd->getPlayerId(), zone->getName())); } diff --git a/servatrice/servatrice.pro b/servatrice/servatrice.pro index 58e5f034..c0de29fe 100755 --- a/servatrice/servatrice.pro +++ b/servatrice/servatrice.pro @@ -36,6 +36,7 @@ SOURCES += src/main.cpp \ ../common/decklist.cpp \ ../common/protocol.cpp \ ../common/protocol_items.cpp \ + ../common/protocol_datastructures.cpp \ ../common/rng_abstract.cpp \ ../common/rng_qt.cpp \ ../common/server.cpp \