GlobalLobbyClient.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "GlobalLobbyWindow.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/WindowHandler.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() = default;
  20. GlobalLobbyClient::GlobalLobbyClient(GlobalLobbyWindow * window)
  21. : networkClient(std::make_unique<NetworkClient>(*this))
  22. , window(window)
  23. {}
  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. std::time_t timeNowC = std::chrono::system_clock::to_time_t(timeNowChrono);
  30. std::tm timeNowTm = *std::localtime(&timeNowC);
  31. MetaString timeFormatted;
  32. timeFormatted.appendRawString("%d:%d");
  33. timeFormatted.replaceNumber(timeNowTm.tm_hour);
  34. timeFormatted.replaceNumber(timeNowTm.tm_min);
  35. return timeFormatted.toString();
  36. }
  37. void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<NetworkConnection> &, const std::vector<uint8_t> & message)
  38. {
  39. // FIXME: find better approach
  40. const char * payloadBegin = reinterpret_cast<const char*>(message.data());
  41. JsonNode json(payloadBegin, message.size());
  42. if (json["type"].String() == "chatHistory")
  43. {
  44. for (auto const & entry : json["messages"].Vector())
  45. {
  46. std::string senderName = entry["senderName"].String();
  47. std::string messageText = entry["messageText"].String();
  48. int ageSeconds = entry["ageSeconds"].Integer();
  49. std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
  50. window->onGameChatMessage(senderName, messageText, timeFormatted);
  51. }
  52. }
  53. if (json["type"].String() == "chatMessage")
  54. {
  55. std::string senderName = json["senderName"].String();
  56. std::string messageText = json["messageText"].String();
  57. std::string timeFormatted = getCurrentTimeFormatted();
  58. window->onGameChatMessage(senderName, messageText, timeFormatted);
  59. }
  60. }
  61. void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<NetworkConnection> &)
  62. {
  63. JsonNode toSend;
  64. toSend["type"].String() = "authentication";
  65. toSend["accountName"].String() = settings["general"]["playerName"].String();
  66. sendMessage(toSend);
  67. }
  68. void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
  69. {
  70. GH.windows().popWindows(1);
  71. CInfoWindow::showInfoDialog("Failed to connect to game lobby!\n" + errorMessage, {});
  72. }
  73. void GlobalLobbyClient::onDisconnected(const std::shared_ptr<NetworkConnection> &)
  74. {
  75. GH.windows().popWindows(1);
  76. CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
  77. }
  78. void GlobalLobbyClient::onTimer()
  79. {
  80. // no-op
  81. }
  82. void GlobalLobbyClient::sendMessage(const JsonNode & data)
  83. {
  84. std::string payloadString = data.toJson(true);
  85. // FIXME: find better approach
  86. uint8_t * payloadBegin = reinterpret_cast<uint8_t*>(payloadString.data());
  87. uint8_t * payloadEnd = payloadBegin + payloadString.size();
  88. std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
  89. networkClient->sendPacket(payloadBuffer);
  90. }
  91. void GlobalLobbyClient::start(const std::string & host, uint16_t port)
  92. {
  93. networkClient->start(host, port);
  94. }
  95. void GlobalLobbyClient::run()
  96. {
  97. networkClient->run();
  98. }
  99. void GlobalLobbyClient::poll()
  100. {
  101. networkClient->poll();
  102. }