private void InternalProcessAllyRecord(int count, string mapId)
        {
            var group = this._site.InitArmyGroup();

            foreach (var gc in group.CharaInfoVoList)
            {
                var c = CharacterCache.GetCharByNo(gc.CharaNo);
                if (c != null && c.Id != this._site.CurrentCharId)
                {
                    var allyBattleList = this._site.BattleArmyRecord(gc.CharaNo);
                    for (var i = 0; i < count; i++)
                    {
                        BattleResult battle     = allyBattleList[i];
                        BattleResult prevBattle = i > 0 ? allyBattleList[i - 1] : null;
                        GenerateBattleChars(battle, true, prevBattle != null ? prevBattle.LifeAfter : null);
                        GenerateBattleTurns(battle, true);

                        var  cd     = CharacterDynamicCache.TryGetValue(c.Id, CharacterDynamicCache.LoadDynamic);
                        bool addExp = i == count - 1;
                        UpdateCharDynamic(battle, cd, mapId, addExp);

                        if (this.OnBattleComplete != null)
                        {
                            battle.TotalCount = cd.BattleLevelTotalCount;
                            battle.EPM        = cd.EPM;

                            this.OnBattleComplete(c.Id, battle);
                        }
                    }
                    BattleScheduler.ResultDic[c.Id] = allyBattleList.Take(count).ToList();
                }
            }
        }
        private void InternalProcessBattleResult(BattleResult battle, string mapId)
        {
            GenerateBattleChars(battle);
            GenerateBattleTurns(battle);

            if (battle.GameItemsList != null)
            {
                this._site.AutoSell();
            }

            UpdateCharDynamic(battle, this._site.Dynamic, mapId);

            if (this.OnBattleComplete != null)
            {
                battle.TotalCount = this._site.Dynamic.BattleLevelTotalCount;
                battle.EPM        = this._site.Dynamic.EPM;

                this.OnBattleComplete(this._charId, battle);
            }

            BattleScheduler.ResultDic[this._site.CurrentCharId] = new List <BattleResult>()
            {
                battle
            };
        }
 private static void TriggerBattleComplete(string charId, BattleResult bt)
 {
     if (OnBattleComplete != null)
     {
         OnBattleComplete(charId, bt);
     }
 }
        private ICharacter FindBattleChar(BattleResult battle, string name)
        {
            ICharacter relChar = battle.LeftList.FirstOrDefault(p => p.Name == name);

            if (relChar == null)
            {
                relChar = battle.RightList.FirstOrDefault(p => p.Name == name);
            }
            return(relChar);
        }
        private void GenerateBattleTurns(BattleResult battle, bool isGroup = false)
        {
            battle.Statistics = new Dictionary <string, BattleStatisticsData>();
            for (int i = 0; i < battle.CombatInfo.Count; i++)
            {
                BattleTurn turn = battle.CombatInfo[i];
                turn.CurrentTurn = i + 1;
                GenerateBattleTurn(battle, turn);

                if (turn.HinjuredConmbatList != null)
                {
                    foreach (var p in turn.HinjuredConmbatList)
                    {
                        GenerateBattleTurn(battle, p, false);

                        MergeHpList(turn.HpList, p.HpList);
                    }
                }
            }
        }
        private int InternalBattle()
        {
            string mapId = EnsureMapId();

            int wait = 0;

            if (this._isDungeon)
            {
                List <BattleResult> battleList = this._site.BattleDungeon();
                InternalPropcessDungeonBattleResult(battleList, mapId);
                InternalProcessAllyRecord(battleList.Count, mapId);
                wait = battleList.Sum(p => p.Wait);
            }
            else
            {
                BattleResult battle = this._site.Battle();
                InternalProcessBattleResult(battle, mapId);
                wait = battle.Wait;
            }

            return(wait);
        }
        private void GenerateBattleChars(BattleResult battle, bool isGroup = false, Dictionary <string, int> lifeBeforeDict = null)
        {
            for (var j = 0; j < battle.GameMonList.Count; j++)
            {
                battle.GameMonList[j].BattleCharId = j.ToString();
            }

            if (isGroup)
            {
                var group = this._site.InitArmyGroup();
                battle.LeftList = group.CharaInfoVoList.Select(p => CharacterCache.GetCharByNo(p.CharaNo)).Where(p => p != null).ToList();
            }
            else
            {
                battle.LeftList = new List <Character>()
                {
                    this._site.CurrentChar
                };
            }

            battle.LifeBefore = new Dictionary <string, int>();
            battle.LifeAfter  = new Dictionary <string, int>();
            if (lifeBeforeDict != null)
            {
                foreach (var p in lifeBeforeDict)
                {
                    battle.LifeBefore.Add(p.Key, p.Value);
                    battle.LifeAfter.Add(p.Key, p.Value);
                }
            }
            else
            {
                foreach (var p in battle.LeftList)
                {
                    battle.LifeBefore[p.Id] = p.Life;
                    battle.LifeAfter[p.Id]  = p.Life;
                }
            }
        }
        private void InternalPropcessDungeonBattleResult(List <BattleResult> battleList, string mapId)
        {
            for (var i = 0; i < battleList.Count; i++)
            {
                BattleResult battle     = battleList[i];
                BattleResult prevBattle = i > 0 ? battleList[i - 1] : null;

                GenerateBattleChars(battle, true, prevBattle != null ? prevBattle.LifeAfter : null);
                GenerateBattleTurns(battle, true);

                bool addExp = i == battleList.Count - 1;
                UpdateCharDynamic(battle, this._site.Dynamic, mapId, addExp);

                if (this.OnBattleComplete != null)
                {
                    battle.TotalCount = this._site.Dynamic.BattleLevelTotalCount;
                    battle.EPM        = this._site.Dynamic.EPM;

                    this.OnBattleComplete(this._charId, battle);
                }
            }

            BattleScheduler.ResultDic[this._site.CurrentCharId] = battleList;
        }
        private void UpdateCharDynamic(BattleResult battle, CharacterDynamic cd, string mapId, bool addExp = true)
        {
            cd.BattleTotalWait += battle.Wait;

            if (this._isDungeon)
            {
                cd.BattleDungeonTotalCount++;
                cd.BattleDungeonLevelTotalCount++;
                cd.BattleDungeonLevelTotalWait += battle.Wait;
            }
            else
            {
                cd.BattleTotalCount++;
                cd.BattleLevelTotalCount++;
                cd.BattleLevelTotalWait += battle.Wait;
            }

            if (addExp)
            {
                cd.BattleTotalExp += battle.DropExp;

                if (this.IsDungeon)
                {
                    cd.BattleDungeonLevelTotalExp += battle.DropExp;
                }
                else
                {
                    cd.BattleLevelTotalExp += battle.DropExp;
                }

                CharacterActivityCache.ClearCache(cd.CharId);

                if (battle.UpgradeLev == true)
                {
                    CharacterCache.ClearCache(cd.CharId);
                }
            }

            if (battle.Success == true)
            {
                if (this._isDungeon)
                {
                    cd.BattleDungeonWinCount++;
                    cd.BattleDungeonLevelWinCount++;
                }
                else
                {
                    cd.BattleWinCount++;
                    cd.BattleLevelWinCount++;
                }
            }

            if (battle.GameItemsList != null)
            {
                var allMaps = this._site.InitAllMaps();
                if (!string.IsNullOrEmpty(mapId))
                {
                    Map map = allMaps.FirstOrDefault(p => p.MapId == mapId);

                    foreach (var item in battle.GameItemsList)
                    {
                        MapItem mi = MapItem.FromItem(item);
                        cd.AppendItem(mapId, mi);

                        if (map != null)//&& !map.ItemsVoList.ContainsKey(mi.ItemId))
                        {
                            map.ItemsVoList[mi.ItemId] = mi;
                        }
                    }
                }
            }

            CharacterDynamicCache.SaveDynamic(cd);
        }
        private void GenerateBattleTurn(BattleResult battle, BattleTurn turn, bool isDirect = true)
        {
            ICharacter attacker = FindBattleChar(battle, turn.Attacker);
            ICharacter target   = FindBattleChar(battle, turn.Hinjured);

            if (target != null)
            {
                turn.HpList = new List <BattleTurnHp>()
                {
                    new BattleTurnHp()
                    {
                        id  = target.Id,
                        hp  = target.Life,
                        chp = turn.SurplusHealth,
                    }
                };

                if (battle.LifeAfter.ContainsKey(target.Id))
                {
                    battle.LifeAfter[target.Id] = turn.SurplusHealth;
                }
            }
            else
            {
                turn.HpList = new List <BattleTurnHp>();
            }

            if (attacker != null)
            {
                if (!battle.Statistics.ContainsKey(attacker.Id))
                {
                    battle.Statistics.Add(attacker.Id, new BattleStatisticsData()
                    {
                        Id = attacker.Id, Name = attacker.Name
                    });
                }

                switch (turn.HurtType)
                {
                case BattleHurtType.PhysicalDamage:
                    battle.Statistics[attacker.Id].PhysicalDamage += ConvertDamage(turn.Hurt);
                    break;

                case BattleHurtType.MagicalDamage:
                    battle.Statistics[attacker.Id].MagicalDamage += ConvertDamage(turn.Hurt);
                    break;

                case BattleHurtType.DotDamage:
                    battle.Statistics[attacker.Id].DotDamage += ConvertDamage(turn.Hurt);
                    break;

                case BattleHurtType.CounterDamage:
                    battle.Statistics[attacker.Id].CounterDamage += ConvertDamage(turn.Hurt);
                    break;

                case BattleHurtType.Heal:
                    battle.Statistics[attacker.Id].Heal += ConvertDamage(turn.Hurt);
                    break;

                case null:
                    battle.Statistics[attacker.Id].NormalDamage += ConvertDamage(turn.Hurt);
                    break;

                default: break;
                }
            }
        }