NetPacksLobbyClient.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "lobby/CSelectionBase.h"
  12. #include "lobby/CLobbyScreen.h"
  13. #include "lobby/OptionsTab.h"
  14. #include "lobby/RandomMapTab.h"
  15. #include "lobby/SelectionTab.h"
  16. #include "lobby/CBonusSelection.h"
  17. #include "CServerHandler.h"
  18. #include "CGameInfo.h"
  19. #include "gui/CGuiHandler.h"
  20. #include "widgets/Buttons.h"
  21. #include "widgets/TextControls.h"
  22. #include "../lib/CConfigHandler.h"
  23. #include "../lib/CGeneralTextHandler.h"
  24. #include "../lib/NetPacksLobby.h"
  25. #include "../lib/serializer/Connection.h"
  26. bool LobbyClientConnected::applyOnLobbyHandler(CServerHandler * handler)
  27. {
  28. // Check if it's LobbyClientConnected for our client
  29. if(uuid == handler->c->uuid)
  30. {
  31. handler->c->connectionID = clientId;
  32. if(!settings["session"]["headless"].Bool())
  33. GH.pushIntT<CLobbyScreen>(static_cast<ESelectionScreen>(handler->screenType));
  34. handler->state = EClientState::LOBBY;
  35. return true;
  36. }
  37. return false;
  38. }
  39. void LobbyClientConnected::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  40. {
  41. }
  42. bool LobbyClientDisconnected::applyOnLobbyHandler(CServerHandler * handler)
  43. {
  44. if(clientId != c->connectionID)
  45. return false;
  46. handler->stopServerConnection();
  47. return true;
  48. }
  49. void LobbyClientDisconnected::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  50. {
  51. if(GH.listInt.size())
  52. GH.popInts(1);
  53. }
  54. void LobbyChatMessage::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  55. {
  56. if(lobby && lobby->card)
  57. {
  58. lobby->card->chat->addNewMessage(playerName + ": " + message);
  59. lobby->card->setChat(true);
  60. if(lobby->buttonChat)
  61. lobby->buttonChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  62. }
  63. }
  64. void LobbyGuiAction::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  65. {
  66. if(!lobby || !handler->isGuest())
  67. return;
  68. switch(action)
  69. {
  70. case NO_TAB:
  71. lobby->toggleTab(lobby->curTab);
  72. break;
  73. case OPEN_OPTIONS:
  74. lobby->toggleTab(lobby->tabOpt);
  75. break;
  76. case OPEN_SCENARIO_LIST:
  77. lobby->toggleTab(lobby->tabSel);
  78. break;
  79. case OPEN_RANDOM_MAP_OPTIONS:
  80. lobby->toggleTab(lobby->tabRand);
  81. break;
  82. }
  83. }
  84. bool LobbyEndGame::applyOnLobbyHandler(CServerHandler * handler)
  85. {
  86. if(handler->state == EClientState::GAMEPLAY)
  87. {
  88. handler->endGameplay(closeConnection, restart);
  89. }
  90. if(restart)
  91. handler->sendStartGame();
  92. return true;
  93. }
  94. bool LobbyStartGame::applyOnLobbyHandler(CServerHandler * handler)
  95. {
  96. if(clientId != -1 && clientId != handler->c->connectionID)
  97. return false;
  98. handler->state = EClientState::STARTING;
  99. if(handler->si->mode != StartInfo::LOAD_GAME || clientId == handler->c->connectionID)
  100. {
  101. auto modeBackup = handler->si->mode;
  102. handler->si = initializedStartInfo;
  103. handler->si->mode = modeBackup;
  104. }
  105. if(settings["session"]["headless"].Bool())
  106. handler->startGameplay(initializedGameState);
  107. return true;
  108. }
  109. void LobbyStartGame::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  110. {
  111. if(clientId != -1 && clientId != handler->c->connectionID)
  112. return;
  113. GH.pushIntT<CLoadingScreen>(std::bind(&CServerHandler::startGameplay, handler, initializedGameState));
  114. }
  115. bool LobbyUpdateState::applyOnLobbyHandler(CServerHandler * handler)
  116. {
  117. hostChanged = state.hostClientId != handler->hostClientId;
  118. static_cast<LobbyState &>(*handler) = state;
  119. return true;
  120. }
  121. void LobbyUpdateState::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  122. {
  123. if(!lobby) //stub: ignore message for game mode
  124. return;
  125. if(!lobby->bonusSel && handler->si->campState && handler->state == EClientState::LOBBY_CAMPAIGN)
  126. {
  127. lobby->bonusSel = std::make_shared<CBonusSelection>();
  128. GH.pushInt(lobby->bonusSel);
  129. }
  130. if(lobby->bonusSel)
  131. lobby->bonusSel->updateAfterStateChange();
  132. else
  133. lobby->updateAfterStateChange();
  134. if(hostChanged)
  135. lobby->toggleMode(handler->isHost());
  136. }
  137. void LobbyShowMessage::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
  138. {
  139. if(!lobby) //stub: ignore message for game mode
  140. return;
  141. lobby->buttonStart->block(false);
  142. handler->showServerError(message);
  143. }