ChatCommand.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::ReferenceCounter(),
  7. name(name),
  8. description(description),
  9. securityLevel(securityLevel)
  10. {}
  11. void ChatCommand::addParam(ChatCommandParameter* param)
  12. {
  13. params.add(param);
  14. }
  15. const Framework::RCArray<ChatCommandParameter>& ChatCommand::getParams() const
  16. {
  17. return params;
  18. }
  19. Framework::Text ChatCommand::getName() const
  20. {
  21. return name;
  22. }
  23. Framework::Text ChatCommand::getHelp() const
  24. {
  25. Framework::Text result = "/";
  26. result += name;
  27. for (ChatCommandParameter* param : params)
  28. {
  29. result += " ";
  30. if (param->isOptional()) result += "[";
  31. result += param->getName();
  32. if (param->isOptional()) result += "]";
  33. }
  34. if (description.getLength() > 0)
  35. {
  36. result += "\n ";
  37. result += description;
  38. }
  39. for (ChatCommandParameter* param : params)
  40. {
  41. if (param->getDescription().getLength() > 0)
  42. {
  43. result += "\n ";
  44. if (param->isOptional()) result += "[";
  45. result += param->getName();
  46. if (param->isOptional()) result += "]";
  47. result += " - ";
  48. result += param->getDescription();
  49. }
  50. }
  51. return result;
  52. }
  53. int ChatCommand::getSecurityLevel(
  54. Framework::RCArray<Framework::Text> params) const
  55. {
  56. return securityLevel;
  57. }
  58. ChatCommandParameter::ChatCommandParameter(
  59. Framework::Text name, Framework::Text description, bool optional)
  60. : ReferenceCounter(),
  61. name(name),
  62. description(description),
  63. optional(optional)
  64. {}
  65. bool ChatCommandParameter::isLegalValue(Framework::Text value) const
  66. {
  67. return 1;
  68. }
  69. Framework::Text ChatCommandParameter::getDefaultValue(Entity* zActor) const
  70. {
  71. return "";
  72. }
  73. Framework::Text ChatCommandParameter::getName() const
  74. {
  75. return name;
  76. }
  77. Framework::Text ChatCommandParameter::getDescription() const
  78. {
  79. return description;
  80. }
  81. bool ChatCommandParameter::isOptional() const
  82. {
  83. return optional;
  84. }
  85. PlayerNameParameter::PlayerNameParameter()
  86. : ChatCommandParameter(
  87. "player", "The name of the player (has to be online)", 0)
  88. {}
  89. bool PlayerNameParameter::isLegalValue(Framework::Text value) const
  90. {
  91. return Game::INSTANCE->zPlayerByName(value) != 0;
  92. }
  93. Framework::Text PlayerNameParameter::getDefaultValue(Entity* zActor) const
  94. {
  95. Player* p = dynamic_cast<Player*>(zActor);
  96. if (p) return p->getName();
  97. return "";
  98. }
  99. IntegerParameter::IntegerParameter(Framework::Text name,
  100. Framework::Text description,
  101. bool optional,
  102. std::function<int(Entity* zEntity)> calculateDefault)
  103. : ChatCommandParameter(name, description, optional),
  104. calculateDefault(calculateDefault)
  105. {}
  106. bool IntegerParameter::isLegalValue(Framework::Text value) const
  107. {
  108. return Text((int)value).istGleich(value);
  109. }
  110. Framework::Text IntegerParameter::getDefaultValue(Entity* zActor) const
  111. {
  112. return Text(calculateDefault(zActor));
  113. }