Example #1
0
        /// <summary>
        /// 用户下注
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="betMoney"></param>
        /// <param name="tableID"></param>
        /// <returns>所有人是否已下注</returns>
        public APIResultBase Bet(string userCode, decimal betMoney, string tableCode)
        {
            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                var gameTable = context.hs_gametable.FirstOrDefault(c => c.TableCode == tableCode);
                if (gameTable.TableStatus != (int)TableStatusEnum.正常)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }
                var game   = context.ff_game.Where(c => c.GameCode == gameTable.TableCode && c.GameStatus == (int)GameStatusEnum.初始化).OrderByDescending(c => c.AddTime).FirstOrDefault();
                var record = context.ff_gamerecord.FirstOrDefault(c => c.RoundCode == game.CurrentRoundCode && c.UserCode == userCode);
                record.BetMoney     = betMoney;
                record.PlayerStatus = (int)PlayerStatusEnum.已下注;
                context.SaveChanges();


                var isNotAllBeted = context.ff_gamerecord.Any(c => c.RoundCode == game.CurrentRoundCode && c.PlayerStatus != (int)PlayerStatusEnum.已下注);
                if (isNotAllBeted == false)
                {
                    //所有人已下注
                    game.GameStatus = (int)GameStatusEnum.已开始;
                    context.SaveChanges();
                    return(JsonModelResult.PackageSuccess(record));
                }
            }
            return(JsonModelResult.PackageFail(OperateResCodeEnum.无));
        }
Example #2
0
        public APIResultBase AddOrUpdate(HS_GameTable gameTable, GameStyleEnum GameStyle)
        {
            if (gameTable.CreateUserCode.IsNullOrEmpty() || gameTable.TableName.IsNullOrEmpty())
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            if (_repository.Get(c => (c.CreateUserCode == gameTable.CreateUserCode || c.PlayerUserCode == gameTable.CreateUserCode) && gameTable.ID < 1 && c.TableStatus == (int)TableStatusEnum.正常).Result.TotalItemsCount > 0)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.时只能创建或占用一个游戏房间));
            }
            if (gameTable.ID > 0)
            {
                //gameTable.AddTime = _repository.GetByKey(gameTable.ID).Result.AddTime;
                _repository.Update(gameTable);
                return(JsonModelResult.PackageSuccess(gameTable.ID.ToString()));
            }
            else
            {
                DateTime now = DateTime.Now;
                gameTable.PlayerUserCode = "";
                gameTable.TableCode      = SignUtil.CreateSign(UsersBll.Instance.GetUserByAdmin(gameTable.CreateUserCode).SecretCode + RandomUtil.CreateRandomStr(8) + now.Ticks);
                gameTable.AddTime        = DateTime.Now;
                using (FriendshipFirstContext context = new FriendshipFirstContext())
                {
                    gameTable.BankerCode = gameTable.CreateUserCode;
                    context.hs_gametable.Add(gameTable);
                    FF_Game game = new FF_Game();
                    game.AddTime          = now;
                    game.BankerCode       = gameTable.CreateUserCode;
                    game.GameStatus       = GameStyle == GameStyleEnum.庄家模式 ? (int)GameStatusEnum.初始化 : (int)GameStatusEnum.已开始;
                    game.CurrentRoundCode = SignUtil.CreateSign(gameTable.CreateUserCode + RandomUtil.CreateRandomStr(8) + game.AddTime.Ticks);
                    game.NextRoundCode    = SignUtil.CreateSign(gameTable.CreateUserCode + RandomUtil.CreateRandomStr(8) + game.AddTime.AddMinutes(5).Ticks);
                    game.GameCode         = gameTable.TableCode;
                    game.GameStyle        = (int)GameStyle;
                    context.ff_game.Add(game);

                    FF_GameRecord record = new FF_GameRecord
                    {
                        AddTime      = now,
                        BetMoney     = 0,
                        IsBanker     = true,
                        PlayerStatus = (int)PlayerStatusEnum.已下注,
                        RoundCode    = game.CurrentRoundCode,
                        UserCode     = game.BankerCode,
                        WinMoney     = 0,
                        Balance      = 0,
                        GameCode     = game.GameCode,
                        IsActivity   = false,
                        RoomIndex    = 0
                    };
                    context.ff_gamerecord.Add(record);

                    context.SaveChanges();
                }
                return(JsonModelResult.PackageSuccess <HS_GameTable>(gameTable));
            }
        }
