/// <summary>
 /// 更新副本信息
 /// </summary>
 public void UpdateGuildCopyMap(int guildid, GuildCopyMap CopyMap)
 {
     lock (GuildCopyMapDict)
     {
         GuildCopyMapDict[guildid] = CopyMap;
     }
 }
        /// <summary>
        /// 更新副本信息
        /// </summary>
        public void UpdateGuildCopyMap(int guildid, int fubenid, int seqid, int mapcode)
        {
            GuildCopyMap CopyMap = new GuildCopyMap()
            {
                GuildID = guildid,
                FuBenID = fubenid,
                SeqID   = seqid,
                MapCode = mapcode,
            };

            UpdateGuildCopyMap(guildid, CopyMap);
        }
        /// <summary>
        /// 找到第一个副本记录
        /// 在跨周的时候陆续删除
        /// </summary>
        public GuildCopyMap FindActiveGuildCopyMap()
        {
            GuildCopyMap CopyMap = null;

            lock (GuildCopyMapDict)
            {
                foreach (var map in GuildCopyMapDict)
                {
                    return(map.Value);
                }
            }
            return(CopyMap);
        }
        /// <summary>
        /// 根据帮会id查找到一个帮会副本记录
        /// </summary>
        public GuildCopyMap FindGuildCopyMap(int guildid)
        {
            GuildCopyMap CopyMap = null;

            lock (GuildCopyMapDict)
            {
                if (GuildCopyMapDict.ContainsKey(guildid))
                {
                    CopyMap = GuildCopyMapDict[guildid];
                }
            }
            return(CopyMap);
        }
        public GuildCopyMap FindActiveGuildCopyMap()
        {
            GuildCopyMap CopyMap = null;

            lock (this.GuildCopyMapDict)
            {
                using (Dictionary <int, GuildCopyMap> .Enumerator enumerator = this.GuildCopyMapDict.GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        KeyValuePair <int, GuildCopyMap> map = enumerator.Current;
                        return(map.Value);
                    }
                }
            }
            return(CopyMap);
        }
        public void CheckCurrGuildCopyMap(GameClient client, out int fubenid, out int seqid, int mapcode)
        {
            fubenid = -1;
            seqid   = -1;
            int            guildid = client.ClientData.Faction;
            GuildCopyMapDB data    = GameManager.GuildCopyMapDBMgr.FindGuildCopyMapDB(guildid, client.ServerId);

            if (null != data)
            {
                DateTime openTime = Global.GetRealDate(data.OpenDay);
                if (Global.BeginOfWeek(openTime) != Global.BeginOfWeek(TimeUtil.NowDateTime()))
                {
                    GameManager.GuildCopyMapDBMgr.ResetGuildCopyMapDB(guildid, client.ServerId);
                    fubenid = this.FirstGuildCopyMapOrder;
                }
                else if (data.FuBenID >= this.LastGuildCopyMapOrder && data.State == 2)
                {
                    fubenid = 0;
                }
                else if (data.State == 2)
                {
                    data.FuBenID = this.GetNextGuildCopyMapIndex(data.FuBenID);
                    data.State   = 0;
                    data.OpenDay = Global.GetOffsetDay(TimeUtil.NowDateTime());
                    if (GameManager.GuildCopyMapDBMgr.UpdateGuildCopyMapDB(data, client.ServerId))
                    {
                        fubenid = data.FuBenID;
                    }
                    this.UpdateGuildCopyMap(guildid, fubenid, -1, -1);
                }
                else
                {
                    fubenid = data.FuBenID;
                    GuildCopyMap CopyMap = this.FindGuildCopyMap(guildid);
                    if (null != CopyMap)
                    {
                        seqid = CopyMap.SeqID;
                    }
                }
            }
        }
        /// <summary>
        /// 检查死亡的怪物是否为帮会副本boss并进行通关处理
        /// </summary>
        public void ProcessMonsterDead(GameClient client, Monster monster)
        {
            // 怪物所在场景不是帮会地图
            if (IsGuildCopyMap(monster.CurrentMapCode) == false)
            {
                return;
            }

            SystemXmlItem systemFuBenItem = null;

            if (!GameManager.systemFuBenMgr.SystemXmlItemDict.TryGetValue(monster.CurrentMapCode, out systemFuBenItem))
            {
                return;
            }

            if (null == systemFuBenItem)
            {
                return;
            }

            int nBossID = systemFuBenItem.GetIntValue("BossID");

#if ___CC___FUCK___YOU___BB___
            // 不是boss
            if (nBossID != monster.XMonsterInfo.MonsterId)
            {
                return;
            }
#else
            // 不是boss
            if (nBossID != monster.MonsterInfo.ExtensionID)
            {
                return;
            }
#endif
            // 没有对应副本?
            CopyMap copyMap = GameManager.CopyMapMgr.FindCopyMap(monster.CurrentCopyMapID);
            if (null == copyMap)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == copyMap), CurrentCopyMapID={0}", monster.CurrentCopyMapID));
                return;
            }

            GuildCopyMap mapData = GameManager.GuildCopyMapMgr.FindGuildCopyMapBySeqID(copyMap.FuBenSeqID);
            if (null == mapData)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == mapData), copyMap.FuBenSeqID={0}", copyMap.FuBenSeqID));
                return;
            }
            int guildid = mapData.GuildID;

            // 玩家没有帮会?
            GuildCopyMapDB data = GameManager.GuildCopyMapDBMgr.FindGuildCopyMapDB(guildid /*client.ClientData.Faction*//*不再用击杀者的帮会id*/, client.ServerId);
            if (null == data)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == data), guildid={0}", client.ClientData.Faction));
                return;
            }

            List <GameClient> objsList = copyMap.GetClientsList();
            // 副本里没人?
            if (null == objsList || objsList.Count <= 0)
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == objsList || objsList.Count <= 0), CurrentCopyMapID={0}", monster.CurrentCopyMapID));
                return;
            }

            // 更新副本记录的状态
            if (copyMap.FubenMapID >= data.FuBenID)
            {
                data.FuBenID = copyMap.FubenMapID;
                data.State   = (int)GuildCopyMapState.Passed;
                if (copyMap.FubenMapID == GameManager.GuildCopyMapMgr.FirstGuildCopyMapOrder)
                {
                    data.Killers = monster.WhoKillMeName;
                }
                else
                {
                    data.Killers += ",";
                    data.Killers += monster.WhoKillMeName;
                }
            }
