GlobalLobbyClient.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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<uint8_t> & 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() == "loginFailed")
  38. return receiveLoginFailed(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::receiveLoginFailed(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. 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. auto loginWindowPtr = loginWindow.lock();
  187. if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
  188. throw std::runtime_error("lobby connection failed without active login window!");
  189. logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
  190. loginWindowPtr->onConnectionFailed(errorMessage);
  191. }
  192. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
  193. {
  194. assert(connection == networkConnection);
  195. networkConnection.reset();
  196. GH.windows().popWindows(1);
  197. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  198. }
  199. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  200. {
  201. std::string payloadString = data.toJson(true);
  202. // FIXME: find better approach
  203. uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
  204. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  205. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  206. networkConnection->sendPacket(payloadBuffer);
  207. }
  208. void GlobalLobbyClient::sendOpenPublicRoom()
  209. {
  210. JsonNode toSend;
  211. toSend["type"].String() = "openGameRoom";
  212. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  213. toSend["roomType"].String() = "public";
  214. sendMessage(toSend);
  215. }
  216. void GlobalLobbyClient::sendOpenPrivateRoom()
  217. {
  218. JsonNode toSend;
  219. toSend["type"].String() = "openGameRoom";
  220. toSend["hostAccountID"] = settings["lobby"]["accountID"];
  221. toSend["roomType"].String() = "private";
  222. sendMessage(toSend);
  223. }
  224. void GlobalLobbyClient::connect()
  225. {
  226. std::string hostname = settings["lobby"]["hostname"].String();
  227. int16_t port = settings["lobby"]["port"].Integer();
  228. CSH->networkHandler->connectToRemote(*this, hostname, port);
  229. }
  230. bool GlobalLobbyClient::isConnected()
  231. {
  232. return networkConnection != nullptr;
  233. }
  234. std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
  235. {
  236. auto loginWindowPtr = loginWindow.lock();
  237. if(loginWindowPtr)
  238. return loginWindowPtr;
  239. auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
  240. loginWindow = loginWindowNew;
  241. return loginWindowNew;
  242. }
  243. std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
  244. {
  245. auto lobbyWindowPtr = lobbyWindow.lock();
  246. if(lobbyWindowPtr)
  247. return lobbyWindowPtr;
  248. lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
  249. lobbyWindow = lobbyWindowPtr;
  250. lobbyWindowLock = lobbyWindowPtr;
  251. return lobbyWindowPtr;
  252. }
  253. const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
  254. {
  255. return activeAccounts;
  256. }
  257. const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
  258. {
  259. return activeRooms;
  260. }
  261. void GlobalLobbyClient::activateInterface()
  262. {
  263. if (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
  264. return;
  265. if (!GH.windows().findWindows<GlobalLobbyLoginWindow>().empty())
  266. return;
  267. if (isConnected())
  268. GH.windows().pushWindow(createLobbyWindow());
  269. else
  270. GH.windows().pushWindow(createLoginWindow());
  271. }
  272. void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
  273. {
  274. JsonNode toSend;
  275. toSend["type"].String() = "clientProxyLogin";
  276. toSend["accountID"] = settings["lobby"]["accountID"];
  277. toSend["accountCookie"] = settings["lobby"]["accountCookie"];
  278. toSend["gameRoomID"] = settings["lobby"]["roomID"];
  279. std::string payloadString = toSend.toJson(true);
  280. // FIXME: find better approach
  281. uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
  282. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  283. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  284. netConnection->sendPacket(payloadBuffer);
  285. }