CommandManager.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <iostream>
  2. #include "common/network/Packets.h"
  3. #include "server/commands/CommandManager.h"
  4. static void commandTest(ServerState&, const CommandArguments& args) {
  5. std::cout << "test command" << std::endl;
  6. for(int i = 0; i < args.getLength(); i++) {
  7. std::cout << " - " << args[i] << std::endl;
  8. }
  9. }
  10. static void commandStop(ServerState& sc, const CommandArguments&) {
  11. sc.running = false;
  12. }
  13. static void commandSay(ServerState& sc, const CommandArguments& args) {
  14. StringBuffer<256> s;
  15. for(int i = 1; i < args.getLength(); i++) {
  16. s.append(args[i]);
  17. s.append(' ');
  18. }
  19. OutPacket out = OutPacket::reliable(260);
  20. out.writeU16(ServerPacket::CHAT);
  21. out.writeString(s);
  22. sc.server.send(out);
  23. std::cout << s << '\n';
  24. }
  25. CommandManager::CommandManager() {
  26. commands.add("test", commandTest);
  27. commands.add("stop", commandStop);
  28. commands.add("say", commandSay);
  29. }
  30. void CommandManager::execute(ServerState& sc, const RawCommand& rawCommand) {
  31. CommandArguments args(rawCommand);
  32. if(args.getLength() == 0) {
  33. std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
  34. return;
  35. }
  36. CommandName command(args[0]);
  37. Command* c = commands.search(command);
  38. if(c == nullptr) {
  39. std::cout << "Unknown command: '" << command << "'" << std::endl;
  40. return;
  41. }
  42. (*c)(sc, args);
  43. }