1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-02 00:10:22 +02:00

Use optional instead of Json for queries

This commit is contained in:
Ivan Savenko 2023-09-19 23:17:25 +03:00
parent 541f7590c5
commit d257fb37f0
18 changed files with 51 additions and 69 deletions

View File

@ -41,12 +41,10 @@ bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit)
int CCallback::selectionMade(int selection, QueryID queryID) int CCallback::selectionMade(int selection, QueryID queryID)
{ {
JsonNode reply(JsonNode::JsonType::DATA_INTEGER); return sendQueryReply(selection, queryID);
reply.Integer() = selection;
return sendQueryReply(reply, queryID);
} }
int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID) int CCallback::sendQueryReply(std::optional<int32_t> reply, QueryID queryID)
{ {
ASSERT_IF_CALLED_WITH_PLAYER ASSERT_IF_CALLED_WITH_PLAYER
if(queryID == QueryID(-1)) if(queryID == QueryID(-1))

View File

@ -82,7 +82,7 @@ public:
virtual void trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr)=0; virtual void trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero = nullptr)=0;
virtual int selectionMade(int selection, QueryID queryID) =0; virtual int selectionMade(int selection, QueryID queryID) =0;
virtual int sendQueryReply(const JsonNode & reply, QueryID queryID) =0; virtual int sendQueryReply(std::optional<int32_t> 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 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 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 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 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); bool teleportHero(const CGHeroInstance *who, const CGTownInstance *where);
int selectionMade(int selection, QueryID queryID) override; int selectionMade(int selection, QueryID queryID) override;
int sendQueryReply(const JsonNode & reply, QueryID queryID) override; int sendQueryReply(std::optional<int32_t> reply, QueryID queryID) override;
int swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) 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 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 int mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2) override; //first goes to the second

View File

