/// <summary>
        /// 使用英雄技能
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public APIResultBase CastHeroPower(string gameCode, string userCode, int target = -1)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode || player.RemainingHeroPowerCastCount < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            BaseHero hero = ctl.GameContext.GetHeroByActivation();

            if (player.Power < (hero.CardAbility as IHeroAbility).Cost)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.没有足够的法力值));
            }

            ctl.CastHeroPower(hero, target);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 装备一件装备
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="cardInGameCode"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public APIResultBase LoadEquip(string gameCode, string userCode, string cardInGameCode)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            if (ctl.GameContext.AllCard.Any(c => c != null && c.CardInGameCode == cardInGameCode && c.CardType == CardType.装备) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            BaseHero hero = ctl.GameContext.GetHeroByActivation(player.IsActivation);
            Card     card = player.HandCards.First(c => c.CardInGameCode == cardInGameCode);

            if (player.Power < card.Cost)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.没有足够的法力值));
            }
            ctl.LoadEquip(hero, card as BaseEquip);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 随从发起攻击
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="cardInGameCode"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public APIResultBase ServantAttack(string gameCode, string userCode, string cardInGameCode, int target)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            if (ctl.GameContext.DeskCards.Any(c => c != null && c.CardInGameCode == cardInGameCode) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            if (player.IsFirst && target < 8)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            else if (player.IsFirst == false && target > 7)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }

            BaseServant servant = ctl.GameContext.DeskCards.First(c => c != null && c.CardInGameCode == cardInGameCode) as BaseServant;

            if (servant.RemainAttackTimes < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            if (ctl.GameContext.DeskCards[target].HasTaunt == false)
            {
                List <BaseBiology> taunts = ctl.GameContext.DeskCards.GetDeskCardsByIsFirst(player.IsFirst ? false : true).Where(c => c != null && c.HasTaunt).ToList();
                for (int i = 0; i < taunts.Count; i++)
                {
                    if (taunts[i].HasTaunt && i != target)
                    {
                        return(JsonModelResult.PackageFail(OperateResCodeEnum.你必须先攻击有嘲讽技能的随从));
                    }
                }
            }
            ctl.ServantAttack(servant, target);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        private Controler_Base Validate(string gameCode, string userCode)
        {
            Controler_Base ctl     = new Controler_Base(_gameCache);
            var            context = _gameCache.GetContext(gameCode);

            if (context == null || !context.Players.Any(x => x.Player.UserCode == userCode))
            {
                return(null);
            }
            ctl.GameContext           = context;
            ctl.GameContext.GameCache = _gameCache;
            return(ctl);
        }
        /// <summary>
        /// 创建一局游戏
        /// </summary>
        /// <param name="firstPlayerCode">先手玩家</param>
        /// <param name="secondPlayerCode">后手玩家</param>
        /// <param name="fristCardGroupCode">先手玩家卡组</param>
        /// <param name="secondCardGroupCode">后手玩家卡组</param>
        /// <returns>游戏ID</returns>
        public APIResultBase CreateGame(string tableCode, PlayerModel firstUser, PlayerModel secondUser, List <UserCardGroupDetailModel> firstCardGroup,
                                        List <UserCardGroupDetailModel> secondCardGroup, GameModel game, string firstUserProfession, string secondUserProfession)
        {
            //if (firstPlayerCode.IsNullOrEmpty() || secondPlayerCode.IsNullOrEmpty() || fristCardGroupCode.IsNullOrEmpty() || secondCardGroupCode.IsNullOrEmpty())
            //{
            //    return JsonModelResult.PackageFail(OperateResCodeEnum.参数错误);
            //}

            //CUsers firstUser = UsersBll.Instance.GetUser(firstPlayerCode);
            if (firstUser == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }

            //CUsers secondUser = UsersBll.Instance.GetUser(secondPlayerCode);
            if (secondUser == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            //var lstCtl = ControllerCache.Lstctl;
            //if (lstCtl.Any(c => c.chessboard.Players.First(x => x.IsFirst).User.UserCode == firstPlayerCode ||
            //c.chessboard.Players.First(x => x.IsFirst == false).User.UserCode == secondPlayerCode))
            //{
            //    res = OperateJsonRes.Error(OperateResCodeEnum.无法多开游戏);
            //    return res;
            //}

            //List<HS_UserCardGroupDetail> firstCardGroup = UserCardGroupDetailBll.Instance.GetCardGroupDetail(fristCardGroupCode, firstPlayerCode);
            if (firstCardGroup == null || firstCardGroup.Count < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }

            //List<HS_UserCardGroupDetail> secondCardGroup = UserCardGroupDetailBll.Instance.GetCardGroupDetail(secondCardGroupCode, secondPlayerCode);
            if (secondCardGroup == null || secondCardGroup.Count < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }

            //GameContextCache.Init();
            //var game = GameBll.Instance.CreateGame(tableCode, firstPlayerCode, secondPlayerCode, fristCardGroupCode, secondCardGroupCode);

            //string firstUserProfession = UserCardGroupBll.Instance.GetCardGroup(fristCardGroupCode, firstPlayerCode).Profession;
            //string secondUserProfession = UserCardGroupBll.Instance.GetCardGroup(secondCardGroupCode, secondPlayerCode).Profession;
            Controler_Base ctl = new Controler_Base(_gameCache);

            ctl.GameStart(game, firstUser, secondUser, firstCardGroup, secondCardGroup, firstUserProfession, secondUserProfession);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 将一名随从从手牌中移到场上
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="cardInGameCode"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public APIResultBase CastServant(string gameCode, string userCode, string cardInGameCode, int location, int target)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (player.HandCards.Any(c => c.CardInGameCode == cardInGameCode) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (location == 0 || location == 8)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            //if (ctl.GameContext.DeskCards[location] != null)
            //{
            //    return JsonModelResult.PackageFail(OperateResCodeEnum.位置已被占用);
            //}
            if (player.IsFirst && (location == 0 || location > 7))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            else if (player.IsFirst == false && (location == 8 || location < 8))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            Card card = player.HandCards.First(c => c.CardInGameCode == cardInGameCode);

            if (player.Power < card.Cost)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.没有足够的法力值));
            }

            ctl.CastServant((BaseServant)card, location, target);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 英雄发起攻击
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="cardInGameCode"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public APIResultBase HeroAttack(string gameCode, string userCode, int target)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (player.IsFirst && target < 8)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            else if (player.IsFirst == false && target > 7)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }

            BaseHero hero = ctl.GameContext.GetHeroByActivation(player.IsActivation);

            if (hero.RemainAttackTimes < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            List <BaseBiology> taunts = ctl.GameContext.DeskCards.GetDeskCardsByIsFirst(player.IsFirst ? false : true).Where(c => c != null && c.HasTaunt).ToList();

            if (taunts != null && taunts.Count > 0 && taunts.Any(c => c.DeskIndex == target) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.你必须先攻击有嘲讽技能的随从));
            }

            ctl.HeroAttack(hero, target);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 打出一张法术牌
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="cardInGameCode"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public APIResultBase CastSpell(string gameCode, string userCode, string cardInGameCode, int target)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (player.HandCards.Any(c => c.CardInGameCode == cardInGameCode) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            Card card = player.HandCards.First(c => c.CardInGameCode == cardInGameCode);

            if (player.Power < card.Cost)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.没有足够的法力值));
            }
            if (card.CastCardPrecondition == CastCardPrecondition.健康 && target > -1)
            {
                BaseBiology biology = ctl.GameContext.DeskCards[target];
                if (biology.Life != biology.BuffLife)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.错误的目标));
                }
            }
            if (card.CastCardPrecondition == CastCardPrecondition.装备有武器 && ctl.GameContext.GetHeroByActivation().Equip == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.你无法施放这个技能));
            }
            ctl.CastSpell((BaseSpell)card, target);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 回合结束
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public APIResultBase TurnStart(string gameCode, string userCode)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            if (VictoryValidate(ctl.GameContext))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经结束));
            }
            var player = ctl.GameContext.GetActivationUserContext();

            if (player == null || player.UserCode != userCode)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }

            ctl.TurnStart();
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output()));
        }
        /// <summary>
        /// 开场换牌
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="lstInitCardIndex"></param>
        /// <returns></returns>
        public APIResultBase SwitchCard(string gameCode, string userCode, List <string> lstInitCardIndex, IShortCodeService shortCodeService)
        {
            string         res = JsonStringResult.VerifyFail();
            Controler_Base ctl = Validate(gameCode, userCode);

            if (ctl == null)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据));
            }
            //if (ctl.TurnIndex != 2)
            //{
            //    return JsonModelResult.PackageFail(OperateResCodeEnum.查询不到需要的数据);
            //}
            if (ctl.GameContext.Players.Any(c => c.UserCode == userCode && c.SwitchDone == false) == false)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            List <int> initCardIndex = new List <int>();

            foreach (string idx in lstInitCardIndex)
            {
                initCardIndex.Add(idx.TryParseInt());
            }
            if (initCardIndex.Any(c => c < 0 || c > 3) || initCardIndex.Any(c => c >= ctl.GameContext.Players.First(x => x.UserCode == userCode).InitCards.Count()))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            var idxGroup = initCardIndex.GroupBy(c => c);

            if (idxGroup.Any(c => c.Count() > 1))
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            ctl.SwitchCard(userCode, initCardIndex, shortCodeService);
            return(JsonModelResult.PackageSuccess(_gameCache.GetContext(ctl.GameContext.GameCode).Output().Players.First(c => c.UserCode == userCode)));
        }