From 6344b987de742cd218ba5cc6f9748364f70e6d84 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sun, 1 Jan 2012 18:15:32 +0100 Subject: [PATCH] fixed pw loyalty in oracle, PB code working --- cockatrice/src/carddatabase.h | 2 +- common/get_pb_extension.cpp | 13 +++++++ common/get_pb_extension.h | 12 +++++++ common/protocol.h | 22 +++++++----- common/server_player.cpp | 5 +-- common/server_protocolhandler.cpp | 56 ++++++++++++------------------- oracle/oracle.pro | 1 + oracle/src/oracleimporter.cpp | 7 ++-- 8 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 common/get_pb_extension.cpp create mode 100644 common/get_pb_extension.h diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index fe892a03..8c521fe4 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -129,7 +129,7 @@ public: const QString &_text = QString(), const QStringList &_colors = QStringList(), int _loyalty = 0, - bool cipt = false, + bool _cipt = false, int _tableRow = 0, const SetList &_sets = SetList(), const QStringMap &_picURLs = QStringMap(), diff --git a/common/get_pb_extension.cpp b/common/get_pb_extension.cpp new file mode 100644 index 00000000..d561f0c8 --- /dev/null +++ b/common/get_pb_extension.cpp @@ -0,0 +1,13 @@ +#include "get_pb_extension.h" +#include +#include + +int getPbExtension(const ::google::protobuf::Message &message) +{ + std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; + message.GetReflection()->ListFields(message, &fieldList); + for (unsigned int j = 0; j < fieldList.size(); ++j) + if (fieldList[j]->is_extension()) + return fieldList[j]->number(); + return -1; +} diff --git a/common/get_pb_extension.h b/common/get_pb_extension.h new file mode 100644 index 00000000..c55aa937 --- /dev/null +++ b/common/get_pb_extension.h @@ -0,0 +1,12 @@ +#ifndef GET_PB_EXTENSION_H +#define GET_PB_EXTENSION_H + +namespace google { + namespace protobuf { + class Message; + } +} + +int getPbExtension(const ::google::protobuf::Message &message); + +#endif diff --git a/common/protocol.h b/common/protocol.h index 52106efa..6277d349 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -9,13 +9,14 @@ #include #include +#include #include "pb/server_message.pb.h" class DeckList; class GameEvent; class GameEventContainer; class GameEventContext; - +#include static const int protocolVersion = 13; class GameEventStorageItem { @@ -23,21 +24,20 @@ public: enum EventRecipient { SendToPrivate = 0x01, SendToOthers = 0x02}; Q_DECLARE_FLAGS(EventRecipients, EventRecipient) private: - ::google::protobuf::Message *event; - int playerId; + GameEvent *event; EventRecipients recipients; public: GameEventStorageItem(const ::google::protobuf::Message &_event, int _playerId, EventRecipients _recipients) - : event(_event.New()), playerId(_playerId), recipients(_recipients) + : event(new GameEvent), recipients(_recipients) { - event->CopyFrom(_event); + event->GetReflection()->MutableMessage(event, _event.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(_event); + event->set_player_id(_playerId); } ~GameEventStorageItem() { delete event; } - const ::google::protobuf::Message &getEvent() const { return *event; } - int getPlayerId() const { return playerId; } + const GameEvent &getGameEvent() const { return *event; } EventRecipients getRecipients() const { return recipients; } }; Q_DECLARE_OPERATORS_FOR_FLAGS(GameEventStorageItem::EventRecipients) @@ -59,9 +59,13 @@ public: delete gameEventList[i]; } - void setGameEventContext(::google::protobuf::Message *_gameEventContext) { gameEventContext = _gameEventContext; } + void setGameEventContext(const ::google::protobuf::Message &_gameEventContext) { + delete gameEventContext; + gameEventContext = new GameEventContext; + gameEventContext->GetReflection()->MutableMessage(gameEventContext, _gameEventContext.GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(_gameEventContext); + } ::google::protobuf::Message *getGameEventContext() const { return gameEventContext; } - + const QList &getGameEventList() const { return gameEventList; } int getPrivatePlayerId() const { return privatePlayerId; } void enqueueGameEvent(const ::google::protobuf::Message &event, int playerId, GameEventStorageItem::EventRecipients recipients = GameEventStorageItem::SendToPrivate | GameEventStorageItem::SendToOthers, int _privatePlayerId = -1) diff --git a/common/server_player.cpp b/common/server_player.cpp index 1f1d4aaa..045fc162 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -419,7 +419,7 @@ Response::ResponseCode Server_Player::moveCard(GameEventStorage &ges, Server_Car Event_DestroyCard event; event.set_zone_name(startzone->getName().toStdString()); event.set_card_id(card->getId()); - ges.setGameEventContext(new Context_MoveCard); + ges.setGameEventContext(Context_MoveCard()); ges.enqueueGameEvent(event, playerId); card->deleteLater(); @@ -561,7 +561,8 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c Event_SetCardAttr event; event.set_zone_name(zone->getName().toStdString()); - event.set_card_id(cardId); + if (cardId != -1) + event.set_card_id(cardId); event.set_attr_name(attrName.toStdString()); event.set_attr_value(result.toStdString()); ges.enqueueGameEvent(event, playerId); diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 688400d7..f0eaaf69 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -345,35 +345,23 @@ Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const if ((resp != Response::RespOk) && (resp != Response::RespNothing)) finalResponseCode = resp; } - /* - * XXX konvertieren zu GameEventStorage (oben deklariert als "ges") - * - GameEventContainer *gQPublic = bla->getGameEventQueuePublic(); - if (gQPublic) { - gameListMutex.lock(); - QPair gamePlayerPair = games.value(gQPublic->game_id()); - if (gamePlayerPair.first) { - GameEventContainer *gQPrivate = bla->getGameEventQueuePrivate(); - GameEventContainer *gQOmniscient = bla->getGameEventQueueOmniscient(); - if (gQPrivate) { - int privatePlayerId = bla->getPrivatePlayerId(); - Server_Player *privatePlayer; - if (privatePlayerId == -1) - privatePlayer = gamePlayerPair.second; - else - privatePlayer = gamePlayerPair.first->getPlayer(privatePlayerId); - if (gQOmniscient) { - gamePlayerPair.first->sendGameEventContainer(gQPublic, privatePlayer, true); - gamePlayerPair.first->sendGameEventContainerOmniscient(gQOmniscient, privatePlayer); - } else - gamePlayerPair.first->sendGameEventContainer(gQPublic, privatePlayer); - privatePlayer->sendProtocolItem(gQPrivate); - } else - gamePlayerPair.first->sendGameEventContainer(gQPublic); - } - gameListMutex.unlock(); + GameEventContainer *contPrivate = new GameEventContainer; + GameEventContainer *contOthers = new GameEventContainer; + const QList &gameEventList = ges.getGameEventList(); + for (int i = 0; i < gameEventList.size(); ++i) { + const GameEvent &event = gameEventList[i]->getGameEvent(); + const GameEventStorageItem::EventRecipients recipients = gameEventList[i]->getRecipients(); + if (recipients.testFlag(GameEventStorageItem::SendToPrivate)) + contPrivate->add_event_list()->CopyFrom(event); + if (recipients.testFlag(GameEventStorageItem::SendToOthers)) + contOthers->add_event_list()->CopyFrom(event); } - */ + if (ges.getGameEventContext()) { + contPrivate->mutable_context()->CopyFrom(*ges.getGameEventContext()); + contOthers->mutable_context()->CopyFrom(*ges.getGameEventContext()); + } + game->sendGameEventContainer(contPrivate, GameEventStorageItem::SendToPrivate, ges.getPrivatePlayerId()); + game->sendGameEventContainer(contOthers, GameEventStorageItem::SendToOthers, ges.getPrivatePlayerId()); return finalResponseCode; } @@ -871,8 +859,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdDeckSelect(const Command_DeckS event.mutable_player_properties()->CopyFrom(player->getProperties()); ges.enqueueGameEvent(event, player->getPlayerId()); - Context_DeckSelect *context = new Context_DeckSelect; - context->set_deck_hash(deck->getDeckHash().toStdString()); + Context_DeckSelect context; + context.set_deck_hash(deck->getDeckHash().toStdString()); ges.setGameEventContext(context); Response_DeckDownload *re = new Response_DeckDownload; @@ -921,7 +909,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdConcede(const Command_Concede Event_PlayerPropertiesChanged event; event.mutable_player_properties()->CopyFrom(player->getProperties()); ges.enqueueGameEvent(event, player->getPlayerId()); - ges.setGameEventContext(new Context_Concede()); + ges.setGameEventContext(Context_Concede()); game->stopGameIfFinished(); if (game->getGameStarted() && (game->getActivePlayer() == player->getPlayerId())) @@ -946,7 +934,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdReadyStart(const Command_Ready Event_PlayerPropertiesChanged event; event.mutable_player_properties()->CopyFrom(player->getProperties()); ges.enqueueGameEvent(event, player->getPlayerId()); - ges.setGameEventContext(new Context_ReadyStart()); + ges.setGameEventContext(Context_ReadyStart()); game->startGameIfReady(); return Response::RespOk; @@ -1012,8 +1000,8 @@ Response::ResponseCode Server_ProtocolHandler::cmdMulligan(const Command_Mulliga if (number == player->getInitialCards()) number = -1; - Context_Mulligan *context = new Context_Mulligan; - context->set_number(number); + Context_Mulligan context; + context.set_number(number); ges.setGameEventContext(context); return Response::RespOk; diff --git a/oracle/oracle.pro b/oracle/oracle.pro index b2d1b41f..89bfe13f 100644 --- a/oracle/oracle.pro +++ b/oracle/oracle.pro @@ -4,6 +4,7 @@ DEPENDPATH += . src INCLUDEPATH += . src ../cockatrice/src MOC_DIR = build OBJECTS_DIR = build +QMAKE_CXXFLAGS_RELEASE += -O2 QT += network svg xml HEADERS += src/oracleimporter.h src/window_main.h ../cockatrice/src/carddatabase.h ../cockatrice/src/settingscache.h diff --git a/oracle/src/oracleimporter.cpp b/oracle/src/oracleimporter.cpp index 18b1be49..c51a2354 100644 --- a/oracle/src/oracleimporter.cpp +++ b/oracle/src/oracleimporter.cpp @@ -124,7 +124,7 @@ CardInfo *OracleImporter::addCard(const QString &setName, bool cipt = (cardText.contains(cardName + " enters the battlefield tapped.")); - card = new CardInfo(this, cardName, cardCost, cardType, cardPT, fullCardText, colors, cipt); + card = new CardInfo(this, cardName, cardCost, cardType, cardPT, fullCardText, colors, cardLoyalty, cipt); int tableRow = 1; QString mainCardType = card->getMainCardType(); if ((mainCardType == "Land") || mArtifact) @@ -134,10 +134,7 @@ CardInfo *OracleImporter::addCard(const QString &setName, else if (mainCardType == "Creature") tableRow = 2; card->setTableRow(tableRow); - - if (mainCardType == "Planeswalker") - card->setLoyalty(cardLoyalty); - + cardHash.insert(cardName, card); } card->setPicURL(setName, getPictureUrl(pictureUrl, cardId, cardName, setName));