CQuery.cpp 3.9 KB

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