CVCMIServer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/serializer/Connection.h"
  12. #include "../lib/StartInfo.h"
  13. #include <boost/program_options.hpp>
  14. class CMapInfo;
  15. struct CPackForLobby;
  16. class CGameHandler;
  17. struct SharedMemory;
  18. struct StartInfo;
  19. struct LobbyInfo;
  20. class PlayerSettings;
  21. class PlayerColor;
  22. template<typename T> class CApplier;
  23. class CBaseForServerApply;
  24. class CBaseForGHApply;
  25. enum class EServerState : ui8
  26. {
  27. LOBBY,
  28. GAMEPLAY,
  29. GAMEPLAY_ENDED,
  30. SHUTDOWN
  31. };
  32. class CVCMIServer : public LobbyInfo
  33. {
  34. std::atomic<bool> restartGameplay; // FIXME: this is just a hack
  35. std::shared_ptr<boost::asio::io_service> io;
  36. std::shared_ptr<TAcceptor> acceptor;
  37. std::shared_ptr<TSocket> upcomingConnection;
  38. std::list<CPackForLobby *> announceQueue;
  39. boost::recursive_mutex mx;
  40. std::shared_ptr<CApplier<CBaseForServerApply>> applier;
  41. public:
  42. std::shared_ptr<CGameHandler> gh;
  43. std::atomic<EServerState> state;
  44. ui16 port;
  45. boost::program_options::variables_map cmdLineOptions;
  46. std::set<std::shared_ptr<CConnection>> connections;
  47. std::atomic<int> currentClientId;
  48. std::atomic<ui8> currentPlayerId;
  49. std::shared_ptr<CConnection> hostClient;
  50. CVCMIServer(boost::program_options::variables_map & opts);
  51. ~CVCMIServer();
  52. void run();
  53. void prepareToStartGame();
  54. void startGameImmidiately();
  55. void startAsyncAccept();
  56. void connectionAccepted(const boost::system::error_code & ec);
  57. void threadHandleClient(std::shared_ptr<CConnection> c);
  58. void threadAnnounceLobby();
  59. void handleReceivedPack(CPackForLobby * pack);
  60. void announcePack(CPackForLobby * pack);
  61. bool passHost(int toConnectionId);
  62. void announceTxt(const std::string & txt, const std::string & playerName = "system");
  63. void addToAnnounceQueue(CPackForLobby * pack);
  64. void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
  65. void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
  66. void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode);
  67. void clientDisconnected(std::shared_ptr<CConnection> c);
  68. void updateAndPropagateLobbyState();
  69. // Work with LobbyInfo
  70. void setPlayer(PlayerColor clickedColor);
  71. void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
  72. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir);
  73. bool canUseThisHero(PlayerColor player, int ID);
  74. std::vector<int> getUsedHeroes();
  75. void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
  76. void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
  77. // Campaigns
  78. void setCampaignMap(int mapId);
  79. void setCampaignBonus(int bonusId);
  80. ui8 getIdOfFirstUnallocatedPlayer() const;
  81. #ifdef VCMI_ANDROID
  82. static void create();
  83. #endif
  84. };