Core/Guild: Handle changing the leader in one db transaction
authorjoschiwald <joschiwald.trinity@gmail.com>
Sun, 22 Apr 2018 12:53:01 +0000 (14:53 +0200)
committerjoschiwald <joschiwald.trinity@gmail.com>
Sun, 22 Apr 2018 12:53:01 +0000 (14:53 +0200)
src/server/game/Entities/Player/Player.h
src/server/game/Guilds/Guild.cpp
src/server/game/Guilds/Guild.h
src/server/game/Handlers/CharacterHandler.cpp

index 7c1614402071aea7c75edecce166b3d5f66d17cb..a514a35d0960b33c605193b6467e71bc879d012c 100644 (file)
@@ -1753,8 +1753,8 @@ class TC_GAME_API Player : public Unit, public GridObject<Player>
         void SendUpdateToOutOfRangeGroupMembers();
 
         void SetInGuild(ObjectGuid::LowType guildId);
-        void SetRank(uint8 rankId) { SetUInt32Value(PLAYER_GUILDRANK, rankId); }
-        uint8 GetRank() const { return uint8(GetUInt32Value(PLAYER_GUILDRANK)); }
+        void SetGuildRank(uint8 rankId) { SetUInt32Value(PLAYER_GUILDRANK, rankId); }
+        uint8 GetGuildRank() const { return uint8(GetUInt32Value(PLAYER_GUILDRANK)); }
         void SetGuildLevel(uint32 level) { SetUInt32Value(PLAYER_GUILDLEVEL, level); }
         uint32 GetGuildLevel() const { return GetUInt32Value(PLAYER_GUILDLEVEL); }
         void SetGuildIdInvited(ObjectGuid::LowType GuildId) { m_GuildIdInvited = GuildId; }
index 431fcc7315b1624cd4a04b24379d5a23469803e8..cc91d056c59d9878351242695e40e9e1c555114b 100644 (file)
@@ -583,8 +583,8 @@ void Guild::Member::ChangeRank(SQLTransaction& trans, uint8 newRank)
     m_rankId = newRank;
 
     // Update rank information in player's field, if he is online.
-    if (Player* player = FindPlayer())
-        player->SetRank(newRank);
+    if (Player* player = FindConnectedPlayer())
+        player->SetGuildRank(newRank);
 
     PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_GUILD_MEMBER_RANK);
     stmt->setUInt8 (0, newRank);
@@ -1516,12 +1516,14 @@ void Guild::HandleSetNewGuildMaster(WorldSession* session, std::string const& na
         // Same for the new one
         if (Member* newGuildMaster = GetMember(name))
         {
-            _SetLeaderGUID(newGuildMaster);
+            SQLTransaction trans = CharacterDatabase.BeginTransaction();
 
-            SQLTransaction trans(nullptr);
+            _SetLeader(trans, newGuildMaster);
             oldGuildMaster->ChangeRank(trans, GR_INITIATE);
 
             SendEventNewLeader(newGuildMaster, oldGuildMaster);
+
+            CharacterDatabase.CommitTransaction(trans);
         }
     }
 }
@@ -2496,8 +2498,8 @@ bool Guild::Validate()
     // Repair the structure of the guild.
     // If the guildmaster doesn't exist or isn't member of the guild
     // attempt to promote another member.
-    Member* pLeader = GetMember(m_leaderGuid);
-    if (!pLeader)
+    Member* leader = GetMember(m_leaderGuid);
+    if (!leader)
     {
         SQLTransaction trans(nullptr);
         DeleteMember(trans, m_leaderGuid);
@@ -2508,8 +2510,8 @@ bool Guild::Validate()
             return false;
         }
     }
-    else if (!pLeader->IsRank(GR_GUILDMASTER))
-        _SetLeaderGUID(pLeader);
+    else if (!leader->IsRank(GR_GUILDMASTER))
+        _SetLeader(trans, leader);
 
     // Check config if multiple guildmasters are allowed
     if (!sConfigMgr->GetBoolDefault("Guild.AllowMultipleGuildMaster", false))
@@ -2632,7 +2634,7 @@ bool Guild::AddMember(SQLTransaction& trans, ObjectGuid guid, uint8 rankId)
         m_members[guid] = member;
         player->SetInGuild(m_id);
         player->SetGuildIdInvited(UI64LIT(0));
-        player->SetRank(rankId);
+        player->SetGuildRank(rankId);
         player->SetGuildLevel(GetLevel());
         SendLoginInfo(player->GetSession());
         name = player->GetName();
