CQuery.cpp 3.2 KB

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