kusanagy

custom_rates

Jan 7th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.93 KB | None | 0 0
  1. #include "ScriptMgr.h"
  2. #include "Chat.h"
  3. #include "Language.h"
  4. #include "CharacterDatabase.h"
  5. #include "DatabaseEnv.h"
  6. #include "World.h"
  7. #include "RBAC.h"
  8. #include "WorldSession.h"
  9. #include "Player.h"
  10. #include "World.h"
  11.  
  12. class CustomRates
  13. {
  14. private:
  15.     static int32 GetRateFromDB(const Player *player, CharacterDatabaseStatements statement)
  16.     {
  17.         PreparedStatement *stmt = CharacterDatabase.GetPreparedStatement(statement);
  18.         stmt->setUInt32(0, player->GetGUID());
  19.         PreparedQueryResult result = CharacterDatabase.Query(stmt);
  20.  
  21.         if (result)
  22.             return static_cast<int32>((*result)[0].GetUInt32());
  23.  
  24.         return -1;
  25.     }
  26.  
  27.     static void SaveRateToDB(const Player *player, uint32 rate, bool update, CharacterDatabaseStatements uStmt, CharacterDatabaseStatements iStmt)
  28.     {
  29.         if (update)
  30.         {
  31.             PreparedStatement *stmt = CharacterDatabase.GetPreparedStatement(uStmt);
  32.             stmt->setUInt32(0, rate);
  33.             stmt->setUInt32(1, player->GetGUID());
  34.             CharacterDatabase.Execute(stmt);
  35.         }
  36.         else
  37.         {
  38.             PreparedStatement *stmt = CharacterDatabase.GetPreparedStatement(iStmt);
  39.             stmt->setUInt32(0, player->GetGUID());
  40.             stmt->setUInt32(1, rate);
  41.             CharacterDatabase.Execute(stmt);
  42.         }
  43.     }
  44. public:
  45.     static void DeleteRateFromDB(uint64 guid, CharacterDatabaseStatements statement)
  46.     {
  47.         PreparedStatement *stmt = CharacterDatabase.GetPreparedStatement(statement);
  48.         //stmt->setUInt32(0, GUID_LOPART(guid));
  49.         CharacterDatabase.Execute(stmt);
  50.     }
  51.  
  52.     static int32 GetXpRateFromDB(const Player *player)
  53.     {
  54.         return GetRateFromDB(player, CHAR_SEL_INDIVIDUAL_XP_RATE);
  55.     }
  56.  
  57.     static int32 GetLootRateFromDB(const Player *player)
  58.     {
  59.         return GetRateFromDB(player, CHAR_SEL_INDIVIDUAL_LOOT_RATE);
  60.     }
  61.  
  62.     static void SaveXpRateToDB(const Player *player, uint32 rate, bool update)
  63.     {
  64.         SaveRateToDB(player, rate, update, CHAR_UPD_INDIVIDUAL_XP_RATE, CHAR_INS_INDIVIDUAL_XP_RATE);
  65.     }
  66.  
  67.     static void SaveLootRateToDB(const Player *player, uint32 rate, bool update)
  68.     {
  69.         SaveRateToDB(player, rate, update, CHAR_UPD_INDIVIDUAL_LOOT_RATE, CHAR_INS_INDIVIDUAL_LOOT_RATE);
  70.     }
  71. };
  72.  
  73. class add_del_rates : public PlayerScript
  74. {
  75. public:
  76.     add_del_rates() : PlayerScript("add_del_rates") { }
  77.  
  78.     void OnDelete(ObjectGuid guid, uint32 /*accountId*/)
  79.     {
  80.         CustomRates::DeleteRateFromDB(guid, CHAR_DEL_INDIVIDUAL_XP_RATE);
  81.         CustomRates::DeleteRateFromDB(guid, CHAR_DEL_INDIVIDUAL_LOOT_RATE);
  82.     }
  83.  
  84.     void OnLogin(Player* player, bool firstLogin)
  85.     {
  86.         // show custom XP rate on login
  87.         int32 rate = CustomRates::GetXpRateFromDB(player);
  88.  
  89.         if (rate != -1 && player->getLevel() != sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL))
  90.         {
  91.             uint32 uRate = static_cast<uint32>(rate);
  92.             player->SetCustomXpRate(uRate);
  93.  
  94.             if (sWorld->getBoolConfig(CONFIG_PLAYER_INDIVIDUAL_XP_RATE_SHOW_ON_LOGIN))
  95.             {
  96.                 if (uRate)
  97.                     ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your XP rate was set to %u.", uRate);
  98.                 else
  99.                     ChatHandler(player->GetSession()).SendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your XP rate was set to 0. You won't gain any XP anymore.");
  100.             }
  101.         }
  102.  
  103.         // show custom loot rate on login
  104.         rate = CustomRates::GetLootRateFromDB(player);
  105.         if (rate != -1)
  106.         {
  107.             uint32 uRate = static_cast<uint32>(rate);
  108.             player->SetCustomLootRate(uRate);
  109.  
  110.             if (sWorld->getBoolConfig(CONFIG_PLAYER_INDIVIDUAL_LOOT_RATE_SHOW_ON_LOGIN))
  111.             {
  112.                 if (uRate)
  113.                     ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your loot rate was set to %u.", uRate);
  114.                 else
  115.                     ChatHandler(player->GetSession()).SendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your loot rate was set to 0. You won't be able to loot anything.");
  116.             }
  117.         }
  118.     }
  119. };
  120.  
  121. class custom_rate_commands : public CommandScript
  122. {
  123. private:
  124.  
  125. public:
  126.     custom_rate_commands() : CommandScript("custom_rate_commands") {}
  127.  
  128.     std::vector<ChatCommand> GetCommands() const override
  129.     {
  130.         static std::vector<ChatCommand> rateCommandTable =
  131.         {
  132.             { "xp",   rbac::RBAC_PERM_COMMAND_XP_RATE,   false, &HandleRateXpCommand,   "" },
  133.             { "loot", rbac::RBAC_PERM_COMMAND_LOOT_RATE, false, &HandleRateLootCommand, "" },
  134.         };
  135.         static std::vector<ChatCommand> commandTable =
  136.         {
  137.             { "rate", rbac::RBAC_PERM_COMMAND_RATE,      false, NULL, "", rateCommandTable },
  138.         };
  139.         return commandTable;
  140.     }
  141.  
  142.     static bool HandleRateXpCommand(ChatHandler *handler, const char *args)
  143.     {
  144.         // take a pointer to the player who uses the command
  145.         Player *me = handler->GetSession() ? handler->GetSession()->GetPlayer() : NULL;
  146.         if (!me)
  147.             return false;
  148.  
  149.         if (sWorld->getIntConfig(CONFIG_CUSTOM_RATE_LOOT_ENABLED) == 0)
  150.         {
  151.             handler->PSendSysMessage("Custom Rate xp is disabled");
  152.             handler->SetSentErrorMessage(true);
  153.             me->SetCustomXpRate(1);
  154.             return false;
  155.         }
  156.  
  157.         // already at max level, no point in using the command at all
  158.         if (me->getLevel() == sWorld->getIntConfig(CONFIG_CUSTOM_XP_LEVEL))
  159.         {
  160.             handler->SendSysMessage("|CFF7BBEF7[Custom Rates]|r: You are already at maximum level.");
  161.             return true;
  162.         }
  163.  
  164.         // no arguments, show current XP rate
  165.         if (!*args)
  166.         {
  167.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your current XP rate is %u.", me->GetCustomXpRate());
  168.             return true;
  169.         }
  170.  
  171.         // first, check if I can use the command
  172.         if (me->GetSession()->GetSecurity() < (int)sWorld->getIntConfig(CONFIG_PLAYER_INDIVIDUAL_XP_RATE_SECURITY))
  173.         {
  174.             handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
  175.             handler->SetSentErrorMessage(true);
  176.             return false;
  177.         }
  178.  
  179.         // take a pointer to player's selection
  180.         Player *target = handler->getSelectedPlayer();
  181.         if (!target || !target->GetSession())
  182.         {
  183.             handler->SendSysMessage(LANG_NO_CHAR_SELECTED);
  184.             handler->SetSentErrorMessage(true);
  185.             return false;
  186.         }
  187.  
  188.         // extract value
  189.         int rate = atoi((char *)args);
  190.         int maxRate = sWorld->getIntConfig(CONFIG_PLAYER_MAXIMUM_INDIVIDUAL_XP_RATE);
  191.         if (rate < 0 || rate > maxRate)
  192.         {
  193.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Invalid rate specified, must be in interval [0,%i].", maxRate);
  194.             handler->SetSentErrorMessage(true);
  195.             return false;
  196.         }
  197.  
  198.         // take a pointer to the player we need to set xp rate to
  199.         // can be either player itself, or his selection, if
  200.         // selection security is lower than his security
  201.         Player *player = NULL;
  202.         if (target == me)
  203.             player = me;
  204.         else
  205.         {
  206.             if (me->GetSession()->GetSecurity() > target->GetSession()->GetSecurity())
  207.                 player = target;
  208.             else
  209.             {
  210.                 handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
  211.                 handler->SetSentErrorMessage(true);
  212.                 return false;
  213.             }
  214.         }
  215.  
  216.         // set player custom XP rate and save it in DB for later use
  217.         uint32 uRate = static_cast<uint32>(rate);
  218.         player->SetCustomXpRate(uRate);
  219.         int32 rateFromDB = CustomRates::GetXpRateFromDB(player);
  220.         if (rateFromDB == -1)
  221.             CustomRates::SaveXpRateToDB(player, uRate, false);
  222.         else
  223.             CustomRates::SaveXpRateToDB(player, uRate, true);
  224.  
  225.         // show a message indicating custom XP rate change
  226.         if (player == me)
  227.         {
  228.             if (uRate == 0)
  229.                 handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your XP rate to 0. You won't gain any XP anymore.");
  230.             else
  231.                 handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your XP rate to %u.", uRate);
  232.         }
  233.         else
  234.         {
  235.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set %s's XP rate to %u.", handler->GetNameLink(player).c_str(), uRate);
  236.             ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: %s has set your XP rate to %u.", handler->GetNameLink().c_str(), uRate);
  237.         }
  238.  
  239.         return true;
  240.     }
  241.  
  242.     static bool HandleRateLootCommand(ChatHandler *handler, const char *args)
  243.     {
  244.         // take a pointer to the player who uses the command
  245.         Player *me = handler->GetSession() ? handler->GetSession()->GetPlayer() : NULL;
  246.         if (!me)
  247.             return false;
  248.  
  249.         // already at max level, no point in using the command at all
  250.         if (me->getLevel() == sWorld->getIntConfig(CONFIG_CUSTOM_LOOT_LEVEL))
  251.         {
  252.             handler->SendSysMessage("|CFF7BBEF7[Custom Rates]|r: You are already at maximum level.");
  253.             return true;
  254.         }
  255.  
  256.         if (sWorld->getIntConfig(CONFIG_CUSTOM_RATE_XP_ENABLED) == 0)
  257.         {
  258.             handler->PSendSysMessage("Custom Rate loot is disabled");
  259.             handler->SetSentErrorMessage(true);
  260.             me->SetCustomLootRate(1);
  261.             return false;
  262.         }
  263.  
  264.         // no arguments, show current loot rate
  265.         if (!*args)
  266.         {
  267.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your current loot rate is %u.", me->GetCustomLootRate());
  268.             return true;
  269.         }
  270.  
  271.         // first, check if I can use the command
  272.         if (me->GetSession()->GetSecurity() < (int)sWorld->getIntConfig(CONFIG_PLAYER_INDIVIDUAL_LOOT_RATE_SECURITY))
  273.         {
  274.             handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
  275.             handler->SetSentErrorMessage(true);
  276.             return false;
  277.         }
  278.  
  279.         // take a pointer to player's selection
  280.         Player *target = handler->getSelectedPlayer();
  281.         if (!target || !target->GetSession())
  282.         {
  283.             handler->SendSysMessage(LANG_NO_CHAR_SELECTED);
  284.             handler->SetSentErrorMessage(true);
  285.             return false;
  286.         }
  287.  
  288.         // extract value
  289.         int rate = atoi((char *)args);
  290.         int maxRate = sWorld->getIntConfig(CONFIG_PLAYER_MAXIMUM_INDIVIDUAL_LOOT_RATE);
  291.         if (rate < 0 || rate > maxRate)
  292.         {
  293.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Invalid rate specified, must be in interval [0,%i].", maxRate);
  294.             handler->SetSentErrorMessage(true);
  295.             return false;
  296.         }
  297.  
  298.         // take a pointer to the player we need to set xp rate to
  299.         // can be either player itself, or his selection, if
  300.         // selection security is lower than his security
  301.         Player *player = NULL;
  302.         if (target == me)
  303.             player = me;
  304.         else
  305.         {
  306.             if (me->GetSession()->GetSecurity() > target->GetSession()->GetSecurity())
  307.                 player = target;
  308.             else
  309.             {
  310.                 handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
  311.                 handler->SetSentErrorMessage(true);
  312.                 return false;
  313.             }
  314.         }
  315.  
  316.         // set player custom loot rate and save it in DB for later use
  317.         uint32 uRate = static_cast<uint32>(rate);
  318.         player->SetCustomLootRate(uRate);
  319.         int32 rateFromDB = CustomRates::GetLootRateFromDB(player);
  320.         if (rateFromDB == -1)
  321.             CustomRates::SaveLootRateToDB(player, uRate, false);
  322.         else
  323.             CustomRates::SaveLootRateToDB(player, uRate, true);
  324.  
  325.         // show a message indicating custom XP rate change
  326.         if (player == me)
  327.         {
  328.             if (uRate == 0)
  329.                 handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your loot rate to 0. You won't gain any XP anymore.");
  330.             else
  331.                 handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your loot rate to %u.", uRate);
  332.         }
  333.         else
  334.         {
  335.             handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set %s's loot rate to %u.", handler->GetNameLink(player).c_str(), uRate);
  336.             ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: %s has set your loot rate to %u.", handler->GetNameLink().c_str(), uRate);
  337.         }
  338.  
  339.         return true;
  340.     }
  341. };
  342.  
  343. void Add_SC_Custom_Rates()
  344. {
  345.     new add_del_rates();
  346.     new custom_rate_commands();
  347. }
Add Comment
Please, Sign In to add comment