ChatCommand.cpp 4.9 KB

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