This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/src/backend/reactions/reaction.cpp
Andreas Maerten cba19d0c33
refactor!: Rewrite of the old notification service (#2866)
The main goal was improving the readability of the original code however some ugliness remains.

- Swapped from pointer singleton to instance singleton
- Actually make use of the alpha logic that used to be present
- Added a counter to notifications to indicate if something is being spammed
- Notification timeouts reset if they're sent to the queue again
2024-03-23 00:04:49 +01:00

85 lines
2.3 KiB
C++

#include "reaction.hpp"
#include "backend/player_command.hpp"
#include "fiber_pool.hpp"
#include "hooking/hooking.hpp"
#include "pointers.hpp"
#include "script.hpp"
#include "services/player_database/player_database_service.hpp"
#include "util/notify.hpp"
namespace big
{
reaction::reaction(const char* event_name, const char* notify_message, const char* announce_message) :
m_event_name(event_name),
m_notify_message(notify_message),
m_announce_message(announce_message)
{
}
void reaction::process_common(player_ptr player)
{
if (add_to_player_db)
{
auto entry = g_player_database_service->get_or_create_player(player);
if (block_joins)
{
entry->block_join = true;
g_player_database_service->save();
}
}
if (kick)
{
g_fiber_pool->queue_job([player] {
dynamic_cast<player_command*>(command::get("multikick"_J))->call(player, {});
});
}
if (timeout)
{
player->block_net_events = true;
player->block_clone_sync = true;
player->block_clone_create = true;
LOGF(WARNING, "{} has been timed out", player->get_name());
}
}
void reaction::process(player_ptr player)
{
if (!player->is_valid())
return;
if ((player->is_friend() && g.session.trust_friends) || player->is_trusted || g.session.trust_session)
return;
if (log)
{
uint64_t rockstar_id = player->get_net_data() == nullptr ? 0 : player->get_net_data()->m_gamer_handle.m_rockstar_id;
LOGF(WARNING, "Received {} from {} ({})", m_event_name, player->get_name(), rockstar_id);
}
if (announce_in_chat)
{
g_fiber_pool->queue_job([player, this] {
auto chat = std::format("{} {}", g.session.chat_output_prefix, std::vformat(g_translation_service.get_translation(m_announce_message), std::make_format_args(player->get_name())));
if (g_hooking->get_original<hooks::send_chat_message>()(*g_pointers->m_gta.m_send_chat_ptr,
g_player_service->get_self()->get_net_data(),
chat.data(),
is_team_only))
notify::draw_chat(chat.c_str(), g_player_service->get_self()->get_name(), is_team_only);
});
}
if (notify)
{
g_notification_service.push_warning("PROTECTIONS"_T.data(),
std::vformat(g_translation_service.get_translation(m_notify_message), std::make_format_args(player->get_name())));
}
process_common(player);
}
}