Пример #1
0
        public bool BetCheck(SqlTransaction trans = null)
        {
            //check close time
            var item = CasinoItem.GetCasinoItem(CasinoItemGuid);

            if (item == null)
            {
                return(false);
            }

            if (DateTime.Now > item.CloseTime)
            {
                return(false);
            }

            if (BetAmount.HasValue && BetAmount <= 0)
            {
                return(false);
            }

            //check user account
            if (BetAmount.HasValue)
            {
                var gamber = new Gambler(UserID, trans);
                if (gamber.Cash < BetAmount.Value)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        public void ReturnBet()
        {
            using (var conn = SQLConn.GetConnection())
            {
                conn.Open();
                var trans = conn.BeginTransaction();
                try
                {
                    var casinoItemGuid = DataAccess.CasinoItem.GetCasinoItemGuidByMatch(MatchGuid,
                                                                                        (int)CasinoType.SingleChoice, trans);
                    if (casinoItemGuid.HasValue)
                    {
                        var item   = CasinoItem.GetCasinoItem(casinoItemGuid.Value);
                        var banker = new Banker(item.BankerID);

                        var dtMatchBet = DataAccess.Bet.GetMatchAllBet(MatchGuid);

                        var totalBet = 0f;

                        foreach (DataRow dr in dtMatchBet.Rows)
                        {
                            if (!Convert.IsDBNull(dr["Bet"]))
                            {
                                var gambler = new Gambler(Convert.ToInt32(dr["UserID"]), trans);
                                gambler.Cash     += Convert.ToSingle(dr["Bet"]);
                                gambler.TotalBet -= Convert.ToSingle(dr["Bet"]);
                                gambler.Update(trans);

                                totalBet += Convert.ToSingle(dr["Bet"]);
                            }
                        }

                        banker.Cash -= totalBet;
                        banker.Update(trans);

                        DataAccess.Bet.DeleteBetByMatchGuid(MatchGuid, trans);
                        DataAccess.BetDetail.CleanBetDetail(trans);

                        trans.Commit();
                    }
                }
                catch
                {
                    trans.Rollback();
                }

                //conn.Close();
            }
        }
Пример #3
0
        public void Insert(string optionValue)
        {
            using (var conn = SQLConn.GetConnection())
            {
                conn.Open();
                var trans = conn.BeginTransaction();
                try
                {
                    if (BetCheck(trans))
                    {
                        //update gambler statistics
                        var gambler = new Gambler(UserID, trans);
                        gambler.TotalBet += BetAmount.GetValueOrDefault(0f);

                        if (BetAmount.HasValue)
                        {
                            gambler.Cash -= BetAmount.GetValueOrDefault(0f);
                        }
                        gambler.Update(trans);

                        var banker = new Banker(CasinoItem.GetCasinoItem(CasinoItemGuid).BankerID);
                        banker.Cash += BetAmount.GetValueOrDefault(0f);
                        banker.Update(trans);

                        var key = DataAccess.Bet.InsertBet(UserID, UserName, CasinoItemGuid, BetAmount, BetRate, trans);
                        MatchChoiceOption.SaveMatchChoiceOption(key, optionValue, trans);

                        trans.Commit();
                    }
                    else
                    {
                        throw new Exception("Failed to create bet (SingleChoice).");
                    }
                }
                catch
                {
                    trans.Rollback();
                }

                //conn.Close();
            }
        }
Пример #4
0
        //public static void CleanNoCasinoItemBet()
        //{
        //    using (var conn = SQLConn.GetConnection())
        //    {
        //        conn.Open();
        //        var trans = conn.BeginTransaction();
        //        try
        //        {
        //            DataAccess.Bet.CleanBet(trans);
        //            DataAccess.BetDetail.CleanBetDetail(trans);
        //            trans.Commit();
        //        }
        //        catch
        //        {
        //            trans.Rollback();
        //        }

        //        //conn.Close();
        //    }
        //}

        public static void ReturnBet(int key)
        {
            using (var conn = SQLConn.GetConnection())
            {
                conn.Open();
                var trans = conn.BeginTransaction();
                try
                {
                    var bet = new Bet(key);

                    if (bet.BetAmount.HasValue && bet.BetAmount >= 0f)
                    {
                        var betAmount = Convert.ToSingle(bet.BetAmount);

                        var banker  = new Banker(CasinoItem.GetCasinoItem(bet.CasinoItemGuid).BankerID);
                        var gambler = new Gambler(bet.UserID, trans);

                        gambler.Cash     += betAmount;
                        gambler.TotalBet -= betAmount;
                        banker.Cash      -= betAmount;

                        gambler.Update(trans);
                        banker.Update(trans);
                    }
                    else if (!bet.BetAmount.HasValue && !bet.Earning.HasValue && bet.EarningDesc == "RP+1")
                    {
                        Users.UpdateUserExtCredits(bet.UserID, 4, -1);
                    }

                    DataAccess.Bet.DeleteBetById(key, trans);
                    DataAccess.BetDetail.CleanBetDetail(trans);
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }
Пример #5
0
        public void CalcBonus()
        {
            if (string.IsNullOrEmpty(ResultHome.ToString()) || string.IsNullOrEmpty(ResultAway.ToString()))
            {
                throw new Exception("You can not calc bonus without a match result");
            }

            using (var conn = SQLConn.GetConnection())
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    var itemGuid = DataAccess.CasinoItem.GetCasinoItemGuidByMatch(MatchGuid,
                                                                                  (int)CasinoType.SingleChoice, trans);

                    if (itemGuid.HasValue)
                    {
                        //single choice bonus
                        var item   = CasinoItem.GetCasinoItem(itemGuid.Value);
                        var banker = new Banker(item.BankerID);

                        var totalEarning = 0f;

                        var betList = Bet.GetBetByCasinoItemGuid(itemGuid.Value, trans);

                        foreach (var bet in betList)
                        {
                            var dt = DataAccess.BetDetail.GetBetDetailByBetId(bet.ID);

                            if (dt != null)
                            {
                                var gambler = new Gambler(bet.UserID, trans);

                                if (bet.IsWin == null)
                                {
                                    var isWin = false;

                                    var dr = dt.Rows[0];

                                    if (dr["DetailName"].ToString() == MatchChoiceOption.HomeWinValue &&
                                        ResultHome > ResultAway)
                                    {
                                        isWin = true;
                                    }
                                    else if (dr["DetailName"].ToString() == MatchChoiceOption.DrawValue &&
                                             ResultHome == ResultAway)
                                    {
                                        isWin = true;
                                    }
                                    else if (dr["DetailName"].ToString() == MatchChoiceOption.AwayWinValue &&
                                             ResultHome < ResultAway)
                                    {
                                        isWin = true;
                                    }

                                    bet.IsWin = isWin;

                                    if (bet.BetAmount.HasValue)
                                    {
                                        totalEarning += bet.BetAmount.Value;

                                        if (isWin)
                                        {
                                            bet.Earning = bet.BetAmount * bet.BetRate;

                                            if (bet.Earning != null)
                                            {
                                                bet.EarningDesc = $"{bet.Earning.Value:N2}";

                                                totalEarning -= bet.Earning.Value;

                                                //add gambler cash

                                                gambler.Cash += bet.Earning.Value;
                                                gambler.Win++;

                                                banker.Cash -= bet.Earning.Value;
                                            }
                                        }
                                        else
                                        {
                                            gambler.Lose++;

                                            bet.Earning     = 0;
                                            bet.EarningDesc = string.Empty;
                                        }
                                    }
                                }

                                bet.Update(trans);
                                gambler.Update(trans);
                            }
                        }

                        banker.Update(trans);

                        item.Earning = totalEarning;
                        item.Save(trans);
                    }

                    itemGuid = DataAccess.CasinoItem.GetCasinoItemGuidByMatch(MatchGuid, (int)CasinoType.MatchResult,
                                                                              trans);

                    if (itemGuid.HasValue)
                    {
                        //match result bonus
                        var betList = Bet.GetBetByCasinoItemGuid(itemGuid.Value, trans);

                        var item = CasinoItem.GetCasinoItem(itemGuid.Value);
                        item.Earning = 0;
                        item.Save(trans);

                        foreach (var bet in betList)
                        {
                            var gambler = new Gambler(bet.UserID, trans);

                            var dt = DataAccess.BetDetail.GetBetDetailByBetId(bet.ID);

                            var betDetail = new MatchResultBetDetail(dt);

                            if (bet.IsWin == null)
                            {
                                if (betDetail.Home == ResultHome && betDetail.Away == ResultAway)
                                {
                                    //win
                                    bet.IsWin       = true;
                                    bet.Earning     = 0;
                                    bet.EarningDesc = "RP+1";

                                    gambler.Win++;

                                    //update user rp

                                    Users.UpdateUserExtCredits(bet.UserID, 4, 1);
                                }
                                else
                                {
                                    //lose
                                    bet.IsWin       = false;
                                    bet.Earning     = 0;
                                    bet.EarningDesc = string.Empty;

                                    gambler.Lose++;
                                }
                            }

                            bet.Update(trans);
                            gambler.Update(trans);
                        }
                    }

                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }
Пример #6
0
        public void Insert(int userId, string username, float winRate, float drawRate, float loseRate)
        {
            using (var conn = SQLConn.GetConnection())
            {
                conn.Open();
                var trans = conn.BeginTransaction();
                try
                {
                    DataAccess.Match.InsertMatch(MatchGuid, Home, Away, PlayTime, LeagueGuid, LeagueName, Round,
                                                 GroupGuid, trans);

                    //add matchResult
                    var itemMatchResult = (MatchResult)CasinoItem.CreateInstance(CasinoType.MatchResult);
                    itemMatchResult.MatchGuid     = MatchGuid;
                    itemMatchResult.CreateTime    = DateTime.Now;
                    itemMatchResult.PublishTime   = DateTime.Now;
                    itemMatchResult.CloseTime     = PlayTime;
                    itemMatchResult.BankerID      = Banker.DefaultBankerID;
                    itemMatchResult.BankerName    = new Banker(Banker.DefaultBankerID).BankerName;
                    itemMatchResult.Earning       = null;
                    itemMatchResult.OwnerID       = userId;
                    itemMatchResult.OwnerUserName = username;

                    itemMatchResult.Save(trans);

                    //add singleChoice
                    var itemSingleChoice = (SingleChoice)CasinoItem.CreateInstance(CasinoType.SingleChoice);
                    itemSingleChoice.MatchGuid     = MatchGuid;
                    itemSingleChoice.CreateTime    = DateTime.Now;
                    itemSingleChoice.PublishTime   = DateTime.Now;
                    itemSingleChoice.CloseTime     = PlayTime;
                    itemSingleChoice.BankerID      = Banker.DefaultBankerID;
                    itemSingleChoice.BankerName    = new Banker(Banker.DefaultBankerID).BankerName;
                    itemSingleChoice.Earning       = null;
                    itemSingleChoice.OwnerID       = userId;
                    itemSingleChoice.OwnerUserName = username;
                    itemSingleChoice.FloatingRate  = false;

                    itemSingleChoice.Options.Add(new ChoiceOption
                    {
                        OptionDisplay = "主队胜",
                        OptionValue   = MatchChoiceOption.HomeWinValue,
                        OptionRate    = winRate,
                        OrderID       = 1
                    });
                    itemSingleChoice.Options.Add(new ChoiceOption
                    {
                        OptionDisplay = "双方平",
                        OptionValue   = MatchChoiceOption.DrawValue,
                        OptionRate    = drawRate,
                        OrderID       = 2
                    });
                    itemSingleChoice.Options.Add(new ChoiceOption
                    {
                        OptionDisplay = "客队胜",
                        OptionValue   = MatchChoiceOption.AwayWinValue,
                        OptionRate    = loseRate,
                        OrderID       = 3
                    });

                    itemSingleChoice.Save(trans);

                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                }
            }
        }