diff --git a/CCallback.cpp b/CCallback.cpp index 4768aaae8..d70af295b 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -41,12 +41,10 @@ bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit) int CCallback::selectionMade(int selection, QueryID queryID) { - JsonNode reply(JsonNode::JsonType::DATA_INTEGER); - reply.Integer() = selection; - return sendQueryReply(reply, queryID); + return sendQueryReply(selection, queryID); } -int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID) +int CCallback::sendQueryReply(std::optional reply, QueryID queryID) { ASSERT_IF_CALLED_WITH_PLAYER if(queryID == QueryID(-1)) diff --git a/CCallback.h b/CCallback.h index 8fb7970f9..32b180828 100644 --- a/CCallback.h +++ b/CCallback.h @@ -82,7 +82,7 @@ public: virtual void trade(const IMarket * market, EMarketMode mode, const std::vector & id1, const std::vector & id2, const std::vector & val1, const CGHeroInstance * hero = nullptr)=0; virtual int selectionMade(int selection, QueryID queryID) =0; - virtual int sendQueryReply(const JsonNode & reply, QueryID queryID) =0; + virtual int sendQueryReply(std::optional reply, QueryID queryID) =0; virtual int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//swaps creatures between two possibly different garrisons // TODO: AI-unsafe code - fix it! virtual int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)=0;//joins first stack to the second (creatures must be same type) virtual int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) =0; //first goes to the second @@ -159,7 +159,7 @@ public: bool moveHero(const CGHeroInstance *h, int3 dst, bool transit = false) override; //dst must be free, neighbouring tile (this function can move hero only by one tile) bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where); int selectionMade(int selection, QueryID queryID) override; - int sendQueryReply(const JsonNode & reply, QueryID queryID) override; + int sendQueryReply(std::optional reply, QueryID queryID) override; int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; int mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index 67c7c4d8e..77735e69e 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -1063,15 +1063,12 @@ void CPlayerInterface::showMapObjectSelectDialog(QueryID askID, const Component auto selectCallback = [=](int selection) { - JsonNode reply(JsonNode::JsonType::DATA_INTEGER); - reply.Integer() = selection; - cb->sendQueryReply(reply, askID); + cb->sendQueryReply(selection, askID); }; auto cancelCallback = [=]() { - JsonNode reply(JsonNode::JsonType::DATA_NULL); - cb->sendQueryReply(reply, askID); + cb->sendQueryReply(std::nullopt, askID); }; const std::string localTitle = title.toString(); diff --git a/lib/NetPacks.h b/lib/NetPacks.h index 093330040..5480dc867 100644 --- a/lib/NetPacks.h +++ b/lib/NetPacks.h @@ -2609,14 +2609,14 @@ struct DLL_LINKAGE BuildBoat : public CPackForServer struct DLL_LINKAGE QueryReply : public CPackForServer { QueryReply() = default; - QueryReply(const QueryID & QID, const JsonNode & Reply) + QueryReply(const QueryID & QID, std::optional Reply) : qid(QID) , reply(Reply) { } QueryID qid; PlayerColor player; - JsonNode reply; + std::optional reply; virtual void visitTyped(ICPackVisitor & visitor) override; diff --git a/lib/pathfinder/CPathfinder.h b/lib/pathfinder/CPathfinder.h index f95b0cbf4..0071f41eb 100644 --- a/lib/pathfinder/CPathfinder.h +++ b/lib/pathfinder/CPathfinder.h @@ -19,7 +19,7 @@ class CGWhirlpool; struct TurnInfo; struct PathfinderOptions; -class CPathfinder +class DLL_LINKAGE CPathfinder { public: friend class CPathfinderHelper; diff --git a/lib/spells/AdventureSpellMechanics.cpp b/lib/spells/AdventureSpellMechanics.cpp index c8fa795c0..5c6dac7a5 100644 --- a/lib/spells/AdventureSpellMechanics.cpp +++ b/lib/spells/AdventureSpellMechanics.cpp @@ -482,11 +482,11 @@ ESpellCastResult TownPortalMechanics::beginCast(SpellCastEnvironment * env, cons if(!parameters.pos.valid() && parameters.caster->getSpellSchoolLevel(owner) >= 2) { - auto queryCallback = [=](const JsonNode & reply) -> void + auto queryCallback = [=](std::optional reply) -> void { - if(reply.getType() == JsonNode::JsonType::DATA_INTEGER) + if(reply.has_value()) { - ObjectInstanceID townId(static_cast(reply.Integer())); + ObjectInstanceID townId(*reply); const CGObjectInstance * o = env->getCb()->getObj(townId, true); if(o == nullptr) diff --git a/lib/spells/ISpellMechanics.h b/lib/spells/ISpellMechanics.h index 7d6e395ca..e3185acd9 100644 --- a/lib/spells/ISpellMechanics.h +++ b/lib/spells/ISpellMechanics.h @@ -56,7 +56,7 @@ public: virtual bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) = 0; //TODO: remove - virtual void genericQuery(Query * request, PlayerColor color, std::function callback) = 0;//TODO: type safety on query, use generic query packet when implemented + virtual void genericQuery(Query * request, PlayerColor color, std::function)> callback) = 0;//TODO: type safety on query, use generic query packet when implemented }; namespace spells diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 8fc6e1a92..b381521dd 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -3120,12 +3120,13 @@ bool CGameHandler::setFormation(ObjectInstanceID hid, ui8 formation) return true; } -bool CGameHandler::queryReply(QueryID qid, const JsonNode & answer, PlayerColor player) +bool CGameHandler::queryReply(QueryID qid, std::optional answer, PlayerColor player) { boost::unique_lock lock(gsm); logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid); - logGlobal->trace(answer.toJson()); + if (answer) + logGlobal->trace("%d", *answer); auto topQuery = queries->topQuery(player); diff --git a/server/CGameHandler.h b/server/CGameHandler.h index b1bf301f5..8309feae1 100644 --- a/server/CGameHandler.h +++ b/server/CGameHandler.h @@ -178,7 +178,7 @@ public: bool hasPlayerAt(PlayerColor player, std::shared_ptr c) const; bool hasBothPlayersAtSameConnection(PlayerColor left, PlayerColor right) const; - bool queryReply( QueryID qid, const JsonNode & answer, PlayerColor player ); + bool queryReply( QueryID qid, std::optional reply, PlayerColor player ); bool buildBoat( ObjectInstanceID objid, PlayerColor player ); bool setFormation( ObjectInstanceID hid, ui8 formation ); bool tradeResources(const IMarket *market, ui32 val, PlayerColor player, ui32 id1, ui32 id2); diff --git a/server/ServerSpellCastEnvironment.cpp b/server/ServerSpellCastEnvironment.cpp index 4f0fe8cba..5f856cae0 100644 --- a/server/ServerSpellCastEnvironment.cpp +++ b/server/ServerSpellCastEnvironment.cpp @@ -93,9 +93,9 @@ bool ServerSpellCastEnvironment::moveHero(ObjectInstanceID hid, int3 dst, bool t return gh->moveHero(hid, dst, teleporting, false); } -void ServerSpellCastEnvironment::genericQuery(Query * request, PlayerColor color, std::function callback) +void ServerSpellCastEnvironment::genericQuery(Query * request, PlayerColor color, std::function)> callback) { - auto query = std::make_shared(gh->queries.get(), color, callback); + auto query = std::make_shared(gh, color, callback); request->queryID = query->queryID; gh->queries->addQuery(query); gh->sendAndApply(request); diff --git a/server/ServerSpellCastEnvironment.h b/server/ServerSpellCastEnvironment.h index 7556783e2..8894160d3 100644 --- a/server/ServerSpellCastEnvironment.h +++ b/server/ServerSpellCastEnvironment.h @@ -36,7 +36,7 @@ public: const CMap * getMap() const override; const CGameInfoCallback * getCb() const override; bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) override; - void genericQuery(Query * request, PlayerColor color, std::function callback) override; + void genericQuery(Query * request, PlayerColor color, std::function)> callback) override; private: CGameHandler * gh; -}; \ No newline at end of file +}; diff --git a/server/queries/BattleQueries.cpp b/server/queries/BattleQueries.cpp index 8092c6bf9..6b18f90f8 100644 --- a/server/queries/BattleQueries.cpp +++ b/server/queries/BattleQueries.cpp @@ -26,7 +26,7 @@ void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisi } CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi): - CGhQuery(owner), + CQuery(owner), battleID(bi->getBattleID()) { belligerents[0] = bi->getSideArmy(0); @@ -37,7 +37,7 @@ CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi): } CBattleQuery::CBattleQuery(CGameHandler * owner): - CGhQuery(owner) + CQuery(owner) { belligerents[0] = belligerents[1] = nullptr; } diff --git a/server/queries/BattleQueries.h b/server/queries/BattleQueries.h index a77805985..ca2077995 100644 --- a/server/queries/BattleQueries.h +++ b/server/queries/BattleQueries.h @@ -17,7 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN class IBattleInfo; VCMI_LIB_NAMESPACE_END -class CBattleQuery : public CGhQuery +class CBattleQuery : public CQuery { public: std::array belligerents; diff --git a/server/queries/CQuery.cpp b/server/queries/CQuery.cpp index cc0025055..c167c7b8a 100644 --- a/server/queries/CQuery.cpp +++ b/server/queries/CQuery.cpp @@ -45,8 +45,9 @@ std::ostream & operator<<(std::ostream & out, QueryPtr query) return out << "[" << query.get() << "] " << query->toString(); } -CQuery::CQuery(QueriesProcessor * Owner): - owner(Owner) +CQuery::CQuery(CGameHandler * gameHandler) + : owner(gameHandler->queries.get()) + , gh(gameHandler) { boost::unique_lock l(QueriesProcessor::mx); @@ -127,7 +128,7 @@ void CQuery::onAdded(PlayerColor color) } -void CQuery::setReply(const JsonNode & reply) +void CQuery::setReply(std::optional reply) { } @@ -141,14 +142,8 @@ bool CQuery::blockAllButReply(const CPack * pack) const return true; } -CGhQuery::CGhQuery(CGameHandler * owner): - CQuery(owner->queries.get()), gh(owner) -{ - -} - CDialogQuery::CDialogQuery(CGameHandler * owner): - CGhQuery(owner) + CQuery(owner) { } @@ -163,14 +158,14 @@ bool CDialogQuery::blocksPack(const CPack * pack) const return blockAllButReply(pack); } -void CDialogQuery::setReply(const JsonNode & reply) +void CDialogQuery::setReply(std::optional reply) { - if(reply.getType() == JsonNode::JsonType::DATA_INTEGER) - answer = reply.Integer(); + if(reply.has_value()) + answer = *reply; } -CGenericQuery::CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function Callback): - CQuery(Owner), callback(Callback) +CGenericQuery::CGenericQuery(CGameHandler * gh, PlayerColor color, std::function)> Callback): + CQuery(gh), callback(Callback) { addPlayer(color); } @@ -190,7 +185,7 @@ void CGenericQuery::onExposure(QueryPtr topQuery) //do nothing } -void CGenericQuery::setReply(const JsonNode & reply) +void CGenericQuery::setReply(std::optional reply) { this->reply = reply; } diff --git a/server/queries/CQuery.h b/server/queries/CQuery.h index 9eddbccdc..5c17c44bc 100644 --- a/server/queries/CQuery.h +++ b/server/queries/CQuery.h @@ -10,7 +10,6 @@ #pragma once #include "../../lib/GameConstants.h" -#include "../../lib/JsonNode.h" VCMI_LIB_NAMESPACE_BEGIN @@ -39,8 +38,7 @@ public: std::vector players; //players that are affected (often "blocked") by query QueryID queryID; - CQuery(QueriesProcessor * Owner); - + CQuery(CGameHandler * gh); virtual bool blocksPack(const CPack *pack) const; //query can block attempting actions by player. Eg. he can't move hero during the battle. @@ -53,11 +51,12 @@ public: virtual void notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const; - virtual void setReply(const JsonNode & reply); + virtual void setReply(std::optional reply); virtual ~CQuery(); protected: QueriesProcessor * owner; + CGameHandler * gh; void addPlayer(PlayerColor color); bool blockAllButReply(const CPack * pack) const; }; @@ -65,21 +64,13 @@ protected: std::ostream &operator<<(std::ostream &out, const CQuery &query); std::ostream &operator<<(std::ostream &out, QueryPtr query); -class CGhQuery : public CQuery -{ -public: - CGhQuery(CGameHandler * owner); -protected: - CGameHandler * gh; -}; - -class CDialogQuery : public CGhQuery +class CDialogQuery : public CQuery { public: CDialogQuery(CGameHandler * owner); virtual bool endsByPlayerAnswer() const override; virtual bool blocksPack(const CPack *pack) const override; - void setReply(const JsonNode & reply) override; + void setReply(std::optional reply) override; protected: std::optional answer; }; @@ -87,14 +78,14 @@ protected: class CGenericQuery : public CQuery { public: - CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function Callback); + CGenericQuery(CGameHandler * gh, PlayerColor color, std::function)> Callback); bool blocksPack(const CPack * pack) const override; bool endsByPlayerAnswer() const override; void onExposure(QueryPtr topQuery) override; - void setReply(const JsonNode & reply) override; + void setReply(std::optional reply) override; void onRemoval(PlayerColor color) override; private: - std::function callback; - JsonNode reply; + std::function)> callback; + std::optional reply; }; diff --git a/server/queries/MapQueries.cpp b/server/queries/MapQueries.cpp index 8772272d6..c19d510fd 100644 --- a/server/queries/MapQueries.cpp +++ b/server/queries/MapQueries.cpp @@ -16,7 +16,7 @@ #include "../../lib/serializer/Cast.h" PlayerStartsTurnQuery::PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player): - CGhQuery(owner) + CQuery(owner) { addPlayer(player); } @@ -42,7 +42,7 @@ bool PlayerStartsTurnQuery::endsByPlayerAnswer() const } CObjectVisitQuery::CObjectVisitQuery(CGameHandler * owner, const CGObjectInstance * Obj, const CGHeroInstance * Hero, int3 Tile): - CGhQuery(owner), visitedObject(Obj), visitingHero(Hero), tile(Tile), removeObjectAfterVisit(false) + CQuery(owner), visitedObject(Obj), visitingHero(Hero), tile(Tile), removeObjectAfterVisit(false) { addPlayer(Hero->tempOwner); } @@ -213,7 +213,7 @@ void CCommanderLevelUpDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQu } CHeroMovementQuery::CHeroMovementQuery(CGameHandler * owner, const TryMoveHero & Tmh, const CGHeroInstance * Hero, bool VisitDestAfterVictory): - CGhQuery(owner), tmh(Tmh), visitDestAfterVictory(VisitDestAfterVictory), hero(Hero) + CQuery(owner), tmh(Tmh), visitDestAfterVictory(VisitDestAfterVictory), hero(Hero) { players.push_back(hero->tempOwner); } diff --git a/server/queries/MapQueries.h b/server/queries/MapQueries.h index dc6890f75..e2b21cae0 100644 --- a/server/queries/MapQueries.h +++ b/server/queries/MapQueries.h @@ -17,7 +17,7 @@ class TurnTimerHandler; //Created when player starts turn //Removed when player accepts a turn -class PlayerStartsTurnQuery : public CGhQuery +class PlayerStartsTurnQuery : public CQuery { public: PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player); @@ -30,7 +30,7 @@ public: //Created when hero visits object. //Removed when query above is resolved (or immediately after visit if no queries were created) -class CObjectVisitQuery : public CGhQuery +class CObjectVisitQuery : public CQuery { public: const CGObjectInstance *visitedObject; @@ -47,7 +47,7 @@ public: //Created when hero attempts move and something happens //(not necessarily position change, could be just an object interaction). -class CHeroMovementQuery : public CGhQuery +class CHeroMovementQuery : public CQuery { public: TryMoveHero tmh; diff --git a/test/game/CGameStateTest.cpp b/test/game/CGameStateTest.cpp index efee6774d..2b6498943 100644 --- a/test/game/CGameStateTest.cpp +++ b/test/game/CGameStateTest.cpp @@ -126,7 +126,7 @@ public: return false; } - void genericQuery(Query * request, PlayerColor color, std::function callback) override + void genericQuery(Query * request, PlayerColor color, std::function)> callback) override { //todo: }