working chat
This commit is contained in:
75
src/Client.cpp
Normal file
75
src/Client.cpp
Normal file
@@ -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<int>(part.color.r * 255), static_cast<int>(part.color.g * 255),
|
||||||
|
static_cast<int>(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);
|
||||||
|
}
|
||||||
31
src/Client.h
Normal file
31
src/Client.h
Normal file
@@ -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();
|
||||||
|
};
|
||||||
|
|
||||||
155
src/main.cpp
155
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 <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
namespace bu = blitz::utils;
|
||||||
std::cout << "hello world!" << std::endl;
|
|
||||||
return 0;
|
struct Argument {
|
||||||
|
std::string Name;
|
||||||
|
std::string Descritpion;
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<Argument> 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<blitz::server::Server> internalServer;
|
||||||
|
|
||||||
|
if (host.empty()) {
|
||||||
|
internalServer = std::make_unique<blitz::server::Server>();
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user