LobbyWindow.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * LobbyWindow.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 "LobbyWindow.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../gui/WindowHandler.h"
  14. #include "../widgets/TextControls.h"
  15. #include "../windows/InfoWindows.h"
  16. #include "../../lib/MetaString.h"
  17. #include "../../lib/CConfigHandler.h"
  18. #include "../../lib/network/NetworkClient.h"
  19. GlobalLobbyClient::GlobalLobbyClient(GlobalLobbyWindow * window)
  20. : networkClient(std::make_unique<NetworkClient>(*this))
  21. , window(window)
  22. {}
  23. static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
  24. {
  25. // FIXME: better/unified way to format date
  26. auto timeNowChrono = std::chrono::system_clock::now();
  27. timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
  28. std::time_t timeNowC = std::chrono::system_clock::to_time_t(timeNowChrono);
  29. std::tm timeNowTm = *std::localtime(&timeNowC);
  30. MetaString timeFormatted;
  31. timeFormatted.appendRawString("%d:%d");
  32. timeFormatted.replaceNumber(timeNowTm.tm_hour);
  33. timeFormatted.replaceNumber(timeNowTm.tm_min);
  34. return timeFormatted.toString();
  35. }
  36. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message)
  37. {
  38. // FIXME: find better approach
  39. const char * payloadBegin = reinterpret_cast<const char*>(message.data());
  40. JsonNode json(payloadBegin, message.size());
  41. if (json["type"].String() == "chatHistory")
  42. {
  43. for (auto const & entry : json["messages"].Vector())
  44. {
  45. std::string senderName = entry["senderName"].String();
  46. std::string messageText = entry["messageText"].String();
  47. int ageSeconds = entry["ageSeconds"].Integer();
  48. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  49. window->onGameChatMessage(senderName, messageText, timeFormatted);
  50. }
  51. }
  52. if (json["type"].String() == "chatMessage")
  53. {
  54. std::string senderName = json["senderName"].String();
  55. std::string messageText = json["messageText"].String();
  56. std::string timeFormatted = getCurrentTimeFormatted();
  57. window->onGameChatMessage(senderName, messageText, timeFormatted);
  58. }
  59. }
  60. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<NetworkConnection> &)
  61. {
  62. JsonNode toSend;
  63. toSend["type"].String() = "authentication";
  64. toSend["accountName"].String() = settings["general"]["playerName"].String();
  65. sendMessage(toSend);
  66. }
  67. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  68. {
  69. GH.windows().popWindows(1);
  70. CInfoWindow::showInfoDialog("Failed to connect to game lobby!\n" + errorMessage, {});
  71. }
  72. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<NetworkConnection> &)
  73. {
  74. GH.windows().popWindows(1);
  75. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  76. }
  77. void GlobalLobbyClient::onTimer()
  78. {
  79. // no-op
  80. }
  81. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  82. {
  83. std::string payloadString = data.toJson(true);
  84. // FIXME: find better approach
  85. uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
  86. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  87. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  88. networkClient->sendPacket(payloadBuffer);
  89. }
  90. void GlobalLobbyClient::start(const std::string & host, uint16_t port)
  91. {
  92. networkClient->start(host, port);
  93. }
  94. void GlobalLobbyClient::run()
  95. {
  96. networkClient->run();
  97. }
  98. void GlobalLobbyClient::poll()
  99. {
  100. networkClient->poll();
  101. }
  102. GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
  103. : window(window)
  104. {
  105. addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
  106. addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
  107. const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
  108. build(config);
  109. }
  110. std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
  111. {
  112. return widget<CLabel>("accountNameLabel");
  113. }
  114. std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
  115. {
  116. return widget<CTextInput>("messageInput");
  117. }
  118. std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
  119. {
  120. return widget<CTextBox>("gameChat");
  121. }
  122. GlobalLobbyWindow::GlobalLobbyWindow():
  123. CWindowObject(BORDERED)
  124. {
  125. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  126. widget = std::make_shared<GlobalLobbyWidget>(this);
  127. pos = widget->pos;
  128. center();
  129. connection = std::make_shared<GlobalLobbyClient>(this);
  130. connection->start("127.0.0.1", 30303);
  131. widget->getAccountNameLabel()->setText(settings["general"]["playerName"].String());
  132. addUsedEvents(TIME);
  133. }
  134. void GlobalLobbyWindow::tick(uint32_t msPassed)
  135. {
  136. connection->poll();
  137. }
  138. void GlobalLobbyWindow::doSendChatMessage()
  139. {
  140. std::string messageText = widget->getMessageInput()->getText();
  141. JsonNode toSend;
  142. toSend["type"].String() = "sendChatMessage";
  143. toSend["messageText"].String() = messageText;
  144. connection->sendMessage(toSend);
  145. widget->getMessageInput()->setText("");
  146. }
  147. void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when)
  148. {
  149. MetaString chatMessageFormatted;
  150. chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
  151. chatMessageFormatted.replaceRawString(when);
  152. chatMessageFormatted.replaceRawString(sender);
  153. chatMessageFormatted.replaceRawString(message);
  154. chatHistory += chatMessageFormatted.toString();
  155. widget->getGameChat()->setText(chatHistory);
  156. }