CommandManager.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. {
  8. std::cout << "test command" << std::endl;
  9. for(size_t i = 0; i < args.size(); i++)
  10. {
  11. std::cout << " - " << args[i] << std::endl;
  12. }
  13. }
  14. static void commandStop(ServerCommands& sc, const std::vector<std::string>&)
  15. {
  16. sc.stop();
  17. }
  18. typedef void (*Command) (ServerCommands&, const std::vector<std::string>&);
  19. static std::unordered_map<std::string, Command> commands(
  20. {
  21. {"test", commandTest},
  22. {"stop", commandStop}
  23. });
  24. void CommandManager::execute(ServerCommands& sc, const std::string& rawCommand)
  25. {
  26. std::vector<std::string> args;
  27. std::string command;
  28. if(CommandUtils::splitString(rawCommand, command, args))
  29. {
  30. std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
  31. return;
  32. }
  33. const std::unordered_map<std::string, Command>::const_iterator& iter = commands.find(command);
  34. if(iter == commands.end())
  35. {
  36. std::cout << "Unknown command: '" << command << "'" << std::endl;
  37. return;
  38. }
  39. iter->second(sc, args);
  40. }