GlobalLobbyWindow.cpp 6.7 KB

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