ChatCommandExecutor.cpp 5.8 KB

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