ChatCommandExecutor.cpp 6.3 KB

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