Browse Source

Simplify server networking code, disable player takeover by AI for now

Ivan Savenko 1 year ago
parent
commit
11c2708d83
2 changed files with 41 additions and 51 deletions
  1. 41 48
      server/CVCMIServer.cpp
  2. 0 3
      server/CVCMIServer.h

+ 41 - 48
server/CVCMIServer.cpp

@@ -381,8 +381,6 @@ void CVCMIServer::onDisconnected(const std::shared_ptr<NetworkConnection> & conn
 	logNetwork->error("Network error receiving a pack. Connection has been closed");
 
 	std::shared_ptr<CConnection> c = findConnection(connection);
-
-	inactiveConnections.push_back(c);
 	vstd::erase(activeConnections, c);
 
 	if(activeConnections.empty() || hostClientId == c->connectionID)
@@ -391,19 +389,12 @@ void CVCMIServer::onDisconnected(const std::shared_ptr<NetworkConnection> & conn
 	if(gh && state == EServerState::GAMEPLAY)
 	{
 		gh->handleClientDisconnection(c);
-	}
-
-	boost::unique_lock<boost::recursive_mutex> queueLock(mx);
 
-//	if(c->connected)
-//	{
-//		auto lcd = std::make_unique<LobbyClientDisconnected>();
-//		lcd->c = c;
-//		lcd->clientId = c->connectionID;
-//		handleReceivedPack(std::move(lcd));
-//	}
-//
-//	logNetwork->info("Thread listening for %s ended", c->toString());
+		auto lcd = std::make_unique<LobbyClientDisconnected>();
+		lcd->c = c;
+		lcd->clientId = c->connectionID;
+		handleReceivedPack(std::move(lcd));
+	}
 }
 
 void CVCMIServer::handleReceivedPack(std::unique_ptr<CPackForLobby> pack)
@@ -508,41 +499,43 @@ void CVCMIServer::clientDisconnected(std::shared_ptr<CConnection> c)
 		state = EServerState::SHUTDOWN;
 		return;
 	}
-	
-	PlayerReinitInterface startAiPack;
-	startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI;
-	
-	for(auto it = playerNames.begin(); it != playerNames.end();)
-	{
-		if(it->second.connection != c->connectionID)
-		{
-			++it;
-			continue;
-		}
 
-		int id = it->first;
-		std::string playerLeftMsgText = boost::str(boost::format("%s (pid %d cid %d) left the game") % id % playerNames[id].name % c->connectionID);
-		announceTxt(playerLeftMsgText); //send lobby text, it will be ignored for non-lobby clients
-		auto * playerSettings = si->getPlayersSettings(id);
-		if(!playerSettings)
-		{
-			++it;
-			continue;
-		}
-		
-		it = playerNames.erase(it);
-		setPlayerConnectedId(*playerSettings, PlayerSettings::PLAYER_AI);
-		
-		if(gh && si && state == EServerState::GAMEPLAY)
-		{
-			gh->playerMessages->broadcastMessage(playerSettings->color, playerLeftMsgText);
-	//		gh->connections[playerSettings->color].insert(hostClient);
-			startAiPack.players.push_back(playerSettings->color);
-		}
-	}
-	
-	if(!startAiPack.players.empty())
-		gh->sendAndApply(&startAiPack);
+	// TODO: close network connection
+
+//	PlayerReinitInterface startAiPack;
+//	startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI;
+//
+//	for(auto it = playerNames.begin(); it != playerNames.end();)
+//	{
+//		if(it->second.connection != c->connectionID)
+//		{
+//			++it;
+//			continue;
+//		}
+//
+//		int id = it->first;
+//		std::string playerLeftMsgText = boost::str(boost::format("%s (pid %d cid %d) left the game") % id % playerNames[id].name % c->connectionID);
+//		announceTxt(playerLeftMsgText); //send lobby text, it will be ignored for non-lobby clients
+//		auto * playerSettings = si->getPlayersSettings(id);
+//		if(!playerSettings)
+//		{
+//			++it;
+//			continue;
+//		}
+//
+//		it = playerNames.erase(it);
+//		setPlayerConnectedId(*playerSettings, PlayerSettings::PLAYER_AI);
+//
+//		if(gh && si && state == EServerState::GAMEPLAY)
+//		{
+//			gh->playerMessages->broadcastMessage(playerSettings->color, playerLeftMsgText);
+//	//		gh->connections[playerSettings->color].insert(hostClient);
+//			startAiPack.players.push_back(playerSettings->color);
+//		}
+//	}
+//
+//	if(!startAiPack.players.empty())
+//		gh->sendAndApply(&startAiPack);
 }
 
 void CVCMIServer::reconnectPlayer(int connId)

+ 0 - 3
server/CVCMIServer.h

@@ -63,9 +63,6 @@ public:
 	std::vector<std::shared_ptr<CConnection>> activeConnections;
 
 private:
-	/// List of all connections that were closed (but can still reconnect later)
-	std::vector<std::shared_ptr<CConnection>> inactiveConnections;
-
 	bool restartGameplay; // FIXME: this is just a hack
 
 	boost::recursive_mutex mx;