CQuery.cpp 3.2 KB

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