CommandManager.cpp 964 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <iostream>
  2. #include "server/commands/CommandManager.h"
  3. static void commandTest(ServerState&, const CommandArguments& args) {
  4. std::cout << "test command" << std::endl;
  5. for(int i = 0; i < args.getLength(); i++) {
  6. std::cout << " - " << args[i] << std::endl;
  7. }
  8. }
  9. static void commandStop(ServerState& sc, const CommandArguments&) {
  10. sc.running = false;
  11. }
  12. CommandManager::CommandManager() {
  13. commands.add("test", commandTest);
  14. commands.add("stop", commandStop);
  15. }
  16. void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) {
  17. CommandArguments args(rawCommand);
  18. if(args.getLength() == 0) {
  19. std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
  20. return;
  21. }
  22. CommandName command(args[0]);
  23. Command* c = commands.search(command);
  24. if(c == nullptr) {
  25. std::cout << "Unknown command: '" << command << "'" << std::endl;
  26. return;
  27. }
  28. (*c)(sc, args);
  29. }