R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
CommandHandler.cpp
Go to the documentation of this file.
1
/*
2
** EPITECH PROJECT, 2025
3
** Created on 14/01/2026.
4
** File description:
5
** CommandHandler.cpp
6
*/
7
8
#include "
CommandHandler.hpp
"
9
#include <sstream>
10
#include "
CommandContext.hpp
"
11
12
#include <memory>
13
#include "../../common/Logger/Logger.hpp"
14
#include "
HelpCommand.hpp
"
15
#include "
ICommand.hpp
"
16
#include "
KickCommand.hpp
"
17
#include "
ListCommand.hpp
"
18
19
namespace
server
{
20
21
CommandHandler::CommandHandler
() {
22
// Register built-in commands
23
registerCommand
(std::make_shared<KickCommand>());
24
registerCommand
(std::make_shared<ListCommand>());
25
26
// HelpCommand needs access to all commands, so we register it after
27
registerCommand
(std::make_shared<HelpCommand>(
_commandList
));
28
}
29
30
void
CommandHandler::registerCommand
(std::shared_ptr<ICommand> command) {
31
std::string name = command->getName();
32
_commands
[name] = command;
33
_commandList
.push_back(command);
34
LOG_DEBUG
(
"Registered command: /"
, name);
35
}
36
37
bool
CommandHandler::isCommand
(
const
std::string &message) {
38
return
!message.empty() && message[0] ==
'/'
;
39
}
40
41
std::pair<std::string, std::vector<std::string>>
CommandHandler::parseCommand
(
42
const
std::string &message) {
43
std::istringstream iss(message.substr(1));
// Skip the '/'
44
std::string commandName;
45
std::vector<std::string> args;
46
47
iss >> commandName;
// First word is command name
48
49
// Rest are arguments
50
std::string arg;
51
while
(iss >> arg) {
52
args.push_back(arg);
53
}
54
55
return
{commandName, args};
56
}
57
58
std::string
CommandHandler::handleCommand
(
const
std::string &message,
const
CommandContext
&context) {
59
if
(!
isCommand
(message)) {
60
return
""
;
61
}
62
63
auto
[commandName, args] =
parseCommand
(message);
64
65
// Find command
66
auto
it =
_commands
.find(commandName);
67
if
(it ==
_commands
.end()) {
68
return
"Unknown command: /"
+ commandName +
". Type /help for available commands."
;
69
}
70
71
// Execute command
72
try
{
73
std::string result = it->second->execute(args, context);
74
LOG_DEBUG
(
"Command /"
, commandName,
" executed by player "
, context.
playerId
);
75
return
result;
76
}
catch
(
const
std::exception &e) {
77
LOG_ERROR
(
"Command execution failed: "
, e.what());
78
return
"Command execution failed: "
+ std::string(e.what());
79
}
80
}
81
82
}
// namespace server
CommandContext.hpp
CommandHandler.hpp
HelpCommand.hpp
ICommand.hpp
KickCommand.hpp
ListCommand.hpp
LOG_DEBUG
#define LOG_DEBUG(...)
Definition
Logger.hpp:180
LOG_ERROR
#define LOG_ERROR(...)
Definition
Logger.hpp:183
server::CommandHandler::CommandHandler
CommandHandler()
Definition
CommandHandler.cpp:21
server::CommandHandler::registerCommand
void registerCommand(std::shared_ptr< ICommand > command)
Register a command.
Definition
CommandHandler.cpp:30
server::CommandHandler::handleCommand
std::string handleCommand(const std::string &message, const CommandContext &context)
Handle a command message.
Definition
CommandHandler.cpp:58
server::CommandHandler::parseCommand
std::pair< std::string, std::vector< std::string > > parseCommand(const std::string &message)
Parse command message into name and arguments.
Definition
CommandHandler.cpp:41
server::CommandHandler::_commandList
std::vector< std::shared_ptr< ICommand > > _commandList
Definition
CommandHandler.hpp:74
server::CommandHandler::isCommand
static bool isCommand(const std::string &message)
Check if a message is a command.
Definition
CommandHandler.cpp:37
server::CommandHandler::_commands
std::unordered_map< std::string, std::shared_ptr< ICommand > > _commands
Definition
CommandHandler.hpp:73
server
Definition
CommandContext.hpp:16
server::CommandContext
Context information for command execution.
Definition
CommandContext.hpp:28
server::CommandContext::playerId
uint32_t playerId
ID of the player executing the command.
Definition
CommandContext.hpp:29
server
Commands
CommandHandler.cpp
Generated by
1.9.8