CVCMIServer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // INetworkListener impl
  53. void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
  54. void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
  55. void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
  56. void onTimer() override;
  57. std::shared_ptr<CGameHandler> gh;
  58. CVCMIServer(uint16_t port, bool connectToLobby, bool runByClient);
  59. ~CVCMIServer();
  60. void run();
  61. bool wasStartedByClient() const;
  62. bool prepareToStartGame();
  63. void prepareToRestart();
  64. void startGameImmediately();
  65. void startAcceptingIncomingConnections();
  66. void threadHandleClient(std::shared_ptr<CConnection> c);
  67. void announcePack(std::unique_ptr<CPackForLobby> pack);
  68. bool passHost(int toConnectionId);
  69. void announceTxt(const MetaString & txt, const std::string & playerName = "system");
  70. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  71. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  72. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  73. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, const std::string & uuid, EStartMode mode);
  74. void clientDisconnected(std::shared_ptr<CConnection> c);
  75. void reconnectPlayer(int connId);
  76. void announceMessage(const MetaString & txt);
  77. void announceMessage(const std::string & txt);
  78. void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
  79. void updateAndPropagateLobbyState();
  80. INetworkHandler & getNetworkHandler();
  81. void setState(EServerState value);
  82. EServerState getState() const;
  83. // Work with LobbyInfo
  84. void setPlayer(PlayerColor clickedColor);
  85. void setPlayerName(PlayerColor player, std::string name);
  86. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  87. void optionSetHero(PlayerColor player, HeroTypeID id);
  88. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  89. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  90. std::vector<HeroTypeID> getUsedHeroes();
  91. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  92. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  93. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  94. void optionSetCastle(PlayerColor player, FactionID id);
  95. // Campaigns
  96. void setCampaignMap(CampaignScenarioID mapId);
  97. void setCampaignBonus(int bonusId);
  98. ui8 getIdOfFirstUnallocatedPlayer() const;
  99. void multiplayerWelcomeMessage();
  100. };