NetPacksLobbyClient.cpp 6.7 KB

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