GlobalLobbyClient.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * GlobalLobbyClient.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 "GlobalLobbyClient.h"
  12. #include "GlobalLobbyWindow.h"
  13. #include "GlobalLobbyLoginWindow.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../windows/InfoWindows.h"
  17. #include "../../lib/MetaString.h"
  18. #include "../../lib/CConfigHandler.h"
  19. #include "../../lib/network/NetworkClient.h"
  20. GlobalLobbyClient::~GlobalLobbyClient()
  21. {
  22. networkClient->stop();
  23. networkThread->join();
  24. }
  25. GlobalLobbyClient::GlobalLobbyClient()
  26. : networkClient(std::make_unique<NetworkClient>(*this))
  27. {
  28. networkThread = std::make_unique<boost::thread>([this](){
  29. networkClient->run();
  30. });
  31. }
  32. static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
  33. {
  34. // FIXME: better/unified way to format date
  35. auto timeNowChrono = std::chrono::system_clock::now();
  36. timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
  37. std::time_t timeNowC = std::chrono::system_clock::to_time_t(timeNowChrono);
  38. std::tm timeNowTm = *std::localtime(&timeNowC);
  39. MetaString timeFormatted;
  40. timeFormatted.appendRawString("%d:%d");
  41. timeFormatted.replaceNumber(timeNowTm.tm_hour);
  42. timeFormatted.replaceNumber(timeNowTm.tm_min);
  43. return timeFormatted.toString();
  44. }
  45. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message)
  46. {
  47. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  48. JsonNode json(message.data(), message.size());
  49. if (json["type"].String() == "authentication")
  50. {
  51. auto loginWindowPtr = loginWindow.lock();
  52. if (!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  53. throw std::runtime_error("lobby connection finished without active login window!");
  54. loginWindowPtr->onConnectionSuccess();
  55. }
  56. if (json["type"].String() == "chatHistory")
  57. {
  58. for (auto const & entry : json["messages"].Vector())
  59. {
  60. std::string senderName = entry["senderName"].String();
  61. std::string messageText = entry["messageText"].String();
  62. int ageSeconds = entry["ageSeconds"].Integer();
  63. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  64. auto lobbyWindowPtr = lobbyWindow.lock();
  65. if(lobbyWindowPtr)
  66. lobbyWindowPtr->onGameChatMessage(senderName, messageText, timeFormatted);
  67. }
  68. }
  69. if (json["type"].String() == "chatMessage")
  70. {
  71. std::string senderName = json["senderName"].String();
  72. std::string messageText = json["messageText"].String();
  73. std::string timeFormatted = getCurrentTimeFormatted();
  74. auto lobbyWindowPtr = lobbyWindow.lock();
  75. if(lobbyWindowPtr)
  76. lobbyWindowPtr->onGameChatMessage(senderName, messageText, timeFormatted);
  77. }
  78. }
  79. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<NetworkConnection> &)
  80. {
  81. JsonNode toSend;
  82. toSend["type"].String() = "authentication";
  83. toSend["accountName"].String() = settings["general"]["playerName"].String();
  84. sendMessage(toSend);
  85. }
  86. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  87. {
  88. auto loginWindowPtr = loginWindow.lock();
  89. if (!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  90. throw std::runtime_error("lobby connection failed without active login window!");
  91. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  92. loginWindowPtr->onConnectionFailed(errorMessage);
  93. }
  94. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<NetworkConnection> &)
  95. {
  96. GH.windows().popWindows(1);
  97. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  98. }
  99. void GlobalLobbyClient::onTimer()
  100. {
  101. // no-op
  102. }
  103. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  104. {
  105. std::string payloadString = data.toJson(true);
  106. // FIXME: find better approach
  107. uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
  108. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  109. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  110. networkClient->sendPacket(payloadBuffer);
  111. }
  112. void GlobalLobbyClient::connect()
  113. {
  114. networkClient->start("127.0.0.1", 30303);
  115. }
  116. bool GlobalLobbyClient::isConnected()
  117. {
  118. return networkClient->isConnected();
  119. }
  120. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  121. {
  122. auto loginWindowPtr = loginWindow.lock();
  123. if (loginWindowPtr)
  124. return loginWindowPtr;
  125. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  126. loginWindow = loginWindowNew;
  127. return loginWindowNew;
  128. }
  129. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  130. {
  131. auto lobbyWindowPtr = lobbyWindow.lock();
  132. if (lobbyWindowPtr)
  133. return lobbyWindowPtr;
  134. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  135. lobbyWindow = lobbyWindowPtr;
  136. lobbyWindowLock = lobbyWindowPtr;
  137. return lobbyWindowPtr;
  138. }