NetPacksLobbyClient.cpp 7.4 KB

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