CVCMIServer.h 4.2 KB

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