1. #include "chat.h"
  2. #include "Config.h"
  3. #include "math.h"
  4. #include "player.h"
  5. #include "RBAC.h"
  6. #include "ScriptMgr.h"
  7. #include "World.h"
  8. /*
  9. * Main command class.
  10. */
  11. class PremCommand
  12. {
  13. public:
  14. PremCommand(const char* _info) : info(_info) { }
  15. void handleHelpCommand(ChatHandler* handler)
  16. {
  17. handler->PSendSysMessage(info);
  18. }
  19. virtual void handleCommand(ChatHandler* handler, Player* player) = 0;
  20. private:
  21. const char* info;
  22. };
  23. /*
  24. * Sub command class(es).
  25. */
  26. class DrinkCommand : public PremCommand
  27. {
  28. public:
  29. DrinkCommand(const char* _info) : PremCommand(_info) { }
  30. void handleCommand(ChatHandler* handler, Player* player)
  31. {
  32. uint8 drink = player->GetDrunkValue();
  33. player->SetDrunkValue(drink + 5);
  34. handler->PSendSysMessage("!Down the hatch!");
  35. }
  36. };
  37. class DrunkCommand : public PremCommand
  38. {
  39. public:
  40. DrunkCommand(const char* _info) : PremCommand(_info) { }
  41. void handleCommand(ChatHandler* handler, Player* player)
  42. {
  43. player->SetDrunkValue(100);
  44. handler->PSendSysMessage("!You drunk!");
  45. }
  46. };
  47. class SoberCommand : public PremCommand
  48. {
  49. public:
  50. SoberCommand(const char* _info) : PremCommand(_info) { }
  51. void handleCommand(ChatHandler* handler, Player* player)
  52. {
  53. player->SetDrunkValue(1);
  54. handler->PSendSysMessage("You're now sober..");
  55. }
  56. };
  57. std::map<std::string, PremCommand*> command_map;
  58. class Premium_Commands : public CommandScript
  59. {
  60. public:
  61. Premium_Commands() : CommandScript("Premium_Commands")
  62. {
  63. // Setting up the commands.
  64. if (command_map.empty())
  65. {
  66. command_map["drink"] = new DrinkCommand("Allows the player to increase +5 to drunk.");
  67. command_map["drunk"] = new DrunkCommand("Allows the player become 100 drunk.");
  68. command_map["sober"] = new SoberCommand("Allows the player to become sober.");
  69. }
  70. }
  71. std::vector<ChatCommand> GetCommands() const
  72. {
  73. static std::vector<ChatCommand> commandTable =
  74. {
  75. { "premium", rbac::RBAC_IN_GRANTED_LIST, true, &handlePremiumCommand, "Premium custom commands." },
  76. };
  77. return commandTable;
  78. }
  79. /*
  80. * Main command handler. This will be executed, before any of the sub command handlers.
  81. * It uses polymorphism to map the commands.
  82. */
  83. static bool handlePremiumCommand(ChatHandler* handler, const char* args)
  84. {
  85. Player* player = handler->GetSession()->GetPlayer();
  86. // Dummy code, replace with original code..
  87. bool premium = true;
  88. if (!premium)
  89. return false;
  90. std::vector<char*> arguments = getArguments(args);
  91. if (arguments.size() == 0)
  92. return false;
  93. std::string command = arguments[0];
  94. arguments.erase(arguments.begin());
  95. PremCommand* commandHandler = command_map[command];
  96. if (commandHandler == NULL)
  97. return false;
  98. commandHandler->handleCommand(handler, player);
  99. return true;
  100. }
  101. private:
  102. /*
  103. * Method to convert the argument string to a vector.
  104. */
  105. static std::vector<char*> getArguments(const char* args)
  106. {
  107. std::vector<char*> arguments;
  108. // Extracting the arguments and push them to the vector.
  109. char* arg = strtok((char*)args, " ");
  110. while (arg != NULL)
  111. {
  112. arguments.push_back(arg);
  113. arg = strtok((char*)NULL, " ");
  114. }
  115. return arguments;
  116. }
  117. };
  118. void AddSC_Premium_System()
  119. {
  120. new Premium_Commands;
  121. }