2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* CVCMIServer.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
#include "../lib/network/NetworkInterface.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/StartInfo.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class CMapInfo;
|
|
|
|
|
|
|
|
struct CPackForLobby;
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
class CConnection;
|
2018-01-05 19:21:07 +02:00
|
|
|
struct StartInfo;
|
|
|
|
struct LobbyInfo;
|
2020-10-01 10:38:06 +02:00
|
|
|
struct PlayerSettings;
|
2018-01-05 19:21:07 +02:00
|
|
|
class PlayerColor;
|
2024-05-02 21:03:23 +02:00
|
|
|
class MetaString;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
template<typename T> class CApplier;
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
|
|
|
class CGameHandler;
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForServerApply;
|
|
|
|
class CBaseForGHApply;
|
2024-01-12 13:30:53 +02:00
|
|
|
class GlobalLobbyProcessor;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
enum class EServerState : ui8
|
|
|
|
{
|
|
|
|
LOBBY,
|
|
|
|
GAMEPLAY,
|
|
|
|
SHUTDOWN
|
|
|
|
};
|
|
|
|
|
2024-01-12 13:30:53 +02:00
|
|
|
class CVCMIServer : public LobbyInfo, public INetworkServerListener, public INetworkTimerListener
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
/// Network server instance that receives and processes incoming connections on active socket
|
2024-01-12 01:10:41 +02:00
|
|
|
std::unique_ptr<INetworkServer> networkServer;
|
2024-01-12 13:30:53 +02:00
|
|
|
std::unique_ptr<GlobalLobbyProcessor> lobbyProcessor;
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2023-12-26 20:54:32 +02:00
|
|
|
std::chrono::steady_clock::time_point gameplayStartTime;
|
|
|
|
std::chrono::steady_clock::time_point lastTimerUpdateTime;
|
|
|
|
|
2024-01-12 13:30:53 +02:00
|
|
|
std::unique_ptr<INetworkHandler> networkHandler;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::shared_ptr<CApplier<CBaseForServerApply>> applier;
|
2024-02-03 19:08:45 +02:00
|
|
|
EServerState state = EServerState::LOBBY;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
std::shared_ptr<CConnection> findConnection(const std::shared_ptr<INetworkConnection> &);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
int currentClientId;
|
|
|
|
ui8 currentPlayerId;
|
2024-02-10 23:56:02 +02:00
|
|
|
uint16_t port;
|
|
|
|
bool runByClient;
|
2023-11-18 16:34:18 +02:00
|
|
|
|
|
|
|
public:
|
2024-02-03 17:04:14 +02:00
|
|
|
/// List of all active connections
|
|
|
|
std::vector<std::shared_ptr<CConnection>> activeConnections;
|
|
|
|
|
2024-01-12 17:21:59 +02:00
|
|
|
// INetworkListener impl
|
2024-02-02 01:27:19 +02:00
|
|
|
void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) override;
|
|
|
|
void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) override;
|
2024-01-12 17:21:59 +02:00
|
|
|
void onNewConnection(const std::shared_ptr<INetworkConnection> &) override;
|
|
|
|
void onTimer() override;
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
std::shared_ptr<CGameHandler> gh;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-02-10 23:56:02 +02:00
|
|
|
CVCMIServer(uint16_t port, bool connectToLobby, bool runByClient);
|
2018-01-05 19:21:07 +02:00
|
|
|
~CVCMIServer();
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void run();
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2024-02-10 23:56:02 +02:00
|
|
|
bool wasStartedByClient() const;
|
2022-09-23 13:02:19 +02:00
|
|
|
bool prepareToStartGame();
|
2022-09-29 19:08:05 +02:00
|
|
|
void prepareToRestart();
|
2023-11-18 16:34:18 +02:00
|
|
|
void startGameImmediately();
|
2024-01-21 16:48:36 +02:00
|
|
|
void startAcceptingIncomingConnections();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
void threadHandleClient(std::shared_ptr<CConnection> c);
|
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
void announcePack(std::unique_ptr<CPackForLobby> pack);
|
2018-01-05 19:21:07 +02:00
|
|
|
bool passHost(int toConnectionId);
|
|
|
|
|
2024-05-02 21:03:23 +02:00
|
|
|
void announceTxt(MetaString txt, const std::string & playerName = "system");
|
2018-01-05 19:21:07 +02:00
|
|
|
void announceTxt(const std::string & txt, const std::string & playerName = "system");
|
|
|
|
|
|
|
|
void setPlayerConnectedId(PlayerSettings & pset, ui8 player) const;
|
|
|
|
void updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpt = {});
|
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
void clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, const std::string & uuid, EStartMode mode);
|
2018-01-05 19:21:07 +02:00
|
|
|
void clientDisconnected(std::shared_ptr<CConnection> c);
|
2022-10-01 16:28:45 +02:00
|
|
|
void reconnectPlayer(int connId);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-05-02 21:03:23 +02:00
|
|
|
void announceMessage(MetaString txt);
|
2023-11-18 16:34:18 +02:00
|
|
|
void announceMessage(const std::string & txt);
|
|
|
|
|
|
|
|
void handleReceivedPack(std::unique_ptr<CPackForLobby> pack);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void updateAndPropagateLobbyState();
|
|
|
|
|
2024-02-03 17:04:14 +02:00
|
|
|
INetworkHandler & getNetworkHandler();
|
|
|
|
|
2023-08-22 17:45:13 +02:00
|
|
|
void setState(EServerState value);
|
|
|
|
EServerState getState() const;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
// Work with LobbyInfo
|
|
|
|
void setPlayer(PlayerColor clickedColor);
|
2023-10-13 23:04:35 +02:00
|
|
|
void setPlayerName(PlayerColor player, std::string name);
|
2018-01-05 19:21:07 +02:00
|
|
|
void optionNextHero(PlayerColor player, int dir); //dir == -1 or +1
|
2023-09-28 18:43:04 +02:00
|
|
|
void optionSetHero(PlayerColor player, HeroTypeID id);
|
|
|
|
HeroTypeID nextAllowedHero(PlayerColor player, HeroTypeID id, int direction);
|
|
|
|
bool canUseThisHero(PlayerColor player, HeroTypeID ID);
|
|
|
|
std::vector<HeroTypeID> getUsedHeroes();
|
2018-01-05 19:21:07 +02:00
|
|
|
void optionNextBonus(PlayerColor player, int dir); //dir == -1 or +1
|
2023-09-28 18:43:04 +02:00
|
|
|
void optionSetBonus(PlayerColor player, PlayerStartingBonus id);
|
2018-01-05 19:21:07 +02:00
|
|
|
void optionNextCastle(PlayerColor player, int dir); //dir == -1 or +
|
2023-09-28 18:43:04 +02:00
|
|
|
void optionSetCastle(PlayerColor player, FactionID id);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
// Campaigns
|
2023-06-25 20:16:03 +02:00
|
|
|
void setCampaignMap(CampaignScenarioID mapId);
|
2018-01-05 19:21:07 +02:00
|
|
|
void setCampaignBonus(int bonusId);
|
|
|
|
|
|
|
|
ui8 getIdOfFirstUnallocatedPlayer() const;
|
2024-05-14 22:48:32 +02:00
|
|
|
|
|
|
|
void multiplayerWelcomeMessage();
|
2018-01-05 19:21:07 +02:00
|
|
|
};
|