ChatCommandExecutor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "ChatCommandExecutor.h"
  2. #include "Game.h"
  3. #include "GrantCommand.h"
  4. #include "SaveCommand.h"
  5. #include "BlockInfoCommand.h"
  6. ChatCommandExecutor::ChatCommandExecutor()
  7. : ReferenceCounter()
  8. {
  9. knownCommands.add(new SaveCommand());
  10. knownCommands.add(new CrantCommand());
  11. knownCommands.add(new BlockInfoCommand());
  12. }
  13. bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor)
  14. {
  15. if (line.hatAt(0, "/"))
  16. {
  17. for (ChatCommand* command : knownCommands)
  18. {
  19. if (line.hatAt(0, Framework::Text("/") + command->getName()))
  20. {
  21. if (line.getLength() > command->getName().getLength() + 1
  22. && !line.hatAt(command->getName().getLength() + 1, " "))
  23. {
  24. continue;
  25. }
  26. Framework::RCArray<Framework::Text> params;
  27. int start = command->getName().getLength() + 2;
  28. bool escaped = 0;
  29. for (int i = command->getName().getLength() + 2;
  30. i < line.getLength();
  31. i++)
  32. {
  33. if (line.hatAt(i, " ") && !escaped)
  34. {
  35. if (start < i)
  36. {
  37. if (line.hatAt(start, "'")
  38. && line.hatAt(i - 1, "'"))
  39. {
  40. params.add(line.getTeilText(start + 1, i - 1));
  41. }
  42. else
  43. {
  44. params.add(line.getTeilText(start, i));
  45. }
  46. }
  47. start = i + 1;
  48. }
  49. else if (line.hatAt(i, "'"))
  50. {
  51. escaped = !escaped;
  52. }
  53. }
  54. if (start < line.getLength())
  55. {
  56. if (line.hatAt(start, "'")
  57. && line.hatAt(line.getLength() - 1, "'"))
  58. {
  59. params.add(
  60. line.getTeilText(start + 1, line.getLength() - 1));
  61. }
  62. else
  63. {
  64. params.add(line.getTeilText(start, line.getLength()));
  65. }
  66. }
  67. int index = 0;
  68. for (ChatCommandParameter* param : command->getParams())
  69. {
  70. if (params.getEintragAnzahl() > index
  71. && param->isLegalValue(*params.z(index)))
  72. {
  73. index++;
  74. }
  75. else
  76. {
  77. bool isError = 0;
  78. if (param->isOptional() && zActor)
  79. {
  80. Framework::Text defaultValue
  81. = param->getDefaultValue(zActor);
  82. if (!param->isLegalValue(defaultValue))
  83. {
  84. isError = 1;
  85. }
  86. else
  87. {
  88. params.add(
  89. new Framework::Text(defaultValue), index++);
  90. }
  91. }
  92. else
  93. {
  94. isError = 1;
  95. }
  96. if (isError)
  97. {
  98. Framework::Text error
  99. = "Illegal parameter at position ";
  100. error.append() << (index + 1) << ": ";
  101. if (params.getEintragAnzahl() > index)
  102. {
  103. error += *params.z(index);
  104. }
  105. else
  106. {
  107. error += "(no parameter was given)";
  108. }
  109. error.append() << "\n" << command->getHelp();
  110. Game::INSTANCE->zChat()->sendMessageTo(
  111. error, zActor, Chat::CHANNEL_ERROR);
  112. return true;
  113. }
  114. }
  115. }
  116. if (index != params.getEintragAnzahl())
  117. {
  118. Framework::Text error = "Illegal number of parameters. "
  119. "First unknown parameter: ";
  120. error.append() << *params.z(index) << "\n"
  121. << command->getHelp();
  122. Game::INSTANCE->zChat()->sendMessageTo(
  123. error, zActor, Chat::CHANNEL_ERROR);
  124. return true;
  125. }
  126. if (zActor
  127. && command->getSecurityLevel(params)
  128. > zActor->getChatSecurityLevel())
  129. {
  130. Framework::Text error
  131. = "This command requires a security level of at least ";
  132. error.append()
  133. << command->getSecurityLevel(params)
  134. << ". You have currently a security level of "
  135. << zActor->getChatSecurityLevel()
  136. << ". Ask someone with the required security lavel to "
  137. "grant you the same level.";
  138. Game::INSTANCE->zChat()->sendMessageTo(
  139. error, zActor, Chat::CHANNEL_ERROR);
  140. return true;
  141. }
  142. command->execute(params, zActor);
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }