CommandManager.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include "server/commands/CommandManager.h"
  4. #include "common/utils/HashedString.h"
  5. #include "common/utils/SplitString.h"
  6. static void commandTest(ServerCommands&, const SplitString& args) {
  7. std::cout << "test command" << std::endl;
  8. for(size_t i = 0; i < args.getLength(); i++) {
  9. std::cout << " - " << args[i] << std::endl;
  10. }
  11. }
  12. static void commandStop(ServerCommands& sc, const SplitString&) {
  13. sc.stop();
  14. }
  15. typedef void (*Command) (ServerCommands&, const SplitString&);
  16. static std::unordered_map<HashedString, Command, HashedString::Hasher> commands( {
  17. {"test", commandTest},
  18. {"stop", commandStop}
  19. });
  20. void CommandManager::execute(ServerCommands& sc, const String& rawCommand) {
  21. SplitString args(rawCommand);
  22. if(args.getLength() == 0) {
  23. std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
  24. return;
  25. }
  26. HashedString command(args[0]);
  27. auto 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. }