12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include <iostream>
- #include <unordered_map>
- #include "server/commands/CommandManager.h"
- #include "common/utils/HashedString.h"
- #include "common/utils/SplitString.h"
- static void commandTest(ServerCommands&, const SplitString& args) {
- std::cout << "test command" << std::endl;
- for(size_t i = 0; i < args.getLength(); i++) {
- std::cout << " - " << args[i] << std::endl;
- }
- }
- static void commandStop(ServerCommands& sc, const SplitString&) {
- sc.stop();
- }
- typedef void (*Command) (ServerCommands&, const SplitString&);
- static std::unordered_map<HashedString, Command, HashedString::Hasher> commands( {
- {"test", commandTest},
- {"stop", commandStop}
- });
- void CommandManager::execute(ServerCommands& sc, const String& rawCommand) {
- SplitString args(rawCommand);
- if(args.getLength() == 0) {
- std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
- return;
- }
- HashedString command(args[0]);
- auto iter = commands.find(command);
- if(iter == commands.end()) {
- std::cout << "Unknown command: '" << command << "'" << std::endl;
- return;
- }
- iter->second(sc, args);
- }
|