GlobalLobbyClient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. if(json["type"].String() == "inviteReceived")
  52. return receiveInviteReceived(json);
  53. logGlobal->error("Received unexpected message from lobby server: %s", json["type"].String());
  54. }
  55. void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
  56. {
  57. auto loginWindowPtr = loginWindow.lock();
  58. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  59. throw std::runtime_error("lobby connection finished without active login window!");
  60. {
  61. Settings configID = settings.write["lobby"]["accountID"];
  62. configID->String() = json["accountID"].String();
  63. Settings configName = settings.write["lobby"]["displayName"];
  64. configName->String() = json["displayName"].String();
  65. Settings configCookie = settings.write["lobby"]["accountCookie"];
  66. configCookie->String() = json["accountCookie"].String();
  67. }
  68. sendClientLogin();
  69. }
  70. void GlobalLobbyClient::receiveOperationFailed(const JsonNode & json)
  71. {
  72. auto loginWindowPtr = loginWindow.lock();
  73. if(loginWindowPtr)
  74. loginWindowPtr->onConnectionFailed(json["reason"].String());
  75. // TODO: handle errors in lobby menu
  76. }
  77. void GlobalLobbyClient::receiveLoginSuccess(const JsonNode & json)
  78. {
  79. {
  80. Settings configCookie = settings.write["lobby"]["accountCookie"];
  81. configCookie->String() = json["accountCookie"].String();
  82. Settings configName = settings.write["lobby"]["displayName"];
  83. configName->String() = json["displayName"].String();
  84. }
  85. auto loginWindowPtr = loginWindow.lock();
  86. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  87. throw std::runtime_error("lobby connection finished without active login window!");
  88. loginWindowPtr->onConnectionSuccess();
  89. }
  90. void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
  91. {
  92. for(const auto & entry : json["messages"].Vector())
  93. {
  94. std::string accountID = entry["accountID"].String();
  95. std::string displayName = entry["displayName"].String();
  96. std::string messageText = entry["messageText"].String();
  97. int ageSeconds = entry["ageSeconds"].Integer();
  98. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  99. auto lobbyWindowPtr = lobbyWindow.lock();
  100. if(lobbyWindowPtr)
  101. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  102. }
  103. }
  104. void GlobalLobbyClient::receiveChatMessage(const JsonNode & json)
  105. {
  106. std::string accountID = json["accountID"].String();
  107. std::string displayName = json["displayName"].String();
  108. std::string messageText = json["messageText"].String();
  109. std::string timeFormatted = getCurrentTimeFormatted();
  110. auto lobbyWindowPtr = lobbyWindow.lock();
  111. if(lobbyWindowPtr)
  112. lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
  113. }
  114. void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
  115. {
  116. activeAccounts.clear();
  117. for (auto const & jsonEntry : json["accounts"].Vector())
  118. {
  119. GlobalLobbyAccount account;
  120. account.accountID = jsonEntry["accountID"].String();
  121. account.displayName = jsonEntry["displayName"].String();
  122. account.status = jsonEntry["status"].String();
  123. activeAccounts.push_back(account);
  124. }
  125. auto lobbyWindowPtr = lobbyWindow.lock();
  126. if(lobbyWindowPtr)
  127. lobbyWindowPtr->onActiveAccounts(activeAccounts);
  128. }
  129. void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
  130. {
  131. activeRooms.clear();
  132. for (auto const & jsonEntry : json["gameRooms"].Vector())
  133. {
  134. GlobalLobbyRoom room;
  135. room.gameRoomID = jsonEntry["gameRoomID"].String();
  136. room.hostAccountID = jsonEntry["hostAccountID"].String();
  137. room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
  138. room.description = jsonEntry["description"].String();
  139. room.playersCount = jsonEntry["playersCount"].Integer();
  140. room.playersLimit = jsonEntry["playersLimit"].Integer();
  141. activeRooms.push_back(room);
  142. }
  143. auto lobbyWindowPtr = lobbyWindow.lock();
  144. if(lobbyWindowPtr)
  145. lobbyWindowPtr->onActiveRooms(activeRooms);
  146. }
  147. void GlobalLobbyClient::receiveInviteReceived(const JsonNode & json)
  148. {
  149. assert(0); //TODO
  150. }
  151. void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
  152. {
  153. Settings configRoom = settings.write["lobby"]["roomID"];
  154. configRoom->String() = json["gameRoomID"].String();
  155. if (json["proxyMode"].Bool())
  156. {
  157. CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {});
  158. CSH->loadMode = ELoadMode::MULTI;
  159. std::string hostname = settings["lobby"]["hostname"].String();
  160. int16_t port = settings["lobby"]["port"].Integer();
  161. CSH->connectToServer(hostname, port);
  162. }
  163. }
  164. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  165. {
  166. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  167. networkConnection = connection;
  168. auto loginWindowPtr = loginWindow.lock();
  169. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  170. throw std::runtime_error("lobby connection established without active login window!");
  171. loginWindowPtr->onConnectionSuccess();
  172. }
  173. void GlobalLobbyClient::sendClientRegister(const std::string & accountName)
  174. {
  175. JsonNode toSend;
  176. toSend["type"].String() = "clientRegister";
  177. toSend["displayName"].String() = accountName;
  178. sendMessage(toSend);
  179. }
  180. void GlobalLobbyClient::sendClientLogin()
  181. {
  182. JsonNode toSend;
  183. toSend["type"].String() = "clientLogin";
  184. toSend["accountID"] = settings["lobby"]["accountID"];
  185. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  186. sendMessage(toSend);
  187. }
  188. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  189. {
  190. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  191. auto loginWindowPtr = loginWindow.lock();
  192. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  193. throw std::runtime_error("lobby connection failed without active login window!");
  194. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  195. loginWindowPtr->onConnectionFailed(errorMessage);
  196. }
  197. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  198. {
  199. boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
  200. assert(connection == networkConnection);
  201. networkConnection.reset();
  202. while (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  203. {
  204. // if global lobby is open, pop all dialogs on top of it as well as lobby itself
  205. GH.windows().popWindows(1);
  206. }
  207. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  208. }
  209. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  210. {
  211. networkConnection->sendPacket(data.toBytes(true));
  212. }
  213. void GlobalLobbyClient::sendOpenPublicRoom()
  214. {
  215. JsonNode toSend;
  216. toSend["type"].String() = "openGameRoom";
  217. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  218. toSend["roomType"].String() = "public";
  219. sendMessage(toSend);
  220. }
  221. void GlobalLobbyClient::sendOpenPrivateRoom()
  222. {
  223. JsonNode toSend;
  224. toSend["type"].String() = "openGameRoom";
  225. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  226. toSend["roomType"].String() = "private";
  227. sendMessage(toSend);
  228. }
  229. void GlobalLobbyClient::connect()
  230. {
  231. std::string hostname = settings["lobby"]["hostname"].String();
  232. int16_t port = settings["lobby"]["port"].Integer();
  233. CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
  234. }
  235. bool GlobalLobbyClient::isConnected() const
  236. {
  237. return networkConnection != nullptr;
  238. }
  239. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  240. {
  241. auto loginWindowPtr = loginWindow.lock();
  242. if(loginWindowPtr)
  243. return loginWindowPtr;
  244. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  245. loginWindow = loginWindowNew;
  246. return loginWindowNew;
  247. }
  248. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  249. {
  250. auto lobbyWindowPtr = lobbyWindow.lock();
  251. if(lobbyWindowPtr)
  252. return lobbyWindowPtr;
  253. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  254. lobbyWindow = lobbyWindowPtr;
  255. lobbyWindowLock = lobbyWindowPtr;
  256. return lobbyWindowPtr;
  257. }
  258. const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
  259. {
  260. return activeAccounts;
  261. }
  262. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
  263. {
  264. return activeRooms;
  265. }
  266. void GlobalLobbyClient::activateInterface()
  267. {
  268. if (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  269. return;
  270. if (!GH.windows().findWindows<GlobalLobbyLoginWindow>().empty())
  271. return;
  272. if (isConnected())
  273. GH.windows().pushWindow(createLobbyWindow());
  274. else
  275. GH.windows().pushWindow(createLoginWindow());
  276. }
  277. void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
  278. {
  279. JsonNode toSend;
  280. toSend["type"].String() = "clientProxyLogin";
  281. toSend["accountID"] = settings["lobby"]["accountID"];
  282. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  283. toSend["gameRoomID"] = settings["lobby"]["roomID"];
  284. netConnection->sendPacket(toSend.toBytes(true));
  285. }