R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::CommandHandler Class Reference

Central command dispatcher. More...

#include <CommandHandler.hpp>

Collaboration diagram for server::CommandHandler:
Collaboration graph

Public Member Functions

 CommandHandler ()
 
 ~CommandHandler ()=default
 
void registerCommand (std::shared_ptr< ICommand > command)
 Register a command.
 
std::string handleCommand (const std::string &message, const CommandContext &context)
 Handle a command message.
 
const std::vector< std::shared_ptr< ICommand > > & getCommands () const
 Get all registered commands.
 

Static Public Member Functions

static bool isCommand (const std::string &message)
 Check if a message is a command.
 

Private Member Functions

std::pair< std::string, std::vector< std::string > > parseCommand (const std::string &message)
 Parse command message into name and arguments.
 

Private Attributes

std::unordered_map< std::string, std::shared_ptr< ICommand > > _commands
 
std::vector< std::shared_ptr< ICommand > > _commandList
 

Detailed Description

Central command dispatcher.

Manages command registration, parsing and execution. Follows the Command pattern with dependency injection.

Usage: CommandHandler handler; handler.registerCommand(std::make_shared<HelpCommand>()); handler.registerCommand(std::make_shared<KickCommand>());

std::string result = handler.handleCommand("/kick player1", context);

Definition at line 33 of file CommandHandler.hpp.

Constructor & Destructor Documentation

◆ CommandHandler()

server::CommandHandler::CommandHandler ( )

Definition at line 21 of file CommandHandler.cpp.

References _commandList, and registerCommand().

Here is the call graph for this function:

◆ ~CommandHandler()

server::CommandHandler::~CommandHandler ( )
default

Member Function Documentation

◆ getCommands()

const std::vector< std::shared_ptr< ICommand > > & server::CommandHandler::getCommands ( ) const
inline

Get all registered commands.

Returns
Vector of all commands

Definition at line 63 of file CommandHandler.hpp.

References _commandList.

◆ handleCommand()

std::string server::CommandHandler::handleCommand ( const std::string &  message,
const CommandContext context 
)

Handle a command message.

Parameters
messageFull message (including /)
contextExecution context
Returns
Response message (empty if command not found)

Definition at line 58 of file CommandHandler.cpp.

References _commands, isCommand(), LOG_DEBUG, LOG_ERROR, parseCommand(), and server::CommandContext::playerId.

Here is the call graph for this function:

◆ isCommand()

bool server::CommandHandler::isCommand ( const std::string &  message)
static

Check if a message is a command.

Parameters
messageMessage to check
Returns
true if message starts with /

Definition at line 37 of file CommandHandler.cpp.

Referenced by handleCommand().

◆ parseCommand()

std::pair< std::string, std::vector< std::string > > server::CommandHandler::parseCommand ( const std::string &  message)
private

Parse command message into name and arguments.

Parameters
messageFull command message
Returns
Pair of (command name, arguments)

Definition at line 41 of file CommandHandler.cpp.

Referenced by handleCommand().

◆ registerCommand()

void server::CommandHandler::registerCommand ( std::shared_ptr< ICommand command)

Register a command.

Parameters
commandCommand instance to register

Definition at line 30 of file CommandHandler.cpp.

References _commandList, _commands, and LOG_DEBUG.

Referenced by CommandHandler().

Member Data Documentation

◆ _commandList

std::vector<std::shared_ptr<ICommand> > server::CommandHandler::_commandList
private

Definition at line 74 of file CommandHandler.hpp.

Referenced by CommandHandler(), getCommands(), and registerCommand().

◆ _commands

std::unordered_map<std::string, std::shared_ptr<ICommand> > server::CommandHandler::_commands
private

Definition at line 73 of file CommandHandler.hpp.

Referenced by handleCommand(), and registerCommand().


The documentation for this class was generated from the following files: