1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-31 00:07:39 +02:00

Added LobbyPrepareStartGame pack to replace old workarounds

This commit is contained in:
Ivan Savenko
2024-02-03 19:57:23 +02:00
parent 2c2bec791c
commit bd4c7e3ac0
12 changed files with 40 additions and 19 deletions

View File

@@ -317,10 +317,8 @@ void CServerHandler::onConnectionEstablished(const NetworkConnectionPtr & netCon
}
c = std::make_shared<CConnection>(netConnection);
nextClient = std::make_unique<CClient>();
c->uuid = uuid;
c->enterLobbyConnectionMode();
c->setCallback(nextClient.get());
sendClientConnecting();
}
@@ -622,6 +620,9 @@ void CServerHandler::sendStartGame(bool allowOnlyAI) const
if(!settings["session"]["headless"].Bool())
GH.windows().createAndPushWindow<CLoadingScreen>();
LobbyPrepareStartGame lpsg;
sendLobbyPack(lpsg);
LobbyStartGame lsg;
if(client)
{
@@ -631,7 +632,6 @@ void CServerHandler::sendStartGame(bool allowOnlyAI) const
* si = * lsg.initializedStartInfo;
}
sendLobbyPack(lsg);
c->enterLobbyConnectionMode();
}
void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
@@ -644,8 +644,6 @@ void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameSta
if(CMM)
CMM->disable();
std::swap(client, nextClient);
highScoreCalc = nullptr;
switch(si->mode)
@@ -693,8 +691,6 @@ void CServerHandler::restartGameplay()
client->endGame();
client.reset();
nextClient = std::make_unique<CClient>();
c->setCallback(nextClient.get());
c->enterLobbyConnectionMode();
}

View File

@@ -107,10 +107,6 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
void threadRunNetwork();
void threadRunServer(bool connectToLobby);
/// temporary helper member that exists while game in lobby mode
/// required to correctly deserialize gamestate using client-side game callback
std::unique_ptr<CClient> nextClient;
void sendLobbyPack(const CPackForLobby & pack) const override;
void onPacketReceived(const NetworkConnectionPtr &, const std::vector<std::byte> & message) override;

View File

@@ -35,6 +35,7 @@ public:
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyUpdateState(LobbyUpdateState & pack) override;
};

View File

@@ -25,6 +25,7 @@
#include "CServerHandler.h"
#include "CGameInfo.h"
#include "Client.h"
#include "gui/CGuiHandler.h"
#include "gui/WindowHandler.h"
#include "widgets/Buttons.h"
@@ -144,6 +145,13 @@ void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame &
handler.sendStartGame();
}
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
{
handler.client = std::make_unique<CClient>();
handler.c->enterLobbyConnectionMode();
handler.c->setCallback(handler.client.get());
}
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
{
if(pack.clientId != -1 && pack.clientId != handler.c->connectionID)

View File

@@ -152,6 +152,7 @@ public:
virtual void visitLobbyGuiAction(LobbyGuiAction & pack) {}
virtual void visitLobbyLoadProgress(LobbyLoadProgress & pack) {}
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) {}
virtual void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) {}
virtual void visitLobbyStartGame(LobbyStartGame & pack) {}
virtual void visitLobbyChangeHost(LobbyChangeHost & pack) {}
virtual void visitLobbyUpdateState(LobbyUpdateState & pack) {}

View File

@@ -718,6 +718,11 @@ void LobbyStartGame::visitTyped(ICPackVisitor & visitor)
visitor.visitLobbyStartGame(*this);
}
void LobbyPrepareStartGame::visitTyped(ICPackVisitor & visitor)
{
visitor.visitLobbyPrepareStartGame(*this);
}
void LobbyChangeHost::visitTyped(ICPackVisitor & visitor)
{
visitor.visitLobbyChangeHost(*this);

View File

@@ -120,6 +120,15 @@ struct DLL_LINKAGE LobbyRestartGame : public CLobbyPackToPropagate
}
};
struct DLL_LINKAGE LobbyPrepareStartGame : public CLobbyPackToPropagate
{
void visitTyped(ICPackVisitor & visitor) override;
template <typename Handler> void serialize(Handler &h)
{
}
};
struct DLL_LINKAGE LobbyStartGame : public CLobbyPackToPropagate
{
// Set by server

View File

@@ -38,6 +38,7 @@ void registerTypesLobbyPacks(Serializer &s)
s.template registerType<CLobbyPackToPropagate, LobbyGuiAction>();
s.template registerType<CLobbyPackToPropagate, LobbyLoadProgress>();
s.template registerType<CLobbyPackToPropagate, LobbyRestartGame>();
s.template registerType<CLobbyPackToPropagate, LobbyPrepareStartGame>();
s.template registerType<CLobbyPackToPropagate, LobbyStartGame>();
s.template registerType<CLobbyPackToPropagate, LobbyChangeHost>();
// Only server send

View File

@@ -62,8 +62,7 @@ CConnection::CConnection(std::weak_ptr<INetworkConnection> networkConnection)
{
assert(networkConnection.lock() != nullptr);
enableSmartPointerSerialization();
disableStackSendingByID();
enterLobbyConnectionMode();
deserializer->version = ESerializationVersion::CURRENT;
}
@@ -139,8 +138,7 @@ void CConnection::enterGameplayConnectionMode(CGameState * gs)
disableSmartPointerSerialization();
setCallback(gs->callback);
packWriter->addStdVecItems(gs);
packReader->addStdVecItems(gs);
enableSmartVectorMemberSerializatoin(gs);
}
void CConnection::disableSmartPointerSerialization()
@@ -161,10 +159,10 @@ void CConnection::disableSmartVectorMemberSerialization()
packWriter->smartVectorMembersSerialization = false;
}
void CConnection::enableSmartVectorMemberSerializatoin()
void CConnection::enableSmartVectorMemberSerializatoin(CGameState * gs)
{
packReader->smartVectorMembersSerialization = true;
packWriter->smartVectorMembersSerialization = true;
packWriter->addStdVecItems(gs);
packReader->addStdVecItems(gs);
}
VCMI_LIB_NAMESPACE_END

View File

@@ -37,7 +37,7 @@ class DLL_LINKAGE CConnection : boost::noncopyable
void disableSmartPointerSerialization();
void enableSmartPointerSerialization();
void disableSmartVectorMemberSerialization();
void enableSmartVectorMemberSerializatoin();
void enableSmartVectorMemberSerializatoin(CGameState * gs);
public:
bool isMyConnection(const std::shared_ptr<INetworkConnection> & otherConnection) const;

View File

@@ -32,6 +32,7 @@ public:
virtual void visitLobbyClientConnected(LobbyClientConnected & pack) override;
virtual void visitLobbyClientDisconnected(LobbyClientDisconnected & pack) override;
virtual void visitLobbyRestartGame(LobbyRestartGame & pack) override;
virtual void visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) override;
virtual void visitLobbyStartGame(LobbyStartGame & pack) override;
virtual void visitLobbyChangeHost(LobbyChangeHost & pack) override;
virtual void visitLobbyChangePlayerOption(LobbyChangePlayerOption & pack) override;

View File

@@ -208,6 +208,11 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestar
c->enterLobbyConnectionMode();
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
{
result = srv.isClientHost(pack.c->connectionID);
}
void ClientPermissionsCheckerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
{
result = srv.isClientHost(pack.c->connectionID);