CommandManager.cpp 1.2 KB

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