From 93a8e1acfb225b75788e7caa9f975dcb9fd5fa4c Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 7 Mar 2024 23:10:01 +0100 Subject: [PATCH] working chat --- src/Client.cpp | 75 ++++++++++++++++++++++++ src/Client.h | 31 ++++++++++ src/main.cpp | 155 ++++++++++++++++++++++++++++++++++++++++++++++++- xmake.lua | 1 + 4 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 src/Client.cpp create mode 100644 src/Client.h diff --git a/src/Client.cpp b/src/Client.cpp new file mode 100644 index 0000000..d12ed51 --- /dev/null +++ b/src/Client.cpp @@ -0,0 +1,75 @@ +#include "Client.h" + +#include "blitz/misc/Format.h" +#include "blitz/misc/Log.h" +#include "blitz/protocol/packets/ChatPacket.h" +#include "blitz/protocol/packets/ConnexionInfoPacket.h" +#include "blitz/protocol/packets/KeepAlivePacket.h" +#include "blitz/protocol/packets/PlayerLoginPacket.h" +#include "blitz/protocol/packets/PlayerShootPacket.h" + +static void PrintColoredText(const protocol::ColoredText& text) { + std::string msg; + for (auto& part : text) { + msg += utils::Format("\033[38;2;%i;%i;%im%s", static_cast(part.color.r * 255), static_cast(part.color.g * 255), + static_cast(part.color.b * 255), part.text.c_str()); + } + msg += "\033[0m"; + utils::LOG(msg); +} + +Client::Client() : network::Connexion(&m_Dispatcher) { + RegisterHandlers(); +} + +Client::~Client() { + GetDispatcher()->UnregisterHandler(this); +} + +void Client::RegisterHandlers() { + GetDispatcher()->RegisterHandler(protocol::PacketType::KeepAlive, this); + GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this); + GetDispatcher()->RegisterHandler(protocol::PacketType::Chat, this); + GetDispatcher()->RegisterHandler(protocol::PacketType::ConnexionInfo, this); +} + +bool Client::Connect(const std::string& pseudo, const std::string& address, std::uint16_t port) { + if (!Connexion::Connect(address, port)) + return false; + + m_PlayerName = pseudo; + return true; +} + +void Client::HandlePacket(const protocol::ChatPacket* packet) { + PrintColoredText(packet->GetMessage()); +} + +void Client::HandlePacket(const protocol::DisconnectPacket* packet) { + utils::LOG("Disconnected !"); +} + +void Client::HandlePacket(const protocol::KeepAlivePacket* packet) { + protocol::KeepAlivePacket response(packet->GetAliveID()); + SendPacket(&response); +} + +void Client::HandlePacket(const protocol::ConnexionInfoPacket* packet) { + m_PlayerID = packet->GetConnectionID(); + Login(m_PlayerName); +} + +void Client::Login(const std::string& pseudo) { + protocol::PlayerLoginPacket packet(pseudo); + SendPacket(&packet); +} + +void Client::SendTextChat(const std::string& msg) { + protocol::ChatPacket packet(protocol::ChatPacket::ColorizeText(msg)); + SendPacket(&packet); +} + +void Client::Shoot() { + protocol::PlayerShootPacket packet; + SendPacket(&packet); +} \ No newline at end of file diff --git a/src/Client.h b/src/Client.h new file mode 100644 index 0000000..eff12a1 --- /dev/null +++ b/src/Client.h @@ -0,0 +1,31 @@ +#pragma once + +#include "blitz/common/Defines.h" +#include "blitz/network/Connexion.h" + +using namespace blitz; + +class Client : public network::Connexion { + private: + std::string m_PlayerName; + game::PlayerID m_PlayerID; + + public: + Client(); + virtual ~Client(); + + virtual void HandlePacket(const protocol::DisconnectPacket* packet) override; + virtual void HandlePacket(const protocol::KeepAlivePacket* packet) override; + virtual void HandlePacket(const protocol::ChatPacket* packet) override; + virtual void HandlePacket(const protocol::ConnexionInfoPacket* packet) override; + + virtual bool Connect(const std::string& pseudo, const std::string& address, std::uint16_t port); + + void SendTextChat(const std::string& msg); + void Shoot(); + + private: + void Login(const std::string& pseudo); + void RegisterHandlers(); +}; + diff --git a/src/main.cpp b/src/main.cpp index 125454a..2a8d249 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,155 @@ +#include "Client.h" +#include "blitz/misc/Format.h" +#include "blitz/misc/Log.h" +#include "blitz/network/Network.h" +#include "server/Server.h" #include +#include -int main(int argc, char** argv){ - std::cout << "hello world!" << std::endl; - return 0; +namespace bu = blitz::utils; + +struct Argument { + std::string Name; + std::string Descritpion; +}; + +static std::vector CMD_ARGUMENTS = { + {"-P\t", "The port to listen/connect to. Default : 25565"}, + {"--help\t", "Displays help"}, + {"-C\t", "Instead of creating an internal server, connects to the one provided"}, + {"-N\t", "Set player name"}, +}; + +static void DisplayHelp() { + bu::LOG("Usage :"); + for (std::size_t i = 0; i < CMD_ARGUMENTS.size(); i++) { + Argument& arg = CMD_ARGUMENTS[i]; + bu::LOG(bu::Format("\t%s\t%s", arg.Name.c_str(), arg.Descritpion.c_str())); + } +} + +class ConsoleThread { + private: + std::thread m_Thread; + bool m_Running = true; + Client& m_Client; + + public: + ConsoleThread(Client& client) : m_Client(client) {} + + void Start() { + m_Thread = std::thread([this]() { + std::string line; + while (m_Running) { + getline(std::cin, line); + + if (line == "/stop") { + bu::LOG("Disconnecting ..."); + m_Running = false; + } else if (line == "/shoot") { + bu::LOG("Shooting ..."); + m_Client.Shoot(); + } else if (line == "/help") { + bu::LOG("use \"/stop\" to disconnect"); + bu::LOG("use \"/shoot\" to shoot"); + } else { + m_Client.SendTextChat(line); + } + } + }); + } + + bool IsRunning() const { + return m_Running; + } + + void Stop() { + m_Thread.join(); + } + + ~ConsoleThread() {} +}; + +static void ProcessArgs(std::string& host, std::uint16_t port, const std::string& pseudo) { + std::unique_ptr internalServer; + + if (host.empty()) { + internalServer = std::make_unique(); + internalServer->Start(port, false); + host = "localhost"; + } + + Client client; + client.Connect(pseudo, host, port); + + ConsoleThread consoleThread{client}; + consoleThread.Start(); + + while (consoleThread.IsRunning()) { + client.UpdateSocket(); + } + + client.CloseConnection(); + consoleThread.Stop(); +} + +int main(int argc, char** args) { + blitz::network::NetworkInitializer network; + + std::uint16_t port = 25565; + std::string host; + std::string pseudo = "pseudo"; + + for (int i = 1; i < argc; i++) { + std::string arg = args[i]; + + if (arg == "--help") { + DisplayHelp(); + return EXIT_SUCCESS; + } + + if (arg == "-P") { + i++; + + if (i >= argc) { + bu::LOG("You must specify a valid port !"); + return EXIT_FAILURE; + } + + try { + port = std::stoi(args[i]); + } catch (std::exception& e) { + bu::LOG("You must specify a valid port !"); + return EXIT_FAILURE; + } + } + + if (arg == "-C") { + i++; + + if (i >= argc) { + bu::LOG("You must specify a valid host !"); + return EXIT_FAILURE; + } + + host = args[i]; + } + + if (arg == "-N") { + i++; + + if (i >= argc) { + bu::LOG("You must specify a valid name !"); + return EXIT_FAILURE; + } + + pseudo = args[i]; + } + } + + ProcessArgs(host, port, pseudo); + + bu::LOG("Goodbye !"); + + return 0; } diff --git a/xmake.lua b/xmake.lua index 1cd7115..5f6495c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,5 +1,6 @@ add_rules("mode.debug", "mode.release") +add_includedirs("Blitz/include") set_languages("c++17") includes("Blitz/xmake/Blitz.lua")