1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-09 13:14:02 +02:00

Merge pull request #3611 from IvanSavenko/lobby_fixes

Networking code fixes
This commit is contained in:
Ivan Savenko 2024-02-13 15:30:26 +02:00 committed by GitHub
commit bfdb9a85f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 29 deletions

View File

@ -163,7 +163,7 @@ void CServerHandler::threadRunNetwork()
logGlobal->info("Ending network thread"); logGlobal->info("Ending network thread");
} }
void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode newServerMode, const std::vector<std::string> & names) void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode newServerMode, const std::vector<std::string> & playerNames)
{ {
hostClientId = -1; hostClientId = -1;
setState(EClientState::NONE); setState(EClientState::NONE);
@ -172,15 +172,15 @@ void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen
th = std::make_unique<CStopWatch>(); th = std::make_unique<CStopWatch>();
logicConnection.reset(); logicConnection.reset();
si = std::make_shared<StartInfo>(); si = std::make_shared<StartInfo>();
playerNames.clear(); localPlayerNames.clear();
si->difficulty = 1; si->difficulty = 1;
si->mode = mode; si->mode = mode;
screenType = screen; screenType = screen;
myNames.clear(); localPlayerNames.clear();
if(!names.empty()) //if have custom set of player names - use it if(!playerNames.empty()) //if have custom set of player names - use it
myNames = names; localPlayerNames = playerNames;
else else
myNames.push_back(settings["general"]["playerName"].String()); localPlayerNames.push_back(settings["general"]["playerName"].String());
} }
GlobalLobbyClient & CServerHandler::getGlobalLobby() GlobalLobbyClient & CServerHandler::getGlobalLobby()
@ -421,7 +421,7 @@ void CServerHandler::sendClientConnecting() const
{ {
LobbyClientConnected lcc; LobbyClientConnected lcc;
lcc.uuid = uuid; lcc.uuid = uuid;
lcc.names = myNames; lcc.names = localPlayerNames;
lcc.mode = si->mode; lcc.mode = si->mode;
sendLobbyPack(lcc); sendLobbyPack(lcc);
} }

View File

@ -101,7 +101,7 @@ class CServerHandler final : public IServerAPI, public LobbyInfo, public INetwor
std::unique_ptr<GlobalLobbyClient> lobbyClient; std::unique_ptr<GlobalLobbyClient> lobbyClient;
std::unique_ptr<CApplier<CBaseForLobbyApply>> applier; std::unique_ptr<CApplier<CBaseForLobbyApply>> applier;
std::shared_ptr<CMapInfo> mapToStart; std::shared_ptr<CMapInfo> mapToStart;
std::vector<std::string> myNames; std::vector<std::string> localPlayerNames;
std::shared_ptr<HighScoreCalculation> highScoreCalc; std::shared_ptr<HighScoreCalculation> highScoreCalc;
boost::thread threadRunLocalServer; boost::thread threadRunLocalServer;
@ -148,7 +148,7 @@ public:
CServerHandler(); CServerHandler();
~CServerHandler(); ~CServerHandler();
void resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode serverMode, const std::vector<std::string> & names); void resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode serverMode, const std::vector<std::string> & playerNames);
void startLocalServerAndConnect(bool connectToLobby); void startLocalServerAndConnect(bool connectToLobby);
void connectToServer(const std::string & addr, const ui16 port); void connectToServer(const std::string & addr, const ui16 port);

View File

@ -22,7 +22,7 @@ bool LobbyServer::isAccountNameValid(const std::string & accountName) const
if(accountName.size() < 4) if(accountName.size() < 4)
return false; return false;
if(accountName.size() < 20) if(accountName.size() > 20)
return false; return false;
for(const auto & c : accountName) for(const auto & c : accountName)

View File

