GlobalLobbyClient.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "GlobalLobbyLoginWindow.h"
  13. #include "GlobalLobbyWindow.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../windows/InfoWindows.h"
  17. #include "../../lib/CConfigHandler.h"
  18. #include "../../lib/MetaString.h"
  19. #include "../../lib/TextOperations.h"
  20. #include "../../lib/network/NetworkClient.h"
  21. GlobalLobbyClient::~GlobalLobbyClient()
  22. {
  23. networkClient->stop();
  24. networkThread->join();
  25. }
  26. GlobalLobbyClient::GlobalLobbyClient()
  27. : networkClient(std::make_unique<NetworkClient>(*this))
  28. {
  29. networkThread = std::make_unique<boost::thread>([this](){
  30. networkClient->run();
  31. });
  32. }
  33. static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
  34. {
  35. // FIXME: better/unified way to format date
  36. auto timeNowChrono = std::chrono::system_clock::now();
  37. timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
  38. return TextOperations::getFormattedTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
  39. }
  40. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message)
  41. {
  42. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  43. JsonNode json(message.data(), message.size());
  44. if(json["type"].String() == "accountCreated")
  45. return receiveAccountCreated(json);
  46. if(json["type"].String() == "loginFailed")
  47. return receiveLoginFailed(json);
  48. if(json["type"].String() == "loginSuccess")
  49. return receiveLoginSuccess(json);
  50. if(json["type"].String() == "chatHistory")
  51. return receiveChatHistory(json);
  52. if(json["type"].String() == "chatMessage")
  53. return receiveChatMessage(json);
  54. if(json["type"].String() == "activeAccounts")
  55. return receiveActiveAccounts(json);
  56. throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
  57. }
  58. void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
  59. {
  60. auto loginWindowPtr = loginWindow.lock();
  61. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  62. throw std::runtime_error("lobby connection finished without active login window!");
  63. {
  64. Settings configID = settings.write["lobby"]["accountID"];
  65. configID->String() = json["accountID"].String();
  66. Settings configName = settings.write["lobby"]["displayName"];
  67. configName->String() = json["displayName"].String();
  68. Settings configCookie = settings.write["lobby"]["accountCookie"];
  69. configCookie->String() = json["accountCookie"].String();
  70. }
  71. sendClientLogin();
  72. }
  73. void GlobalLobbyClient::receiveLoginFailed(const JsonNode & json)
  74. {
  75. auto loginWindowPtr = loginWindow.lock();
  76. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  77. throw std::runtime_error("lobby connection finished without active login window!");
  78. loginWindowPtr->onConnectionFailed(json["reason"].String());
  79. }
  80. void GlobalLobbyClient::receiveLoginSuccess(const JsonNode & json)
  81. {
  82. {
  83. Settings configCookie = settings.write["lobby"]["accountCookie"];
  84. configCookie->String() = json["accountCookie"].String();
  85. Settings configName = settings.write["lobby"]["displayName"];
  86. configName->String() = json["displayName"].String();
  87. }
  88. auto loginWindowPtr = loginWindow.lock();
  89. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  90. throw std::runtime_error("lobby connection finished without active login window!");
  91. loginWindowPtr->onConnectionSuccess();
  92. }
  93. void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
  94. {
  95. for(const auto & entry : json["messages"].Vector())
  96. {
  97. std::string accountID = entry["accountID"].String();
  98. std::string displayName = entry["displayName"].String();
  99. std::string messageText = entry["messageText"].String();
  100. int ageSeconds = entry["ageSeconds"].Integer();
  101. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  102. auto lobbyWindowPtr = lobbyWindow.lock();
  103. if(lobbyWindowPtr)
  104. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  105. }
  106. }
  107. void GlobalLobbyClient::receiveChatMessage(const JsonNode & json)
  108. {
  109. std::string accountID = json["accountID"].String();
  110. std::string displayName = json["displayName"].String();
  111. std::string messageText = json["messageText"].String();
  112. std::string timeFormatted = getCurrentTimeFormatted();
  113. auto lobbyWindowPtr = lobbyWindow.lock();
  114. if(lobbyWindowPtr)
  115. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  116. }
  117. void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
  118. {
  119. //for (auto const & jsonEntry : json["messages"].Vector())
  120. //{
  121. // std::string accountID = jsonEntry["accountID"].String();
  122. // std::string displayName = jsonEntry["displayName"].String();
  123. //}
  124. }
  125. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<NetworkConnection> &)
  126. {
  127. JsonNode toSend;
  128. std::string accountID = settings["lobby"]["accountID"].String();
  129. if(accountID.empty())
  130. sendClientRegister();
  131. else
  132. sendClientLogin();
  133. }
  134. void GlobalLobbyClient::sendClientRegister()
  135. {
  136. JsonNode toSend;
  137. toSend["type"].String() = "clientRegister";
  138. toSend["displayName"] = settings["lobby"]["displayName"];
  139. sendMessage(toSend);
  140. }
  141. void GlobalLobbyClient::sendClientLogin()
  142. {
  143. JsonNode toSend;
  144. toSend["type"].String() = "clientLogin";
  145. toSend["accountID"] = settings["lobby"]["accountID"];
  146. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  147. sendMessage(toSend);
  148. }
  149. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  150. {
  151. auto loginWindowPtr = loginWindow.lock();
  152. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  153. throw std::runtime_error("lobby connection failed without active login window!");
  154. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  155. loginWindowPtr->onConnectionFailed(errorMessage);
  156. }
  157. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<NetworkConnection> &)
  158. {
  159. GH.windows().popWindows(1);
  160. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  161. }
  162. void GlobalLobbyClient::onTimer()
  163. {
  164. // no-op
  165. }
  166. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  167. {
  168. std::string payloadString = data.toJson(true);
  169. // FIXME: find better approach
  170. uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
  171. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  172. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  173. networkClient->sendPacket(payloadBuffer);
  174. }
  175. void GlobalLobbyClient::connect()
  176. {
  177. std::string hostname = settings["lobby"]["hostname"].String();
  178. int16_t port = settings["lobby"]["port"].Integer();
  179. networkClient->start(hostname, port);
  180. }
  181. bool GlobalLobbyClient::isConnected()
  182. {
  183. return networkClient->isConnected();
  184. }
  185. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  186. {
  187. auto loginWindowPtr = loginWindow.lock();
  188. if(loginWindowPtr)
  189. return loginWindowPtr;
  190. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  191. loginWindow = loginWindowNew;
  192. return loginWindowNew;
  193. }
  194. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  195. {
  196. auto lobbyWindowPtr = lobbyWindow.lock();
  197. if(lobbyWindowPtr)
  198. return lobbyWindowPtr;
  199. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  200. lobbyWindow = lobbyWindowPtr;
  201. lobbyWindowLock = lobbyWindowPtr;
  202. return lobbyWindowPtr;
  203. }