CVCMIServer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * CVCMIServer.h, 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. #pragma once
  11. #include "../lib/network/NetworkInterface.h"
  12. #include "../lib/StartInfo.h"
  13. #include "../lib/ResourceSet.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CMapInfo;
  16. struct CPackForLobby;
  17. class CConnection;
  18. struct StartInfo;
  19. struct LobbyInfo;
  20. struct PlayerSettings;
  21. class PlayerColor;
  22. class MetaString;
  23. template<typename T> class CApplier;
  24. VCMI_LIB_NAMESPACE_END
  25. class CGameHandler;
  26. class CBaseForServerApply;
  27. class CBaseForGHApply;
  28. class GlobalLobbyProcessor;
  29. enum class EServerState : ui8
  30. {
  31. LOBBY,
  32. GAMEPLAY,
  33. SHUTDOWN
  34. };
  35. class CVCMIServer : public LobbyInfo, public INetworkServerListener, public INetworkTimerListener
  36. {
  37. /// Network server instance that receives and processes incoming connections on active socket
  38. std::unique_ptr<INetworkServer> networkServer;
  39. std::unique_ptr<GlobalLobbyProcessor> lobbyProcessor;
  40. std::chrono::steady_clock::time_point gameplayStartTime;
  41. std::chrono::steady_clock::time_point lastTimerUpdateTime;
  42. std::unique_ptr<INetworkHandler> networkHandler;
  43. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  44. EServerState state = EServerState::LOBBY;
  45. std::shared_ptr<CConnection> findConnection(const std::shared_ptr<INetworkConnection> &);
  46. int currentClientId;
  47. ui8 currentPlayerId;
  48. uint16_t port;
  49. bool runByClient;
  50. public:
  51. /// List of all active connections
  52. std::vector<std::shared_ptr<CConnection>> activeConnections;
  53. uint16_t prepare(bool connectToLobby);
  54. // INetworkListener impl
  55. void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
  56. void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
  57. void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
  58. void onTimer() override;
  59. std::shared_ptr<CGameHandler> gh;
  60. CVCMIServer(uint16_t port, bool runByClient);
  61. ~CVCMIServer();
  62. void run();
  63. bool wasStartedByClient() const;
  64. bool prepareToStartGame();
  65. void prepareToRestart();
  66. void startGameImmediately();
  67. uint16_t startAcceptingIncomingConnections();
  68. void threadHandleClient(std::shared_ptr<CConnection> c);
  69. void announcePack(std::unique_ptr<CPackForLobby> pack);
  70. bool passHost(int toConnectionId);
  71. void announceTxt(const MetaString & txt, const std::string & playerName = "system");
  72. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  73. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  74. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  75. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, const std::string & uuid, EStartMode mode);
  76. void clientDisconnected(std::shared_ptr<CConnection> c);
  77. void reconnectPlayer(int connId);
  78. void announceMessage(const MetaString & txt);
  79. void announceMessage(const std::string & txt);
  80. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  81. void updateAndPropagateLobbyState();
  82. INetworkHandler & getNetworkHandler();
  83. void setState(EServerState value);
  84. EServerState getState() const;
  85. // Work with LobbyInfo
  86. void setPlayer(PlayerColor clickedColor);
  87. void setPlayerName(PlayerColor player, std::string name);
  88. void setPlayerHandicap(PlayerColor player, PlayerSettings::Handicap handicap);
  89. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  90. void optionSetHero(PlayerColor player, HeroTypeID id);
  91. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  92. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  93. std::vector<HeroTypeID> getUsedHeroes();
  94. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  95. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  96. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  97. void optionSetCastle(PlayerColor player, FactionID id);
  98. // Campaigns
  99. void setCampaignMap(CampaignScenarioID mapId);
  100. void setCampaignBonus(int bonusId);
  101. ui8 getIdOfFirstUnallocatedPlayer() const;
  102. void multiplayerWelcomeMessage();
  103. };