Example #3
0
        /// <summary>
        /// 用户离开房间
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="gameCode"></param>
        /// <returns>是否已经全部离开</returns>
        public bool LeavingRoom(string userCode, string gameCode)
        {
            bool all = false;

            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                var record = context.ff_gamerecord.Where(c => c.UserCode == userCode && c.GameCode == gameCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
                record.IsActivity = false;
                context.SaveChanges();
                all = context.ff_gamerecord.Any(c => c.RoundCode == record.RoundCode && c.IsActivity == true) == false;
            }
            return(all);
        }
Example #4
0
        /// <summary>
        /// 换庄
        /// </summary>
        /// <param name="gameCode"></param>
        /// <param name="userCode"></param>
        /// <param name="targetUserCode"></param>
        /// <returns></returns>
        public APIResultBase SwitchBanker(string gameCode, string userCode, string targetUserCode)
        {
            List <CGameUser> lstUsers = null;

            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                var game = context.ff_game.Where(c => c.GameCode == gameCode).FirstOrDefault();
                if (game.GameStatus != (int)GameStatusEnum.已结算 && game.GameStatus != (int)GameStatusEnum.初始化)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }

                var user       = context.ff_gamerecord.FirstOrDefault(c => c.UserCode == userCode && c.RoundCode == game.CurrentRoundCode);
                var targetUser = context.ff_gamerecord.FirstOrDefault(c => c.UserCode == targetUserCode && c.RoundCode == game.CurrentRoundCode);
                if (user.IsBanker == false && targetUser.IsBanker == false)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }
                if (user.IsBanker)
                {
                    user.IsBanker     = false;
                    user.PlayerStatus = (int)PlayerStatusEnum.未准备;

                    targetUser.IsBanker     = true;
                    targetUser.PlayerStatus = (int)PlayerStatusEnum.已下注;
                    targetUser.BetMoney     = 0;
                }
                else
                {
                    user.IsBanker     = true;
                    user.PlayerStatus = (int)PlayerStatusEnum.已下注;
                    user.BetMoney     = 0;

                    targetUser.IsBanker     = false;
                    targetUser.PlayerStatus = (int)PlayerStatusEnum.未准备;
                    targetUser.BetMoney     = 0;
                }

                context.SaveChanges();

                lstUsers = GameRecordBll.Instance.GetUsers(game.GameCode, context);

                //lstUsers = data.Where(c => c.UserCode == userCode && c.GameCode == gameCode).OrderByDescending(c => c.AddTime).ToList();
            }
            return(JsonModelResult.PackageSuccess(lstUsers));
        }
Example #5
0
 /// <summary>
 /// 重新下注
 /// </summary>
 /// <param name="userCode"></param>
 /// <param name="tableCode"></param>
 /// <returns></returns>
 public APIResultBase CancelBet(string userCode, string tableCode)
 {
     using (FriendshipFirstContext context = new FriendshipFirstContext())
     {
         var gameTable = context.hs_gametable.FirstOrDefault(c => c.TableCode == tableCode);
         if (gameTable.TableStatus != (int)TableStatusEnum.正常)
         {
             return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
         }
         var game = context.ff_game.Where(c => c.GameCode == tableCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
         if (game.GameStatus != (int)GameStatusEnum.初始化)
         {
             return(JsonModelResult.PackageFail(OperateResCodeEnum.当前游戏无法重新下注));
         }
         var record = context.ff_gamerecord.FirstOrDefault(c => c.RoundCode == game.CurrentRoundCode && c.UserCode == userCode);
         record.BetMoney     = 0;
         record.PlayerStatus = (int)PlayerStatusEnum.未准备;
         context.SaveChanges();
     }
     return(JsonModelResult.PackageSuccess());
 }
Example #6
0
        public APIResultBase GameRestart(string userCode, string tableCode)
        {
            List <CGameUser> lstRec = null;

            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                var gameTable = context.hs_gametable.FirstOrDefault(c => c.TableCode == tableCode);
                if (gameTable.TableStatus != (int)TableStatusEnum.正常)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }
                var game = context.ff_game.Where(c => c.GameCode == tableCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
                if (game.GameStatus != (int)GameStatusEnum.已结算)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }
                game.GameStatus = (int)GameStatusEnum.初始化;

                lstRec = GameRecordBll.Instance.GetUsers(game.GameCode, context);
                context.SaveChanges();
            }
            return(JsonModelResult.PackageSuccess(lstRec));
        }
