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)
{
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<int32_t> reply, QueryID queryID)
{
ASSERT_IF_CALLED_WITH_PLAYER
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 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 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<int32_t> 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

View File

@ -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();

View File

@ -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<int32_t> Reply)
: qid(QID)
, reply(Reply)
{
}
QueryID qid;
PlayerColor player;
JsonNode reply;
std::optional<int32_t> reply;
virtual void visitTyped(ICPackVisitor & visitor) override;

View File

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

View File

@ -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<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);
if(o == nullptr)

View File

@ -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<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

View File

@ -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<int32_t> answer, PlayerColor player)
{
boost::unique_lock<boost::recursive_mutex> 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);

View File

@ -178,7 +178,7 @@ public:
bool hasPlayerAt(PlayerColor player, std::shared_ptr<CConnection> c) 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 setFormation( ObjectInstanceID hid, ui8 formation );
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);
}
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;
gh->queries->addQuery(query);
gh->sendAndApply(request);

View File

@ -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<void(const JsonNode &)> callback) override;
void genericQuery(Query * request, PlayerColor color, std::function<void(std::optional<int32_t>)> callback) override;
private:
CGameHandler * gh;
};
};

View File

@ -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;
}

View File

@ -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<const CArmedInstance *,2> belligerents;

View File

@ -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<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;
}
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<int32_t> 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<void(const JsonNode &)> Callback):
CQuery(Owner), callback(Callback)
CGenericQuery::CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> 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<int32_t> reply)
{
this->reply = reply;
}

View File

@ -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<PlayerColor> 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<int32_t> 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<int32_t> reply) override;
protected:
std::optional<ui32> answer;
};
@ -87,14 +78,14 @@ protected:
class CGenericQuery : public CQuery
{
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 endsByPlayerAnswer() const 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;
private:
std::function<void(const JsonNode &)> callback;
JsonNode reply;
std::function<void(std::optional<int32_t>)> callback;
std::optional<int32_t> reply;
};

View File

@ -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);
}

View File

@ -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;

View File

@ -126,7 +126,7 @@ public:
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:
}