NetPacksLobbyClient.cpp 7.6 KB

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