ChatCommandExecutor.cpp 6.1 KB

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