ChatCommandExecutor.cpp 5.8 KB

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