diff --git a/README.md b/README.md index ad67178..750929a 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ This module grants players a random reward item every some time of session playt * Rewards are chosen from a configurable list * Items are added directly to the player’s bags. If bags are full, the item is sent via in-game mail +List of Items: https://docs.google.com/spreadsheets/d/1ELCKMdPya1XaBS-YImPtIuEjEPXj2yf1AuVOHhf-QHs/edit?usp=sharing + **Upcoming features** * Add amount for reward item diff --git a/apps/ci/ci-codestyle.sh b/apps/ci/ci-codestyle.sh old mode 100644 new mode 100755 diff --git a/conf/mod-reward-played-time-improved.conf.dist b/conf/mod-reward-played-time-improved.conf.dist index 7986a75..9aec6da 100644 --- a/conf/mod-reward-played-time-improved.conf.dist +++ b/conf/mod-reward-played-time-improved.conf.dist @@ -7,6 +7,9 @@ RewardPlayedTime.Enable = 1 RewardPlayedTime.Announce = 1 +# Add item to the player bag (1) or to the mailbox (0) +RewardPlayedTime.AddToBag = 1 + # Reward intervals in seconds RewardPlayedTime.RewardInterval = 3600 -RewardPlayedTime.RewardItems = 117, 159, 2589, 4306, 4338, 43015, 33447, 33448, 38260, 44817, 22250, 51809, 37705, 37702, 22445, 22573, 22574 +RewardPlayedTime.RewardItems = 1401, 1412, 1416, 117, 159, 414, 436, 118, 1205, 4536, 4540, 4541, 4542, 4544, 3770, 3771, 4599, 4601, 4602, 4604, 4605, 4606, 4607, 4608, 8952, 8953, 2589, 2592, 4306, 4338, 14047, 2835, 2836, 2838, 2770, 2771, 2772, 3355, 3356, 3357, 3358, 2447, 765, 2449, 1210, 2450, 13463, 13464, 13465, 22445, 22573, 22574, 37702, 37705, 22572, 22575, 37701, 37704, 22785, 22786, 22787, 23424, 23425, 43015, 33447, 33448, 38260, 44817, 22250, 36932, 35624, 36918, 51809, 47257, 44326, 47213 diff --git a/src/RPT_loader.cpp b/src/RPT_loader.cpp index 367cd1d..ac06373 100644 --- a/src/RPT_loader.cpp +++ b/src/RPT_loader.cpp @@ -2,12 +2,8 @@ * Copyright (C) 2016+ AzerothCore , released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3 */ -// From SC void AddRewardPlayedTimeScripts(); -// Add all -// cf. the naming convention https://github.com/azerothcore/azerothcore-wotlk/blob/master/doc/changelog/master.md#how-to-upgrade-4 -// additionally replace all '-' in the module folder name with '_' here void Addmod_reward_played_time_improvedScripts() { AddRewardPlayedTimeScripts(); diff --git a/src/RewardPlayedTime.cpp b/src/RewardPlayedTime.cpp index f0c6245..f0e2135 100644 --- a/src/RewardPlayedTime.cpp +++ b/src/RewardPlayedTime.cpp @@ -7,116 +7,133 @@ #include "Config.h" #include "Chat.h" +bool modRptiEnabled; +bool modRptiAnnounce; +bool modRptiAddToBag; +uint32 modRptiRewardIntervalMinutes; + +std::vector modRptiItems; +std::unordered_map modRptiTimers; + +class mod_reward_time_played_conf : public WorldScript +{ +public: + mod_reward_time_played_conf() : WorldScript("mod_reward_time_played_conf", {}) { } + + // Load Configuration Settings + void OnBeforeConfigLoad(bool /*reload*/) override + { + modRptiEnabled = sConfigMgr->GetOption("RewardPlayedTime.Enable", true); + modRptiAnnounce = sConfigMgr->GetOption("RewardPlayedTime.Announce", true); + modRptiAddToBag = sConfigMgr->GetOption("RewardPlayedTime.AddToBag", true); + modRptiRewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); + + // Get reward list + std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); + std::stringstream ss(itemList); + std::string token; + modRptiItems.clear(); + while (std::getline(ss, token, ',')) + { + modRptiItems.push_back(std::stoul(token)); + } + LOG_INFO("module", "[RewardPlayedTime]: Loaded " + std::to_string(modRptiItems.size()) + " rewards."); + } +}; + class RewardPlayedTime : public PlayerScript { public: RewardPlayedTime() : PlayerScript("RewardPlayedTime") { } - // std::vector> RewardItems; - std::unordered_map timers; std::string mail_subject = "RewardPlayedTime"; std::string mail_body = "Congratulations! For your hard work you got a reward, keep it up!"; void OnPlayerLogin(Player* player) override { - if (!sConfigMgr->GetOption("RewardPlayedTime.Enable", true)) + if (!modRptiEnabled) { return; } - - if (sConfigMgr->GetOption("RewardPlayedTime.Announce", true) ) + if (modRptiAnnounce) { ChatHandler(player->GetSession()).PSendSysMessage("This server is running the |cff4CFF00Reward Time Played Improved |rmodule."); } - timers[player->GetGUID()] = 0; + modRptiTimers[player->GetGUID()] = 0; } void OnPlayerBeforeUpdate(Player* player, uint32 p_time) override { - if (!sConfigMgr->GetOption("RewardPlayedTime.Enable", true)) + if (!modRptiEnabled) { return; } - uint32 rewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); - uint32 intervalMs = rewardIntervalMinutes * SECOND * IN_MILLISECONDS; + uint32 intervalMs = modRptiRewardIntervalMinutes * SECOND * IN_MILLISECONDS; - ObjectGuid guid = player->GetGUID(); - - auto player_timer = timers.find(guid); - if (player_timer == timers.end()) + auto player_timer = modRptiTimers.find(player->GetGUID()); + if (player_timer == modRptiTimers.end()) { return; // player not tracked } - if (player->isAFK()) { return; } player_timer->second += p_time; + if (player_timer->second < intervalMs) { return; } - - player_timer->second = 0; // reset timer - - // Get reward list - std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); - std::vector items; - - std::stringstream ss(itemList); - std::string token; - while (std::getline(ss, token, ',')) + if (modRptiItems.empty()) { - items.push_back(std::stoul(token)); - } - - if (items.empty()) - { - LOG_WARN("module", "[RewardPlayedTime]: RewardItems list could not be parsed. Check your config!"); + LOG_WARN("module", "[RewardPlayedTime]: RewardItems list is empty. Check your config!"); return; // no items configured } - int32 roll = urand(0, items.size() - 1); - uint32 rewardItemId = items[roll]; - + uint32 rewardItemId = RollReward(); SendRewardToPlayer(player, rewardItemId, 1); + + player_timer->second = 0; // reset timer + } + + void OnPlayerLogout(Player* player) override + { + if (!modRptiEnabled) { + return; + } + + modRptiTimers.erase(player->GetGUID()); + } + + uint32 RollReward() + { + int32 roll = urand(0, modRptiItems.size() - 1); + return modRptiItems[roll]; } void SendRewardToPlayer(Player* receiver, uint32 itemId, uint32 count) { - ItemTemplate const* item_template = sObjectMgr->GetItemTemplate(itemId); - if (!item_template) + if (!ValidateItemId(itemId, count)) { - LOG_ERROR("module", "[RewardPlayedTime]: The itemId is invalid: {}", itemId); return; } - if (count < 1 || (item_template->MaxCount > 0 && count > uint32(item_template->MaxCount))) - { - LOG_ERROR("module", "[RewardPlayedTime]: The item count is invalid: {} : {}", itemId, count); - return; - } - - std::ostringstream item_quality_string; - item_quality_string << std::hex << ItemQualityColors[item_template->Quality]; ChatHandler(receiver->GetSession()).PSendSysMessage("Congratulations! For your hard work you got a reward!"); - if (receiver->IsInWorld() && receiver->AddItem(itemId, 1)) - { - // // Show item link in chat - // ChatHandler(receiver->GetSession()).PSendSysMessage("You got - |c{}|Hitem:{}:0:0:0:0:0:0:0:0:0|h[{}]|h|r", - // item_quality_string.str(), - // item_template->ItemId, - // item_template->Name1); - return; + if (modRptiAddToBag) { + if (receiver->IsInWorld() && receiver->AddItem(itemId, 1)) + { + return; + } + ChatHandler(receiver->GetSession()).PSendSysMessage("Oh no! But don't worry, your item is send to your mailbox."); + } else { + ChatHandler(receiver->GetSession()).PSendSysMessage("Your item is send to your mailbox."); } - ChatHandler(receiver->GetSession()).PSendSysMessage("Oh no! But don't worry, your item is send to your mailbox."); - MailDraft draft(mail_subject, mail_body); CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction(); @@ -130,17 +147,26 @@ public: CharacterDatabase.CommitTransaction(trans); } - void OnPlayerLogout(Player* player) override + bool ValidateItemId(uint32 itemId, uint32 count) { - if (!sConfigMgr->GetOption("RewardPlayedTime.Enable", true)) { - return; + ItemTemplate const* item_template = sObjectMgr->GetItemTemplate(itemId); + if (!item_template) + { + LOG_ERROR("module", "[RewardPlayedTime]: The itemId is invalid: {}", itemId); + return false; + } + if (count < 1 || (item_template->MaxCount > 0 && count > uint32(item_template->MaxCount))) + { + LOG_ERROR("module", "[RewardPlayedTime]: The item count is invalid: {} : {}", itemId, count); + return false; } - timers.erase(player->GetGUID()); + return true; } }; void AddRewardPlayedTimeScripts() { + new mod_reward_time_played_conf(); new RewardPlayedTime(); }