ChatCommandExecutor.cpp 6.1 KB

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