CommandManager.cpp 997 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <iostream>
  2. #include "server/commands/CommandManager.h"
  3. #include "server/commands/CommandUtils.h"
  4. #include "server/commands/GeneralCommands.h"
  5. CommandManager::CommandManager()
  6. {
  7. registerCommand("test", GeneralCommands::test);
  8. registerCommand("stop", GeneralCommands::stop);
  9. }
  10. void CommandManager::registerCommand(const std::string& name, Command command)
  11. {
  12. commands[name] = command;
  13. }
  14. void CommandManager::execute(ServerCommands& sc, const std::string& rawCommand) const
  15. {
  16. std::vector<std::string> args;
  17. std::string command;
  18. if(CommandUtils::splitString(rawCommand, command, args))
  19. {
  20. std::cout << "Invalid command syntax: '" << rawCommand << "'" << std::endl;
  21. return;
  22. }
  23. const std::unordered_map<std::string, Command>::const_iterator& iter = commands.find(command);
  24. if(iter == commands.end())
  25. {
  26. std::cout << "Unknown command: '" << command << "'" << std::endl;
  27. return;
  28. }
  29. iter->second(sc, args);
  30. }