Skip to content

Commit

Permalink
refactor: Improve Transmog Plus code (#161)
Browse files Browse the repository at this point in the history
* refactor: Improve Transmog Plus code

* Update Transmogrification.h
  • Loading branch information
Nyeriah authored Aug 16, 2024
1 parent a571805 commit e994a14
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
77 changes: 24 additions & 53 deletions src/Transmogrification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,12 @@ bool Transmogrification::IsAllowedQuality(uint32 quality, ObjectGuid const &play
{
switch (quality)
{
case ITEM_QUALITY_POOR: return AllowPoor || isPlusWhiteGreyEligible(playerGuid);
case ITEM_QUALITY_NORMAL: return AllowCommon || isPlusWhiteGreyEligible(playerGuid);
case ITEM_QUALITY_POOR: return AllowPoor || IsPlusFeatureEligible(playerGuid, PLUS_FEATURE_GREY_ITEMS);
case ITEM_QUALITY_NORMAL: return AllowCommon || IsPlusFeatureEligible(playerGuid, PLUS_FEATURE_GREY_ITEMS);
case ITEM_QUALITY_UNCOMMON: return AllowUncommon;
case ITEM_QUALITY_RARE: return AllowRare;
case ITEM_QUALITY_EPIC: return AllowEpic;
case ITEM_QUALITY_LEGENDARY: return AllowLegendary || isPlusLegendaryEligible(playerGuid);
case ITEM_QUALITY_LEGENDARY: return AllowLegendary || IsPlusFeatureEligible(playerGuid, PLUS_FEATURE_LEGENDARY_ITEMS);
case ITEM_QUALITY_ARTIFACT: return AllowArtifact;
case ITEM_QUALITY_HEIRLOOM: return AllowHeirloom;
default: return false;
Expand Down Expand Up @@ -1134,19 +1134,24 @@ void Transmogrification::LoadConfig(bool reload)
/* TransmogPlus */
IsTransmogPlusEnabled = sConfigMgr->GetOption<bool>("Transmogrification.EnablePlus", false);

plusDataMap.clear();

std::string stringMembershipIds = sConfigMgr->GetOption<std::string>("Transmogrification.MembershipLevels", "");
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false)) {
MembershipIds.push_back(Acore::StringTo<uint32>(itr).value());
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false))
{
plusDataMap[PLUS_FEATURE_GREY_ITEMS].push_back(Acore::StringTo<uint32>(itr).value());
}

stringMembershipIds = sConfigMgr->GetOption<std::string>("Transmogrification.MembershipLevelsLegendary", "");
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false)) {
MembershipIdsLegendary.push_back(Acore::StringTo<uint32>(itr).value());
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false))
{
plusDataMap[PLUS_FEATURE_LEGENDARY_ITEMS].push_back(Acore::StringTo<uint32>(itr).value());
}

