ChatCommand.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "ChatCommand.h"
  2. #include <AsynchronCall.h>
  3. #include "Game.h"
  4. #include "Player.h"
  5. ChatCommand::ChatCommand(
  6. Framework::Text name, Framework::Text description, int securityLevel)
  7. : Framework::ConsoleCommand(name),
  8. description(description),
  9. securityLevel(securityLevel)
  10. {}
  11. void ChatCommand::addParam(ChatCommandParameter* param)
  12. {
  13. params.add(param);
  14. }
  15. bool ChatCommand::execute(Framework::RCArray<Framework::Text>& params) const
  16. {
  17. return execute(params, 0);
  18. }
  19. void ChatCommand::addAutocompletePossibilities(
  20. const Framework::RCArray<Framework::Text>& args,
  21. bool appendToLast,
  22. Framework::RCArray<Framework::Text>& possibilities) const
  23. {
  24. int index
  25. = appendToLast ? args.getEintragAnzahl() - 1 : args.getEintragAnzahl();
  26. if (index < 0 || index >= params.getEintragAnzahl()) return;
  27. for (Framework::Text* possibility : params.z(index)->getAutocompleteValues(
  28. appendToLast ? *args.z(args.getEintragAnzahl() - 1)
  29. : Framework::Text("")))
  30. {
  31. possibilities.add(
  32. dynamic_cast<Framework::Text*>(possibility->getThis()));
  33. }
  34. if (possibilities.getEintragAnzahl() == 0
  35. || (possibilities.getEintragAnzahl() == 1 && appendToLast
  36. && possibilities.z(0)->istGleich(
  37. args.get(args.getEintragAnzahl() - 1))))
  38. {
  39. Framework::Text help = getHelp();
  40. while (help.hat("\n"))
  41. {
  42. Framework::Text* line = help.getTeilText(0, help.positionVon("\n"));
  43. Framework::Logging::info() << line->getText();
  44. help.remove(0, line->getLength() + 1);
  45. line->release();
  46. }
  47. if (help.getLength() > 0)
  48. {
  49. Framework::Logging::info() << help;
  50. }
  51. }
  52. }
  53. const Framework::RCArray<ChatCommandParameter>& ChatCommand::getParams() const
  54. {
  55. return params;
  56. }
  57. Framework::Text ChatCommand::getHelp() const
  58. {
  59. Framework::Text result = "/";
  60. result += getName();
  61. for (ChatCommandParameter* param : params)
  62. {
  63. result += " ";
  64. if (param->isOptional()) result += "[";
  65. result += param->getName();
  66. if (param->isOptional()) result += "]";
  67. }
  68. if (description.getLength() > 0)
  69. {
  70. result.append() << "\n " << description;
  71. }
  72. for (ChatCommandParameter* param : params)
  73. {
  74. if (param->getDescription().getLength() > 0)
  75. {
  76. result += "\n ";
  77. if (param->isOptional()) result += "[";
  78. result += param->getName();
  79. if (param->isOptional()) result += "]";
  80. result.append() << " - " << param->getDescription();
  81. }
  82. }
  83. return result;
  84. }
  85. int ChatCommand::getSecurityLevel(
  86. Framework::RCArray<Framework::Text> params) const
  87. {
  88. return securityLevel;
  89. }
  90. ChatCommandParameter::ChatCommandParameter(
  91. Framework::Text name, Framework::Text description, bool optional)
  92. : ReferenceCounter(),
  93. name(name),
  94. description(description),
  95. optional(optional)
  96. {}
  97. bool ChatCommandParameter::isLegalValue(Framework::Text value) const
  98. {
  99. return 1;
  100. }
  101. Framework::Text ChatCommandParameter::getDefaultValue(Entity* zActor) const
  102. {
  103. return "";
  104. }
  105. Framework::Text ChatCommandParameter::getName() const
  106. {
  107. return name;
  108. }
  109. Framework::Text ChatCommandParameter::getDescription() const
  110. {
  111. return description;
  112. }
  113. bool ChatCommandParameter::isOptional() const
  114. {
  115. return optional;
  116. }
  117. Framework::RCArray<Framework::Text> ChatCommandParameter::getAutocompleteValues(
  118. const Framework::Text& current) const
  119. {
  120. return Framework::RCArray<Framework::Text>();
  121. }
  122. PlayerNameParameter::PlayerNameParameter()
  123. : ChatCommandParameter(
  124. "player", "The name of the player (has to be online)", 0)
  125. {}
  126. bool PlayerNameParameter::isLegalValue(Framework::Text value) const
  127. {
  128. return Game::INSTANCE->zPlayerByName(value) != 0;
  129. }
  130. Framework::Text PlayerNameParameter::getDefaultValue(Entity* zActor) const
  131. {
  132. Player* p = dynamic_cast<Player*>(zActor);
  133. if (p) return p->getName();
  134. return "";
  135. }
  136. Framework::RCArray<Framework::Text> PlayerNameParameter::getAutocompleteValues(
  137. const Framework::Text& current) const
  138. {
  139. Framework::RCArray<Framework::Text> result;
  140. Game::INSTANCE->listPlayerNames(result);
  141. for (auto iterator = result.begin(); iterator;)
  142. {
  143. if (iterator->hatAt(0, current))
  144. {
  145. iterator.remove();
  146. }
  147. else
  148. ++iterator;
  149. }
  150. return result;
  151. }
  152. IntegerParameter::IntegerParameter(Framework::Text name,
  153. Framework::Text description,
  154. bool optional,
  155. std::function<int(Entity* zEntity)> calculateDefault)
  156. : ChatCommandParameter(name, description, optional),
  157. calculateDefault(calculateDefault)
  158. {}
  159. bool IntegerParameter::isLegalValue(Framework::Text value) const
  160. {
  161. return Framework::Text((int)value).istGleich(value);
  162. }
  163. Framework::Text IntegerParameter::getDefaultValue(Entity* zActor) const
  164. {
  165. return Framework::Text(calculateDefault(zActor));
  166. }