GlobalLobbyClient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "../CServerHandler.h"
  18. #include "../mainmenu/CMainMenu.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/MetaString.h"
  21. #include "../../lib/TextOperations.h"
  22. GlobalLobbyClient::GlobalLobbyClient() = default;
  23. GlobalLobbyClient::~GlobalLobbyClient() = default;
  24. static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
  25. {
  26. // FIXME: better/unified way to format date
  27. auto timeNowChrono = std::chrono::system_clock::now();
  28. timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
  29. return TextOperations::getFormattedTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
  30. }
  31. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
  32. {
  33. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  34. JsonNode json(message.data(), message.size());
  35. if(json["type"].String() == "accountCreated")
  36. return receiveAccountCreated(json);
  37. if(json["type"].String() == "operationFailed")
  38. return receiveOperationFailed(json);
  39. if(json["type"].String() == "loginSuccess")
  40. return receiveLoginSuccess(json);
  41. if(json["type"].String() == "chatHistory")
  42. return receiveChatHistory(json);
  43. if(json["type"].String() == "chatMessage")
  44. return receiveChatMessage(json);
  45. if(json["type"].String() == "activeAccounts")
  46. return receiveActiveAccounts(json);
  47. if(json["type"].String() == "activeGameRooms")
  48. return receiveActiveGameRooms(json);
  49. if(json["type"].String() == "joinRoomSuccess")
  50. return receiveJoinRoomSuccess(json);
  51. throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
  52. }
  53. void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
  54. {
  55. auto loginWindowPtr = loginWindow.lock();
  56. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  57. throw std::runtime_error("lobby connection finished without active login window!");
  58. {
  59. Settings configID = settings.write["lobby"]["accountID"];
  60. configID->String() = json["accountID"].String();
  61. Settings configName = settings.write["lobby"]["displayName"];
  62. configName->String() = json["displayName"].String();
  63. Settings configCookie = settings.write["lobby"]["accountCookie"];
  64. configCookie->String() = json["accountCookie"].String();
  65. }
  66. sendClientLogin();
  67. }
  68. void GlobalLobbyClient::receiveOperationFailed(const JsonNode & json)
  69. {
  70. auto loginWindowPtr = loginWindow.lock();
  71. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  72. throw std::runtime_error("lobby connection finished without active login window!");
  73. loginWindowPtr->onConnectionFailed(json["reason"].String());
  74. }
  75. void GlobalLobbyClient::receiveLoginSuccess(const JsonNode & json)
  76. {
  77. {
  78. Settings configCookie = settings.write["lobby"]["accountCookie"];
  79. configCookie->String() = json["accountCookie"].String();
  80. Settings configName = settings.write["lobby"]["displayName"];
  81. configName->String() = json["displayName"].String();
  82. }
  83. auto loginWindowPtr = loginWindow.lock();
  84. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  85. throw std::runtime_error("lobby connection finished without active login window!");
  86. loginWindowPtr->onConnectionSuccess();
  87. }
  88. void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
  89. {
  90. for(const auto & entry : json["messages"].Vector())
  91. {
  92. std::string accountID = entry["accountID"].String();
  93. std::string displayName = entry["displayName"].String();
  94. std::string messageText = entry["messageText"].String();
  95. int ageSeconds = entry["ageSeconds"].Integer();
  96. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  97. auto lobbyWindowPtr = lobbyWindow.lock();
  98. if(lobbyWindowPtr)
  99. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  100. }
  101. }
  102. void GlobalLobbyClient::receiveChatMessage(const JsonNode & json)
  103. {
  104. std::string accountID = json["accountID"].String();
  105. std::string displayName = json["displayName"].String();
  106. std::string messageText = json["messageText"].String();
  107. std::string timeFormatted = getCurrentTimeFormatted();
  108. auto lobbyWindowPtr = lobbyWindow.lock();
  109. if(lobbyWindowPtr)
  110. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  111. }
  112. void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
  113. {
  114. activeAccounts.clear();
  115. for (auto const & jsonEntry : json["accounts"].Vector())
  116. {
  117. GlobalLobbyAccount account;
  118. account.accountID = jsonEntry["accountID"].String();
  119. account.displayName = jsonEntry["displayName"].String();
  120. account.status = jsonEntry["status"].String();
  121. activeAccounts.push_back(account);
  122. }
  123. auto lobbyWindowPtr = lobbyWindow.lock();
  124. if(lobbyWindowPtr)
  125. lobbyWindowPtr->onActiveAccounts(activeAccounts);
  126. }
  127. void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
  128. {
  129. activeRooms.clear();
  130. for (auto const & jsonEntry : json["gameRooms"].Vector())
  131. {
  132. GlobalLobbyRoom room;
  133. room.gameRoomID = jsonEntry["gameRoomID"].String();
  134. room.hostAccountID = jsonEntry["hostAccountID"].String();
  135. room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
  136. room.description = jsonEntry["description"].String();
  137. // room.status = jsonEntry["status"].String();
  138. room.playersCount = jsonEntry["playersCount"].Integer();
  139. room.playersLimit = jsonEntry["playersLimit"].Integer();
  140. activeRooms.push_back(room);
  141. }
  142. auto lobbyWindowPtr = lobbyWindow.lock();
  143. if(lobbyWindowPtr)
  144. lobbyWindowPtr->onActiveRooms(activeRooms);
  145. }
  146. void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
  147. {
  148. Settings configRoom = settings.write["lobby"]["roomID"];
  149. configRoom->String() = json["gameRoomID"].String();
  150. if (json["proxyMode"].Bool())
  151. {
  152. CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {});
  153. CSH->loadMode = ELoadMode::MULTI;
  154. std::string hostname = settings["lobby"]["hostname"].String();
  155. int16_t port = settings["lobby"]["port"].Integer();
  156. CSH->connectToServer(hostname, port);
  157. }
  158. }
  159. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  160. {
  161. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  162. networkConnection = connection;
  163. JsonNode toSend;
  164. std::string accountID = settings["lobby"]["accountID"].String();
  165. if(accountID.empty())
  166. sendClientRegister();
  167. else
  168. sendClientLogin();
  169. }
  170. void GlobalLobbyClient::sendClientRegister()
  171. {
  172. JsonNode toSend;
  173. toSend["type"].String() = "clientRegister";
  174. toSend["displayName"] = settings["lobby"]["displayName"];
  175. sendMessage(toSend);
  176. }
  177. void GlobalLobbyClient::sendClientLogin()
  178. {
  179. JsonNode toSend;
  180. toSend["type"].String() = "clientLogin";
  181. toSend["accountID"] = settings["lobby"]["accountID"];
  182. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  183. sendMessage(toSend);
  184. }
  185. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  186. {
  187. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  188. auto loginWindowPtr = loginWindow.lock();
  189. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  190. throw std::runtime_error("lobby connection failed without active login window!");
  191. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  192. loginWindowPtr->onConnectionFailed(errorMessage);
  193. }
  194. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  195. {
  196. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  197. assert(connection == networkConnection);
  198. networkConnection.reset();
  199. GH.windows().popWindows(1);
  200. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  201. }
  202. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  203. {
  204. networkConnection->sendPacket(data.toBytes(true));
  205. }
  206. void GlobalLobbyClient::sendOpenPublicRoom()
  207. {
  208. JsonNode toSend;
  209. toSend["type"].String() = "openGameRoom";
  210. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  211. toSend["roomType"].String() = "public";
  212. sendMessage(toSend);
  213. }
  214. void GlobalLobbyClient::sendOpenPrivateRoom()
  215. {
  216. JsonNode toSend;
  217. toSend["type"].String() = "openGameRoom";
  218. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  219. toSend["roomType"].String() = "private";
  220. sendMessage(toSend);
  221. }
  222. void GlobalLobbyClient::connect()
  223. {
  224. std::string hostname = settings["lobby"]["hostname"].String();
  225. int16_t port = settings["lobby"]["port"].Integer();
  226. CSH->networkHandler->connectToRemote(*this, hostname, port);
  227. }
  228. bool GlobalLobbyClient::isConnected()
  229. {
  230. return networkConnection != nullptr;
  231. }
  232. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  233. {
  234. auto loginWindowPtr = loginWindow.lock();
  235. if(loginWindowPtr)
  236. return loginWindowPtr;
  237. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  238. loginWindow = loginWindowNew;
  239. return loginWindowNew;
  240. }
  241. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  242. {
  243. auto lobbyWindowPtr = lobbyWindow.lock();
  244. if(lobbyWindowPtr)
  245. return lobbyWindowPtr;
  246. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  247. lobbyWindow = lobbyWindowPtr;
  248. lobbyWindowLock = lobbyWindowPtr;
  249. return lobbyWindowPtr;
  250. }
  251. const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
  252. {
  253. return activeAccounts;
  254. }
  255. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
  256. {
  257. return activeRooms;
  258. }
  259. void GlobalLobbyClient::activateInterface()
  260. {
  261. if (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  262. return;
  263. if (!GH.windows().findWindows<GlobalLobbyLoginWindow>().empty())
  264. return;
  265. if (isConnected())
  266. GH.windows().pushWindow(createLobbyWindow());
  267. else
  268. GH.windows().pushWindow(createLoginWindow());
  269. }
  270. void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
  271. {
  272. JsonNode toSend;
  273. toSend["type"].String() = "clientProxyLogin";
  274. toSend["accountID"] = settings["lobby"]["accountID"];
  275. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  276. toSend["gameRoomID"] = settings["lobby"]["roomID"];
  277. netConnection->sendPacket(toSend.toBytes(true));
  278. }