Example #7
0
        /// <summary>
        /// 占座儿
        /// </summary>
        /// <param name="gameTableID"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public APIResultBase ZhanZuoEr(string tableCode, string userCode, string password)
        {
            if (tableCode.IsNullOrEmpty() || userCode.IsNullOrEmpty())
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            var lstTables = _repository.GetList(c => c.TableCode == tableCode && c.Password == password).Result;

            if (lstTables.TotalItemsCount < 1)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
            }
            var gameTable = lstTables.Items.First();

            //if (gameTable.TableStatus != (int)TableStatusEnum.正常)
            //{
            //    return JsonModelResult.PackageFail(OperateResCodeEnum.参数错误);
            //}


            if (_repository.Get(c => (c.PlayerUserCode == userCode || c.CreateUserCode == userCode) && c.TableCode != tableCode && c.TableStatus == (int)TableStatusEnum.正常).Result.TotalItemsCount > 0)
            {
                return(JsonModelResult.PackageFail(OperateResCodeEnum.时只能创建或占用一个游戏房间));
            }
            CGameUser recordRes = null;

            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                var game = GameBll.Instance.GetGame(gameTable.TableCode);

                if (game != null)
                {
                    var data = GameRecordBll.Instance.GetUsers(game.GameCode, context);

                    recordRes = data.FirstOrDefault(c => c.RoundCode == game.CurrentRoundCode && c.UserCode == userCode);

                    if (game.GameStatus == (int)GameStatusEnum.结算中)
                    {
                        //if (recordRes != null)
                        //{
                        //    return JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经开始);
                        //}
                        if (recordRes == null)
                        {
                            return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经开始));
                        }
                        if (recordRes.PlayerStatus != (int)PlayerStatusEnum.已下注)
                        {
                            return(JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经开始));
                        }
                    }

                    if (recordRes == null)
                    {
                        context.ff_gamerecord.Add(new FF_GameRecord
                        {
                            AddTime      = DateTime.Now,
                            BetMoney     = 0,
                            IsBanker     = false,
                            PlayerStatus = game.GameStyle == (int)GameStyleEnum.自由模式 ? (int)PlayerStatusEnum.已下注 : (int)PlayerStatusEnum.未准备,
                            RoundCode    = game.CurrentRoundCode,
                            UserCode     = userCode,
                            WinMoney     = 0,
                            Balance      = 0,
                            GameCode     = game.GameCode,
                            IsActivity   = true,
                            RoomIndex    = data.Count(c => c.RoundCode == game.CurrentRoundCode)
                        });
                    }
                    else
                    {
                        if (!recordRes.IsActivity)
                        {
                            recordRes.IsActivity = true;
                        }
                    }
                    context.SaveChanges();
                    if (recordRes == null)
                    {
                        recordRes = context.ff_gamerecord.Join(context.ff_user, g => g.UserCode, u => u.UserCode, (g, u) => new CGameUser
                        {
                            UserName     = u.UserName,
                            NickName     = u.NickName,
                            HeadImg      = u.HeadImg,
                            OpenID       = u.OpenID,
                            UserCode     = u.UserCode,
                            Balance      = g.Balance,
                            BetMoney     = g.BetMoney,
                            IsBanker     = g.IsBanker,
                            PlayerStatus = g.PlayerStatus,
                            RoundCode    = g.RoundCode,
                            WinMoney     = g.WinMoney,
                            GameCode     = g.GameCode,
                            AddTime      = g.AddTime,
                            RoomIndex    = g.RoomIndex
                        }).Where(c => c.UserCode == userCode && c.GameCode == game.GameCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
                    }
                }
                //var game = GameBll.Instance.GetGame(gameTable.TableCode);
                //FF_GameRecord record = null;
                //if (game != null)
                //{
                //    var recordRes = GameRecordBll.Instance.GetRecord(game.CurrentRoundCode, userCode);

                //    if (game.GameStatus == (int)GameStatusEnum.结算中)
                //    {
                //        if (recordRes.code == (int)OperateResCodeEnum.查询不到需要的数据)
                //        {
                //            return JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经开始);
                //        }
                //        var tempRec = ((APISingleModelResult<FF_GameRecord>)recordRes).data;
                //        if (tempRec.PlayerStatus != (int)PlayerStatusEnum.已下注)
                //        {
                //            return JsonModelResult.PackageFail(OperateResCodeEnum.游戏已经开始);
                //        }
                //    }
                //    if (recordRes.code == (int)OperateResCodeEnum.查询不到需要的数据)
                //    {
                //        record = new FF_GameRecord
                //        {
                //            AddTime = DateTime.Now,
                //            BetMoney = 0,
                //            IsBanker = false,
                //            PlayerStatus = (int)PlayerStatusEnum.未准备,
                //            RoundCode = game.CurrentRoundCode,
                //            UserCode = userCode,
                //            WinMoney = 0,
                //            Balance = 0,
                //            GameCode = game.GameCode,
                //            IsActivity = true
                //        };
                //        GameRecordBll.Instance.Insert(record);
                //    }
                //    else
                //    {
                //        record = ((APISingleModelResult<FF_GameRecord>)recordRes).data;
                //        if (!record.IsActivity)
                //        {
                //            record.IsActivity = true;
                //            GameRecordBll.Instance.Update(record);
                //        }
                //    }
                //}
                return(JsonModelResult.PackageSuccess(recordRes));
            }
        }
