/// <summary>
        /// 下注
        /// </summary>
        /// <param name="bet">下注資料</param>
        /// <returns></returns>
        public bool AddAssetsByBet(betDto bet)
        {
            Assets assDb   = db.Assets.Where(p => p.UserId == bet.userId && p.unitSn == bet.unitSn).FirstOrDefault();
            double fAssets = (assDb == null ? 0 : assDb.assets);

            if (assDb == null)
            {
                assDb = new Assets
                {
                    UserId = bet.userId,
                    unitSn = bet.unitSn.Value,
                    assets = fAssets + bet.money.Value * -1
                };
                db.Assets.Add(assDb);
            }
            else
            {
                assDb.assets += bet.money.Value * -1;
            }
            db.SaveChanges();

            AssetsRecord assr = new AssetsRecord
            {
                UserId   = bet.userId,
                unitSn   = bet.unitSn.Value,
                assets   = bet.money.Value * -1,
                gameSn   = bet.gameSn,
                topicSn  = bet.topicSn,
                choiceSn = bet.choiceSn,
                inpdate  = DateTime.Now,
                type     = -1
            };

            db.AssetsRecord.Add(assr);
            db.SaveChanges();


            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 寫入下注資料,並扣除錢幣
        /// </summary>
        /// <param name="bet"></param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task <bool> CreateBet(betDto bet)
        {
            try
            {
                bool isTrue = true;
                HttpResponseMessage response = await client.PostAsJsonAsync("api/bets", bet);

                if (response.IsSuccessStatusCode)
                {
                    new AssetsRepository().AddAssetsByBet(bet);
                }

                isTrue = addChoiceMoney(bet.choiceSn.Value, bet.money.Value);


                return(isTrue);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async System.Threading.Tasks.Task <ActionResult> Create(betViewModel bv)
        {
            aJaxDto ajd = new aJaxDto();

            try
            {
                if (User.Identity.GetUserId() == null)
                {
                    ajd.ErrorMsg  = "預測前須先登入\n請先至會員登入中心進行登入註冊,謝謝";
                    ajd.isTrue    = false;
                    ajd.ErrorCode = 2;
                    throw new Exception("error");
                }

                if (!ModelState.IsValid)
                {
                    ajd.ErrorMsg = "";
                    foreach (var errs in ModelState.Values.Where(p => p.Errors.Count > 0).Select(p => p.Errors).Distinct())
                    {
                        foreach (var e in errs)
                        {
                            ajd.ErrorMsg += e.ErrorMessage + "\n";
                        }
                        //ajd.ErrorMsg += "";
                    }

                    ajd.isTrue = false;
                    throw new Exception("error");
                }
                if (!User.Identity.GetEmailConfirmed() && !User.Identity.GetPhoneNumberConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成Email驗證以及手機驗證\n請先至會員中心進行Email驗證和手機驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }

                if (!User.Identity.GetEmailConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成Email驗證\n請先至會員中心進行Email驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                if (!User.Identity.GetPhoneNumberConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成手機驗證\n請先至會員中心進行手機驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }


                List <betDto>   bet_list      = new List <betDto>();
                GamesRepository _game         = new GamesRepository();
                int             firstChoiceSn = bv.betList.First().getChoiceSn(System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"]);
                gameDto         gd            = await _game.GetGameDetailByChoiceSn(firstChoiceSn);

                double Assets = new AssetsRepository().getAssetsByUserID(User.Identity.GetUserId(), 1);
                Session["Assets"] = new AssetsRepository().getAssetsListByUserID(User.Identity.GetUserId());
                //竄改
                int[] bvarray = new int[] { 2, 2, 2, 4, 5 };
                if (gd.betModel == 6)
                {
                    int    bj         = 0;
                    double moneyclcik = 0;

                    for (var bi = 0; bi < 5; bi++)
                    {
                        for (int i = bj; i < bj + bvarray[bi]; i++)
                        {
                            if (bv.betList[i].money != null)
                            {
                                Regex regex = new Regex(@"^\+?[0-9]{1,10}[0][0]$|^$");
                                Match match = regex.Match(bv.betList[i].money.ToString());
                                if (!match.Success)
                                {
                                    ajd.ErrorMsg = "請勿竄改前端資料";
                                    ajd.isTrue   = false;
                                    throw new Exception("error");
                                }
                                if ((double)bv.betList[i].money <= 0)
                                {
                                    ajd.ErrorMsg = "請勿竄改前端資料";
                                    ajd.isTrue   = false;
                                    throw new Exception("error");
                                }


                                if (moneyclcik == 0)
                                {
                                    moneyclcik = (double)bv.betList[i].money;
                                }
                                if (moneyclcik != (double)bv.betList[i].money)
                                {
                                    ajd.ErrorMsg = "請勿竄改前端資料";
                                    ajd.isTrue   = false;
                                    throw new Exception("error");
                                }
                            }
                        }

                        if (bi < 4)
                        {
                            bj += 2;
                        }
                        else if (bi == 4)
                        {
                            bj += 4;
                        }
                        else
                        {
                            bj += 5;
                        }
                    }
                }
                double nabobMoney = 0;

                foreach (var b in bv.betList)
                {
                    if (!b.money.HasValue || b.money <= 0)
                    {
                        continue;
                    }
                    //bv.encryptedKey = System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"];
                    betDto bet = new betDto();;
                    bet.userId   = User.Identity.GetUserId();
                    bet.unitSn   = 1;
                    bet.comSn    = 1;
                    bet.money    = b.money;
                    bet.choiceSn = b.getChoiceSn(System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"]);

                    if (bet.choiceSn.HasValue)
                    {
                        topicDto t = await _game.GetTopicByChoiceSn(bet.choiceSn.Value);

                        if (t != null)
                        {
                            bet.topicSn = t.sn;
                        }
                    }
                    bet.gameSn = gd.sn;

                    if (Assets < bet.money)
                    {
                        ajd.ErrorMsg = "剩餘彩金不足";
                        ajd.isTrue   = false;
                        throw new Exception("error");
                    }
                    else
                    {
                        if (gd.betModel != 6)
                        {
                            Assets -= bet.money.Value;
                        }
                    }

                    /*if (!gd.canbet)
                     * {
                     *  ajd.ErrorMsg = "不可下注";
                     *  ajd.isTrue = false;
                     *  throw new Exception("error");
                     * }*/

                    foreach (topicDto t in gd.topicList)
                    {
                        if (t.choiceList.Where(p => p.sn == bet.choiceSn).Count() > 0)
                        {
                            bool canbet = (t.walk == 1) ? true : t.canbet;
                            //t.canbet = (t.walk == 1) ? true : t.canbet;
                            if (!canbet)
                            {
                                ajd.ErrorMsg = "不可預測";
                                ajd.isTrue   = false;
                                throw new Exception("error");
                            }
                        }
                    }
                    bet_list.Add(bet);
                }



                if (gd.betModel != 6)
                {
                    foreach (var bet in bet_list)
                    {
                        bool isTrue = await _game.CreateBet(bet);

                        if (isTrue)
                        {
                            ajd.isTrue = isTrue;
                        }
                        else
                        {
                            ajd.ErrorMsg = "系統錯誤,請重整後再重新預測";
                            throw new Exception("error");
                        }
                    }
                }
                else
                {
                    var betm = bet_list.Where(x => x.money != null).ToList();

                    bool isTrue = await _game.NabobCreateBet(betm);

                    if (isTrue)
                    {
                        ajd.isTrue = isTrue;
                    }
                    else
                    {
                        ajd.ErrorMsg = "系統錯誤,請重整後再重新預測";
                        throw new Exception("error");
                    }
                }


                if (ajd.isTrue)
                {
                    Session["Assets"] = new AssetsRepository().getAssetsListByUserID(User.Identity.GetUserId());
                    new SignalRHelper().UpdateChoiceMoney(gd, encryptedKey, gd.md5GameSn);
                }
                else
                {
                    ajd.ErrorMsg = "魚骨幣發生問題,請重整後再重新預測";
                    throw new Exception("error");
                }

                return(Json(ajd));
            }
            catch
            {
                return(Json(ajd));
            }
        }
Ejemplo n.º 4
0
        public async System.Threading.Tasks.Task <ActionResult> LottoCreate(betViewModel bv)
        {
            aJaxDto ajd = new aJaxDto();

            try
            {
                if (!ModelState.IsValid)
                {
                    ajd.ErrorMsg = "";
                    foreach (var errs in ModelState.Values.Where(p => p.Errors.Count > 0).Select(p => p.Errors).Distinct())
                    {
                        foreach (var e in errs)
                        {
                            ajd.ErrorMsg += e.ErrorMessage + "\n";
                        }
                        //ajd.ErrorMsg += "";
                    }

                    ajd.isTrue = false;
                    throw new Exception("error");
                }

                if (!User.Identity.GetEmailConfirmed() && !User.Identity.GetPhoneNumberConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成Email驗證以及手機驗證\n請先至會員中心進行Email驗證和手機驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }

                if (!User.Identity.GetEmailConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成Email驗證\n請先至會員中心進行Email驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                if (!User.Identity.GetPhoneNumberConfirmed())
                {
                    ajd.ErrorMsg = "預測前須先完成手機驗證\n請先至會員中心進行手機驗證,謝謝";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }



                int?bvcount = 0;
                foreach (var bvlt in bv.betList)
                {
                    if (bvlt.strsn != "5")
                    {
                        bvcount += bvlt.count;
                    }
                }
                if (bvcount == 0)
                {
                    ajd.ErrorMsg = "請在填空單選擇一個以上的選項";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }

                double moneyclcik = 0;
                for (var bi = 0; bi < bv.betList.Count; bi++)
                {
                    if (bi == 0)
                    {
                        moneyclcik = (double)bv.betList[bi].money;
                    }
                    else
                    {
                        if (bv.betList[bi].money != moneyclcik)
                        {
                            ajd.ErrorMsg = "請勿竄改前端資料";
                            ajd.isTrue   = false;
                            throw new Exception("error");
                        }
                    }

                    Regex regex = new Regex(@"^\+?[0-9]{1,10}[0][0]$|^$");
                    Match match = regex.Match(bv.betList[bi].money.ToString());
                    if (!match.Success)
                    {
                        ajd.ErrorMsg = "請勿竄改前端資料";
                        ajd.isTrue   = false;
                        throw new Exception("error");
                    }
                }



                List <betDto>   bet_list      = new List <betDto>();
                GamesRepository _game         = new GamesRepository();
                int             firstChoiceSn = bv.betList.First().getChoiceSn(System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"]);
                gameDto         gd            = await _game.GetGameDetailByChoiceSn(firstChoiceSn);

                double Assets = new AssetsRepository().getAssetsByUserID(User.Identity.GetUserId(), 1);
                Session["Assets"] = new AssetsRepository().getAssetsListByUserID(User.Identity.GetUserId());

                //bv.encryptedKey = System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"];
                betDto bet = new betDto();
                bet.userId   = User.Identity.GetUserId();
                bet.unitSn   = 1;
                bet.comSn    = 1;
                bet.money    = bv.betList[0].money;
                bet.choiceSn = bv.betList[0].getChoiceSn(System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"]);
                if (bet.choiceSn.HasValue)
                {
                    topicDto t = await _game.GetTopicByChoiceSn(bet.choiceSn.Value);

                    if (t != null)
                    {
                        bet.topicSn = t.sn;
                    }
                }
                bet.gameSn = gd.sn;
                List <betCountDto> betlistmodel = new List <betCountDto>();
                foreach (var b in bv.betList)
                {
                    betCountDto betcount = new betCountDto();
                    betcount.betSn       = bet.choiceSn;
                    betcount.choiceStr   = b.strsn;
                    betcount.unitSn      = 1;
                    betcount.choiceCount = b.count;
                    betlistmodel.Add(betcount);
                }

                bet.betCount = betlistmodel;



                if (Assets < bet.money)
                {
                    ajd.ErrorMsg = "剩餘魚骨幣不足";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }
                else
                {
                    Assets -= bet.money.Value;
                }

                if (!gd.canbet)
                {
                    ajd.ErrorMsg = "不可預測";
                    ajd.isTrue   = false;
                    throw new Exception("error");
                }

                foreach (topicDto t in gd.topicList)
                {
                    if (t.choiceList.Where(p => p.sn == bet.choiceSn).Count() > 0)
                    {
                        if (!t.canbet)
                        {
                            ajd.ErrorMsg = "不可預測";
                            ajd.isTrue   = false;
                            throw new Exception("error");
                        }
                    }
                }



                bet_list.Add(bet);

                /* foreach (var b in bv.betList)
                 * {
                 *   if (!b.money.HasValue || b.money <= 0)
                 *       continue;
                 *   //bv.encryptedKey = System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"];
                 *   betDto bet = new betDto(); ;
                 *   bet.userId = User.Identity.GetUserId();
                 *   bet.unitSn = 1;
                 *   bet.comSn = 1;
                 *   bet.money = b.money;
                 *   bet.choiceSn = b.getChoiceSn(System.Web.Configuration.WebConfigurationManager.AppSettings["encryptedKey"]);
                 *   if (bet.choiceSn.HasValue)
                 *   {
                 *       topicDto t = await _game.GetTopicByChoiceSn(bet.choiceSn.Value);
                 *       if (t != null)
                 *       {
                 *           bet.topicSn = t.sn;
                 *       }
                 *   }
                 *   bet.gameSn = gd.sn;
                 *
                 *   if (Assets < bet.money)
                 *   {
                 *       ajd.ErrorMsg = "剩餘彩金不足";
                 *       ajd.isTrue = false;
                 *       throw new Exception("error");
                 *   }
                 *   else
                 *   {
                 *       Assets -= bet.money.Value;
                 *   }
                 *
                 *   if (!gd.canbet)
                 *   {
                 *       ajd.ErrorMsg = "不可下注";
                 *       ajd.isTrue = false;
                 *       throw new Exception("error");
                 *   }
                 *
                 *   foreach (topicDto t in gd.topicList)
                 *   {
                 *       if (t.choiceList.Where(p => p.sn == bet.choiceSn).Count() > 0)
                 *       {
                 *           if (!t.canbet)
                 *           {
                 *               ajd.ErrorMsg = "不可下注";
                 *               ajd.isTrue = false;
                 *               throw new Exception("error");
                 *           }
                 *       }
                 *   }
                 *   bet_list.Add(bet);
                 * }*/

                foreach (var betlist in bet_list)
                {
                    bool isTrue = await _game.CreateBet(betlist);

                    if (isTrue)
                    {
                        ajd.isTrue = isTrue;
                    }
                    else
                    {
                        ajd.ErrorMsg = "系統錯誤,請重整後再重新預測";
                        throw new Exception("error");
                    }
                }

                if (ajd.isTrue)
                {
                    Session["Assets"] = new AssetsRepository().getAssetsListByUserID(User.Identity.GetUserId());
                    new SignalRHelper().UpdateChoiceMoney(gd, encryptedKey, gd.md5GameSn);
                }
                else
                {
                    ajd.ErrorMsg = "魚骨幣發生問題,請重整後再重新預測";
                    throw new Exception("error");
                }

                return(Json(ajd));
            }
            catch
            {
                return(Json(ajd));
            }
        }
Ejemplo n.º 5
0
        public IHttpActionResult Postbet(betDto bet)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            user  u      = db.users.Where(p => p.userID == bet.userId && p.comSn == bet.comSn).FirstOrDefault();
            game  g      = db.games.Where(x => x.sn == bet.gameSn).FirstOrDefault();
            topic dgontc = db.topics.Where(x => x.gameSn == bet.gameSn).FirstOrDefault();

            topic      tc       = db.topics.Where(x => x.sn == bet.topicSn).FirstOrDefault();
            List <bet> bettotal = db.bets.Where(p => p.topicSn == tc.sn).ToList();

            //龍的傳人才新增
            if (g.betModel == 5)
            {
                prizepoolRecord ppr = new prizepoolRecord();
                ppr.unitSn   = 1;
                ppr.userSn   = u.sn;
                ppr.gameSn   = bet.gameSn;
                ppr.topicSn  = bet.topicSn;
                ppr.choiceSn = bet.choiceSn;
                ppr.assets   = (double)bet.money * (double)dgontc.outlay / 100;
                ppr.type     = 2;
                ppr.inpdate  = DateTime.Now;
                db.prizepoolRecords.Add(ppr);
                db.SaveChanges();
            }


            if (u == null)
            {
                return(BadRequest("UserID不存在"));
            }
            bet.userSn = u.sn;
            bet.valid  = 1;
            Mapper.Initialize(cfg => {
                cfg.CreateMap <betDto, bet>();
            });
            bet b = Mapper.Map <bet>(bet);

            choice cho = db.choices.Where(p => p.sn == b.choiceSn).FirstOrDefault();

            b.Odds    = cho.Odds;
            b.topicSn = cho.topicSn;
            if (tc.walk == 1)
            {
                b.totalmoney  = bettotal.Sum(x => x.money);
                b.totalmoney += bet.money;
            }

            db.bets.Add(b);
            db.SaveChanges();
            //只有新模式才會新增


            if (bet.betCount != null)
            {
                foreach (betCountDto bcd in bet.betCount)
                {
                    //樂透模式才會新增

                    betCount bc = new betCount();
                    //只有在可下注單位的資料要存
                    //if (game.betUnit.Contains(cs.unitSn.ToString()))
                    //{
                    bc.betSn       = b.sn;
                    bc.choiceStr   = bcd.choiceStr;
                    bc.choiceCount = bcd.choiceCount;
                    bc.unitSn      = 1;

                    db.betCounts.Add(bc);
                    db.SaveChanges();
                    //cs = new choiceStrRepository().setData(cs);
                    //}
                }
            }



            return(CreatedAtRoute("DefaultApi", new { id = bet.sn }, bet));
        }