GlobalLobbyWindow.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * GlobalLobbyWindow.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 "GlobalLobbyWindow.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "GlobalLobbyServerSetup.h"
  14. #include "GlobalLobbyWidget.h"
  15. #include "../CServerHandler.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../widgets/TextControls.h"
  19. #include "../widgets/ObjectLists.h"
  20. #include "../../lib/CConfigHandler.h"
  21. #include "../../lib/MetaString.h"
  22. GlobalLobbyWindow::GlobalLobbyWindow()
  23. : CWindowObject(BORDERED)
  24. {
  25. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  26. widget = std::make_shared<GlobalLobbyWidget>(this);
  27. pos = widget->pos;
  28. center();
  29. widget->getAccountNameLabel()->setText(settings["lobby"]["displayName"].String());
  30. doOpenChannel("global", "english");
  31. }
  32. void GlobalLobbyWindow::doOpenChannel(const std::string & channelType, const std::string & channelName)
  33. {
  34. currentChannelType = channelType;
  35. currentChannelName = channelName;
  36. chatHistory.clear();
  37. widget->getGameChat()->setText("");
  38. auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName);
  39. for (auto const & entry : history)
  40. onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName);
  41. }
  42. void GlobalLobbyWindow::doSendChatMessage()
  43. {
  44. std::string messageText = widget->getMessageInput()->getText();
  45. JsonNode toSend;
  46. toSend["type"].String() = "sendChatMessage";
  47. toSend["channelType"].String() = currentChannelType;
  48. toSend["channelName"].String() = currentChannelName;
  49. toSend["messageText"].String() = messageText;
  50. CSH->getGlobalLobby().sendMessage(toSend);
  51. widget->getMessageInput()->setText("");
  52. }
  53. void GlobalLobbyWindow::doCreateGameRoom()
  54. {
  55. GH.windows().createAndPushWindow<GlobalLobbyServerSetup>();
  56. }
  57. void GlobalLobbyWindow::doInviteAccount(const std::string & accountID)
  58. {
  59. JsonNode toSend;
  60. toSend["type"].String() = "sendInvite";
  61. toSend["accountID"].String() = accountID;
  62. CSH->getGlobalLobby().sendMessage(toSend);
  63. }
  64. void GlobalLobbyWindow::doJoinRoom(const std::string & roomID)
  65. {
  66. JsonNode toSend;
  67. toSend["type"].String() = "joinGameRoom";
  68. toSend["gameRoomID"].String() = roomID;
  69. CSH->getGlobalLobby().sendMessage(toSend);
  70. }
  71. void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when, const std::string & channelType, const std::string & channelName)
  72. {
  73. if (channelType != currentChannelType || channelName != currentChannelName)
  74. return; // TODO: send ping to player that another channel got a new message
  75. MetaString chatMessageFormatted;
  76. chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
  77. chatMessageFormatted.replaceRawString(when);
  78. chatMessageFormatted.replaceRawString(sender);
  79. chatMessageFormatted.replaceRawString(message);
  80. chatHistory += chatMessageFormatted.toString();
  81. widget->getGameChat()->setText(chatHistory);
  82. }
  83. void GlobalLobbyWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
  84. {
  85. if (accounts.size() == widget->getAccountList()->size())
  86. widget->getAccountList()->reset();
  87. else
  88. widget->getAccountList()->resize(accounts.size());
  89. }
  90. void GlobalLobbyWindow::onActiveRooms(const std::vector<GlobalLobbyRoom> & rooms)
  91. {
  92. if (rooms.size() == widget->getRoomList()->size())
  93. widget->getRoomList()->reset();
  94. else
  95. widget->getRoomList()->resize(rooms.size());
  96. }
  97. void GlobalLobbyWindow::onMatchesHistory(const std::vector<GlobalLobbyRoom> & history)
  98. {
  99. if (history.size() == widget->getMatchList()->size())
  100. widget->getMatchList()->reset();
  101. else
  102. widget->getMatchList()->resize(history.size());
  103. }
  104. void GlobalLobbyWindow::onJoinedRoom()
  105. {
  106. widget->getAccountList()->reset();
  107. }
  108. void GlobalLobbyWindow::onLeftRoom()
  109. {
  110. widget->getAccountList()->reset();
  111. }