示例#1
0
        public IActionResult GetDataLineChart()
        {
            var user      = _sysUserService.Queryable().FirstOrDefault(x => x.Id == HttpContext.Session.GetObjectFromJson <SysUserViewModel>("CurrentUser").Id);
            var viewModel = Mapper.Map <GameHistoryViewModel>(user);

            var lotteryHistory = _lotteryHistoryService.Query()
                                 .Include(x => x.Lottery)
                                 .Include(x => x.LotteryPrize)
                                 .Where(x => x.SysUserId == user.Id && x.Result != EnumGameResult.REFUND.ToString())
                                 .Select(x => new { x.LotteryId, x.CreatedDate, x.Result, x.Lottery.UnitPrice, Value = (x.LotteryPrize != null) ? x.LotteryPrize.Value : 0 })
                                 .GroupBy(x => x.LotteryId)
                                 .Select(y => new GameHistoryViewModel
            {
                CreatedDate = y.Select(x => x.CreatedDate).OrderByDescending(x => x).FirstOrDefault(),
                Amount      = (y.Select(x => x).Count() * y.Select(x => x.UnitPrice).FirstOrDefault()),
                Award       = (y.Select(x => x).Where(x => x.Result == EnumGameResult.WIN.ToString()).Sum(x => x.Value)),
            })
                                 .AsQueryable()
                                 .ToList();

            var pricePredictionHistory = _pricePredictionHistoryService
                                         .Queryable()
                                         .Where(x => x.SysUserId == user.Id && x.Result != EnumGameResult.REFUND.ToString())
                                         .Select(x => Mapper.Map <GameHistoryViewModel>(x))
                                         .ToList();

            var gameHistoryList = lotteryHistory.Concat(pricePredictionHistory).ToList();

            viewModel.MonthlyInvest = gameHistoryList.AsQueryable()
                                      .GroupBy(x => x.CreatedDate.Date)
                                      .Select(y => new WalletChangeViewModel {
                Date = y.Select(x => x.CreatedDate.ToString("yyyy-MM-dd")).FirstOrDefault(), Amount = y.Sum(x => x.Amount)
            })
                                      .ToList();
            viewModel.MonthlyInvest.Reverse();

            viewModel.AssetChange = gameHistoryList.AsQueryable()
                                    .Where(x => x.Result != string.Empty)
                                    .GroupBy(x => x.CreatedDate.Date)
                                    .Select(y => new WalletChangeViewModel {
                Date = y.Select(x => x.CreatedDate.ToString("yyyy-MM-dd")).FirstOrDefault(), Amount = y.Sum(x => (x.Award.Value - x.Amount))
            })
                                    .ToList();
            viewModel.AssetChange.Reverse();

            viewModel.BonusChange = gameHistoryList.AsQueryable()
                                    .Where(x => x.Result != string.Empty)
                                    .GroupBy(x => x.CreatedDate.Date)
                                    .Select(y => new WalletChangeViewModel {
                Date = y.Select(x => x.CreatedDate.ToString("yyyy-MM-dd")).FirstOrDefault(), Amount = y.Sum(x => x.Award.Value)
            })
                                    .ToList();
            viewModel.BonusChange.Reverse();


            return(new JsonResult(new { success = true, message = JsonConvert.SerializeObject(viewModel) }));
        }
        public IViewComponentResult Invoke(PricePredictionViewComponentViewModel viewModel)
        {
            var tokenAmount    = viewModel.TokenAmount;
            var predictedTrend = viewModel.PredictedTrend;
            var isDisabled     = viewModel.IsDisabled;
            var coinBase       = viewModel.Coinbase;

            viewModel = _pricePredictionService.Queryable().Where(x => x.Id == viewModel.Id)
                        .Select(x => Mapper.Map <PricePredictionViewComponentViewModel>(x)).FirstOrDefault();
            viewModel.TokenAmount    = tokenAmount;
            viewModel.PredictedTrend = predictedTrend;
            viewModel.IsDisabled     = isDisabled;
            viewModel.BTCPricePredictionChartTitle = ((EnumCurrencyPair)Enum.Parse(typeof(EnumCurrencyPair), coinBase)) == EnumCurrencyPair.BTCUSDT ? LangDetailHelper.Get(HttpContext.Session.GetInt32("LangId").Value, "BTCPricePredictionChartTitle") : ""; // TODO: Add more chart title if there are more coinbases
            viewModel.BTCPricePredictionSeriesName = ((EnumCurrencyPair)Enum.Parse(typeof(EnumCurrencyPair), coinBase)) == EnumCurrencyPair.BTCUSDT ? LangDetailHelper.Get(HttpContext.Session.GetInt32("LangId").Value, "BTCPricePredictionSeriesName") : ""; // TODO: Add more chart title if there are more coinbases

            //Calculate percentage
            decimal highPrediction = _pricePredictionHistoryService
                                     .Queryable()
                                     .Where(x => x.PricePredictionId == viewModel.Id && x.Prediction == EnumPricePredictionStatus.HIGH.ToBoolean() && x.Result != EnumGameResult.REFUND.ToString())
                                     .Count();

            decimal lowPrediction = _pricePredictionHistoryService
                                    .Queryable()
                                    .Where(x => x.PricePredictionId == viewModel.Id && x.Prediction == EnumPricePredictionStatus.LOW.ToBoolean() && x.Result != EnumGameResult.REFUND.ToString())
                                    .Count();


            if (highPrediction + lowPrediction == 0)
            {
                viewModel.HighPercentage = viewModel.LowPercentage = 50;
            }
            else
            {
                viewModel.HighPercentage = Math.Round((highPrediction / (highPrediction + lowPrediction) * 100), 2);
                viewModel.LowPercentage  = 100 - viewModel.HighPercentage;
            }
            //////////////////////////

            var btcCurrentPriceResult = ServiceClient.BTCCurrentPriceClient.GetBTCCurrentPriceAsync();

            btcCurrentPriceResult.Wait();
            if (btcCurrentPriceResult.Result.Status.Code == 0)
            {
                viewModel.CurrentBTCRate         = btcCurrentPriceResult.Result.Price;
                viewModel.CurrentBTCRateInString = btcCurrentPriceResult.Result.Price.ToString("#,##0.00");
            }

            // Get btc previous rates 12h before until now
            var btcPriceInUTC = _btcPriceService.Queryable()
                                .Where(x => x.Time >= viewModel.OpenBettingTime.AddHours(-CPLConstant.HourBeforeInChart).ToUTCUnixTimeInSeconds())
                                .ToList();
            var lowestRate = btcPriceInUTC.Min(x => x.Price) - CPLConstant.LowestRateBTCInterval;

            if (lowestRate < 0)
            {
                lowestRate = 0;
            }
            viewModel.PreviousBtcRate = JsonConvert.SerializeObject(btcPriceInUTC);
            viewModel.LowestBtcRate   = lowestRate;
            return(View(viewModel));
        }
        public IActionResult ConfirmPrediction(int pricePredictionId, decimal betAmount, bool predictedTrend)
        {
            var user            = HttpContext.Session.GetObjectFromJson <SysUserViewModel>("CurrentUser");
            var pricePrediction = _pricePredictionService.Queryable().FirstOrDefault(x => x.Id == pricePredictionId);

            if (DateTime.Now > pricePrediction.CloseBettingTime)
            {
                return(new JsonResult(new { success = false, message = LangDetailHelper.Get(HttpContext.Session.GetInt32("LangId").Value, "OverBettingTime") }));
            }
            else
            {
                if (user != null)
                {
                    var currentUser = _sysUserService.Queryable().FirstOrDefault(x => x.Id == user.Id);
                    if (betAmount < currentUser.TokenAmount)
                    {
                        var predictionRecord = new PricePredictionHistory()
                        {
                            PricePredictionId = pricePredictionId, Amount = betAmount, CreatedDate = DateTime.Now, Prediction = predictedTrend, SysUserId = user.Id
                        };
                        _pricePredictionHistoryService.Insert(predictionRecord);

                        currentUser.TokenAmount -= betAmount;
                        _sysUserService.Update(currentUser);

                        _unitOfWork.SaveChanges();

                        decimal highPercentage;
                        decimal lowPercentage;
                        //Calculate percentage
                        decimal highPrediction = _pricePredictionHistoryService
                                                 .Queryable()
                                                 .Where(x => x.PricePredictionId == pricePredictionId && x.Prediction == EnumPricePredictionStatus.HIGH.ToBoolean() && x.Result != EnumGameResult.REFUND.ToString())
                                                 .Count();

                        decimal lowPrediction = _pricePredictionHistoryService
                                                .Queryable()
                                                .Where(x => x.PricePredictionId == pricePredictionId && x.Prediction == EnumPricePredictionStatus.LOW.ToBoolean() && x.Result != EnumGameResult.REFUND.ToString())
                                                .Count();


                        if (highPrediction + lowPrediction == 0)
                        {
                            highPercentage = lowPercentage = 50;
                        }
                        else
                        {
                            highPercentage = Math.Round((highPrediction / (highPrediction + lowPrediction) * 100), 2);
                            lowPercentage  = 100 - highPercentage;
                        }
                        //////////////////////////


                        _progressHubContext.Clients.All.SendAsync("predictedUserProgress", highPercentage, lowPercentage, pricePredictionId);


                        return(new JsonResult(new { success = true, token = currentUser.TokenAmount.ToString("N0"), message = LangDetailHelper.Get(HttpContext.Session.GetInt32("LangId").Value, "BettingSuccessfully") }));
                    }
                    return(new JsonResult(new { success = false, message = LangDetailHelper.Get(HttpContext.Session.GetInt32("LangId").Value, "InsufficientFunds") }));
                }
                return(new JsonResult(new
                {
                    success = true,
                    url = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{Url.Action("LogIn", "Authentication")}?returnUrl={Url.Action("Index", "PricePrediction")}"
                }));
            }
        }