#if ___CC___FUCK___YOU___BB___
            //触发战盟事件
            GlobalEventSource.getInstance().fireEvent(ZhanMengShijianEvent.createKillBossEvent(Global.FormatRoleName4(client), client.ClientData.Faction,
                                                                                               monster.XMonsterInfo.MonsterId, client.ServerId));
#else
            //触发战盟事件
            GlobalEventSource.getInstance().fireEvent(ZhanMengShijianEvent.createKillBossEvent(Global.FormatRoleName4(client), client.ClientData.Faction,
                                                                                               monster.MonsterInfo.ExtensionID, client.ServerId));
#endif
            // 检查是否更新成功
            bool result = GameManager.GuildCopyMapDBMgr.UpdateGuildCopyMapDB(data, client.ServerId);
            if (false == result)
            {
                string logStr = @"GuildCopyMapManager::ProcessMonsterDead (false == result), 
                        data.GuildID={0}, data.FuBenID={1}, data.State={2}, data.OpenDay={3}, data.Killers={4}";
                LogManager.WriteLog(LogTypes.Error, string.Format(logStr, data.GuildID, data.FuBenID, data.State, data.OpenDay, data.Killers));
                return;
            }
        }
        /// <summary>
        /// 检查帮会副本的数据库状态,找到应该进入的副本id,副本流水id,副本code
        /// fubenid = 0 表示本周的副本全通关了
        /// fubenid = -1 表示在调用之前应该先判断玩家是否有帮会
        /// </summary>
        public void CheckCurrGuildCopyMap(GameClient client, out int fubenid, out int seqid, int mapcode)
        {
            fubenid = -1;
            seqid   = -1;
            mapcode = -1;
            int guildid = client.ClientData.Faction;

            GuildCopyMapDB data = GameManager.GuildCopyMapDBMgr.FindGuildCopyMapDB(guildid, client.ServerId);

            // 没有帮会 否则不应该为空
            if (null == data)
            {
                return;
            }

            // 开始时间不是本周
            DateTime openTime = Global.GetRealDate(data.OpenDay);

            if (Global.BeginOfWeek(openTime) != Global.BeginOfWeek(TimeUtil.NowDateTime()))
            {
                // 重置数据库信息 并使用第一个副本id 让玩家重新开始
                GameManager.GuildCopyMapDBMgr.ResetGuildCopyMapDB(guildid, client.ServerId);
                fubenid = FirstGuildCopyMapOrder;
                return;
            }

            // 到达最后一关并且已经通了
            if (data.FuBenID >= LastGuildCopyMapOrder && data.State == (int)GuildCopyMapState.Passed)
            {
                fubenid = 0;
                return;
            }

            // 已经通关
            if (data.State == (int)GuildCopyMapState.Passed)
            {
                // 更新数据库
                data.FuBenID = GetNextGuildCopyMapIndex(data.FuBenID);
                data.State   = (int)GuildCopyMapState.NotOpen;
                data.OpenDay = Global.GetOffsetDay(TimeUtil.NowDateTime());

                if (GameManager.GuildCopyMapDBMgr.UpdateGuildCopyMapDB(data, client.ServerId))
                {
                    fubenid = data.FuBenID;
                }
                UpdateGuildCopyMap(guildid, fubenid, -1, -1);
                return;
            }
            else
            {
                fubenid = data.FuBenID;
            }

            //if (!IsGuildCopyMap(newmapid))
            //    newmapid = FirstGuildCopyMapOrder;

            GuildCopyMap CopyMap = FindGuildCopyMap(guildid);

            if (null == CopyMap)
            {
                return;
            }

            seqid = CopyMap.SeqID;
        }
 public void ProcessMonsterDead(GameClient client, Monster monster)
 {
     if (this.IsGuildCopyMap(monster.CurrentMapCode))
     {
         SystemXmlItem systemFuBenItem = null;
         if (GameManager.systemFuBenMgr.SystemXmlItemDict.TryGetValue(monster.CurrentMapCode, out systemFuBenItem))
         {
             if (null != systemFuBenItem)
             {
                 int nBossID = systemFuBenItem.GetIntValue("BossID", -1);
                 if (nBossID == monster.MonsterInfo.ExtensionID)
                 {
                     CopyMap copyMap = GameManager.CopyMapMgr.FindCopyMap(monster.CurrentCopyMapID);
                     if (null == copyMap)
                     {
                         LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == copyMap), CurrentCopyMapID={0}", monster.CurrentCopyMapID), null, true);
                     }
                     else
                     {
                         GuildCopyMap mapData = GameManager.GuildCopyMapMgr.FindGuildCopyMapBySeqID(copyMap.FuBenSeqID);
                         if (null == mapData)
                         {
                             LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == mapData), copyMap.FuBenSeqID={0}", copyMap.FuBenSeqID), null, true);
                         }
                         else
                         {
                             int            guildid = mapData.GuildID;
                             GuildCopyMapDB data    = GameManager.GuildCopyMapDBMgr.FindGuildCopyMapDB(guildid, client.ServerId);
                             if (null == data)
                             {
                                 LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == data), guildid={0}", client.ClientData.Faction), null, true);
                             }
                             else
                             {
                                 List <GameClient> objsList = copyMap.GetClientsList();
                                 if (objsList == null || objsList.Count <= 0)
                                 {
                                     LogManager.WriteLog(LogTypes.Error, string.Format("GuildCopyMapManager::ProcessMonsterDead (null == objsList || objsList.Count <= 0), CurrentCopyMapID={0}", monster.CurrentCopyMapID), null, true);
                                 }
                                 else
                                 {
                                     if (copyMap.FubenMapID >= data.FuBenID)
                                     {
                                         data.FuBenID = copyMap.FubenMapID;
                                         data.State   = 2;
                                         if (copyMap.FubenMapID == GameManager.GuildCopyMapMgr.FirstGuildCopyMapOrder)
                                         {
                                             data.Killers = monster.WhoKillMeName;
                                         }
                                         else
                                         {
                                             GuildCopyMapDB guildCopyMapDB = data;
                                             guildCopyMapDB.Killers += ",";
                                             GuildCopyMapDB guildCopyMapDB2 = data;
                                             guildCopyMapDB2.Killers += monster.WhoKillMeName;
                                         }
                                     }
                                     GlobalEventSource.getInstance().fireEvent(ZhanMengShijianEvent.createKillBossEvent(Global.FormatRoleName4(client), client.ClientData.Faction, monster.MonsterInfo.ExtensionID, client.ServerId));
                                     if (!GameManager.GuildCopyMapDBMgr.UpdateGuildCopyMapDB(data, client.ServerId))
                                     {
                                         string logStr = "GuildCopyMapManager::ProcessMonsterDead (false == result), \r\n                        data.GuildID={0}, data.FuBenID={1}, data.State={2}, data.OpenDay={3}, data.Killers={4}";
                                         LogManager.WriteLog(LogTypes.Error, string.Format(logStr, new object[]
                                         {
                                             data.GuildID,
                                             data.FuBenID,
                                             data.State,
                                             data.OpenDay,
                                             data.Killers
                                         }), null, true);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }