Add function to find STKPeer by player name

This commit is contained in:
Benau 2019-12-03 14:10:38 +08:00
parent a3c6c63be8
commit ddbdf2d244
2 changed files with 24 additions and 0 deletions

View File

@ -1547,6 +1547,28 @@ std::shared_ptr<STKPeer> STKHost::findPeerByHostId(uint32_t id) const
return ret != m_peers.end() ? ret->second : nullptr;
} // findPeerByHostId
//-----------------------------------------------------------------------------
std::shared_ptr<STKPeer>
STKHost::findPeerByName(const core::stringw& name) const
{
std::lock_guard<std::mutex> lock(m_peers_mutex);
auto ret = std::find_if(m_peers.begin(), m_peers.end(),
[name](const std::pair<ENetPeer*, std::shared_ptr<STKPeer> >& p)
{
bool found = false;
for (auto& profile : p.second->getPlayerProfiles())
{
if (profile->getName() == name)
{
found = true;
break;
}
}
return found;
});
return ret != m_peers.end() ? ret->second : nullptr;
} // findPeerByName
//-----------------------------------------------------------------------------
void STKHost::initClientNetwork(ENetEvent& event, Network* new_network)
{

View File

@ -256,6 +256,8 @@ public:
// ------------------------------------------------------------------------
std::shared_ptr<STKPeer> findPeerByHostId(uint32_t id) const;
// ------------------------------------------------------------------------
std::shared_ptr<STKPeer> findPeerByName(const core::stringw& name) const;
// ------------------------------------------------------------------------
void sendPacketExcept(STKPeer* peer, NetworkString *data,
bool reliable = true);
// ------------------------------------------------------------------------