Add /mute, /unmute and /listmute to mute certain player chat message

This commit is contained in:
Benau 2021-02-07 01:33:37 +08:00
parent 03a684e62e
commit 6e3744157d
2 changed files with 128 additions and 1 deletions

View File

@ -798,8 +798,11 @@ void ServerLobby::handleChat(Event* event)
chat->setSynchronous(true);
chat->addUInt8(LE_CHAT).encodeString16(message);
const bool game_started = m_state.load() != WAITING_FOR_START_GAME;
core::stringw sender_name =
event->getPeer()->getPlayerProfiles()[0]->getName();
STKHost::get()->sendPacketToAllPeersWith(
[game_started, sender_in_game, target_team](STKPeer* p)
[game_started, sender_in_game, target_team, sender_name, this]
(STKPeer* p)
{
if (game_started)
{
@ -819,6 +822,15 @@ void ServerLobby::handleChat(Event* event)
return false;
}
}
for (auto& peer : m_peers_muted_players)
{
if (auto peer_sp = peer.first.lock())
{
if (peer_sp.get() == p &&
peer.second.find(sender_name) != peer.second.end())
return false;
}
}
return true;
}, chat);
event->getPeer()->updateLastMessage();
@ -1356,6 +1368,15 @@ void ServerLobby::asynchronousUpdate()
m_rs_state.store(RS_NONE);
}
for (auto it = m_peers_muted_players.begin();
it != m_peers_muted_players.end();)
{
if (it->first.expired())
it = m_peers_muted_players.erase(it);
else
it++;
}
#ifdef ENABLE_SQLITE3
pollDatabase();
#endif
@ -5808,6 +5829,109 @@ void ServerLobby::handleServerCommand(Event* event,
peer->sendPacket(chat, true/*reliable*/);
delete chat;
}
else if (argv[0] == "mute")
{
std::shared_ptr<STKPeer> player_peer;
std::string result_msg;
core::stringw player_name;
NetworkString* result = NULL;
if (argv.size() != 2 || argv[1].empty())
goto mute_error;
player_name = StringUtils::utf8ToWide(argv[1]);
player_peer = STKHost::get()->findPeerByName(player_name);
if (!player_peer || player_peer == peer)
goto mute_error;
m_peers_muted_players[peer].insert(player_name);
result = getNetworkString();
result->addUInt8(LE_CHAT);
result->setSynchronous(true);
result_msg = "Muted player ";
result_msg += argv[1];
result->encodeString16(StringUtils::utf8ToWide(result_msg));
peer->sendPacket(result, true/*reliable*/);
delete result;
return;
mute_error:
NetworkString* error = getNetworkString();
error->addUInt8(LE_CHAT);
error->setSynchronous(true);
std::string msg = "Usage: /mute player_name (not including yourself)";
error->encodeString16(StringUtils::utf8ToWide(msg));
peer->sendPacket(error, true/*reliable*/);
delete error;
}
else if (argv[0] == "unmute")
{
std::shared_ptr<STKPeer> player_peer;
std::string result_msg;
core::stringw player_name;
NetworkString* result = NULL;
if (argv.size() != 2 || argv[1].empty())
goto unmute_error;
player_name = StringUtils::utf8ToWide(argv[1]);
for (auto it = m_peers_muted_players[peer].begin();
it != m_peers_muted_players[peer].end();)
{
if (*it == player_name)
{
it = m_peers_muted_players[peer].erase(it);
goto unmute_found;
}
else
{
it++;
}
}
goto unmute_error;
unmute_found:
result = getNetworkString();
result->addUInt8(LE_CHAT);
result->setSynchronous(true);
result_msg = "Unmuted player ";
result_msg += argv[1];
result->encodeString16(StringUtils::utf8ToWide(result_msg));
peer->sendPacket(result, true/*reliable*/);
delete result;
return;
unmute_error:
NetworkString* error = getNetworkString();
error->addUInt8(LE_CHAT);
error->setSynchronous(true);
std::string msg = "Usage: /unmute player_name";
error->encodeString16(StringUtils::utf8ToWide(msg));
peer->sendPacket(error, true/*reliable*/);
delete error;
}
else if (argv[0] == "listmute")
{
NetworkString* chat = getNetworkString();
chat->addUInt8(LE_CHAT);
chat->setSynchronous(true);
core::stringw total;
for (auto& name : m_peers_muted_players[peer])
{
total += name;
total += " ";
}
if (total.empty())
chat->encodeString16("No player has been muted by you");
else
{
total += "muted";
chat->encodeString16(total);
}
peer->sendPacket(chat, true/*reliable*/);
delete chat;
}
else
{
NetworkString* chat = getNetworkString();

View File

@ -166,6 +166,9 @@ private:
std::map<std::weak_ptr<STKPeer>, bool,
std::owner_less<std::weak_ptr<STKPeer> > > m_peers_ready;
std::map<std::weak_ptr<STKPeer>, std::set<irr::core::stringw>,
std::owner_less<std::weak_ptr<STKPeer> > > m_peers_muted_players;
std::weak_ptr<Online::Request> m_server_registering;
/** Timeout counter for various state. */