Fix anti party spam crash code

Increased code quality
Void messages until spam stops
This commit is contained in:
TotallyNotElite 2019-10-19 23:08:18 +02:00
parent 12b644ef66
commit 52c9c38223

View File

@ -21,42 +21,44 @@ static settings::Boolean enable_antispam{ "chat.party.anticrash.enabled", "false
// max messages in 5 seconds before being flagged // max messages in 5 seconds before being flagged
static settings::Int spam_rate{ "chat.party.anticrash.limit-rate", "12" }; static settings::Int spam_rate{ "chat.party.anticrash.limit-rate", "12" };
int messages_since_reset{ 0 }; static int spam_messages_since_reset{ 0 };
Timer spam_timer{}; static Timer spam_timer{};
static bool spam_blocked = true;
DEFINE_HOOKED_METHOD(FireEventClientSide, bool, IGameEventManager2 *this_, IGameEvent *event) DEFINE_HOOKED_METHOD(FireEventClientSide, bool, IGameEventManager2 *this_, IGameEvent *event)
{ {
if (enable_antispam) if (enable_antispam && strcmp(event->GetName(), "party_chat") == 0)
if (!strcmp(event->GetName(), "party_chat"))
{ {
// You have violated the law, please sit in timeout for your sins // Increase total count
if (messages_since_reset >= *spam_rate) spam_messages_since_reset++;
// Limit reached
if (spam_messages_since_reset > *spam_rate)
{ {
// Way to reuse same timer spam_blocked = true;
if (messages_since_reset == *spam_rate) spam_timer.update();
{ spam_messages_since_reset = 0;
spam_timer.test_and_set(0);
messages_since_reset++;
}
// You're free, now flee
if (spam_timer.check(15000))
messages_since_reset = 0;
// How about we don't send that anywhere m'lad
else
return true;
} }
// Not banned yet if (spam_blocked)
if (messages_since_reset < 12)
{ {
// Way to reset timer every 5 seconds // Check if we can lift the ban again
if (spam_timer.check(5000)) if (spam_timer.test_and_set(10000))
{ {
spam_timer.test_and_set(0); spam_messages_since_reset = 0;
messages_since_reset = 0; // Need to readd 1 to the timer since the current message counts towards the next "epoch"
spam_messages_since_reset++;
spam_blocked = false;
} }
// Increment spam counter else
messages_since_reset++; // Void messages to avoid crash
return true;
}
// We havent reached the limit yet. Reset the limit if last reset is 5 seconds in the past
else if (spam_timer.test_and_set(5000))
{
spam_messages_since_reset = 0;
// Need to readd 1 to the timer since the current message counts towards the next "epoch"
spam_messages_since_reset++;
} }
} }
hacks::tf2::killstreak::fire_event(event); hacks::tf2::killstreak::fire_event(event);