123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include "ChatCommandExecutor.h"
- #include "Game.h"
- #include "GrantCommand.h"
- #include "SaveCommand.h"
- ChatCommandExecutor::ChatCommandExecutor()
- : ReferenceCounter()
- {
- knownCommands.add(new SaveCommand());
- knownCommands.add(new CrantCommand());
- }
- bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor)
- {
- if (line.hatAt(0, "/"))
- {
- for (ChatCommand* command : knownCommands)
- {
- if (line.hatAt(0, Framework::Text("/") + command->getName()))
- {
- if (line.getLength() > command->getName().getLength() + 1
- && !line.hatAt(command->getName().getLength() + 1, " "))
- {
- continue;
- }
- Framework::RCArray<Framework::Text> params;
- int start = command->getName().getLength() + 2;
- bool escaped = 0;
- for (int i = command->getName().getLength() + 2;
- i < line.getLength();
- i++)
- {
- if (line.hatAt(i, " ") && !escaped)
- {
- if (start < i)
- {
- if (line.hatAt(start, "'")
- && line.hatAt(i - 1, "'"))
- {
- params.add(line.getTeilText(start + 1, i - 1));
- }
- else
- {
- params.add(line.getTeilText(start, i));
- }
- }
- start = i + 1;
- }
- else if (line.hatAt(i, "'"))
- {
- escaped = !escaped;
- }
- }
- if (start < line.getLength())
- {
- if (line.hatAt(start, "'")
- && line.hatAt(line.getLength() - 1, "'"))
- {
- params.add(
- line.getTeilText(start + 1, line.getLength() - 1));
- }
- else
- {
- params.add(line.getTeilText(start, line.getLength()));
- }
- }
- int index = 0;
- for (ChatCommandParameter* param : command->getParams())
- {
- if (params.getEintragAnzahl() > index
- && param->isLegalValue(*params.z(index)))
- {
- index++;
- }
- else
- {
- bool isError = 0;
- if (param->isOptional() && zActor)
- {
- Framework::Text defaultValue
- = param->getDefaultValue(zActor);
- if (!param->isLegalValue(defaultValue))
- {
- isError = 1;
- }
- else
- {
- params.add(
- new Framework::Text(defaultValue), index++);
- }
- }
- else
- {
- isError = 1;
- }
- if (isError)
- {
- Framework::Text error
- = "Illegal parameter at position ";
- error.append() << (index + 1) << ": ";
- if (params.getEintragAnzahl() > index)
- {
- error += *params.z(index);
- }
- else
- {
- error += "(no parameter was given)";
- }
- error.append() << "\n" << command->getHelp();
- Game::INSTANCE->zChat()->sendMessageTo(
- error, zActor, Chat::CHANNEL_ERROR);
- return true;
- }
- }
- }
- if (index != params.getEintragAnzahl())
- {
- Framework::Text error = "Illegal number of parameters. "
- "First unknown parameter: ";
- error.append() << *params.z(index) << "\n"
- << command->getHelp();
- Game::INSTANCE->zChat()->sendMessageTo(
- error, zActor, Chat::CHANNEL_ERROR);
- return true;
- }
- if (zActor
- && command->getSecurityLevel(params)
- > zActor->getChatSecurityLevel())
- {
- Framework::Text error
- = "This command requires a security level of at least ";
- error.append()
- << command->getSecurityLevel(params)
- << ". You have currently a security level of "
- << zActor->getChatSecurityLevel()
- << ". Ask someone with the required security lavel to "
- "grant you the same level.";
- Game::INSTANCE->zChat()->sendMessageTo(
- error, zActor, Chat::CHANNEL_ERROR);
- return true;
- }
- command->execute(params, zActor);
- return true;
- }
- }
- }
- return false;
- }
|