|
| 1 | + |
| 2 | +#include "ChatManager.hpp" |
| 3 | + |
| 4 | +#include <chrono> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +#include "openvic-simulation/GameManager.hpp" |
| 8 | +#include "openvic-simulation/multiplayer/ClientManager.hpp" |
| 9 | +#include "openvic-simulation/multiplayer/PacketType.hpp" |
| 10 | +#include "openvic-simulation/utility/ErrorMacros.hpp" |
| 11 | + |
| 12 | +using namespace OpenVic; |
| 13 | + |
| 14 | +ChatGroup::ChatGroup(index_t index, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) |
| 15 | + : index { index }, clients { std::move(clients) } {} |
| 16 | + |
| 17 | +ChatManager::ChatManager(ClientManager* client_manager) : client_manager { client_manager } {} |
| 18 | + |
| 19 | +bool ChatManager::send_private_message(BaseMultiplayerManager::client_id_type to, memory::string&& message) { |
| 20 | + MessageData data { MessageType::PRIVATE, std::move(message), to }; |
| 21 | + |
| 22 | + ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data)); |
| 23 | + bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log); |
| 24 | + return was_sent; |
| 25 | +} |
| 26 | + |
| 27 | +bool ChatManager::send_private_message(BaseMultiplayerManager::client_id_type to, std::string_view message) { |
| 28 | + return send_private_message(to, memory::string { message }); |
| 29 | +} |
| 30 | + |
| 31 | +bool ChatManager::send_public_message(memory::string&& message) { |
| 32 | + MessageData data { MessageType::PUBLIC, std::move(message) }; |
| 33 | + |
| 34 | + ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data)); |
| 35 | + bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log); |
| 36 | + return was_sent; |
| 37 | +} |
| 38 | + |
| 39 | +bool ChatManager::send_public_message(std::string_view message) { |
| 40 | + return send_public_message(memory::string { message }); |
| 41 | +} |
| 42 | + |
| 43 | +bool ChatManager::send_group_message(ChatGroup const& group, memory::string&& message) { |
| 44 | + MessageData data { MessageType::GROUP, std::move(message), group.get_index() }; |
| 45 | + |
| 46 | + ChatMessageLog const& log = log_message(client_manager->get_client_id(), std::move(data)); |
| 47 | + bool was_sent = client_manager->broadcast_packet(PacketTypes::send_chat_message, &log); |
| 48 | + return was_sent; |
| 49 | +} |
| 50 | + |
| 51 | +bool ChatManager::send_group_message(ChatGroup::index_t group_id, memory::string&& message) { |
| 52 | + OV_ERR_FAIL_INDEX_V(group_id, groups.size(), false); |
| 53 | + return send_group_message(groups[group_id], std::move(message)); |
| 54 | +} |
| 55 | + |
| 56 | +bool ChatManager::send_group_message(ChatGroup const& group, std::string_view message) { |
| 57 | + return send_group_message(group, memory::string { message }); |
| 58 | +} |
| 59 | + |
| 60 | +bool ChatManager::send_group_message(ChatGroup::index_t group_id, std::string_view message) { |
| 61 | + return send_group_message(group_id, memory::string { message }); |
| 62 | +} |
| 63 | + |
| 64 | +ChatMessageLog const& ChatManager::log_message( // |
| 65 | + BaseMultiplayerManager::client_id_type from, MessageData&& message, |
| 66 | + std::chrono::time_point<std::chrono::system_clock> timestamp |
| 67 | +) { |
| 68 | + ChatMessageLog const& log = messages.emplace_back( |
| 69 | + from, std::move(message), std::chrono::time_point_cast<std::chrono::milliseconds>(timestamp).time_since_epoch().count() |
| 70 | + ); |
| 71 | + message_logged(log); |
| 72 | + return log; |
| 73 | +} |
| 74 | + |
| 75 | +ChatMessageLog const* ChatManager::_log_message(ChatMessageLog&& log) { |
| 76 | + std::chrono::time_point cur = std::chrono::system_clock::now(); |
| 77 | + int64_t current_epoch = std::chrono::time_point_cast<std::chrono::milliseconds>(cur).time_since_epoch().count(); |
| 78 | + OV_ERR_FAIL_COND_V_MSG(log.timestamp > current_epoch, nullptr, "Invalid chat message log, cannot log future timestamps."); |
| 79 | + |
| 80 | + ChatMessageLog const& moved_log = messages.emplace_back(std::move(log)); |
| 81 | + message_logged(moved_log); |
| 82 | + return &moved_log; |
| 83 | +} |
| 84 | + |
| 85 | +memory::vector<ChatMessageLog> const& ChatManager::get_message_logs() const { |
| 86 | + return messages; |
| 87 | +} |
| 88 | + |
| 89 | +void ChatManager::create_group(memory::vector<BaseMultiplayerManager::client_id_type>&& clients) { |
| 90 | + client_manager->broadcast_packet(PacketTypes::add_chat_group, clients); |
| 91 | +} |
| 92 | + |
| 93 | +void ChatManager::_create_group(memory::vector<BaseMultiplayerManager::client_id_type>&& clients) { |
| 94 | + groups.push_back({ groups.size(), std::move(clients) }); |
| 95 | + ChatGroup const& last = groups.back(); |
| 96 | + group_created(last); |
| 97 | +} |
| 98 | + |
| 99 | +void ChatManager::set_group(ChatGroup::index_t group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) { |
| 100 | + OV_ERR_FAIL_INDEX(group_id, groups.size()); |
| 101 | + client_manager->broadcast_packet(PacketTypes::modify_chat_group, PacketChatGroupModifyData { group_id, clients }); |
| 102 | +} |
| 103 | + |
| 104 | +void ChatManager::_set_group(ChatGroup::index_t group_id, memory::vector<BaseMultiplayerManager::client_id_type>&& clients) { |
| 105 | + ChatGroup& group = groups[group_id]; |
| 106 | + std::swap(group.clients, clients); |
| 107 | + group_modified(group, clients); |
| 108 | +} |
| 109 | + |
| 110 | +ChatGroup const& ChatManager::get_group(ChatGroup::index_t group_index) const { |
| 111 | + return groups.at(group_index); |
| 112 | +} |
| 113 | + |
| 114 | +void ChatManager::delete_group(ChatGroup::index_t group_id) { |
| 115 | + OV_ERR_FAIL_INDEX(group_id, groups.size()); |
| 116 | + client_manager->broadcast_packet( |
| 117 | + PacketTypes::delete_chat_group, |
| 118 | + PacketType::argument_type { std::in_place_index<PacketTypes::delete_chat_group.packet_id>, group_id } |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +void ChatManager::_delete_group(ChatGroup::index_t group_id) { |
| 123 | + groups.erase(groups.begin() + group_id); |
| 124 | + group_deleted(group_id); |
| 125 | +} |
0 commit comments