1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* CQuery.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CQuery.h"
#include "QueriesProcessor.h"
#include "../CGameHandler.h"
#include "../../lib/networkPacks/PacksForServer.h"
std::ostream & operator<<(std::ostream & out, const CQuery & query)
{
return out << query.toString();
}
std::ostream & operator<<(std::ostream & out, QueryPtr query)
{
return out << "[" << query.get() << "] " << query->toString();
}
CQuery::CQuery(CGameHandler * gameHandler)
: owner(gameHandler->queries.get())
, gh(gameHandler)
{
static QueryID QID = QueryID(0);
queryID = ++QID;
logGlobal->trace("Created a new query with id %d", queryID);
}
CQuery::~CQuery()
{
logGlobal->trace("Destructed the query with id %d", queryID);
}
void CQuery::addPlayer(PlayerColor color)
{
if(color.isValidPlayer())
players.push_back(color);
}
std::string CQuery::toString() const
{
const auto size = players.size();
const std::string plural = size > 1 ? "s" : "";
std::string names;
for(size_t i = 0; i < size; i++)
{
names += boost::to_upper_copy<std::string>(players[i].toString());
if(i < size - 2)
names += ", ";
else if(size > 1 && i == size - 2)
names += " and ";
}
std::string ret = boost::str(boost::format("A query of type '%s' and qid = %d affecting player%s %s")
% typeid(*this).name()
% queryID
% plural
% names
);
return ret;
}
bool CQuery::endsByPlayerAnswer() const
{
return false;
}
void CQuery::onRemoval(PlayerColor color)
{
}
bool CQuery::blocksPack(const CPackForServer * pack) const
{
return false;
}
void CQuery::notifyObjectAboutRemoval(const CGObjectInstance * visitedObject, const CGHeroInstance * visitingHero) const
{
}
void CQuery::onExposure(QueryPtr topQuery)
{
logGlobal->trace("Exposed query with id %d", queryID);
owner->popQuery(*this);
}
void CQuery::onAdding(PlayerColor color)
{
}
void CQuery::onAdded(PlayerColor color)
{
}
void CQuery::setReply(std::optional<int32_t> reply)
{
}
bool CQuery::blockAllButReply(const CPackForServer * pack) const
{
//We accept only query replies from correct player
if(auto reply = dynamic_cast<const QueryReply*>(pack))
return !vstd::contains(players, reply->player);
return true;
}
CDialogQuery::CDialogQuery(CGameHandler * owner):
CQuery(owner)
{
}
bool CDialogQuery::endsByPlayerAnswer() const
{
return true;
}
bool CDialogQuery::blocksPack(const CPackForServer * pack) const
{
return blockAllButReply(pack);
}
void CDialogQuery::setReply(std::optional<int32_t> reply)
{
if(reply.has_value())
answer = *reply;
}
CGenericQuery::CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback):
CQuery(gh), callback(Callback)
{
addPlayer(color);
}
bool CGenericQuery::blocksPack(const CPackForServer * pack) const
{
return blockAllButReply(pack);
}
bool CGenericQuery::endsByPlayerAnswer() const
{
return true;
}
void CGenericQuery::onExposure(QueryPtr topQuery)
{
//do nothing
}
void CGenericQuery::setReply(std::optional<int32_t> reply)
{
this->reply = reply;
}
void CGenericQuery::onRemoval(PlayerColor color)
{
callback(reply);
}
|