ChatCommand.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.append() << "\n " << description;
  37. }
  38. for (ChatCommandParameter* param : params)
  39. {
  40. if (param->getDescription().getLength() > 0)
  41. {
  42. result += "\n ";
  43. if (param->isOptional()) result += "[";
  44. result += param->getName();
  45. if (param->isOptional()) result += "]";
  46. result.append() << " - " << param->getDescription();
  47. }
  48. }
  49. return result;
  50. }
  51. int ChatCommand::getSecurityLevel(
  52. Framework::RCArray<Framework::Text> params) const
  53. {
  54. return securityLevel;
  55. }
  56. ChatCommandParameter::ChatCommandParameter(
  57. Framework::Text name, Framework::Text description, bool optional)
  58. : ReferenceCounter(),
  59. name(name),
  60. description(description),
  61. optional(optional)
  62. {}
  63. bool ChatCommandParameter::isLegalValue(Framework::Text value) const
  64. {
  65. return 1;
  66. }
  67. Framework::Text ChatCommandParameter::getDefaultValue(Entity* zActor) const
  68. {
  69. return "";
  70. }
  71. Framework::Text ChatCommandParameter::getName() const
  72. {
  73. return name;
  74. }
  75. Framework::Text ChatCommandParameter::getDescription() const
  76. {
  77. return description;
  78. }
  79. bool ChatCommandParameter::isOptional() const
  80. {
  81. return optional;
  82. }
  83. PlayerNameParameter::PlayerNameParameter()
  84. : ChatCommandParameter(
  85. "player", "The name of the player (has to be online)", 0)
  86. {}
  87. bool PlayerNameParameter::isLegalValue(Framework::Text value) const
  88. {
  89. return Game::INSTANCE->zPlayerByName(value) != 0;
  90. }
  91. Framework::Text PlayerNameParameter::getDefaultValue(Entity* zActor) const
  92. {
  93. Player* p = dynamic_cast<Player*>(zActor);
  94. if (p) return p->getName();
  95. return "";
  96. }
  97. IntegerParameter::IntegerParameter(Framework::Text name,
  98. Framework::Text description,
  99. bool optional,
  100. std::function<int(Entity* zEntity)> calculateDefault)
  101. : ChatCommandParameter(name, description, optional),
  102. calculateDefault(calculateDefault)
  103. {}
  104. bool IntegerParameter::isLegalValue(Framework::Text value) const
  105. {
  106. return Framework::Text((int)value).istGleich(value);
  107. }
  108. Framework::Text IntegerParameter::getDefaultValue(Entity* zActor) const
  109. {
  110. return Framework::Text(calculateDefault(zActor));
  111. }