Example #8
0
        /// <summary>
        /// 结算
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="targetUserCode"></param>
        /// <param name="gameCode"></param>
        /// <param name="money"></param>
        /// <returns></returns>
        public APIResultBase Settlement(string userCode, string targetUserCode, string gameCode, decimal money)
        {
            FF_Game   game     = null;
            CGameUser gameUser = null;

            using (FriendshipFirstContext context = new FriendshipFirstContext())
            {
                game = context.ff_game.Where(c => c.GameCode == gameCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
                if (game.GameStatus != (int)GameStatusEnum.已开始 && game.GameStatus != (int)GameStatusEnum.结算中)
                {
                    return(JsonModelResult.PackageFail(OperateResCodeEnum.参数错误));
                }
                game.GameStatus = (int)GameStatusEnum.结算中;
                var lstRec       = context.ff_gamerecord.Where(c => c.RoundCode == game.CurrentRoundCode);
                var record       = lstRec.FirstOrDefault(c => c.RoundCode == game.CurrentRoundCode && c.UserCode == userCode);
                var targetRecord = lstRec.FirstOrDefault(c => c.RoundCode == game.CurrentRoundCode && c.UserCode == targetUserCode);
                record.BetMoney  = 0;
                record.Balance  += money;
                record.WinMoney += money;

                bool isAllSettlemented = false;
                if (game.GameStyle == (int)GameStyleEnum.庄家模式)
                {
                    if (record.IsBanker == false)
                    {
                        record.PlayerStatus = (int)PlayerStatusEnum.已结算;
                    }
                    else if (lstRec.Count(c => c.IsBanker == false && c.PlayerStatus != (int)PlayerStatusEnum.已结算) <= 1)
                    {
                        record.PlayerStatus = (int)PlayerStatusEnum.已结算;
                    }

                    targetRecord.Balance  -= money;
                    targetRecord.WinMoney -= money;
                    if (targetRecord.IsBanker == false)
                    {
                        targetRecord.PlayerStatus = (int)PlayerStatusEnum.已结算;
                    }
                    else if (lstRec.Count(c => c.IsBanker == false && c.PlayerStatus != (int)PlayerStatusEnum.已结算) <= 1)
                    {
                        targetRecord.PlayerStatus = (int)PlayerStatusEnum.已结算;
                    }
                    isAllSettlemented = (record.PlayerStatus == (int)PlayerStatusEnum.已结算 && targetRecord.PlayerStatus == (int)PlayerStatusEnum.已结算);
                }
                else
                {
                    record.PlayerStatus = (int)PlayerStatusEnum.已结算;

                    targetRecord.Balance     -= money;
                    targetRecord.WinMoney    -= money;
                    targetRecord.PlayerStatus = (int)PlayerStatusEnum.已结算;

                    isAllSettlemented = !lstRec.Any(c => c.PlayerStatus != (int)PlayerStatusEnum.已结算 && c.UserCode != userCode && c.UserCode != targetUserCode);
                }

                if (isAllSettlemented)
                {
                    DateTime now = DateTime.Now;
                    var      lst = lstRec.ToList();
                    game.CurrentRoundCode = game.NextRoundCode;
                    game.NextRoundCode    = SignUtil.CreateSign(game.BankerCode + RandomUtil.CreateRandomStr(8) + now.Ticks);

                    if (game.GameStyle == (int)GameStyleEnum.庄家模式)
                    {
                        game.GameStatus = (int)GameStatusEnum.已结算;
                        AddUserToNextBankerRound(lst, game, now, context);
                    }
                    else
                    {
                        game.GameStatus = (int)GameStatusEnum.已开始;
                        AddUserToNextFreeModelRound(lst, game, now, context);
                    }
                }
                context.SaveChanges();
                gameUser = GameRecordBll.Instance.GetUser(userCode, game.GameCode, context);

                //gameUser = data.Where(c => c.UserCode == userCode && c.GameCode == gameCode).OrderByDescending(c => c.AddTime).FirstOrDefault();
            }
            return(JsonModelResult.PackageSuccess(gameUser));
        }