GlobalLobbyWindow.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/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. widget->getGameChat()->setText("");
  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. 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. unreadInvites.erase(roomID);
  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. widget->getGameChat()->setText(chatHistory);
  109. }
  110. bool GlobalLobbyWindow::isChannelUnread(const std::string & channelType, const std::string & channelName) const
  111. {
  112. return unreadChannels.count(channelType + "_" + channelName) > 0;
  113. }
  114. void GlobalLobbyWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
  115. {
  116. if (accounts.size() == widget->getAccountList()->size())
  117. widget->getAccountList()->reset();
  118. else
  119. widget->getAccountList()->resize(accounts.size());
  120. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.players");
  121. text.replaceNumber(accounts.size());
  122. widget->getAccountListHeader()->setText(text.toString());
  123. }
  124. void GlobalLobbyWindow::onActiveRooms(const std::vector<GlobalLobbyRoom> & rooms)
  125. {
  126. if (rooms.size() == widget->getRoomList()->size())
  127. widget->getRoomList()->reset();
  128. else
  129. widget->getRoomList()->resize(rooms.size());
  130. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.rooms");
  131. text.replaceNumber(rooms.size());
  132. widget->getRoomListHeader()->setText(text.toString());
  133. }
  134. void GlobalLobbyWindow::onMatchesHistory(const std::vector<GlobalLobbyRoom> & history)
  135. {
  136. if (history.size() == widget->getMatchList()->size())
  137. widget->getMatchList()->reset();
  138. else
  139. widget->getMatchList()->resize(history.size());
  140. MetaString text = MetaString::createFromTextID("vcmi.lobby.header.history");
  141. text.replaceNumber(history.size());
  142. widget->getMatchListHeader()->setText(text.toString());
  143. }
  144. void GlobalLobbyWindow::onInviteReceived(const std::string & invitedRoomID)
  145. {
  146. unreadInvites.insert(invitedRoomID);
  147. widget->getRoomList()->reset();
  148. }
  149. bool GlobalLobbyWindow::isInviteUnread(const std::string & gameRoomID) const
  150. {
  151. return unreadInvites.count(gameRoomID) > 0;
  152. }
  153. void GlobalLobbyWindow::onJoinedRoom()
  154. {
  155. widget->getAccountList()->reset();
  156. }
  157. void GlobalLobbyWindow::onLeftRoom()
  158. {
  159. widget->getAccountList()->reset();
  160. }