GlobalLobbyClient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. logGlobal->error("Received unexpected message from lobby server: %s", 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)
  72. loginWindowPtr->onConnectionFailed(json["reason"].String());
  73. // TODO: handle errors in lobby menu
  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.playersCount = jsonEntry["playersCount"].Integer();
  138. room.playersLimit = jsonEntry["playersLimit"].Integer();
  139. activeRooms.push_back(room);
  140. }
  141. auto lobbyWindowPtr = lobbyWindow.lock();
  142. if(lobbyWindowPtr)
  143. lobbyWindowPtr->onActiveRooms(activeRooms);
  144. }
  145. void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
  146. {
  147. Settings configRoom = settings.write["lobby"]["roomID"];
  148. configRoom->String() = json["gameRoomID"].String();
  149. if (json["proxyMode"].Bool())
  150. {
  151. CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {});
  152. CSH->loadMode = ELoadMode::MULTI;
  153. std::string hostname = settings["lobby"]["hostname"].String();
  154. int16_t port = settings["lobby"]["port"].Integer();
  155. CSH->connectToServer(hostname, port);
  156. }
  157. }
  158. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  159. {
  160. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  161. networkConnection = connection;
  162. JsonNode toSend;
  163. std::string accountID = settings["lobby"]["accountID"].String();
  164. if(accountID.empty())
  165. sendClientRegister();
  166. else
  167. sendClientLogin();
  168. }
  169. void GlobalLobbyClient::sendClientRegister()
  170. {
  171. JsonNode toSend;
  172. toSend["type"].String() = "clientRegister";
  173. toSend["displayName"] = settings["lobby"]["displayName"];
  174. sendMessage(toSend);
  175. }
  176. void GlobalLobbyClient::sendClientLogin()
  177. {
  178. JsonNode toSend;
  179. toSend["type"].String() = "clientLogin";
  180. toSend["accountID"] = settings["lobby"]["accountID"];
  181. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  182. sendMessage(toSend);
  183. }
  184. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  185. {
  186. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  187. auto loginWindowPtr = loginWindow.lock();
  188. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  189. throw std::runtime_error("lobby connection failed without active login window!");
  190. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  191. loginWindowPtr->onConnectionFailed(errorMessage);
  192. }
  193. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  194. {
  195. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  196. assert(connection == networkConnection);
  197. networkConnection.reset();
  198. while (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  199. {
  200. // if global lobby is open, pop all dialogs on top of it as well as lobby itself
  201. GH.windows().popWindows(1);
  202. }
  203. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  204. }
  205. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  206. {
  207. networkConnection->sendPacket(data.toBytes(true));
  208. }
  209. void GlobalLobbyClient::sendOpenPublicRoom()
  210. {
  211. JsonNode toSend;
  212. toSend["type"].String() = "openGameRoom";
  213. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  214. toSend["roomType"].String() = "public";
  215. sendMessage(toSend);
  216. }
  217. void GlobalLobbyClient::sendOpenPrivateRoom()
  218. {
  219. JsonNode toSend;
  220. toSend["type"].String() = "openGameRoom";
  221. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  222. toSend["roomType"].String() = "private";
  223. sendMessage(toSend);
  224. }
  225. void GlobalLobbyClient::connect()
  226. {
  227. std::string hostname = settings["lobby"]["hostname"].String();
  228. int16_t port = settings["lobby"]["port"].Integer();
  229. CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
  230. }
  231. bool GlobalLobbyClient::isConnected()
  232. {
  233. return networkConnection != nullptr;
  234. }
  235. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  236. {
  237. auto loginWindowPtr = loginWindow.lock();
  238. if(loginWindowPtr)
  239. return loginWindowPtr;
  240. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  241. loginWindow = loginWindowNew;
  242. return loginWindowNew;
  243. }
  244. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  245. {
  246. auto lobbyWindowPtr = lobbyWindow.lock();
  247. if(lobbyWindowPtr)
  248. return lobbyWindowPtr;
  249. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  250. lobbyWindow = lobbyWindowPtr;
  251. lobbyWindowLock = lobbyWindowPtr;
  252. return lobbyWindowPtr;
  253. }
  254. const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
  255. {
  256. return activeAccounts;
  257. }
  258. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
  259. {
  260. return activeRooms;
  261. }
  262. void GlobalLobbyClient::activateInterface()
  263. {
  264. if (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  265. return;
  266. if (!GH.windows().findWindows<GlobalLobbyLoginWindow>().empty())
  267. return;
  268. if (isConnected())
  269. GH.windows().pushWindow(createLobbyWindow());
  270. else
  271. GH.windows().pushWindow(createLoginWindow());
  272. }
  273. void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
  274. {
  275. JsonNode toSend;
  276. toSend["type"].String() = "clientProxyLogin";
  277. toSend["accountID"] = settings["lobby"]["accountID"];
  278. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  279. toSend["gameRoomID"] = settings["lobby"]["roomID"];
  280. netConnection->sendPacket(toSend.toBytes(true));
  281. }