Add configurable chat consecutive interval value

This commit is contained in:
Benau 2019-12-13 18:24:17 +08:00
parent 5d399f1203
commit 725d587566
4 changed files with 30 additions and 12 deletions

View File

@ -63,6 +63,9 @@ The current server configuration xml looks like this:
<!-- If this value is set to false, the server will ignore chat messages from all players. -->
<chat value="true" />
<!-- If client sends more than chat-consecutive-interval / 2 chats within this value (read in seconds), it will be ignore, negative value to disable. -->
<chat-consecutive-interval value="8" />
<!-- Allow players to vote for which track to play. If this value is set to false, the server will randomly pick the next track to play. -->
<track-voting value="true" />

View File

@ -699,19 +699,29 @@ void ServerLobby::handleChat(Event* event)
event->getPeer()->updateLastActivity();
const bool sender_in_game = event->getPeer()->isWaitingForGame();
int last_message = event->getPeer()->getLastMessage();
int elipsed_time = StkTime::getMonoTimeMs() - last_message;
int64_t last_message = event->getPeer()->getLastMessage();
int64_t elapsed_time = (int64_t)StkTime::getMonoTimeMs() - last_message;
// Increment consecutive_messages if last message is less than 5s ago
if (elipsed_time < 5000)
// Read ServerConfig for formula and details
if (ServerConfig::m_chat_consecutive_interval > 0 &&
elapsed_time < ServerConfig::m_chat_consecutive_interval * 1000)
event->getPeer()->updateConsecutiveMessages(true);
else
event->getPeer()->updateConsecutiveMessages(false);
// Ignore message if there is already 3 consecutive messages
if (event->getPeer()->getConsecutiveMessages() >= 3)
if (ServerConfig::m_chat_consecutive_interval > 0 &&
event->getPeer()->getConsecutiveMessages() >
ServerConfig::m_chat_consecutive_interval / 2)
{
NetworkString* chat = getNetworkString();
chat->setSynchronous(true);
core::stringw warn = "Spam detected";
chat->addUInt8(LE_CHAT).encodeString16(warn);
event->getPeer()->sendPacket(chat, true/*reliable*/);
delete chat;
return;
}
core::stringw message;
event->data().decodeString16(&message, 360/*max_len*/);
if (message.size() > 0)

View File

@ -149,6 +149,12 @@ namespace ServerConfig
"If this value is set to false, the server will ignore chat messages "
"from all players."));
SERVER_CFG_PREFIX IntServerConfigParam m_chat_consecutive_interval
SERVER_CFG_DEFAULT(IntServerConfigParam(8, "chat-consecutive-interval",
"If client sends more than chat-consecutive-interval / 2 chats within "
"this value (read in seconds), it will be ignore, negative value to "
"disable."));
SERVER_CFG_PREFIX BoolServerConfigParam m_track_voting
SERVER_CFG_DEFAULT(BoolServerConfigParam(true, "track-voting",
"Allow players to vote for which track to play. If this value is set "

View File

@ -281,19 +281,18 @@ public:
void updateLastMessage()
{ m_last_message.store((int64_t)StkTime::getMonoTimeMs()); }
// ------------------------------------------------------------------------
int getLastMessage()
int64_t getLastMessage() const
{ return m_last_message; }
// ------------------------------------------------------------------------
void updateConsecutiveMessages(bool tooFast)
void updateConsecutiveMessages(bool too_fast)
{
if (tooFast)
if (too_fast)
m_consecutive_messages++;
else
m_consecutive_messages = 0;
}
// ------------------------------------------------------------------------
int getConsecutiveMessages()
{ return m_consecutive_messages; }
int getConsecutiveMessages() const { return m_consecutive_messages; }
}; // STKPeer
#endif // STK_PEER_HPP