Use current team info to determine next player team

This commit is contained in:
Benau 2018-08-31 19:00:14 +08:00
parent 15853c4e32
commit 7fe6023e4f
3 changed files with 41 additions and 17 deletions

View File

@ -241,3 +241,22 @@ void GameSetup::sortPlayersForTeamGame()
player->setTeam((KartTeam)(i % 2)); player->setTeam((KartTeam)(i % 2));
} }
} // sortPlayersForTeamGame } // sortPlayersForTeamGame
// ----------------------------------------------------------------------------
std::pair<int, int> GameSetup::getPlayerTeamInfo() const
{
std::lock_guard<std::mutex> lock(m_players_mutex);
int red_count = 0;
int blue_count = 0;
for (auto& p : m_players)
{
auto player = p.lock();
if (!player)
continue;
if (player->getTeam() == KART_TEAM_RED)
red_count++;
else if (player->getTeam() == KART_TEAM_BLUE)
blue_count++;
}
return std::make_pair(red_count, blue_count);
} // getPlayerTeamInfo

View File

@ -29,6 +29,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
class NetworkPlayerProfile; class NetworkPlayerProfile;
@ -171,7 +172,8 @@ public:
m_hit_capture_limit = hc; m_hit_capture_limit = hc;
m_battle_time_limit = time; m_battle_time_limit = time;
} }
// ------------------------------------------------------------------------
std::pair<int, int> getPlayerTeamInfo() const;
}; };
#endif // GAME_SETUP_HPP #endif // GAME_SETUP_HPP

View File

@ -766,22 +766,11 @@ void ServerLobby::startSelection(const Event *event)
} }
} }
auto players = m_game_setup->getConnectedPlayers();
const unsigned player_count = (unsigned)players.size();
if (NetworkConfig::get()->hasTeamChoosing() && race_manager->teamEnabled()) if (NetworkConfig::get()->hasTeamChoosing() && race_manager->teamEnabled())
{ {
int red_count = 0; auto red_blue = m_game_setup->getPlayerTeamInfo();
int blue_count = 0; if ((red_blue.first == 0 || red_blue.second == 0) &&
for (auto& player : players) red_blue.first + red_blue.second != 1)
{
if (player->getTeam() == KART_TEAM_RED)
red_count++;
else if (player->getTeam() == KART_TEAM_BLUE)
blue_count++;
if (red_count != 0 && blue_count != 0)
break;
}
if ((red_count == 0 || blue_count == 0) && player_count != 1)
{ {
Log::warn("ServerLobby", "Bad team choosing."); Log::warn("ServerLobby", "Bad team choosing.");
NetworkString* bt = getNetworkString(); NetworkString* bt = getNetworkString();
@ -835,7 +824,7 @@ void ServerLobby::startSelection(const Event *event)
while (it != m_available_kts.second.end()) while (it != m_available_kts.second.end())
{ {
Track* t = track_manager->getTrack(*it); Track* t = track_manager->getTrack(*it);
if (t->getMaxArenaPlayers() < player_count) if (t->getMaxArenaPlayers() < m_game_setup->getPlayerCount())
{ {
it = m_available_kts.second.erase(it); it = m_available_kts.second.erase(it);
} }
@ -1507,6 +1496,7 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
} }
unsigned player_count = data.getUInt8(); unsigned player_count = data.getUInt8();
auto red_blue = m_game_setup->getPlayerTeamInfo();
for (unsigned i = 0; i < player_count; i++) for (unsigned i = 0; i < player_count; i++)
{ {
core::stringw name; core::stringw name;
@ -1520,7 +1510,20 @@ void ServerLobby::handleUnencryptedConnection(std::shared_ptr<STKPeer> peer,
per_player_difficulty, (uint8_t)i, KART_TEAM_NONE); per_player_difficulty, (uint8_t)i, KART_TEAM_NONE);
if (NetworkConfig::get()->hasTeamChoosing() && if (NetworkConfig::get()->hasTeamChoosing() &&
race_manager->teamEnabled()) race_manager->teamEnabled())
player->setTeam((KartTeam)(peer->getHostId() % 2)); {
KartTeam cur_team = KART_TEAM_NONE;
if (red_blue.first > red_blue.second)
{
cur_team = KART_TEAM_BLUE;
red_blue.second++;
}
else
{
cur_team = KART_TEAM_RED;
red_blue.first++;
}
player->setTeam(cur_team);
}
peer->addPlayer(player); peer->addPlayer(player);
} }