diff --git a/include/Cubed/gameplay/chat_message.hpp b/include/Cubed/gameplay/chat_message.hpp index 8725cd2..c711d2d 100644 --- a/include/Cubed/gameplay/chat_message.hpp +++ b/include/Cubed/gameplay/chat_message.hpp @@ -1,11 +1,15 @@ #pragma once +#include "Cubed/ui/color.hpp" + #include #include namespace Cubed { struct ChatMessage { std::string player; std::string text; + Color color; + bool system_msg = false; uint64_t time = 0; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/gameplay/server_world.hpp b/include/Cubed/gameplay/server_world.hpp index 971e42c..b3618fa 100644 --- a/include/Cubed/gameplay/server_world.hpp +++ b/include/Cubed/gameplay/server_world.hpp @@ -11,6 +11,7 @@ #include "Cubed/tools/priority_thread_pool.hpp" #include "Cubed/tools/recent_queue.hpp" #include "Cubed/tools/thread_pool.hpp" +#include "Cubed/ui/color.hpp" #include "world/block_change.pb.h" #include @@ -192,6 +193,7 @@ private: int threads); void send_server_stop(); - void boardcast_message(const std::string& name, const std::string& message); + void boardcast_message(const std::string& name, const std::string& message, + Color color = Color::WHITE, bool system_msg = false); }; } // namespace Cubed diff --git a/include/Cubed/ui/color.hpp b/include/Cubed/ui/color.hpp index 33a6cb4..f14ddcb 100644 --- a/include/Cubed/ui/color.hpp +++ b/include/Cubed/ui/color.hpp @@ -6,7 +6,7 @@ namespace Cubed { enum class Color { - BLACK, + BLACK = 0, WHITE, RED, GREEN, @@ -21,6 +21,39 @@ enum class Color { BROWN }; +inline constexpr Color color_from_int(int i) { + switch (i) { + case std::to_underlying(Color::BLACK): + return Color::BLACK; + case std::to_underlying(Color::WHITE): + return Color::WHITE; + case std::to_underlying(Color::RED): + return Color::RED; + case std::to_underlying(Color::GREEN): + return Color::GREEN; + case std::to_underlying(Color::BLUE): + return Color::BLUE; + case std::to_underlying(Color::YELLOW): + return Color::YELLOW; + case std::to_underlying(Color::CYAN): + return Color::CYAN; + case std::to_underlying(Color::MAGENTA): + return Color::MAGENTA; + case std::to_underlying(Color::GRAY): + return Color::GRAY; + case std::to_underlying(Color::ORANGE): + return Color::ORANGE; + case std::to_underlying(Color::PURPLE): + return Color::PURPLE; + case std::to_underlying(Color::PINK): + return Color::PINK; + case std::to_underlying(Color::BROWN): + return Color::BROWN; + default: + return Color::WHITE; + } +} + inline constexpr glm::vec4 color_value(Color color) { using glm::vec4; diff --git a/src/gameplay/client_world.cpp b/src/gameplay/client_world.cpp index 6bfc17c..4c1d460 100644 --- a/src/gameplay/client_world.cpp +++ b/src/gameplay/client_world.cpp @@ -752,7 +752,10 @@ void ClientWorld::request_exit() { } void ClientWorld::receive_chat_message(ChatMsg& msg) { - m_message_queue.emplace(msg.name(), msg.msg(), Tools::get_time_ticks()); + Color color = color_from_int(msg.color()); + + m_message_queue.emplace(msg.name(), msg.msg(), color, msg.system_msg(), + Tools::get_time_ticks()); } void ClientWorld::receive_voice_message(VoiceMsg& msg) { diff --git a/src/gameplay/server_world.cpp b/src/gameplay/server_world.cpp index 00f5ef3..9748bee 100644 --- a/src/gameplay/server_world.cpp +++ b/src/gameplay/server_world.cpp @@ -681,19 +681,25 @@ void ServerWorld::handle_player_login(const std::string& name, rsp->set_success(true); rsp->set_uuid(uuid); session->send(make_packet(*rsp), 0); + + boardcast_message("Server", std::format("Player {} Join Game", name), + Color::YELLOW, true); } void ServerWorld::handle_player_exit(const std::string& uuid) { std::shared_ptr exit_session; ChunkPosSet old_set; + std::string name; { std::lock_guard lock(m_player_mutex); auto it = m_players.find(uuid); if (it != m_players.end()) { - Logger::info("Player {} Exit the Server", it->second.get_name()); + name = it->second.get_name(); + Logger::info("Player {} Exit the Server", name); exit_session = it->second.get_session(); old_set = std::move(it->second.get_chunk_pos_set()); m_players.erase(it); + } else { Logger::error("Player {} isn't in Server", uuid); return; @@ -723,6 +729,9 @@ void ServerWorld::handle_player_exit(const std::string& uuid) { s->send(make_packet(*rsp), 0); } } + + boardcast_message("Server", std::format("Player {} Exit Game", name), + Color::YELLOW, true); } glm::vec3 ServerWorld::get_player_pos(const std::string& uuid) const { @@ -910,7 +919,8 @@ void ServerWorld::send_server_stop() { } void ServerWorld::boardcast_message(const std::string& name, - const std::string& message) { + const std::string& message, Color color, + bool system_msg) { std::vector> m_session; { @@ -925,6 +935,8 @@ void ServerWorld::boardcast_message(const std::string& name, msg->set_msg(message); msg->set_name(name); + msg->set_color(std::to_underlying(color)); + msg->set_system_msg(system_msg); for (auto& s : m_session) { s->send(make_packet(*msg)); diff --git a/src/proto/chat/chat.proto b/src/proto/chat/chat.proto index cfbb2c8..c7ba3d0 100644 --- a/src/proto/chat/chat.proto +++ b/src/proto/chat/chat.proto @@ -3,6 +3,8 @@ import "common/vector3.proto"; message ChatMsg { string name = 1; string msg = 2; + int32 color = 3; + bool system_msg = 4; } message VoiceMsg { diff --git a/src/ui/chat_box.cpp b/src/ui/chat_box.cpp index 5200c11..93abb1a 100644 --- a/src/ui/chat_box.cpp +++ b/src/ui/chat_box.cpp @@ -12,12 +12,14 @@ void ChatBox::add_message(ChatMessage& message) { m_messages.pop_front(); } - std::string str = std::format("<{}>{}", message.player, message.text); + std::string str = message.system_msg + ? std::format("{}", message.text) + : std::format("<{}>{}", message.player, message.text); auto split_str = wrap_message(str); for (auto it = split_str.begin(); it != split_str.end(); ++it) { auto lable = std::make_unique