CVCMIServer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. 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. EServerState state = EServerState::LOBBY;
  42. std::shared_ptr<CConnection> findConnection(const std::shared_ptr<INetworkConnection> &);
  43. int currentClientId;
  44. ui8 currentPlayerId;
  45. uint16_t port;
  46. bool runByClient;
  47. public:
  48. /// List of all active connections
  49. std::vector<std::shared_ptr<CConnection>> activeConnections;
  50. uint16_t prepare(bool connectToLobby, bool listenForConnections);
  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 runByClient);
  58. ~CVCMIServer();
  59. void run();
  60. bool wasStartedByClient() const;
  61. bool prepareToStartGame();
  62. void prepareToRestart();
  63. void startGameImmediately();
  64. uint16_t startAcceptingIncomingConnections(bool listenForConnections);
  65. void threadHandleClient(std::shared_ptr<CConnection> c);
  66. void announcePack(CPackForLobby & pack);
  67. bool passHost(int toConnectionId);
  68. void announceTxt(const MetaString & txt, const std::string & playerName = "system");
  69. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  70. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  71. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  72. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, const std::string & uuid, EStartMode mode);
  73. void clientDisconnected(std::shared_ptr<CConnection> c);
  74. void reconnectPlayer(int connId);
  75. void announceMessage(const MetaString & txt);
  76. void announceMessage(const std::string & txt);
  77. void handleReceivedPack(std::shared_ptr<CConnection> connection, CPackForLobby & pack);
  78. void updateAndPropagateLobbyState();
  79. INetworkHandler & getNetworkHandler();
  80. INetworkServer & getNetworkServer();
  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 setPlayerHandicap(PlayerColor player, Handicap handicap);
  87. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  88. void optionSetHero(PlayerColor player, HeroTypeID id);
  89. HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
  90. bool canUseThisHero(PlayerColor player, HeroTypeID ID);
  91. std::vector<HeroTypeID> getUsedHeroes();
  92. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  93. void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
  94. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  95. void optionSetCastle(PlayerColor player, FactionID id);
  96. // Campaigns
  97. void setCampaignMap(CampaignScenarioID mapId);
  98. void setCampaignBonus(int bonusId);
  99. ui8 getIdOfFirstUnallocatedPlayer() const;
  100. void multiplayerWelcomeMessage();
  101. };