GlobalLobbyWindow.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/CTextInput.h"
  19. #include "../widgets/Slider.h"
  20. #include "../widgets/ObjectLists.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../../lib/texts/Languages.h"
  23. #include "../../lib/texts/MetaString.h"
  24. #include "../../lib/texts/TextOperations.h"
  25. GlobalLobbyWindow::GlobalLobbyWindow()
  26. : CWindowObject(BORDERED)
  27. {
  28. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  29. widget = std::make_shared<GlobalLobbyWidget>(this);
  30. pos = widget->pos;
  31. center();
  32. widget->getAccountNameLabel()->setText(CSH->getGlobalLobby().getAccountDisplayName());
  33. doOpenChannel("global", "english", Languages::getLanguageOptions("english").nameNative);
  34. widget->getChannelListHeader()->setText(MetaString::createFromTextID("vcmi.lobby.header.channels").toString());
  35. }
  36. bool GlobalLobbyWindow::isChannelOpen(const std::string & testChannelType, const std::string & testChannelName) const
  37. {
  38. return testChannelType == currentChannelType && testChannelName == currentChannelName;
  39. }
  40. void GlobalLobbyWindow::doOpenChannel(const std::string & channelType, const std::string & channelName, const std::string & roomDescription)
  41. {
  42. currentChannelType = channelType;
  43. currentChannelName = channelName;
  44. chatHistory.clear();
  45. unreadChannels.erase(channelType + "_" + channelName);
  46. auto history = CSH->getGlobalLobby().getChannelHistory(channelType, channelName);
  47. for(const auto & entry : history)
  48. onGameChatMessage(entry.displayName, entry.messageText, entry.timeFormatted, channelType, channelName);
  49. refreshChatText();
  50. MetaString text;
  51. text.appendTextID("vcmi.lobby.header.chat." + channelType);
  52. text.replaceRawString(roomDescription);
  53. widget->getGameChatHeader()->setText(text.toString());
  54. // Update currently selected item in UI
  55. // WARNING: this invalidates function parameters since some of them are members of objects that will be destroyed by reset
  56. widget->getAccountList()->reset();
  57. widget->getChannelList()->reset();
  58. widget->getMatchList()->reset();
  59. redraw();
  60. }
  61. void GlobalLobbyWindow::doSendChatMessage()
  62. {
  63. std::string messageText = widget->getMessageInput()->getText();
  64. JsonNode toSend;
  65. toSend["type"].String() = "sendChatMessage";
  66. toSend["channelType"].String() = currentChannelType;
  67. toSend["channelName"].String() = currentChannelName;
  68. toSend["messageText"].String() = messageText;
  69. assert(TextOperations::isValidUnicodeString(messageText));
  70. CSH->getGlobalLobby().sendMessage(toSend);
  71. widget->getMessageInput()->setText("");
  72. }
  73. void GlobalLobbyWindow::doCreateGameRoom()
  74. {
  75. GH.windows().createAndPushWindow<GlobalLobbyServerSetup>();
  76. }
  77. void GlobalLobbyWindow::doInviteAccount(const std::string & accountID)
  78. {
  79. JsonNode toSend;
  80. toSend["type"].String() = "sendInvite";
  81. toSend["accountID"].String() = accountID;
  82. CSH->getGlobalLobby().sendMessage(toSend);
  83. }
  84. void GlobalLobbyWindow::doJoinRoom(const std::string & roomID)
  85. {
  86. JsonNode toSend;
  87. toSend["type"].String() = "joinGameRoom";
  88. toSend["gameRoomID"].String() = roomID;
  89. CSH->getGlobalLobby().sendMessage(toSend);
  90. }
  91. void GlobalLobbyWindow::onGameChatMessage(const std::string & sender, const std::string & message, const std::string & when, const std::string & channelType, const std::string & channelName)
  92. {
  93. if (channelType != currentChannelType || channelName != currentChannelName)
  94. {
  95. // mark channel as unread
  96. unreadChannels.insert(channelType + "_" + channelName);
  97. widget->getAccountList()->reset();
  98. widget->getChannelList()->reset();
  99. widget->getMatchList()->reset();
  100. return;
  101. }
  102. MetaString chatMessageFormatted;
  103. chatMessageFormatted.appendRawString("[%s] {%s}: %s\n");
  104. chatMessageFormatted.replaceRawString(when);
  105. chatMessageFormatted.replaceRawString(sender);
  106. chatMessageFormatted.replaceRawString(message);
  107. chatHistory += chatMessageFormatted.toString();
  108. }
  109. void GlobalLobbyWindow::refreshChatText()
  110. {
  111. widget->getGameChat()->setText(chatHistory);
  112. if (widget->getGameChat()->slider)
  113. widget->getGameChat()->slider->scrollToMax();
  114. }
  115. bool GlobalLobbyWindow::isChannelUnread(const std::string & channelType, const std::string & channelName) const
  116. {
  117. return unreadChannels.count(channelType + "_" + channelName) > 0;
  118. }
  119. void GlobalLobbyWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
  120. {
  121. if (accounts.size() == widget->getAccountList()->size())
  122. widget->getAccountList()->reset();
  123. else
  124. widget->getAccountList()->resize(accounts.size());
  125. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.players");
  126. text.replaceNumber(accounts.size());
  127. widget->getAccountListHeader()->setText(text.toString());
  128. }
  129. void GlobalLobbyWindow::onActiveGameRooms(const std::vector<GlobalLobbyRoom> & rooms)
  130. {
  131. if (rooms.size() == widget->getRoomList()->size())
  132. widget->getRoomList()->reset();
  133. else
  134. widget->getRoomList()->resize(rooms.size());
  135. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.rooms");
  136. text.replaceNumber(rooms.size());
  137. widget->getRoomListHeader()->setText(text.toString());
  138. }
  139. void GlobalLobbyWindow::onMatchesHistory(const std::vector<GlobalLobbyRoom> & history)
  140. {
  141. if (history.size() == widget->getMatchList()->size())
  142. widget->getMatchList()->reset();
  143. else
  144. widget->getMatchList()->resize(history.size());
  145. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.history");
  146. text.replaceNumber(history.size());
  147. widget->getMatchListHeader()->setText(text.toString());
  148. }
  149. void GlobalLobbyWindow::onInviteReceived(const std::string & invitedRoomID)
  150. {
  151. widget->getRoomList()->reset();
  152. }
  153. void GlobalLobbyWindow::onJoinedRoom()
  154. {
  155. widget->getAccountList()->reset();
  156. }
  157. void GlobalLobbyWindow::onLeftRoom()
  158. {
  159. widget->getAccountList()->reset();
  160. }