stringMembershipIds = sConfigMgr->GetOption<std::string>("Transmogrification.MembershipLevelsPet", "");
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false)) {
MembershipIdsPet.push_back(Acore::StringTo<uint32>(itr).value());
for (auto& itr : Acore::Tokenize(stringMembershipIds, ',', false))
{
plusDataMap[PLUS_FEATURE_PET].push_back(Acore::StringTo<uint32>(itr).value());
}

PetSpellId = sConfigMgr->GetOption<uint32>("Transmogrification.PetSpellId", 2000100);
Expand All @@ -1168,7 +1173,8 @@ void Transmogrification::DeleteFakeFromDB(ObjectGuid::LowType itemLowGuid, Chara
CharacterDatabase.Execute("DELETE FROM custom_transmogrification WHERE GUID = {}", itemGUID.GetCounter());
}

uint32 Transmogrification::getPlayerMembershipLevel(ObjectGuid const & playerGuid) const {
uint32 Transmogrification::getPlayerMembershipLevel(ObjectGuid const & playerGuid) const
{
CharacterCacheEntry const* playerData = sCharacterCache->GetCharacterCacheByGuid(playerGuid);
if (!playerData)
return 0;
Expand All @@ -1182,59 +1188,24 @@ uint32 Transmogrification::getPlayerMembershipLevel(ObjectGuid const & playerGui
return 0;
}

bool Transmogrification::isPlusWhiteGreyEligible(ObjectGuid const &playerGuid) const {
if (!IsTransmogPlusEnabled)
return false;

if (MembershipIds.size() == 0)
return false;

const auto membershipLevel = getPlayerMembershipLevel(playerGuid);
if (membershipLevel == 0)
return false;

for (const auto& itr : MembershipIds)
{
if (itr == membershipLevel)
return true;
}

return false;
}


bool Transmogrification::isPlusLegendaryEligible(ObjectGuid const & playerGuid) const {
bool Transmogrification::IsPlusFeatureEligible(ObjectGuid const &playerGuid, uint32 feature) const
{
if (!IsTransmogPlusEnabled)
return false;

if (MembershipIdsLegendary.size() == 0)
auto it = plusDataMap.find(feature);
if (it == plusDataMap.end() || it->second.empty())
return false;

const auto membershipLevel = getPlayerMembershipLevel(playerGuid);
if (membershipLevel == 0)
return false;

for (const auto& itr : MembershipIdsLegendary)
{
if (itr == membershipLevel)
return true;
}

return false;
}


bool Transmogrification::isTransmogPlusPetEligible(ObjectGuid const & playerGuid) const {
if (MembershipIdsPet.size() == 0)
return false;

const auto membershipLevel = getPlayerMembershipLevel(playerGuid);
if (membershipLevel == 0)
if (!membershipLevel)
return false;

for (const auto& itr : MembershipIdsPet)
const auto& membershipLevels = it->second;
for (const auto& level : membershipLevels)
{
if (itr == membershipLevel)
if (level == membershipLevel)
return true;
}

Expand Down
18 changes: 11 additions & 7 deletions src/Transmogrification.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ const uint32 AllArmorTiers[4] =
ITEM_SUBCLASS_ARMOR_CLOTH
};

enum PlusFeatures
{
PLUS_FEATURE_GREY_ITEMS,
PLUS_FEATURE_LEGENDARY_ITEMS,
PLUS_FEATURE_PET
};

class Transmogrification
{
public:
Expand All @@ -97,6 +104,8 @@ class Transmogrification
typedef std::unordered_map<ObjectGuid, transmog2Data> transmogMap;
typedef std::unordered_map<uint32, std::vector<uint32>> collectionCacheMap;
typedef std::unordered_map<uint32, std::string> searchStringMap;
typedef std::unordered_map<uint32, std::vector<uint32>> transmogPlusData;
transmogPlusData plusDataMap;
transmogMap entryMap; // entryMap[pGUID][iGUID] = entry
transmogData dataMap; // dataMap[iGUID] = pGUID
collectionCacheMap collectionCache;
Expand Down Expand Up @@ -250,14 +259,9 @@ class Transmogrification

// Transmog Plus
bool IsTransmogPlusEnabled;
std::vector<uint32> MembershipIds;
std::vector<uint32> MembershipIdsLegendary;
std::vector<uint32> MembershipIdsPet;

[[nodiscard]] bool IsPlusFeatureEligible(ObjectGuid const& playerGuid, uint32 feature) const;
uint32 getPlayerMembershipLevel(ObjectGuid const & playerGuid) const;
bool isPlusWhiteGreyEligible(ObjectGuid const & playerGuid) const;
bool isPlusLegendaryEligible(ObjectGuid const & playerGuid) const;
bool isTransmogPlusPetEligible(ObjectGuid const & playerGuid) const;

};
#define sTransmogrification Transmogrification::instance()

Expand Down
9 changes: 4 additions & 5 deletions src/cs_transmog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,15 @@ class transmog_commandscript : public CommandScript
if (Player* player = PlayerIdentifier::FromSelf(handler)->GetConnectedPlayer())
{

if (sTransmogrification->IsTransmogPlusEnabled) {
if (sTransmogrification->isTransmogPlusPetEligible(player->GetGUID())) {
if (sTransmogrification->IsTransmogPlusEnabled)
if (sTransmogrification->IsPlusFeatureEligible(player->GetGUID(), PLUS_FEATURE_PET))
{
player->CastSpell((Unit*)nullptr, sTransmogrification->PetSpellId, true);
return true;
}
}

if (player->GetSession()->GetSecurity() < SEC_MODERATOR) {
if (player->GetSession()->GetSecurity() < SEC_MODERATOR)
return true;
}

player->CastSpell((Unit*)nullptr, sTransmogrification->PetSpellId, true);
}
Expand Down

0 comments on commit e994a14

Please sign in to comment.