@ -1063,15 +1063,12 @@ void CPlayerInterface::showMapObjectSelectDialog(QueryID askID, const Component
auto selectCallback = [=](int selection) auto selectCallback = [=](int selection)
{ {
JsonNode reply(JsonNode::JsonType::DATA_INTEGER); cb->sendQueryReply(selection, askID);
reply.Integer() = selection;
cb->sendQueryReply(reply, askID);
}; };
auto cancelCallback = [=]() auto cancelCallback = [=]()
{ {
JsonNode reply(JsonNode::JsonType::DATA_NULL); cb->sendQueryReply(std::nullopt, askID);
cb->sendQueryReply(reply, askID);
}; };
const std::string localTitle = title.toString(); const std::string localTitle = title.toString();

View File

@ -2609,14 +2609,14 @@ struct DLL_LINKAGE BuildBoat : public CPackForServer
struct DLL_LINKAGE QueryReply : public CPackForServer struct DLL_LINKAGE QueryReply : public CPackForServer
{ {
QueryReply() = default; QueryReply() = default;
QueryReply(const QueryID & QID, const JsonNode & Reply) QueryReply(const QueryID & QID, std::optional<int32_t> Reply)
: qid(QID) : qid(QID)
, reply(Reply) , reply(Reply)
{ {
} }
QueryID qid; QueryID qid;
PlayerColor player; PlayerColor player;
JsonNode reply; std::optional<int32_t> reply;
virtual void visitTyped(ICPackVisitor & visitor) override; virtual void visitTyped(ICPackVisitor & visitor) override;

View File

@ -19,7 +19,7 @@ class CGWhirlpool;
struct TurnInfo; struct TurnInfo;
struct PathfinderOptions; struct PathfinderOptions;
class CPathfinder class DLL_LINKAGE CPathfinder
{ {
public: public:
friend class CPathfinderHelper; friend class CPathfinderHelper;

View File

@ -482,11 +482,11 @@ ESpellCastResult TownPortalMechanics::beginCast(SpellCastEnvironment * env, cons
if(!parameters.pos.valid() && parameters.caster->getSpellSchoolLevel(owner) >= 2) if(!parameters.pos.valid() && parameters.caster->getSpellSchoolLevel(owner) >= 2)
{ {
auto queryCallback = [=](const JsonNode & reply) -> void auto queryCallback = [=](std::optional<int32_t> reply) -> void
{ {
if(reply.getType() == JsonNode::JsonType::DATA_INTEGER) if(reply.has_value())
{ {
ObjectInstanceID townId(static_cast<si32>(reply.Integer())); ObjectInstanceID townId(*reply);
const CGObjectInstance * o = env->getCb()->getObj(townId, true); const CGObjectInstance * o = env->getCb()->getObj(townId, true);
if(o == nullptr) if(o == nullptr)

View File

@ -56,7 +56,7 @@ public:
virtual bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) = 0; //TODO: remove virtual bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) = 0; //TODO: remove
virtual void genericQuery(Query * request, PlayerColor color, std::function<void(const JsonNode &)> callback) = 0;//TODO: type safety on query, use generic query packet when implemented virtual void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) = 0;//TODO: type safety on query, use generic query packet when implemented
}; };
namespace spells namespace spells

View File

@ -3120,12 +3120,13 @@ bool CGameHandler::setFormation(ObjectInstanceID hid, ui8 formation)
return true; return true;
} }
bool CGameHandler::queryReply(QueryID qid, const JsonNode & answer, PlayerColor player) bool CGameHandler::queryReply(QueryID qid, std::optional<int32_t> answer, PlayerColor player)
{ {
boost::unique_lock<boost::recursive_mutex> lock(gsm); boost::unique_lock<boost::recursive_mutex> lock(gsm);
logGlobal->trace("Player %s attempts answering query %d with answer:", player, qid); 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); auto topQuery = queries->topQuery(player);

View File

@ -178,7 +178,7 @@ public:
bool hasPlayerAt(PlayerColor player, std::shared_ptr<CConnection> c) const; bool hasPlayerAt(PlayerColor player, std::shared_ptr<CConnection> c) const;
bool hasBothPlayersAtSameConnection(PlayerColor left, PlayerColor right) const; bool hasBothPlayersAtSameConnection(PlayerColor left, PlayerColor right) const;
bool queryReply( QueryID qid, const JsonNode & answer, PlayerColor player ); bool queryReply( QueryID qid, std::optional<int32_t> reply, PlayerColor player );
bool buildBoat( ObjectInstanceID objid, PlayerColor player ); bool buildBoat( ObjectInstanceID objid, PlayerColor player );
bool setFormation( ObjectInstanceID hid, ui8 formation ); bool setFormation( ObjectInstanceID hid, ui8 formation );
bool tradeResources(const IMarket *market, ui32 val, PlayerColor player, ui32 id1, ui32 id2); bool tradeResources(const IMarket *market, ui32 val, PlayerColor player, ui32 id1, ui32 id2);

View File

@ -93,9 +93,9 @@ bool ServerSpellCastEnvironment::moveHero(ObjectInstanceID hid, int3 dst, bool t
return gh->moveHero(hid, dst, teleporting, false); return gh->moveHero(hid, dst, teleporting, false);
} }
void ServerSpellCastEnvironment::genericQuery(Query * request, PlayerColor color, std::function<void(const JsonNode&)> callback) void ServerSpellCastEnvironment::genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback)
{ {
auto query = std::make_shared<CGenericQuery>(gh->queries.get(), color, callback); auto query = std::make_shared<CGenericQuery>(gh, color, callback);
request->queryID = query->queryID; request->queryID = query->queryID;
gh->queries->addQuery(query); gh->queries->addQuery(query);
gh->sendAndApply(request); gh->sendAndApply(request);

View File

@ -36,7 +36,7 @@ public:
const CMap * getMap() const override; const CMap * getMap() const override;
const CGameInfoCallback * getCb() const override; const CGameInfoCallback * getCb() const override;
bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) override; bool moveHero(ObjectInstanceID hid, int3 dst, bool teleporting) override;
void genericQuery(Query * request, PlayerColor color, std::function<void(const JsonNode &)> callback) override; void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) override;
private: private:
CGameHandler * gh; CGameHandler * gh;
}; };

View File

@ -26,7 +26,7 @@ void CBattleQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisi
} }
CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi): CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi):
CGhQuery(owner), CQuery(owner),
battleID(bi->getBattleID()) battleID(bi->getBattleID())
{ {
belligerents[0] = bi->getSideArmy(0); belligerents[0] = bi->getSideArmy(0);
@ -37,7 +37,7 @@ CBattleQuery::CBattleQuery(CGameHandler * owner, const IBattleInfo * bi):
} }
CBattleQuery::CBattleQuery(CGameHandler * owner): CBattleQuery::CBattleQuery(CGameHandler * owner):
CGhQuery(owner) CQuery(owner)
{ {
belligerents[0] = belligerents[1] = nullptr; belligerents[0] = belligerents[1] = nullptr;
} }

