123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include "ChatCommand.h"
- #include <AsynchronCall.h>
- #include "Game.h"
- #include "Player.h"
- ChatCommand::ChatCommand(
- Framework::Text name, Framework::Text description, int securityLevel)
- : Framework::ConsoleCommand(name),
- description(description),
- securityLevel(securityLevel)
- {}
- void ChatCommand::addParam(ChatCommandParameter* param)
- {
- params.add(param);
- }
- bool ChatCommand::execute(Framework::RCArray<Framework::Text>& params) const
- {
- return execute(params, 0);
- }
- void ChatCommand::addAutocompletePossibilities(
- const Framework::RCArray<Framework::Text>& args,
- bool appendToLast,
- Framework::RCArray<Framework::Text>& possibilities) const
- {
- int index
- = appendToLast ? args.getEintragAnzahl() - 1 : args.getEintragAnzahl();
- if (index < 0 || index >= params.getEintragAnzahl()) return;
- for (Framework::Text* possibility : params.z(index)->getAutocompleteValues(
- appendToLast ? *args.z(args.getEintragAnzahl() - 1)
- : Framework::Text("")))
- {
- possibilities.add(
- dynamic_cast<Framework::Text*>(possibility->getThis()));
- }
- if (possibilities.getEintragAnzahl() == 0
- || (possibilities.getEintragAnzahl() == 1 && appendToLast
- && possibilities.z(0)->istGleich(
- args.get(args.getEintragAnzahl() - 1))))
- {
- Framework::Text help = getHelp();
- while (help.hat("\n"))
- {
- Framework::Text* line = help.getTeilText(0, help.positionVon("\n"));
- Framework::Logging::info() << line->getText();
- help.remove(0, line->getLength() + 1);
- line->release();
- }
- if (help.getLength() > 0)
- {
- Framework::Logging::info() << help;
- }
- }
- }
- const Framework::RCArray<ChatCommandParameter>& ChatCommand::getParams() const
- {
- return params;
- }
- Framework::Text ChatCommand::getHelp() const
- {
- Framework::Text result = "/";
- result += getName();
- for (ChatCommandParameter* param : params)
- {
- result += " ";
- if (param->isOptional()) result += "[";
- result += param->getName();
- if (param->isOptional()) result += "]";
- }
- if (description.getLength() > 0)
- {
- result.append() << "\n " << description;
- }
- for (ChatCommandParameter* param : params)
- {
- if (param->getDescription().getLength() > 0)
- {
- result += "\n ";
- if (param->isOptional()) result += "[";
- result += param->getName();
- if (param->isOptional()) result += "]";
- result.append() << " - " << param->getDescription();
- }
- }
- return result;
- }
- int ChatCommand::getSecurityLevel(
- Framework::RCArray<Framework::Text> params) const
- {
- return securityLevel;
- }
- ChatCommandParameter::ChatCommandParameter(
- Framework::Text name, Framework::Text description, bool optional)
- : ReferenceCounter(),
- name(name),
- description(description),
- optional(optional)
- {}
- bool ChatCommandParameter::isLegalValue(Framework::Text value) const
- {
- return 1;
- }
- Framework::Text ChatCommandParameter::getDefaultValue(Entity* zActor) const
- {
- return "";
- }
- Framework::Text ChatCommandParameter::getName() const
- {
- return name;
- }
- Framework::Text ChatCommandParameter::getDescription() const
- {
- return description;
- }
- bool ChatCommandParameter::isOptional() const
- {
- return optional;
- }
- Framework::RCArray<Framework::Text> ChatCommandParameter::getAutocompleteValues(
- const Framework::Text& current) const
- {
- return Framework::RCArray<Framework::Text>();
- }
- PlayerNameParameter::PlayerNameParameter()
- : ChatCommandParameter(
- "player", "The name of the player (has to be online)", 0)
- {}
- bool PlayerNameParameter::isLegalValue(Framework::Text value) const
- {
- return Game::INSTANCE->zPlayerByName(value) != 0;
- }
- Framework::Text PlayerNameParameter::getDefaultValue(Entity* zActor) const
- {
- Player* p = dynamic_cast<Player*>(zActor);
- if (p) return p->getName();
- return "";
- }
- Framework::RCArray<Framework::Text> PlayerNameParameter::getAutocompleteValues(
- const Framework::Text& current) const
- {
- Framework::RCArray<Framework::Text> result;
- Game::INSTANCE->listPlayerNames(result);
- for (auto iterator = result.begin(); iterator;)
- {
- if (iterator->hatAt(0, current))
- {
- iterator.remove();
- }
- else
- ++iterator;
- }
- return result;
- }
- IntegerParameter::IntegerParameter(Framework::Text name,
- Framework::Text description,
- bool optional,
- std::function<int(Entity* zEntity)> calculateDefault)
- : ChatCommandParameter(name, description, optional),
- calculateDefault(calculateDefault)
- {}
- bool IntegerParameter::isLegalValue(Framework::Text value) const
- {
- return Framework::Text((int)value).istGleich(value);
- }
- Framework::Text IntegerParameter::getDefaultValue(Entity* zActor) const
- {
- return Framework::Text(calculateDefault(zActor));
- }
|