@ -250,8 +250,8 @@ void CVCMIServer::prepareToRestart()
campaignBonus = si->campState->getBonusID(campaignMap).value_or(-1); campaignBonus = si->campState->getBonusID(campaignMap).value_or(-1);
} }
for(auto c : activeConnections) for(auto activeConnection : activeConnections)
c->enterLobbyConnectionMode(); activeConnection->enterLobbyConnectionMode();
gh = nullptr; gh = nullptr;
} }
@ -322,8 +322,8 @@ bool CVCMIServer::prepareToStartGame()
void CVCMIServer::startGameImmediately() void CVCMIServer::startGameImmediately()
{ {
for(auto c : activeConnections) for(auto activeConnection : activeConnections)
c->enterGameplayConnectionMode(gh->gs); activeConnection->enterGameplayConnectionMode(gh->gs);
gh->start(si->mode == EStartMode::LOAD_GAME); gh->start(si->mode == EStartMode::LOAD_GAME);
setState(EServerState::GAMEPLAY); setState(EServerState::GAMEPLAY);
@ -364,14 +364,13 @@ void CVCMIServer::handleReceivedPack(std::unique_ptr<CPackForLobby> pack)
void CVCMIServer::announcePack(std::unique_ptr<CPackForLobby> pack) void CVCMIServer::announcePack(std::unique_ptr<CPackForLobby> pack)
{ {
for(auto c : activeConnections) for(auto activeConnection : activeConnections)
{ {
// FIXME: we need to avoid sending something to client that not yet get answer for LobbyClientConnected // FIXME: we need to avoid sending something to client that not yet get answer for LobbyClientConnected
// Until UUID set we only pass LobbyClientConnected to this client // Until UUID set we only pass LobbyClientConnected to this client
//if(c->uuid == uuid && !dynamic_cast<LobbyClientConnected *>(pack.get())) //if(c->uuid == uuid && !dynamic_cast<LobbyClientConnected *>(pack.get()))
// continue; // continue;
activeConnection->sendPack(pack.get());
c->sendPack(pack.get());
} }
applier->getApplier(CTypeList::getInstance().getTypeID(pack.get()))->applyOnServerAfter(this, pack.get()); applier->getApplier(CTypeList::getInstance().getTypeID(pack.get()))->applyOnServerAfter(this, pack.get());
@ -396,14 +395,14 @@ void CVCMIServer::announceTxt(const std::string & txt, const std::string & playe
bool CVCMIServer::passHost(int toConnectionId) bool CVCMIServer::passHost(int toConnectionId)
{ {
for(auto c : activeConnections) for(auto activeConnection : activeConnections)
{ {
if(isClientHost(c->connectionID)) if(isClientHost(activeConnection->connectionID))
continue; continue;
if(c->connectionID != toConnectionId) if(activeConnection->connectionID != toConnectionId)
continue; continue;
hostClientId = c->connectionID; hostClientId = activeConnection->connectionID;
announceTxt(boost::str(boost::format("Pass host to connection %d") % toConnectionId)); announceTxt(boost::str(boost::format("Pass host to connection %d") % toConnectionId));
return true; return true;
} }
@ -447,10 +446,10 @@ void CVCMIServer::clientConnected(std::shared_ptr<CConnection> c, std::vector<st
} }
} }
void CVCMIServer::clientDisconnected(std::shared_ptr<CConnection> c) void CVCMIServer::clientDisconnected(std::shared_ptr<CConnection> connection)
{ {
c->getConnection()->close(); connection->getConnection()->close();
vstd::erase(activeConnections, c); vstd::erase(activeConnections, connection);
// PlayerReinitInterface startAiPack; // PlayerReinitInterface startAiPack;
// startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI; // startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI;

View File

@ -206,8 +206,8 @@ void ApplyOnServerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack) void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
{ {
for(const auto & c : srv.activeConnections) for(const auto & connection : srv.activeConnections)
c->enterLobbyConnectionMode(); connection->enterLobbyConnectionMode();
} }
void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack) void ClientPermissionsCheckerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
@ -250,11 +250,11 @@ void ApplyOnServerAfterAnnounceNetPackVisitor::visitLobbyStartGame(LobbyStartGam
srv.startGameImmediately(); srv.startGameImmediately();
else else
{ {
for(const auto & c : srv.activeConnections) for(const auto & connection : srv.activeConnections)
{ {
if(c->connectionID == pack.clientId) if(connection->connectionID == pack.clientId)
{ {
c->enterGameplayConnectionMode(srv.gh->gameState()); connection->enterGameplayConnectionMode(srv.gh->gameState());
srv.reconnectPlayer(pack.clientId); srv.reconnectPlayer(pack.clientId);
} }
} }