GlobalLobbyClient.cpp 20 KB

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