LobbyWindow.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. LobbyClient::LobbyClient(LobbyWindow * 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 LobbyClient::onPacketReceived(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 LobbyClient::onConnectionEstablished()
  61. {
  62. JsonNode toSend;
  63. toSend["type"].String() = "authentication";
  64. toSend["accountName"].String() = settings["general"]["playerName"].String();
  65. sendMessage(toSend);
  66. }
  67. void LobbyClient::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 LobbyClient::onDisconnected()
  73. {
  74. GH.windows().popWindows(1);
  75. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  76. }
  77. void LobbyClient::sendMessage(const JsonNode & data)
  78. {
  79. std::string payloadString = data.toJson(true);
  80. // FIXME: find better approach
  81. uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
  82. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  83. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  84. networkClient->sendPacket(payloadBuffer);
  85. }
  86. void LobbyClient::start(const std::string & host, uint16_t port)
  87. {
  88. networkClient->start(host, port);
  89. }
  90. void LobbyClient::run()
  91. {
  92. networkClient->run();
  93. }
  94. void LobbyClient::poll()
  95. {
  96. networkClient->poll();
  97. }
  98. LobbyWidget::LobbyWidget(LobbyWindow * window)
  99. : window(window)
  100. {
  101. addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
  102. addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
  103. const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
  104. build(config);
  105. }
  106. std::shared_ptr<CLabel> LobbyWidget::getAccountNameLabel()
  107. {
  108. return widget<CLabel>("accountNameLabel");
  109. }
  110. std::shared_ptr<CTextInput> LobbyWidget::getMessageInput()
  111. {
  112. return widget<CTextInput>("messageInput");
  113. }
  114. std::shared_ptr<CTextBox> LobbyWidget::getGameChat()
  115. {
  116. return widget<CTextBox>("gameChat");
  117. }
  118. LobbyWindow::LobbyWindow():
  119. CWindowObject(BORDERED)
  120. {
  121. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  122. widget = std::make_shared<LobbyWidget>(this);
  123. pos = widget->pos;
  124. center();
  125. connection = std::make_shared<LobbyClient>(this);
  126. connection->start("127.0.0.1", 30303);
  127. widget->getAccountNameLabel()->setText(settings["general"]["playerName"].String());
  128. addUsedEvents(TIME);
  129. }
  130. void LobbyWindow::tick(uint32_t msPassed)
  131. {
  132. connection->poll();
  133. }
  134. void LobbyWindow::doSendChatMessage()
  135. {
  136. std::string messageText = widget->getMessageInput()->getText();
  137. JsonNode toSend;
  138. toSend["type"].String() = "sendChatMessage";
  139. toSend["messageText"].String() = messageText;
  140. connection->sendMessage(toSend);
  141. widget->getMessageInput()->setText("");
  142. }
  143. void LobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when)
  144. {
  145. MetaString chatMessageFormatted;
  146. chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
  147. chatMessageFormatted.replaceRawString(when);
  148. chatMessageFormatted.replaceRawString(sender);
  149. chatMessageFormatted.replaceRawString(message);
  150. chatHistory += chatMessageFormatted.toString();
  151. widget->getGameChat()->setText(chatHistory);
  152. }