CVCMIServer.h 3.9 KB

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