ChatCommandExecutor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "ChatCommandExecutor.h"
  2. #include "Game.h"
  3. #include "SaveCommand.h"
  4. ChatCommandExecutor::ChatCommandExecutor()
  5. : ReferenceCounter()
  6. {
  7. knownCommands.add(new SaveCommand());
  8. }
  9. bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor)
  10. {
  11. if (line.hatAt(0, "/"))
  12. {
  13. for (ChatCommand* command : knownCommands)
  14. {
  15. if (line.hatAt(0, Framework::Text("/") + command->getName()))
  16. {
  17. if (line.getLength() > command->getName().getLength() + 1
  18. && !line.hatAt(command->getName().getLength() + 1, " "))
  19. {
  20. continue;
  21. }
  22. // TODO: check security level
  23. Framework::RCArray<Framework::Text> params;
  24. int start = command->getName().getLength() + 2;
  25. bool escaped = 0;
  26. for (int i = command->getName().getLength() + 2;
  27. i < line.getLength();
  28. i++)
  29. {
  30. if (line.hatAt(i, " ") && !escaped)
  31. {
  32. if (start < i)
  33. {
  34. if (line.hatAt(start, "'")
  35. && line.hatAt(i - 1, "'"))
  36. {
  37. params.add(line.getTeilText(start + 1, i - 1));
  38. }
  39. else
  40. {
  41. params.add(line.getTeilText(start, i));
  42. }
  43. }
  44. start = i + 1;
  45. }
  46. else if (line.hatAt(i, "'"))
  47. {
  48. escaped = !escaped;
  49. }
  50. }
  51. int index = 0;
  52. for (ChatCommandParameter* param : command->getParams())
  53. {
  54. if (params.getEintragAnzahl() > index && param->isLegalValue(*params.z(index)))
  55. {
  56. index++;
  57. }
  58. else
  59. {
  60. if (param->isOptional() && zActor)
  61. {
  62. params.add(new Framework::Text(
  63. param->getDefaultValue(zActor)),
  64. index++);
  65. }
  66. else
  67. {
  68. Framework::Text error
  69. = "Illegal parameter at position ";
  70. error += (index + 1);
  71. error += ": ";
  72. if (params.getEintragAnzahl() > index)
  73. {
  74. error += *params.z(index);
  75. }
  76. else
  77. {
  78. error += "(no parameter was given)";
  79. }
  80. error += "\n";
  81. error += command->getHelp();
  82. if (zActor)
  83. {
  84. Game::INSTANCE->zChat()->sendMessageTo(
  85. error, zActor, Chat::CHANNEL_ERROR);
  86. }
  87. else
  88. {
  89. std::cout << error << std::endl;
  90. }
  91. return true;
  92. }
  93. }
  94. }
  95. if (index != params.getEintragAnzahl())
  96. {
  97. Framework::Text error = "Illegal number of parameters. "
  98. "First unknown parameter: ";
  99. error += *params.z(index);
  100. error += "\n";
  101. error += command->getHelp();
  102. if (zActor)
  103. {
  104. Game::INSTANCE->zChat()->sendMessageTo(
  105. error, zActor, Chat::CHANNEL_ERROR);
  106. }
  107. else
  108. {
  109. std::cout << error << std::endl;
  110. }
  111. }
  112. command->execute(params, zActor);
  113. return true;
  114. }
  115. }
  116. }
  117. return false;
  118. }