View File

@ -17,7 +17,7 @@ VCMI_LIB_NAMESPACE_BEGIN
class IBattleInfo; class IBattleInfo;
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END
class CBattleQuery : public CGhQuery class CBattleQuery : public CQuery
{ {
public: public:
std::array<const CArmedInstance *,2> belligerents; std::array<const CArmedInstance *,2> belligerents;

View File

@ -45,8 +45,9 @@ std::ostream & operator<<(std::ostream & out, QueryPtr query)
return out << "[" << query.get() << "] " << query->toString(); return out << "[" << query.get() << "] " << query->toString();
} }
CQuery::CQuery(QueriesProcessor * Owner): CQuery::CQuery(CGameHandler * gameHandler)
owner(Owner) : owner(gameHandler->queries.get())
, gh(gameHandler)
{ {
boost::unique_lock<boost::mutex> l(QueriesProcessor::mx); boost::unique_lock<boost::mutex> l(QueriesProcessor::mx);
@ -127,7 +128,7 @@ void CQuery::onAdded(PlayerColor color)
} }
void CQuery::setReply(const JsonNode & reply) void CQuery::setReply(std::optional<int32_t> reply)
{ {
} }
@ -141,14 +142,8 @@ bool CQuery::blockAllButReply(const CPack * pack) const
return true; return true;
} }
CGhQuery::CGhQuery(CGameHandler * owner):
CQuery(owner->queries.get()), gh(owner)
{
}
CDialogQuery::CDialogQuery(CGameHandler * owner): CDialogQuery::CDialogQuery(CGameHandler * owner):
CGhQuery(owner) CQuery(owner)
{ {
} }
@ -163,14 +158,14 @@ bool CDialogQuery::blocksPack(const CPack * pack) const
return blockAllButReply(pack); return blockAllButReply(pack);
} }
void CDialogQuery::setReply(const JsonNode & reply) void CDialogQuery::setReply(std::optional<int32_t> reply)
{ {
if(reply.getType() == JsonNode::JsonType::DATA_INTEGER) if(reply.has_value())
answer = reply.Integer(); answer = *reply;
} }
CGenericQuery::CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback): CGenericQuery::CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback):
CQuery(Owner), callback(Callback) CQuery(gh), callback(Callback)
{ {
addPlayer(color); addPlayer(color);
} }
@ -190,7 +185,7 @@ void CGenericQuery::onExposure(QueryPtr topQuery)
//do nothing //do nothing
} }
void CGenericQuery::setReply(const JsonNode & reply) void CGenericQuery::setReply(std::optional<int32_t> reply)
{ {
this->reply = reply; this->reply = reply;
} }

View File

