GlobalLobbyWindow.cpp 5.9 KB

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