GlobalLobbyRoomWindow.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * GlobalLobbyRoomWindow.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 "GlobalLobbyRoomWindow.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "GlobalLobbyDefines.h"
  14. #include "GlobalLobbyWindow.h"
  15. #include "../CGameInfo.h"
  16. #include "../CServerHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../mainmenu/CMainMenu.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../widgets/GraphicalPrimitiveCanvas.h"
  24. #include "../widgets/ObjectLists.h"
  25. #include "../../lib/modding/CModHandler.h"
  26. #include "../../lib/modding/CModInfo.h"
  27. #include "../../lib/texts/CGeneralTextHandler.h"
  28. #include "../../lib/texts/MetaString.h"
  29. GlobalLobbyRoomAccountCard::GlobalLobbyRoomAccountCard(const GlobalLobbyAccount & accountDescription)
  30. {
  31. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  32. pos.w = 130;
  33. pos.h = 40;
  34. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64), 1);
  35. labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, accountDescription.displayName);
  36. labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, accountDescription.status);
  37. }
  38. GlobalLobbyRoomModCard::GlobalLobbyRoomModCard(const GlobalLobbyRoomModInfo & modInfo)
  39. {
  40. const std::map<ModVerificationStatus, std::string> statusToString = {
  41. { ModVerificationStatus::NOT_INSTALLED, "missing" },
  42. { ModVerificationStatus::DISABLED, "disabled" },
  43. { ModVerificationStatus::EXCESSIVE, "excessive" },
  44. { ModVerificationStatus::VERSION_MISMATCH, "version" },
  45. { ModVerificationStatus::FULL_MATCH, "compatible" }
  46. };
  47. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  48. pos.w = 200;
  49. pos.h = 40;
  50. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64), 1);
  51. labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, modInfo.modName);
  52. labelVersion = std::make_shared<CLabel>(195, 30, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::YELLOW, modInfo.version);
  53. labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.mod.state." + statusToString.at(modInfo.status)));
  54. }
  55. static std::string getJoinRoomErrorMessage(const GlobalLobbyRoom & roomDescription, const std::vector<GlobalLobbyRoomModInfo> & modVerificationList)
  56. {
  57. bool publicRoom = roomDescription.statusID == "public";
  58. bool privateRoom = roomDescription.statusID == "private";
  59. bool gameStarted = !publicRoom && !privateRoom;
  60. bool hasInvite = CSH->getGlobalLobby().isInvitedToRoom(roomDescription.gameRoomID);
  61. bool alreadyInRoom = CSH->inGame();
  62. if (alreadyInRoom)
  63. return "vcmi.lobby.preview.error.playing";
  64. if (gameStarted)
  65. return "vcmi.lobby.preview.error.busy";
  66. CModVersion localVersion = CModVersion::fromString(VCMI_VERSION_STRING);
  67. CModVersion hostVersion = CModVersion::fromString(roomDescription.gameVersion);
  68. // 1.5.X can play with each other, but not with 1.X.Y
  69. if (localVersion.major != hostVersion.major || localVersion.minor != hostVersion.minor)
  70. return "vcmi.lobby.preview.error.version";
  71. if (roomDescription.playerLimit == roomDescription.participants.size())
  72. return "vcmi.lobby.preview.error.full";
  73. if (privateRoom && !hasInvite)
  74. return "vcmi.lobby.preview.error.invite";
  75. for(const auto & mod : modVerificationList)
  76. {
  77. switch (mod.status)
  78. {
  79. case ModVerificationStatus::NOT_INSTALLED:
  80. case ModVerificationStatus::DISABLED:
  81. case ModVerificationStatus::EXCESSIVE:
  82. return "vcmi.lobby.preview.error.mods";
  83. break;
  84. case ModVerificationStatus::VERSION_MISMATCH:
  85. case ModVerificationStatus::FULL_MATCH:
  86. break;
  87. default:
  88. assert(0);
  89. }
  90. }
  91. return "";
  92. }
  93. GlobalLobbyRoomWindow::GlobalLobbyRoomWindow(GlobalLobbyWindow * window, const std::string & roomUUID)
  94. : CWindowObject(BORDERED)
  95. , window(window)
  96. , roomUUID(roomUUID)
  97. {
  98. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  99. pos.w = 400;
  100. pos.h = 400;
  101. GlobalLobbyRoom roomDescription = CSH->getGlobalLobby().getActiveRoomByName(roomUUID);
  102. for(const auto & modEntry : ModVerificationInfo::verifyListAgainstLocalMods(roomDescription.modList))
  103. {
  104. GlobalLobbyRoomModInfo modInfo;
  105. modInfo.status = modEntry.second;
  106. if (modEntry.second == ModVerificationStatus::EXCESSIVE)
  107. modInfo.version = CGI->modh->getModInfo(modEntry.first).getVerificationInfo().version.toString();
  108. else
  109. modInfo.version = roomDescription.modList.at(modEntry.first).version.toString();
  110. if (modEntry.second == ModVerificationStatus::NOT_INSTALLED)
  111. modInfo.modName = roomDescription.modList.at(modEntry.first).name;
  112. else
  113. modInfo.modName = CGI->modh->getModInfo(modEntry.first).getVerificationInfo().name;
  114. modVerificationList.push_back(modInfo);
  115. }
  116. MetaString subtitleText;
  117. subtitleText.appendTextID("vcmi.lobby.preview.subtitle");
  118. subtitleText.replaceRawString(roomDescription.description);
  119. subtitleText.replaceRawString(roomDescription.hostAccountDisplayName);
  120. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  121. labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.preview.title").toString());
  122. labelSubtitle = std::make_shared<CLabel>( pos.w / 2, 40, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, subtitleText.toString());
  123. labelVersionTitle = std::make_shared<CLabel>( 10, 60, FONT_MEDIUM, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.preview.version").toString());
  124. labelVersionValue = std::make_shared<CLabel>( 10, 80, FONT_MEDIUM, ETextAlignment::CENTERLEFT, Colors::WHITE, roomDescription.gameVersion);
  125. buttonJoin = std::make_shared<CButton>(Point(10, 360), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onJoin(); }, EShortcut::GLOBAL_ACCEPT);
  126. buttonClose = std::make_shared<CButton>(Point(100, 360), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }, EShortcut::GLOBAL_CANCEL);
  127. MetaString joinStatusText;
  128. std::string errorMessage = getJoinRoomErrorMessage(roomDescription, modVerificationList);
  129. if (!errorMessage.empty())
  130. {
  131. joinStatusText.appendTextID("vcmi.lobby.preview.error.header");
  132. joinStatusText.appendRawString("\n");
  133. joinStatusText.appendTextID(errorMessage);
  134. }
  135. else
  136. joinStatusText.appendTextID("vcmi.lobby.preview.allowed");
  137. labelJoinStatus = std::make_shared<CTextBox>(joinStatusText.toString(), Rect(10, 280, 150, 70), 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  138. const auto & createAccountCardCallback = [participants = roomDescription.participants](size_t index) -> std::shared_ptr<CIntObject>
  139. {
  140. if(index < participants.size())
  141. return std::make_shared<GlobalLobbyRoomAccountCard>(participants[index]);
  142. return std::make_shared<CIntObject>();
  143. };
  144. accountListBackground = std::make_shared<TransparentFilledRectangle>(Rect(8, 98, 150, 180), ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1);
  145. accountList = std::make_shared<CListBox>(createAccountCardCallback, Point(10, 116), Point(0, 40), 4, roomDescription.participants.size(), 0, 1 | 4, Rect(130, 0, 160, 160));
  146. accountList->setRedrawParent(true);
  147. accountListTitle = std::make_shared<CLabel>( 12, 109, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.preview.players").toString());
  148. const auto & createModCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  149. {
  150. if(index < modVerificationList.size())
  151. return std::make_shared<GlobalLobbyRoomModCard>(modVerificationList[index]);
  152. return std::make_shared<CIntObject>();
  153. };
  154. modListBackground = std::make_shared<TransparentFilledRectangle>(Rect(178, 48, 220, 340), ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1);
  155. modList = std::make_shared<CListBox>(createModCardCallback, Point(180, 66), Point(0, 40), 8, modVerificationList.size(), 0, 1 | 4, Rect(200, 0, 320, 320));
  156. modList->setRedrawParent(true);
  157. modListTitle = std::make_shared<CLabel>( 182, 59, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.preview.mods").toString());
  158. buttonJoin->block(!errorMessage.empty());
  159. filledBackground->setPlayerColor(PlayerColor(1));
  160. center();
  161. }
  162. void GlobalLobbyRoomWindow::onJoin()
  163. {
  164. window->doJoinRoom(roomUUID);
  165. }
  166. void GlobalLobbyRoomWindow::onClose()
  167. {
  168. close();
  169. }