From e95fc49e68db44be4e7a442b32f85b3acf883826 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 1 Sep 2025 19:27:37 +0200 Subject: [PATCH 1/4] refactor --- src/RPT_loader.cpp | 4 --- src/RewardPlayedTime.cpp | 57 ++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 23 deletions(-) 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..b652dc1 100644 --- a/src/RewardPlayedTime.cpp +++ b/src/RewardPlayedTime.cpp @@ -7,24 +7,54 @@ #include "Config.h" #include "Chat.h" +bool modEnabled; +bool modAnnounce; +uint32 rewardIntervalMinutes; + +// std::vector> RewardItems; +std::vector items; +std::unordered_map timers; + + +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 + { + modEnabled = sConfigMgr->GetOption("RewardPlayedTime.Enable", true); + modAnnounce = sConfigMgr->GetOption("RewardPlayedTime.Announce", true); + rewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); + + // Get reward list + std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); + std::stringstream ss(itemList); + std::string token; + items.clear(); + while (std::getline(ss, token, ',')) + { + items.push_back(std::stoul(token)); + } + } +}; + 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 (!modEnabled) { return; } - - if (sConfigMgr->GetOption("RewardPlayedTime.Announce", true) ) + if (modAnnounce) { ChatHandler(player->GetSession()).PSendSysMessage("This server is running the |cff4CFF00Reward Time Played Improved |rmodule."); } @@ -34,12 +64,11 @@ public: void OnPlayerBeforeUpdate(Player* player, uint32 p_time) override { - if (!sConfigMgr->GetOption("RewardPlayedTime.Enable", true)) + if (!modEnabled) { return; } - uint32 rewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); uint32 intervalMs = rewardIntervalMinutes * SECOND * IN_MILLISECONDS; ObjectGuid guid = player->GetGUID(); @@ -63,17 +92,6 @@ public: 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, ',')) - { - items.push_back(std::stoul(token)); - } - if (items.empty()) { LOG_WARN("module", "[RewardPlayedTime]: RewardItems list could not be parsed. Check your config!"); @@ -132,7 +150,7 @@ public: void OnPlayerLogout(Player* player) override { - if (!sConfigMgr->GetOption("RewardPlayedTime.Enable", true)) { + if (!modEnabled) { return; } @@ -142,5 +160,6 @@ public: void AddRewardPlayedTimeScripts() { + new mod_reward_time_played_conf(); new RewardPlayedTime(); } From 09bba47c89b8f9e8134954bab2c7db2c342a1075 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 1 Sep 2025 19:52:09 +0200 Subject: [PATCH 2/4] refactor --- src/RewardPlayedTime.cpp | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/RewardPlayedTime.cpp b/src/RewardPlayedTime.cpp index b652dc1..bb96512 100644 --- a/src/RewardPlayedTime.cpp +++ b/src/RewardPlayedTime.cpp @@ -7,13 +7,13 @@ #include "Config.h" #include "Chat.h" -bool modEnabled; -bool modAnnounce; -uint32 rewardIntervalMinutes; +bool modRptiEnabled; +bool modRptiAnnounce; +uint32 modRptiRewardIntervalMinutes; // std::vector> RewardItems; -std::vector items; -std::unordered_map timers; +std::vector modRptiItems; +std::unordered_map modRptiTimers; class mod_reward_time_played_conf : public WorldScript @@ -24,18 +24,18 @@ public: // Load Configuration Settings void OnBeforeConfigLoad(bool /*reload*/) override { - modEnabled = sConfigMgr->GetOption("RewardPlayedTime.Enable", true); - modAnnounce = sConfigMgr->GetOption("RewardPlayedTime.Announce", true); - rewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); + modRptiEnabled = sConfigMgr->GetOption("RewardPlayedTime.Enable", true); + modRptiAnnounce = sConfigMgr->GetOption("RewardPlayedTime.Announce", true); + modRptiRewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); // Get reward list std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); std::stringstream ss(itemList); std::string token; - items.clear(); + modRptiItems.clear(); while (std::getline(ss, token, ',')) { - items.push_back(std::stoul(token)); + modRptiItems.push_back(std::stoul(token)); } } }; @@ -50,31 +50,31 @@ public: void OnPlayerLogin(Player* player) override { - if (!modEnabled) + if (!modRptiEnabled) { return; } - if (modAnnounce) + 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 (!modEnabled) + if (!modRptiEnabled) { return; } - 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(guid); + if (player_timer == modRptiTimers.end()) { return; // player not tracked } @@ -92,14 +92,14 @@ public: player_timer->second = 0; // reset timer - if (items.empty()) + if (modRptiItems.empty()) { LOG_WARN("module", "[RewardPlayedTime]: RewardItems list could not be parsed. Check your config!"); return; // no items configured } - int32 roll = urand(0, items.size() - 1); - uint32 rewardItemId = items[roll]; + int32 roll = urand(0, modRptiItems.size() - 1); + uint32 rewardItemId = modRptiItems[roll]; SendRewardToPlayer(player, rewardItemId, 1); } @@ -150,11 +150,11 @@ public: void OnPlayerLogout(Player* player) override { - if (!modEnabled) { + if (!modRptiEnabled) { return; } - timers.erase(player->GetGUID()); + modRptiTimers.erase(player->GetGUID()); } }; From 9735b0c3d53fb55b06c3bf477e1ac529a0cc5038 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 6 Sep 2025 19:57:41 +0200 Subject: [PATCH 3/4] add more items, conf send only email --- README.md | 2 + .../mod-reward-played-time-improved.conf.dist | 5 +- src/RewardPlayedTime.cpp | 82 ++++++++++--------- 3 files changed, 51 insertions(+), 38 deletions(-) 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/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/RewardPlayedTime.cpp b/src/RewardPlayedTime.cpp index bb96512..ee37f90 100644 --- a/src/RewardPlayedTime.cpp +++ b/src/RewardPlayedTime.cpp @@ -9,9 +9,9 @@ bool modRptiEnabled; bool modRptiAnnounce; +bool modRptiAddToBag; uint32 modRptiRewardIntervalMinutes; -// std::vector> RewardItems; std::vector modRptiItems; std::unordered_map modRptiTimers; @@ -26,6 +26,7 @@ public: { 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 @@ -37,6 +38,7 @@ public: { modRptiItems.push_back(std::stoul(token)); } + LOG_INFO("module", "[RewardPlayedTime]: Loaded " + std::to_string(modRptiItems.size()) + " rewards."); } }; @@ -71,70 +73,68 @@ public: uint32 intervalMs = modRptiRewardIntervalMinutes * SECOND * IN_MILLISECONDS; - ObjectGuid guid = player->GetGUID(); - - auto player_timer = modRptiTimers.find(guid); + 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 - if (modRptiItems.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, modRptiItems.size() - 1); - uint32 rewardItemId = modRptiItems[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(); @@ -148,13 +148,21 @@ public: CharacterDatabase.CommitTransaction(trans); } - void OnPlayerLogout(Player* player) override + bool ValidateItemId(uint32 itemId, uint32 count) { - if (!modRptiEnabled) { - 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; } - modRptiTimers.erase(player->GetGUID()); + return true; } }; From 83153cd7187fee19c4fbc2bac852991a0148838e Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 6 Sep 2025 20:00:04 +0200 Subject: [PATCH 4/4] fix style --- apps/ci/ci-codestyle.sh | 0 src/RewardPlayedTime.cpp | 3 +-- 2 files changed, 1 insertion(+), 2 deletions(-) mode change 100644 => 100755 apps/ci/ci-codestyle.sh diff --git a/apps/ci/ci-codestyle.sh b/apps/ci/ci-codestyle.sh old mode 100644 new mode 100755 diff --git a/src/RewardPlayedTime.cpp b/src/RewardPlayedTime.cpp index ee37f90..f0e2135 100644 --- a/src/RewardPlayedTime.cpp +++ b/src/RewardPlayedTime.cpp @@ -15,7 +15,6 @@ uint32 modRptiRewardIntervalMinutes; std::vector modRptiItems; std::unordered_map modRptiTimers; - class mod_reward_time_played_conf : public WorldScript { public: @@ -30,7 +29,7 @@ public: modRptiRewardIntervalMinutes = sConfigMgr->GetOption("RewardPlayedTime.RewardInterval", 3600); // Get reward list - std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); + std::string itemList = sConfigMgr->GetOption("RewardPlayedTime.RewardItems", ""); std::stringstream ss(itemList); std::string token; modRptiItems.clear();