@ -10,7 +10,6 @@
#pragma once #pragma once
#include "../../lib/GameConstants.h" #include "../../lib/GameConstants.h"
#include "../../lib/JsonNode.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -39,8 +38,7 @@ public:
std::vector<PlayerColor> players; //players that are affected (often "blocked") by query std::vector<PlayerColor> players; //players that are affected (often "blocked") by query
QueryID queryID; 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. 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 notifyObjectAboutRemoval(const CObjectVisitQuery &objectVisit) const;
virtual void setReply(const JsonNode & reply); virtual void setReply(std::optional<int32_t> reply);
virtual ~CQuery(); virtual ~CQuery();
protected: protected:
QueriesProcessor * owner; QueriesProcessor * owner;
CGameHandler * gh;
void addPlayer(PlayerColor color); void addPlayer(PlayerColor color);
bool blockAllButReply(const CPack * pack) const; 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, const CQuery &query);
std::ostream &operator<<(std::ostream &out, QueryPtr query); std::ostream &operator<<(std::ostream &out, QueryPtr query);
class CGhQuery : public CQuery class CDialogQuery : public CQuery
{
public:
CGhQuery(CGameHandler * owner);
protected:
CGameHandler * gh;
};
class CDialogQuery : public CGhQuery
{ {
public: public:
CDialogQuery(CGameHandler * owner); CDialogQuery(CGameHandler * owner);
virtual bool endsByPlayerAnswer() const override; virtual bool endsByPlayerAnswer() const override;
virtual bool blocksPack(const CPack *pack) const override; virtual bool blocksPack(const CPack *pack) const override;
void setReply(const JsonNode & reply) override; void setReply(std::optional<int32_t> reply) override;
protected: protected:
std::optional<ui32> answer; std::optional<ui32> answer;
}; };
@ -87,14 +78,14 @@ protected:
class CGenericQuery : public CQuery class CGenericQuery : public CQuery
{ {
public: public:
CGenericQuery(QueriesProcessor * Owner, PlayerColor color, std::function<void(const JsonNode &)> Callback); CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback);
bool blocksPack(const CPack * pack) const override; bool blocksPack(const CPack * pack) const override;
bool endsByPlayerAnswer() const override; bool endsByPlayerAnswer() const override;
void onExposure(QueryPtr topQuery) override; void onExposure(QueryPtr topQuery) override;
void setReply(const JsonNode & reply) override; void setReply(std::optional<int32_t> reply) override;
void onRemoval(PlayerColor color) override; void onRemoval(PlayerColor color) override;
private: private:
std::function<void(const JsonNode &)> callback; std::function<void(std::optional<int32_t>)> callback;
JsonNode reply; std::optional<int32_t> reply;
}; };

View File

@ -16,7 +16,7 @@
#include "../../lib/serializer/Cast.h" #include "../../lib/serializer/Cast.h"
PlayerStartsTurnQuery::PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player): PlayerStartsTurnQuery::PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player):
CGhQuery(owner) CQuery(owner)
{ {
addPlayer(player); addPlayer(player);
} }
@ -42,7 +42,7 @@ bool PlayerStartsTurnQuery::endsByPlayerAnswer() const
} }
CObjectVisitQuery::CObjectVisitQuery(CGameHandler * owner, const CGObjectInstance * Obj, const CGHeroInstance * Hero, int3 Tile): 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); addPlayer(Hero->tempOwner);
} }
@ -213,7 +213,7 @@ void CCommanderLevelUpDialogQuery::notifyObjectAboutRemoval(const CObjectVisitQu
} }
CHeroMovementQuery::CHeroMovementQuery(CGameHandler * owner, const TryMoveHero & Tmh, const CGHeroInstance * Hero, bool VisitDestAfterVictory): 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); players.push_back(hero->tempOwner);
} }

View File

@ -17,7 +17,7 @@ class TurnTimerHandler;
//Created when player starts turn //Created when player starts turn
//Removed when player accepts a turn //Removed when player accepts a turn
class PlayerStartsTurnQuery : public CGhQuery class PlayerStartsTurnQuery : public CQuery
{ {
public: public:
PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player); PlayerStartsTurnQuery(CGameHandler * owner, PlayerColor player);
@ -30,7 +30,7 @@ public:
//Created when hero visits object. //Created when hero visits object.
//Removed when query above is resolved (or immediately after visit if no queries were created) //Removed when query above is resolved (or immediately after visit if no queries were created)
class CObjectVisitQuery : public CGhQuery class CObjectVisitQuery : public CQuery
{ {
public: public:
const CGObjectInstance *visitedObject; const CGObjectInstance *visitedObject;
@ -47,7 +47,7 @@ public:
//Created when hero attempts move and something happens //Created when hero attempts move and something happens
//(not necessarily position change, could be just an object interaction). //(not necessarily position change, could be just an object interaction).
class CHeroMovementQuery : public CGhQuery class CHeroMovementQuery : public CQuery
{ {
public: public:
TryMoveHero tmh; TryMoveHero tmh;

View File

@ -126,7 +126,7 @@ public:
return false; return false;
} }
void genericQuery(Query * request, PlayerColor color, std::function<void(const JsonNode &)> callback) override void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) override
{ {
//todo: //todo:
} }