diff --git a/src/AllMapScript.cpp b/src/AllMapScript.cpp index 5c6a710..7399f68 100644 --- a/src/AllMapScript.cpp +++ b/src/AllMapScript.cpp @@ -112,6 +112,10 @@ public: return; } sMpDataStore->RemoveInstanceData(map->GetId(), map->GetInstanceId()); + + // If there is player data for this map reset it to default values + + } }; diff --git a/src/GroupScript.cpp b/src/GroupScript.cpp index 101b38c..6e8aa25 100644 --- a/src/GroupScript.cpp +++ b/src/GroupScript.cpp @@ -18,22 +18,33 @@ class MythicPlus_GroupScript : public GroupScript Player* player = ObjectAccessor::FindPlayer(guid); if (!player) { - MpLogger::warn("Player not found for guid {}", guid); + MpLogger::warn("Player not found for guid {}", guid.GetCounter()); return; } + MpGroupData *gd = sMpDataStore->GetGroupData(group->GetGUID()); MpPlayerData* pd = sMpDataStore->GetPlayerData(guid); if(!pd) { - MpPlayerData playerData = MpPlayerData(); - playerData.player = player; - playerData.groupId = group->GetGUID().GetCounter(); - + MpDifficulty difficulty = GetPlayerDifficulty(player); + MpPlayerData playerData = MpPlayerData(player, difficulty, group->GetGUID().GetCounter()); + pd = &playerData; sMpDataStore->AddPlayerData(guid, playerData); + } else { + + // If the player is joining a new group then reset the death counters otherwise let them ride + if (pd->groupId != group->GetGUID().GetCounter()) { + pd->groupId = group->GetGUID().GetCounter(); + pd->ResetAllDeathCounts(); + } + } + + if(!gd) { + MpLogger::warn("Group data not found for group {}", group->GetGUID().GetCounter()); return; } - // sMpDataStore->AddGroupData(group->GetGUID(), gd); + gd->AddPlayerData(pd); } void OnCreate(Group* group, Player* leader) override { @@ -45,9 +56,21 @@ class MythicPlus_GroupScript : public GroupScript return; } + // Start a group and set the data up for the group + MpDifficulty difficulty = GetPlayerDifficulty(leader); + MpGroupData gd = MpGroupData(group, difficulty); + // Insert the leader of the group after group data struct is built + MpPlayerData* pd = sMpDataStore->GetPlayerData(leader->GetGUID()); + if(pd) { + gd.AddPlayerData(pd); + } else { + MpPlayerData playerData = MpPlayerData(leader, difficulty, group->GetGUID().GetCounter()); + gd.AddPlayerData(&playerData); + } - // sMpDataStore->AddGroupData(group->GetGUID(), gd); + // Store the data into our memory store + sMpDataStore->AddGroupData(group, gd); } void OnDisband(Group* group) override { @@ -60,3 +83,18 @@ void Add_MP_GroupScripts() MpLogger::debug("Add_MP_GroupScripts()"); new MythicPlus_GroupScript(); } + +// Get the difficulty for a player that is assigned +MpDifficulty GetPlayerDifficulty(Player* player) { + if(!player) { + return; + } + + MpPlayerData* pd = sMpDataStore->GetPlayerData(player->GetGUID()); + if(pd) { + return pd->difficulty; + } else { + return player->GetDifficulty(false) == Difficulty::DUNGEON_DIFFICULTY_NORMAL ? MP_DIFFICULTY_NORMAL : MP_DIFFICULTY_HEROIC; + } + +} diff --git a/src/MpDataStore.cpp b/src/MpDataStore.cpp index 6b4c94b..93c2e1b 100644 --- a/src/MpDataStore.cpp +++ b/src/MpDataStore.cpp @@ -1,5 +1,6 @@ #include "CharacterDatabase.h" #include "MpDataStore.h" +#include "Group.h" #include "MpLogger.h" // Adds an entry for the group difficult to memory and updats database @@ -24,9 +25,9 @@ void MpDataStore::AddGroupData(Group *group, MpGroupData groupData) { // if we set a lower difficulty and we are in an instance we need to kick the group out and reset the instance. Map* map = group->GetLeader()->GetMap(); - if(group->InInstance()) { + auto instance = map->ToInstanceMap(); + if(instance->HavePlayers()) { - auto instance = map->ToInstanceMap(); instance->Reset(2); // 2 = reset all const Map::PlayerList players = map->GetPlayers(); @@ -35,6 +36,7 @@ void MpDataStore::AddGroupData(Group *group, MpGroupData groupData) { player->GetSession()->SendNotification("The group leader has changed the difficulty setting. You have been removed from the instance."); } } + } _groupData->at(guid) = groupData; @@ -42,9 +44,9 @@ void MpDataStore::AddGroupData(Group *group, MpGroupData groupData) { } else { Map* map = group->GetLeader()->GetMap(); - if(group->InInstance()) { + auto instance = map->ToInstanceMap(); + if(instance->HavePlayers()) { - auto instance = map->ToInstanceMap(); instance->Reset(2); // 2 = reset all const Map::PlayerList players = map->GetPlayers(); @@ -110,7 +112,7 @@ void MpDataStore::PushGroupInstanceKey(Group *group, uint32 mapId, uint32 instan return; } - _groupData->at(guid).instanceDataKeys.push_back(GetInstanceDataKey(mapId, instanceId)); + // Need to potentialy reset here? } // This clears out any group data from memory and the database diff --git a/src/MpDataStore.h b/src/MpDataStore.h index f1da1b7..d845e0c 100644 --- a/src/MpDataStore.h +++ b/src/MpDataStore.h @@ -23,33 +23,11 @@ enum MpDifficulty { MP_DIFFICULTY_ASCENDANT = 5 }; -struct MpGroupData -{ - Group* group; - MpDifficulty difficulty; - uint32 deaths; - - std::vector> instanceDataKeys; - MpGroupData() : group(nullptr), difficulty(MpDifficulty{}), deaths(0) { - instanceDataKeys.reserve(32); - } - MpGroupData(Group* group, MpDifficulty difficulty, uint32 deaths) - : group(group), difficulty(difficulty), deaths(deaths) { - instanceDataKeys.reserve(32); - } - - std::string ToString() const { - return "MpGroupData: { group: " + std::to_string(group->GetGUID().GetCounter()) + - ", difficulty: " + std::to_string(difficulty) + - ", deaths: " + std::to_string(deaths) + " }"; - } - -}; - struct MpPlayerInstanceData { uint32 deaths; }; +// struct used to track player performance in an instance. struct MpPlayerData { Player* player; @@ -59,9 +37,87 @@ struct MpPlayerData // list of maps and instance player is bound to and mythic data related to it std::map,MpPlayerInstanceData> instanceData; - MpPlayerData() : player(nullptr), groupId(0) { - instanceData = std::map,MpPlayerInstanceData>(); + MpPlayerData(Player* p, MpDifficulty diff, uint32_t groupId) + : player(p), difficulty(diff), groupId(groupId) {} + + void AddDeath(uint32 mapId, uint32 instanceId) { + auto key = std::make_pair(mapId, instanceId); + if(instanceData.contains(key)) { + instanceData[key].deaths++; + } else { + instanceData[key] = MpPlayerInstanceData{.deaths = 1}; + } } + + uint32 GetDeaths(uint32 mapId, uint32 instanceId) const { + auto key = std::make_pair(mapId, instanceId); + if(instanceData.contains(key)) { + return instanceData.at(key).deaths; + } + return 0; + } + + void ResetDeathCount(uint32 mapId, uint32 instanceId) { + auto key = std::make_pair(mapId, instanceId); + if(instanceData.contains(key)) { + instanceData[key].deaths = 0; + } + } + + void ResetAllDeathCounts() { + for(auto& [key, data] : instanceData) { + data.deaths = 0; + } + } +}; + +// Struct used to track entire group performance. +struct MpGroupData +{ + Group* group; + MpDifficulty difficulty; + std::vector players; + + MpGroupData() : group(nullptr), difficulty(MpDifficulty{}) { + players.reserve(5); + } + + MpGroupData(Group* group, MpDifficulty difficulty) + : group(group), difficulty(difficulty) { + players.reserve(5); + } + + uint32 GetDeaths(uint32 mapId, uint32 instanceId) const { + uint32 deaths = 0; + for (const MpPlayerData* player : players) { + deaths += player->GetDeaths(mapId, instanceId); + } + return deaths; + } + + void ResetGroupDeaths(uint32 mapId, uint32 instanceId) { + for (MpPlayerData* player : players) { + player->ResetDeathCount(mapId, instanceId); + } + } + + void AddPlayerData(MpPlayerData* playerData) { + players.push_back(playerData); + } + + std::string ToString() const { + Map* map = group->GetLeader()->GetMap(); + + uint32 deaths = 0; + if(map) { + deaths = GetDeaths(map->GetId(), map->GetInstanceId()); + } + + return "MpGroupData: { group: " + std::to_string(group->GetGUID().GetCounter()) + + ", difficulty: " + std::to_string(difficulty) + + ", deaths: " + std::to_string(deaths) + " }"; + } + }; struct MpScaleFactor @@ -255,7 +311,7 @@ public: } } - const MpGroupData* GetGroupData(ObjectGuid guid) const { + MpGroupData* GetGroupData(ObjectGuid guid) { if (_groupData->contains(guid)) { return &_groupData->at(guid); @@ -264,7 +320,7 @@ public: } } - const MpGroupData* GetGroupData(Player *player) const { + MpGroupData* GetGroupData(Player *player) { return GetGroupData(player->GetGroup()->GetGUID()); }; @@ -318,8 +374,8 @@ public: int32 LoadScaleFactors(); // Database API calls - void MpDataStore::SavePlayerInstanceData(Player* player, MpPlayerData const* playerData); - void MpDataStore::SavePlayerDungeonStats(Group* group, MpGroupData const* groupData); + void SavePlayerInstanceData(Player* player, MpPlayerData const* playerData); + //SavePlayerDungeonStats(Group* group, MpGroupData const* groupData); static MpDataStore* instance() { static MpDataStore instance;