@@ -2712,11 +2714,7 @@ void Guild::DeleteMember(SQLTransaction& trans, ObjectGuid guid, bool isDisbandi
             return;
         }
 
-        _SetLeaderGUID(newLeader);
-
-        // If player not online data in data field will be loaded from guild tabs no need to update it !!
-        if (Player* newLeaderPlayer = newLeader->FindPlayer())
-            newLeaderPlayer->SetRank(GR_GUILDMASTER);
+        _SetLeader(trans, newLeader);
 
         // If leader does not exist (at guild loading with deleted leader) do not send broadcasts
         if (oldLeader)
@@ -2737,7 +2735,7 @@ void Guild::DeleteMember(SQLTransaction& trans, ObjectGuid guid, bool isDisbandi
     if (player)
     {
         player->SetInGuild(UI64LIT(0));
-        player->SetRank(0);
+        player->SetGuildRank(0);
         player->SetGuildLevel(0);
 
         for (GuildPerkSpellsEntry const* entry : sGuildPerkSpellsStore)
@@ -2955,21 +2953,25 @@ bool Guild::_ModifyBankMoney(SQLTransaction& trans, uint64 amount, bool add)
     return true;
 }
 
-void Guild::_SetLeaderGUID(Member* pLeader)
+void Guild::_SetLeader(SQLTransaction& trans, Member* leader)
 {
-    if (!pLeader)
+    if (!leader)
         return;
 
-    SQLTransaction trans = CharacterDatabase.BeginTransaction();
-    m_leaderGuid = pLeader->GetGUID();
-    pLeader->ChangeRank(trans, GR_GUILDMASTER);
+    bool isInTransaction = bool(trans);
+    if (!isInTransaction)
+        trans = CharacterDatabase.BeginTransaction();
+
+    m_leaderGuid = leader->GetGUID();
+    leader->ChangeRank(trans, GR_GUILDMASTER);
 
     PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_GUILD_LEADER);
     stmt->setUInt64(0, m_leaderGuid.GetCounter());
     stmt->setUInt64(1, m_id);
     trans->Append(stmt);
 
-    CharacterDatabase.CommitTransaction(trans);
+    if (!isInTransaction)
+        CharacterDatabase.CommitTransaction(trans);
 }
 
 void Guild::_SetRankBankMoneyPerDay(uint8 rankId, uint32 moneyPerDay)
index fc0699a581090f1623ec994b6db498a476d3d217..d8684ebd3eaaf3f44397594c239ee317b7df6af3 100644 (file)
@@ -929,7 +929,7 @@ class TC_GAME_API Guild
         bool _IsLeader(Player* player) const;
         void _DeleteBankItems(SQLTransaction& trans, bool removeItemsFromDB = false);
         bool _ModifyBankMoney(SQLTransaction& trans, uint64 amount, bool add);
-        void _SetLeaderGUID(Member* pLeader);
+        void _SetLeader(SQLTransaction& trans, Member* leader);
 
         void _SetRankBankMoneyPerDay(uint8 rankId, uint32 moneyPerDay);
         void _SetRankBankTabRightsAndSlots(uint8 rankId, GuildBankRightsAndSlots rightsAndSlots, bool saveToDB = true);
index 673ced13f10fb8a83d468d775b65b72bb191eaca..a428843d6bebd77531075311ab1ee715e0fd97c0 100644 (file)
@@ -956,21 +956,17 @@ void WorldSession::HandlePlayerLogin(LoginQueryHolder* holder)
     {
         Field* fields = resultGuild->Fetch();
         pCurrChar->SetInGuild(fields[0].GetUInt64());
-        pCurrChar->SetRank(fields[1].GetUInt8());
+        pCurrChar->SetGuildRank(fields[1].GetUInt8());
         if (Guild* guild = sGuildMgr->GetGuildById(pCurrChar->GetGuildId()))
             pCurrChar->SetGuildLevel(guild->GetLevel());
     }
     else if (pCurrChar->GetGuildId())                        // clear guild related fields in case wrong data about non existed membership
     {
         pCurrChar->SetInGuild(UI64LIT(0));
-        pCurrChar->SetRank(0);
+        pCurrChar->SetGuildRank(0);
         pCurrChar->SetGuildLevel(0);
     }
 
-    //WorldPacket data(SMSG_LEARNED_DANCE_MOVES, 4+4);
-    //data << uint64(0);
-    //SendPacket(&data);
-
     // TODO: Move this to BattlePetMgr::SendJournalLock() just to have all packets in one file
     WorldPackets::BattlePet::BattlePetJournalLockAcquired lock;
     SendPacket(lock.Write());