GlobalLobbyClient.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 "GlobalLobbyInviteWindow.h"
  13. #include "GlobalLobbyLoginWindow.h"
  14. #include "GlobalLobbyObserver.h"
  15. #include "GlobalLobbyWindow.h"
  16. #include "../CServerHandler.h"
  17. #include "../GameEngine.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../mainmenu/CMainMenu.h"
  20. #include "../media/ISoundPlayer.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/json/JsonUtils.h"
  24. #include "../../lib/texts/CGeneralTextHandler.h"
  25. #include "../../lib/texts/MetaString.h"
  26. #include "../../lib/texts/TextOperations.h"
  27. #include "../../lib/VCMI_Lib.h"
  28. GlobalLobbyClient::GlobalLobbyClient()
  29. {
  30. auto customChannels = settings["lobby"]["languageRooms"].convertTo<std::vector<std::string>>();
  31. if (customChannels.empty())
  32. {
  33. activeChannels.emplace_back("english");
  34. if (VLC->generaltexth->getPreferredLanguage() != "english")
  35. activeChannels.emplace_back(VLC->generaltexth->getPreferredLanguage());
  36. }
  37. else
  38. {
  39. activeChannels = customChannels;
  40. }
  41. }
  42. void GlobalLobbyClient::addChannel(const std::string & channel)
  43. {
  44. activeChannels.emplace_back(channel);
  45. auto lobbyWindowPtr = lobbyWindow.lock();
  46. if(lobbyWindowPtr)
  47. lobbyWindowPtr->refreshActiveChannels();
  48. JsonNode toSend;
  49. toSend["type"].String() = "requestChatHistory";
  50. toSend["channelType"].String() = "global";
  51. toSend["channelName"].String() = channel;
  52. CSH->getGlobalLobby().sendMessage(toSend);
  53. Settings languageRooms = settings.write["lobby"]["languageRooms"];
  54. languageRooms->Vector().clear();
  55. for (const auto & lang : activeChannels)
  56. languageRooms->Vector().push_back(JsonNode(lang));
  57. }
  58. void GlobalLobbyClient::closeChannel(const std::string & channel)
  59. {
  60. vstd::erase(activeChannels, channel);
  61. auto lobbyWindowPtr = lobbyWindow.lock();
  62. if(lobbyWindowPtr)
  63. lobbyWindowPtr->refreshActiveChannels();
  64. Settings languageRooms = settings.write["lobby"]["languageRooms"];
  65. languageRooms->Vector().clear();
  66. for (const auto & lang : activeChannels)
  67. languageRooms->Vector().push_back(JsonNode(lang));
  68. }
  69. GlobalLobbyClient::~GlobalLobbyClient() = default;
  70. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
  71. {
  72. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  73. JsonNode json(message.data(), message.size(), "<lobby network packet>");
  74. if(json["type"].String() == "accountCreated")
  75. return receiveAccountCreated(json);
  76. if(json["type"].String() == "operationFailed")
  77. return receiveOperationFailed(json);
  78. if(json["type"].String() == "clientLoginSuccess")
  79. return receiveClientLoginSuccess(json);
  80. if(json["type"].String() == "chatHistory")
  81. return receiveChatHistory(json);
  82. if(json["type"].String() == "chatMessage")
  83. return receiveChatMessage(json);
  84. if(json["type"].String() == "activeAccounts")
  85. return receiveActiveAccounts(json);
  86. if(json["type"].String() == "activeGameRooms")
  87. return receiveActiveGameRooms(json);
  88. if(json["type"].String() == "joinRoomSuccess")
  89. return receiveJoinRoomSuccess(json);
  90. if(json["type"].String() == "inviteReceived")
  91. return receiveInviteReceived(json);
  92. if(json["type"].String() == "matchesHistory")
  93. return receiveMatchesHistory(json);
  94. logGlobal->error("Received unexpected message from lobby server: %s", json["type"].String());
  95. }
  96. void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
  97. {
  98. auto loginWindowPtr = loginWindow.lock();
  99. if(!loginWindowPtr || !ENGINE->windows().topWindow<GlobalLobbyLoginWindow>())
  100. throw std::runtime_error("lobby connection finished without active login window!");
  101. {
  102. setAccountID(json["accountID"].String());
  103. setAccountDisplayName(json["displayName"].String());
  104. setAccountCookie(json["accountCookie"].String());
  105. }
  106. sendClientLogin();
  107. }
  108. void GlobalLobbyClient::receiveOperationFailed(const JsonNode & json)
  109. {
  110. auto loginWindowPtr = loginWindow.lock();
  111. if(loginWindowPtr)
  112. loginWindowPtr->onConnectionFailed(json["reason"].String());
  113. logGlobal->warn("Operation failed! Reason: %s", json["reason"].String());
  114. // TODO: handle errors in lobby menu
  115. }
  116. void GlobalLobbyClient::receiveClientLoginSuccess(const JsonNode & json)
  117. {
  118. accountLoggedIn = true;
  119. setAccountDisplayName(json["displayName"].String());
  120. setAccountCookie(json["accountCookie"].String());
  121. auto loginWindowPtr = loginWindow.lock();
  122. if(!loginWindowPtr || !ENGINE->windows().topWindow<GlobalLobbyLoginWindow>())
  123. throw std::runtime_error("lobby connection finished without active login window!");
  124. loginWindowPtr->onLoginSuccess();
  125. }
  126. void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
  127. {
  128. std::string channelType = json["channelType"].String();
  129. std::string channelName = json["channelName"].String();
  130. std::string channelKey = channelType + '_' + channelName;
  131. // create empty entry, potentially replacing any pre-existing data
  132. chatHistory[channelKey] = {};
  133. auto lobbyWindowPtr = lobbyWindow.lock();
  134. for(const auto & entry : json["messages"].Vector())
  135. {
  136. GlobalLobbyChannelMessage message;
  137. message.accountID = entry["accountID"].String();
  138. message.displayName = entry["displayName"].String();
  139. message.messageText = entry["messageText"].String();
  140. std::chrono::seconds ageSeconds (entry["ageSeconds"].Integer());
  141. message.timeFormatted = TextOperations::getCurrentFormattedTimeLocal(-ageSeconds);
  142. chatHistory[channelKey].push_back(message);
  143. if(lobbyWindowPtr && lobbyWindowPtr->isChannelOpen(channelType, channelName))
  144. lobbyWindowPtr->onGameChatMessage(message.displayName, message.messageText, message.timeFormatted, channelType, channelName);
  145. }
  146. if(lobbyWindowPtr && lobbyWindowPtr->isChannelOpen(channelType, channelName))
  147. lobbyWindowPtr->refreshChatText();
  148. }
  149. void GlobalLobbyClient::receiveChatMessage(const JsonNode & json)
  150. {
  151. GlobalLobbyChannelMessage message;
  152. message.accountID = json["accountID"].String();
  153. message.displayName = json["displayName"].String();
  154. message.messageText = json["messageText"].String();
  155. message.timeFormatted = TextOperations::getCurrentFormattedTimeLocal();
  156. std::string channelType = json["channelType"].String();
  157. std::string channelName = json["channelName"].String();
  158. std::string channelKey = channelType + '_' + channelName;
  159. chatHistory[channelKey].push_back(message);
  160. auto lobbyWindowPtr = lobbyWindow.lock();
  161. if(lobbyWindowPtr)
  162. {
  163. lobbyWindowPtr->onGameChatMessage(message.displayName, message.messageText, message.timeFormatted, channelType, channelName);
  164. lobbyWindowPtr->refreshChatText();
  165. if(channelType == "player" || (lobbyWindowPtr->isChannelOpen(channelType, channelName) && lobbyWindowPtr->isActive()))
  166. ENGINE->sound().playSound(AudioPath::builtin("CHAT"));
  167. }
  168. }
  169. void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
  170. {
  171. activeAccounts.clear();
  172. for(const auto & jsonEntry : json["accounts"].Vector())
  173. {
  174. GlobalLobbyAccount account;
  175. account.accountID = jsonEntry["accountID"].String();
  176. account.displayName = jsonEntry["displayName"].String();
  177. account.status = jsonEntry["status"].String();
  178. activeAccounts.push_back(account);
  179. }
  180. auto lobbyWindowPtr = lobbyWindow.lock();
  181. if(lobbyWindowPtr)
  182. lobbyWindowPtr->onActiveAccounts(activeAccounts);
  183. for (auto const & window : ENGINE->windows().findWindows<GlobalLobbyObserver>())
  184. window->onActiveAccounts(activeAccounts);
  185. }
  186. void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
  187. {
  188. activeRooms.clear();
  189. for(const auto & jsonEntry : json["gameRooms"].Vector())
  190. {
  191. GlobalLobbyRoom room;
  192. room.gameRoomID = jsonEntry["gameRoomID"].String();
  193. room.hostAccountID = jsonEntry["hostAccountID"].String();
  194. room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
  195. room.description = jsonEntry["description"].String();
  196. room.statusID = jsonEntry["status"].String();
  197. room.gameVersion = jsonEntry["version"].String();
  198. room.modList = ModVerificationInfo::jsonDeserializeList(jsonEntry["mods"]);
  199. std::chrono::seconds ageSeconds (jsonEntry["ageSeconds"].Integer());
  200. room.startDateFormatted = TextOperations::getCurrentFormattedDateTimeLocal(-ageSeconds);
  201. for(const auto & jsonParticipant : jsonEntry["participants"].Vector())
  202. {
  203. GlobalLobbyAccount account;
  204. account.accountID = jsonParticipant["accountID"].String();
  205. account.displayName = jsonParticipant["displayName"].String();
  206. room.participants.push_back(account);
  207. }
  208. for(const auto & jsonParticipant : jsonEntry["invited"].Vector())
  209. {
  210. GlobalLobbyAccount account;
  211. account.accountID = jsonParticipant["accountID"].String();
  212. account.displayName = jsonParticipant["displayName"].String();
  213. room.invited.push_back(account);
  214. }
  215. room.playerLimit = jsonEntry["playerLimit"].Integer();
  216. activeRooms.push_back(room);
  217. }
  218. auto lobbyWindowPtr = lobbyWindow.lock();
  219. if(lobbyWindowPtr)
  220. lobbyWindowPtr->onActiveGameRooms(activeRooms);
  221. for (auto const & window : ENGINE->windows().findWindows<GlobalLobbyObserver>())
  222. window->onActiveGameRooms(activeRooms);
  223. }
  224. void GlobalLobbyClient::receiveMatchesHistory(const JsonNode & json)
  225. {
  226. matchesHistory.clear();
  227. for(const auto & jsonEntry : json["matchesHistory"].Vector())
  228. {
  229. GlobalLobbyRoom room;
  230. room.gameRoomID = jsonEntry["gameRoomID"].String();
  231. room.hostAccountID = jsonEntry["hostAccountID"].String();
  232. room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
  233. room.description = jsonEntry["description"].String();
  234. room.statusID = jsonEntry["status"].String();
  235. std::chrono::seconds ageSeconds (jsonEntry["ageSeconds"].Integer());
  236. room.startDateFormatted = TextOperations::getCurrentFormattedDateTimeLocal(-ageSeconds);
  237. for(const auto & jsonParticipant : jsonEntry["participants"].Vector())
  238. {
  239. GlobalLobbyAccount account;
  240. account.accountID = jsonParticipant["accountID"].String();
  241. account.displayName = jsonParticipant["displayName"].String();
  242. room.participants.push_back(account);
  243. }
  244. room.playerLimit = jsonEntry["playerLimit"].Integer();
  245. matchesHistory.push_back(room);
  246. }
  247. auto lobbyWindowPtr = lobbyWindow.lock();
  248. if(lobbyWindowPtr)
  249. lobbyWindowPtr->onMatchesHistory(matchesHistory);
  250. }
  251. void GlobalLobbyClient::receiveInviteReceived(const JsonNode & json)
  252. {
  253. auto lobbyWindowPtr = lobbyWindow.lock();
  254. std::string gameRoomID = json["gameRoomID"].String();
  255. std::string accountID = json["accountID"].String();
  256. activeInvites.insert(gameRoomID);
  257. if(lobbyWindowPtr)
  258. {
  259. std::string message = MetaString::createFromTextID("vcmi.lobby.invite.notification").toString();
  260. std::string time = TextOperations::getCurrentFormattedTimeLocal();
  261. lobbyWindowPtr->onGameChatMessage("System", message, time, "player", accountID);
  262. lobbyWindowPtr->onInviteReceived(gameRoomID);
  263. lobbyWindowPtr->refreshChatText();
  264. }
  265. ENGINE->sound().playSound(AudioPath::builtin("CHAT"));
  266. }
  267. void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
  268. {
  269. if (json["proxyMode"].Bool())
  270. {
  271. CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, { CSH->getGlobalLobby().getAccountDisplayName() });
  272. CSH->loadMode = ELoadMode::MULTI;
  273. std::string hostname = getServerHost();
  274. uint16_t port = getServerPort();
  275. CSH->connectToServer(hostname, port);
  276. }
  277. // NOTE: must be set after CSH->resetStateForLobby call
  278. currentGameRoomUUID = json["gameRoomID"].String();
  279. }
  280. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
  281. {
  282. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  283. networkConnection = connection;
  284. auto loginWindowPtr = loginWindow.lock();
  285. if(!loginWindowPtr || !ENGINE->windows().topWindow<GlobalLobbyLoginWindow>())
  286. throw std::runtime_error("lobby connection established without active login window!");
  287. loginWindowPtr->onConnectionSuccess();
  288. }
  289. void GlobalLobbyClient::sendClientRegister(const std::string & accountName)
  290. {
  291. JsonNode toSend;
  292. toSend["type"].String() = "clientRegister";
  293. toSend["displayName"].String() = accountName;
  294. toSend["language"].String() = VLC->generaltexth->getPreferredLanguage();
  295. toSend["version"].String() = VCMI_VERSION_STRING;
  296. sendMessage(toSend);
  297. }
  298. void GlobalLobbyClient::sendClientLogin()
  299. {
  300. JsonNode toSend;
  301. toSend["type"].String() = "clientLogin";
  302. toSend["accountID"].String() = getAccountID();
  303. toSend["accountCookie"].String() = getAccountCookie();
  304. toSend["language"].String() = VLC->generaltexth->getPreferredLanguage();
  305. toSend["version"].String() = VCMI_VERSION_STRING;
  306. for (const auto & language : activeChannels)
  307. toSend["languageRooms"].Vector().push_back(JsonNode(language));
  308. sendMessage(toSend);
  309. }
  310. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  311. {
  312. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  313. auto loginWindowPtr = loginWindow.lock();
  314. if(!loginWindowPtr || !ENGINE->windows().topWindow<GlobalLobbyLoginWindow>())
  315. throw std::runtime_error("lobby connection failed without active login window!");
  316. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  317. loginWindowPtr->onConnectionFailed(errorMessage);
  318. }
  319. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
  320. {
  321. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  322. assert(connection == networkConnection);
  323. networkConnection.reset();
  324. accountLoggedIn = false;
  325. while (!ENGINE->windows().findWindows<GlobalLobbyWindow>().empty())
  326. {
  327. // if global lobby is open, pop all dialogs on top of it as well as lobby itself
  328. ENGINE->windows().popWindows(1);
  329. }
  330. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  331. }
  332. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  333. {
  334. assert(JsonUtils::validate(data, "vcmi:lobbyProtocol/" + data["type"].String(), data["type"].String() + " pack"));
  335. networkConnection->sendPacket(data.toBytes());
  336. }
  337. void GlobalLobbyClient::sendOpenRoom(const std::string & mode, int playerLimit)
  338. {
  339. JsonNode toSend;
  340. toSend["type"].String() = "activateGameRoom";
  341. toSend["hostAccountID"].String() = getAccountID();
  342. toSend["roomType"].String() = mode;
  343. toSend["playerLimit"].Integer() = playerLimit;
  344. sendMessage(toSend);
  345. }
  346. void GlobalLobbyClient::connect()
  347. {
  348. std::string hostname = getServerHost();
  349. uint16_t port = getServerPort();
  350. CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
  351. }
  352. bool GlobalLobbyClient::isLoggedIn() const
  353. {
  354. return networkConnection != nullptr && accountLoggedIn;
  355. }
  356. bool GlobalLobbyClient::isConnected() const
  357. {
  358. return networkConnection != nullptr;
  359. }
  360. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  361. {
  362. auto loginWindowPtr = loginWindow.lock();
  363. if(loginWindowPtr)
  364. return loginWindowPtr;
  365. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  366. loginWindow = loginWindowNew;
  367. return loginWindowNew;
  368. }
  369. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  370. {
  371. auto lobbyWindowPtr = lobbyWindow.lock();
  372. if(lobbyWindowPtr)
  373. return lobbyWindowPtr;
  374. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  375. lobbyWindow = lobbyWindowPtr;
  376. lobbyWindowLock = lobbyWindowPtr;
  377. return lobbyWindowPtr;
  378. }
  379. const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
  380. {
  381. return activeAccounts;
  382. }
  383. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
  384. {
  385. return activeRooms;
  386. }
  387. const std::vector<std::string> & GlobalLobbyClient::getActiveChannels() const
  388. {
  389. return activeChannels;
  390. }
  391. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getMatchesHistory() const
  392. {
  393. return matchesHistory;
  394. }
  395. const GlobalLobbyRoom & GlobalLobbyClient::getActiveRoomByName(const std::string & roomUUID) const
  396. {
  397. for (auto const & room : activeRooms)
  398. {
  399. if (room.gameRoomID == roomUUID)
  400. return room;
  401. }
  402. throw std::out_of_range("Failed to find room with UUID of " + roomUUID);
  403. }
  404. const std::vector<GlobalLobbyChannelMessage> & GlobalLobbyClient::getChannelHistory(const std::string & channelType, const std::string & channelName) const
  405. {
  406. static const std::vector<GlobalLobbyChannelMessage> emptyVector;
  407. std::string keyToTest = channelType + '_' + channelName;
  408. if (chatHistory.count(keyToTest) == 0)
  409. {
  410. if (channelType != "global")
  411. {
  412. JsonNode toSend;
  413. toSend["type"].String() = "requestChatHistory";
  414. toSend["channelType"].String() = channelType;
  415. toSend["channelName"].String() = channelName;
  416. CSH->getGlobalLobby().sendMessage(toSend);
  417. }
  418. return emptyVector;
  419. }
  420. else
  421. return chatHistory.at(keyToTest);
  422. }
  423. void GlobalLobbyClient::activateInterface()
  424. {
  425. if (ENGINE->windows().topWindow<GlobalLobbyWindow>() != nullptr)
  426. {
  427. ENGINE->windows().popWindows(1);
  428. return;
  429. }
  430. if (!ENGINE->windows().findWindows<GlobalLobbyWindow>().empty())
  431. return;
  432. if (!ENGINE->windows().findWindows<GlobalLobbyLoginWindow>().empty())
  433. return;
  434. if (isLoggedIn())
  435. ENGINE->windows().pushWindow(createLobbyWindow());
  436. else
  437. ENGINE->windows().pushWindow(createLoginWindow());
  438. ENGINE->windows().topWindow<CIntObject>()->center();
  439. }
  440. void GlobalLobbyClient::activateRoomInviteInterface()
  441. {
  442. ENGINE->windows().createAndPushWindow<GlobalLobbyInviteWindow>();
  443. }
  444. void GlobalLobbyClient::setAccountID(const std::string & accountID)
  445. {
  446. Settings configID = persistentStorage.write["lobby"][getServerHost()]["accountID"];
  447. configID->String() = accountID;
  448. }
  449. void GlobalLobbyClient::setAccountCookie(const std::string & accountCookie)
  450. {
  451. Settings configCookie = persistentStorage.write["lobby"][getServerHost()]["accountCookie"];
  452. configCookie->String() = accountCookie;
  453. }
  454. void GlobalLobbyClient::setAccountDisplayName(const std::string & accountDisplayName)
  455. {
  456. Settings configName = persistentStorage.write["lobby"][getServerHost()]["displayName"];
  457. configName->String() = accountDisplayName;
  458. }
  459. const std::string & GlobalLobbyClient::getAccountID() const
  460. {
  461. return persistentStorage["lobby"][getServerHost()]["accountID"].String();
  462. }
  463. const std::string & GlobalLobbyClient::getAccountCookie() const
  464. {
  465. return persistentStorage["lobby"][getServerHost()]["accountCookie"].String();
  466. }
  467. const std::string & GlobalLobbyClient::getAccountDisplayName() const
  468. {
  469. return persistentStorage["lobby"][getServerHost()]["displayName"].String();
  470. }
  471. const std::string & GlobalLobbyClient::getServerHost() const
  472. {
  473. return settings["lobby"]["hostname"].String();
  474. }
  475. uint16_t GlobalLobbyClient::getServerPort() const
  476. {
  477. return settings["lobby"]["port"].Integer();
  478. }
  479. void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
  480. {
  481. JsonNode toSend;
  482. toSend["type"].String() = "clientProxyLogin";
  483. toSend["accountID"].String() = getAccountID();
  484. toSend["accountCookie"].String() = getAccountCookie();
  485. toSend["gameRoomID"].String() = currentGameRoomUUID;
  486. assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
  487. netConnection->sendPacket(toSend.toBytes());
  488. }
  489. void GlobalLobbyClient::resetMatchState()
  490. {
  491. currentGameRoomUUID.clear();
  492. }
  493. const std::string & GlobalLobbyClient::getCurrentGameRoomID() const
  494. {
  495. return currentGameRoomUUID;
  496. }
  497. void GlobalLobbyClient::sendMatchChatMessage(const std::string & messageText)
  498. {
  499. if (!isLoggedIn())
  500. return; // we are not playing with lobby
  501. if (currentGameRoomUUID.empty())
  502. return; // we are not playing through lobby
  503. JsonNode toSend;
  504. toSend["type"].String() = "sendChatMessage";
  505. toSend["channelType"].String() = "match";
  506. toSend["channelName"].String() = currentGameRoomUUID;
  507. toSend["messageText"].String() = messageText;
  508. assert(TextOperations::isValidUnicodeString(messageText));
  509. CSH->getGlobalLobby().sendMessage(toSend);
  510. }
  511. bool GlobalLobbyClient::isInvitedToRoom(const std::string & gameRoomID)
  512. {
  513. if (activeInvites.count(gameRoomID) > 0)
  514. return true;
  515. const auto & gameRoom = CSH->getGlobalLobby().getActiveRoomByName(gameRoomID);
  516. for (auto const & invited : gameRoom.invited)
  517. {
  518. if (invited.accountID == getAccountID())
  519. return true;
  520. }
  521. return false;
  522. }