GlobalLobbyWindow.cpp 6.6 KB

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