NetPacksLobbyClient.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * NetPacksLobbyClient.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 "LobbyClientNetPackVisitors.h"
  12. #include "lobby/CSelectionBase.h"
  13. #include "lobby/CLobbyScreen.h"
  14. #include "lobby/OptionsTab.h"
  15. #include "lobby/RandomMapTab.h"
  16. #include "lobby/TurnOptionsTab.h"
  17. #include "lobby/ExtraOptionsTab.h"
  18. #include "lobby/SelectionTab.h"
  19. #include "lobby/CBonusSelection.h"
  20. #include "lobby/BattleOnlyMode.h"
  21. #include "globalLobby/GlobalLobbyWindow.h"
  22. #include "globalLobby/GlobalLobbyServerSetup.h"
  23. #include "globalLobby/GlobalLobbyClient.h"
  24. #include "CServerHandler.h"
  25. #include "GameChatHandler.h"
  26. #include "Client.h"
  27. #include "GameEngine.h"
  28. #include "GameInstance.h"
  29. #include "gui/WindowHandler.h"
  30. #include "widgets/Buttons.h"
  31. #include "widgets/TextControls.h"
  32. #include "media/CMusicHandler.h"
  33. #include "media/IVideoPlayer.h"
  34. #include "windows/GUIClasses.h"
  35. #include "../lib/CConfigHandler.h"
  36. #include "../lib/campaign/CampaignState.h"
  37. #include "../lib/serializer/GameConnection.h"
  38. #include "../lib/texts/CGeneralTextHandler.h"
  39. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
  40. {
  41. result = false;
  42. // Check if it's LobbyClientConnected for our client
  43. if(pack.uuid == handler.logicConnection->uuid)
  44. {
  45. handler.logicConnection->setSerializationVersion(pack.version);
  46. handler.logicConnection->connectionID = pack.clientId;
  47. if(handler.mapToStart)
  48. {
  49. handler.setMapInfo(handler.mapToStart);
  50. }
  51. else if(!settings["session"]["headless"].Bool())
  52. {
  53. if (ENGINE->windows().topWindow<CSimpleJoinScreen>())
  54. ENGINE->windows().popWindows(1);
  55. if (!ENGINE->windows().findWindows<GlobalLobbyServerSetup>().empty())
  56. {
  57. assert(handler.serverMode == EServerMode::LOBBY_HOST);
  58. // announce opened game room
  59. // TODO: find better approach?
  60. int roomType = settings["lobby"]["roomType"].Integer();
  61. int roomPlayerLimit = settings["lobby"]["roomPlayerLimit"].Integer();
  62. if (roomType != 0)
  63. handler.getGlobalLobby().sendOpenRoom("private", roomPlayerLimit);
  64. else
  65. handler.getGlobalLobby().sendOpenRoom("public", roomPlayerLimit);
  66. }
  67. while (!ENGINE->windows().findWindows<GlobalLobbyWindow>().empty())
  68. {
  69. // if global lobby is open, pop all dialogs on top of it as well as lobby itself
  70. ENGINE->windows().popWindows(1);
  71. }
  72. bool hideScreen = handler.campaignStateToSend && (!handler.campaignStateToSend->campaignSet.empty() || handler.campaignStateToSend->lastScenario());
  73. ENGINE->windows().createAndPushWindow<CLobbyScreen>(handler.screenType, hideScreen);
  74. }
  75. handler.setState(EClientState::LOBBY);
  76. }
  77. }
  78. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  79. {
  80. if(pack.clientId != handler.logicConnection->connectionID)
  81. {
  82. result = false;
  83. return;
  84. }
  85. }
  86. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
  87. {
  88. if(auto w = ENGINE->windows().topWindow<CLoadingScreen>())
  89. ENGINE->windows().popWindow(w);
  90. if(ENGINE->windows().count() > 0)
  91. ENGINE->windows().popWindows(1);
  92. }
  93. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
  94. {
  95. handler.getGameChat().onNewLobbyMessageReceived(pack.playerName, pack.message.toString());
  96. }
  97. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
  98. {
  99. if(!lobby || !handler.isGuest())
  100. return;
  101. if(auto topWindow = ENGINE->windows().topWindow<BattleOnlyModeWindow>())
  102. topWindow->close();
  103. switch(pack.action)
  104. {
  105. case LobbyGuiAction::NO_TAB:
  106. lobby->toggleTab(lobby->curTab);
  107. break;
  108. case LobbyGuiAction::OPEN_OPTIONS:
  109. lobby->toggleTab(lobby->tabOpt);
  110. break;
  111. case LobbyGuiAction::OPEN_SCENARIO_LIST:
  112. lobby->toggleTab(lobby->tabSel);
  113. break;
  114. case LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS:
  115. lobby->toggleTab(lobby->tabRand);
  116. break;
  117. case LobbyGuiAction::OPEN_TURN_OPTIONS:
  118. lobby->toggleTab(lobby->tabTurnOptions);
  119. break;
  120. case LobbyGuiAction::OPEN_EXTRA_OPTIONS:
  121. lobby->toggleTab(lobby->tabExtraOptions);
  122. break;
  123. case LobbyGuiAction::BATTLE_MODE:
  124. BattleOnlyMode::openBattleWindow();
  125. break;
  126. }
  127. }
  128. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
  129. {
  130. assert(handler.getState() == EClientState::GAMEPLAY);
  131. handler.restartGameplay();
  132. handler.sendStartGame();
  133. }
  134. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
  135. {
  136. handler.logicConnection->enterLobbyConnectionMode();
  137. }
  138. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  139. {
  140. handler.setState(EClientState::STARTING);
  141. if(handler.si->mode != EStartMode::LOAD_GAME)
  142. {
  143. auto modeBackup = handler.si->mode;
  144. handler.si = pack.initializedStartInfo;
  145. handler.si->mode = modeBackup;
  146. }
  147. handler.client = std::make_unique<CClient>();
  148. handler.startGameplay(pack.initializedGameState);
  149. }
  150. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
  151. {
  152. if(auto w = ENGINE->windows().topWindow<CLoadingScreen>())
  153. {
  154. w->finish();
  155. w->tick(0);
  156. w->redraw();
  157. }
  158. }
  159. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyLoadProgress(LobbyLoadProgress & pack)
  160. {
  161. if(auto w = ENGINE->windows().topWindow<CLoadingScreen>())
  162. {
  163. w->set(pack.progress);
  164. w->tick(0);
  165. w->redraw();
  166. }
  167. }
  168. void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState & pack)
  169. {
  170. pack.hostChanged = pack.state.hostClientId != handler.hostClientId;
  171. static_cast<LobbyState &>(handler) = pack.state;
  172. if(handler.mapToStart && handler.mi)
  173. {
  174. handler.startMapAfterConnection(nullptr);
  175. handler.sendStartGame();
  176. }
  177. }
  178. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState & pack)
  179. {
  180. if(!lobby) //stub: ignore message for game mode
  181. return;
  182. if(!lobby->bonusSel && handler.si->campState && handler.getState() == EClientState::LOBBY_CAMPAIGN)
  183. {
  184. auto bonusSel = std::make_shared<CBonusSelection>();
  185. lobby->bonusSel = bonusSel;
  186. if(!handler.si->campState->conqueredScenarios().size() && !handler.si->campState->getIntroVideo().empty() && ENGINE->video().open(handler.si->campState->getIntroVideo(), 1))
  187. {
  188. ENGINE->music().stopMusic();
  189. ENGINE->windows().createAndPushWindow<VideoWindow>(handler.si->campState->getIntroVideo(), handler.si->campState->getVideoRim().empty() ? ImagePath::builtin("INTRORIM") : handler.si->campState->getVideoRim(), false, 1, [bonusSel](bool skipped){
  190. if(!GAME->server().si->campState->getMusic().empty())
  191. ENGINE->music().playMusic(GAME->server().si->campState->getMusic(), true, false);
  192. ENGINE->windows().pushWindow(bonusSel);
  193. });
  194. }
  195. else
  196. ENGINE->windows().pushWindow(bonusSel);
  197. }
  198. if(lobby->bonusSel)
  199. lobby->bonusSel->updateAfterStateChange();
  200. else
  201. lobby->updateAfterStateChange();
  202. if(pack.hostChanged || pack.refreshList)
  203. lobby->toggleMode(handler.isHost());
  204. }
  205. void ApplyOnLobbyScreenNetPackVisitor::visitLobbyShowMessage(LobbyShowMessage & pack)
  206. {
  207. if(!lobby) //stub: ignore message for game mode
  208. return;
  209. lobby->buttonStart->block(false);
  210. handler.showServerError(pack.message.toString());
  211. }
  212. void ApplyOnLobbyScreenNetPackVisitor::visitLobbySetBattleOnlyModeStartInfo(LobbySetBattleOnlyModeStartInfo & pack)
  213. {
  214. if(auto topWindow = ENGINE->windows().topWindow<BattleOnlyModeWindow>())
  215. topWindow->applyStartInfo(pack.startInfo);
  216. }