ChatCommandExecutor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "ChatCommandExecutor.h"
  2. #include "Game.h"
  3. #include "SaveCommand.h"
  4. #include "GrantCommand.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 += (index + 1);
  99. error += ": ";
  100. if (params.getEintragAnzahl() > index)
  101. {
  102. error += *params.z(index);
  103. }
  104. else
  105. {
  106. error += "(no parameter was given)";
  107. }
  108. error += "\n";
  109. error += command->getHelp();
  110. Game::INSTANCE->zChat()->sendMessageTo(
  111. error, zActor, Chat::CHANNEL_ERROR);
  112. return true;
  113. }
  114. }
  115. }
  116. if (index != params.getEintragAnzahl())
  117. {
  118. Framework::Text error = "Illegal number of parameters. "
  119. "First unknown parameter: ";
  120. error += *params.z(index);
  121. error += "\n";
  122. error += 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 += command->getSecurityLevel(params);
  134. error += ". You have currently a security level of ";
  135. error += zActor->getChatSecurityLevel();
  136. error += ". Ask someone with the required security lavel "
  137. "to grant you the same level.";
  138. Game::INSTANCE->zChat()->sendMessageTo(
  139. error, zActor, Chat::CHANNEL_ERROR);
  140. return true;
  141. }
  142. command->execute(params, zActor);
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }