NetPacksLobbyClient.cpp 7.1 KB

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