CQuery.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/networkPacks/PacksForServer.h"
  16. std::ostream & operator<<(std::ostream & out, const CQuery & query)
  17. {
  18. return out << query.toString();
  19. }
  20. std::ostream & operator<<(std::ostream & out, QueryPtr query)
  21. {
  22. return out << "[" << query.get() << "] " << query->toString();
  23. }
  24. CQuery::CQuery(CGameHandler * gameHandler)
  25. : owner(gameHandler->queries.get())
  26. , gh(gameHandler)
  27. {
  28. static QueryID QID = QueryID(0);
  29. queryID = ++QID;
  30. logGlobal->trace("Created a new query with id %d", queryID);
  31. }
  32. CQuery::~CQuery()
  33. {
  34. logGlobal->trace("Destructed the query with id %d", queryID);
  35. }
  36. void CQuery::addPlayer(PlayerColor color)
  37. {
  38. if(color.isValidPlayer())
  39. players.push_back(color);
  40. }
  41. std::string CQuery::toString() const
  42. {
  43. const auto size = players.size();
  44. const std::string plural = size > 1 ? "s" : "";
  45. std::string names;
  46. for(size_t i = 0; i < size; i++)
  47. {
  48. names += boost::to_upper_copy<std::string>(players[i].toString());
  49. if(i < size - 2)
  50. names += ", ";
  51. else if(size > 1 && i == size - 2)
  52. names += " and ";
  53. }
  54. std::string ret = boost::str(boost::format("A query of type '%s' and qid = %d affecting player%s %s")
  55. % typeid(*this).name()
  56. % queryID
  57. % plural
  58. % names
  59. );
  60. return ret;
  61. }
  62. bool CQuery::endsByPlayerAnswer() const
  63. {
  64. return false;
  65. }
  66. void CQuery::onRemoval(PlayerColor color)
  67. {
  68. }
  69. bool CQuery::blocksPack(const CPack * pack) const
  70. {
  71. return false;
  72. }
  73. void CQuery::notifyObjectAboutRemoval(const CObjectVisitQuery & objectVisit) const
  74. {
  75. }
  76. void CQuery::onExposure(QueryPtr topQuery)
  77. {
  78. logGlobal->trace("Exposed query with id %d", queryID);
  79. owner->popQuery(*this);
  80. }
  81. void CQuery::onAdding(PlayerColor color)
  82. {
  83. }
  84. void CQuery::onAdded(PlayerColor color)
  85. {
  86. }
  87. void CQuery::setReply(std::optional<int32_t> reply)
  88. {
  89. }
  90. bool CQuery::blockAllButReply(const CPack * pack) const
  91. {
  92. //We accept only query replies from correct player
  93. if(auto reply = dynamic_ptr_cast<QueryReply>(pack))
  94. return !vstd::contains(players, reply->player);
  95. return true;
  96. }
  97. CDialogQuery::CDialogQuery(CGameHandler * owner):
  98. CQuery(owner)
  99. {
  100. }
  101. bool CDialogQuery::endsByPlayerAnswer() const
  102. {
  103. return true;
  104. }
  105. bool CDialogQuery::blocksPack(const CPack * pack) const
  106. {
  107. return blockAllButReply(pack);
  108. }
  109. void CDialogQuery::setReply(std::optional<int32_t> reply)
  110. {
  111. if(reply.has_value())
  112. answer = *reply;
  113. }
  114. CGenericQuery::CGenericQuery(CGameHandler * gh, PlayerColor color, std::function<void(std::optional<int32_t>)> Callback):
  115. CQuery(gh), callback(Callback)
  116. {
  117. addPlayer(color);
  118. }
  119. bool CGenericQuery::blocksPack(const CPack * pack) const
  120. {
  121. return blockAllButReply(pack);
  122. }
  123. bool CGenericQuery::endsByPlayerAnswer() const
  124. {
  125. return true;
  126. }
  127. void CGenericQuery::onExposure(QueryPtr topQuery)
  128. {
  129. //do nothing
  130. }
  131. void CGenericQuery::setReply(std::optional<int32_t> reply)
  132. {
  133. this->reply = reply;
  134. }
  135. void CGenericQuery::onRemoval(PlayerColor color)
  136. {
  137. callback(reply);
  138. }