// // ────────────────────────────────────────────────────────────────────────────────── // :::::: P U B L I C F U N C T I O N S : : : : : : : : // ────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Check the winner of a fb /// </summary> /// <param name="footballBet">The fb to check</param> /// <param name="_context">The database context</param> /// <param name="hub">The notification hub</param> public static void checkWinner(FootballBet footballBet, ApplicationDBContext _context, IHubContext<NotificationHub> hub) { _context.Entry(footballBet).Reference("type").Load(); _context.Entry(footballBet).Reference("typePay").Load(); _context.Entry(footballBet).Reference("MatchDay").Load(); _context.Entry(footballBet).Reference("Group").Load(); _context.Entry(footballBet).Collection("userBets").Load(); int time = getTypeTime(footballBet.type.name); Group group = footballBet.Group; List<List<Guid>> winners; winners = CheckBetType.isWinner(footballBet, _context) ? calculateResult(footballBet, time) : calculateTypeScore(footballBet, time); if (CheckBetType.isJackpotExact(footballBet, _context)) { payJackpot(footballBet, winners.First(), group, _context); } else if (CheckBetType.isJackpotCloser(footballBet, _context)) { payJackpotCloser(footballBet, winners, group, _context); } else if (CheckBetType.isSoloExact(footballBet, _context)) { paySoloBet(footballBet, winners.First(), group, _context); } else throw new Exception(); _context.Update(group); launchNews(footballBet, _context, hub); }
/// <summary> /// Check if the params of the fb are correct /// </summary> /// <param name="type_bet">A new TypeFootballBet object, to save the type on it</param> /// <param name="typeBet">The name of the bet type</param> /// <param name="type_pay">A new TypePay object, to save the type on it</param> /// <param name="typePay">The name of the pay type</param> /// <returns>True if the types are correct, false otherwise</returns> private bool checkParams(ref TypeFootballBet type_bet, string typeBet, ref TypePay type_pay, string typePay) { try { var typeB = _context.TypeFootballBet.Where(t => t.name == typeBet); var typeP = _context.TypePay.Where(t => t.name == typePay); if (typeB.Count() != 1 || typeP.Count() != 1) { return(false); } if (CheckBetType.typeIsWinner(typeB.First()) && CheckBetType.typeIsCloser(typeB.First())) { return(false); } type_bet = typeB.First(); type_pay = typeP.First(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Cancel a user fb /// </summary> /// <param name="order">The info to cancel the user fb</param> /// See <see cref="Areas.Bet.Models.CancelUserFootballBet"/> to know the param structure /// <returns>The IActionResult of the cancel a user fb action</returns> /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure public IActionResult cancelUserFootballBet([FromBody] CancelUserFootballBet order) { User caller = TokenUserManager.getUserFromToken(HttpContext, _context); if (!caller.open) { return(BadRequest(new { error = "YoureBanned" })); } if (AdminPolicy.isAdmin(caller, _context)) { return(BadRequest("notAllowed")); } UserGroup ugCaller = new UserGroup(); Group group = new Group(); FootballBet footballBet = new FootballBet(); UserFootballBet userFootballBet = new UserFootballBet(); if (!UserInFootballBet.check(caller, ref group, order.groupName, ref ugCaller, ref footballBet, order.footballBet, _context, false)) { return(BadRequest()); } if (footballBet.cancelled) { return(BadRequest(new { error = "CancelBetCancelled" })); } if (footballBet.ended) { return(BadRequest(new { error = "CancelBetEnded" })); } if (footballBet.dateLastBet < DateTime.Now) { return(BadRequest(new { error = "CancelBetLastBetPassed" })); } if (!checkBet(ref userFootballBet, order.userBet, footballBet, caller)) { return(BadRequest()); } try { ugCaller.coins += CheckBetType.calculateCancelRate(footballBet, userFootballBet.bet, _context); userFootballBet.valid = false; userFootballBet.dateInvalid = DateTime.Now; _context.Update(ugCaller); _context.Update(userFootballBet); _context.SaveChanges(); return(Ok(GroupPageManager.GetPage(caller, group, _context))); } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Check if the guessed of the bet is correct on its type /// </summary> /// <param name="fb">The fb</param> /// <param name="homeGoals">The home goals guessed by the user</param> /// <param name="awayGoals">The away goals guessed by the user</param> /// <param name="winner">The winner guessed by the user</param> /// <returns>Check if the guessed by the user is correct on the fb type</returns> private bool checkTypePriceWithBet(FootballBet fb, int?homeGoals, int?awayGoals, int?winner) { bool type_winner = CheckBetType.isWinner(fb, _context); if (type_winner && (winner == null || winner > 2 || winner < 0)) { return(false); } if (!type_winner && (homeGoals == null || awayGoals == null || (homeGoals < 0 || homeGoals > 20) || (awayGoals < 0 || awayGoals > 20))) { return(false); } return(true); }
// // ──────────────────────────────────────────────────────────────────────────────────── // :::::: P R I V A T E F U N C T I O N S : : : : : : : : // ──────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Check the new user fb /// </summary> /// <param name="userBet">The coins bet by the user</param> /// <param name="userCoins">The total coins of the user</param> /// <param name="fb">The fb to bet on</param> /// <returns>True if the user can do the bet, false otherwise</returns> private bool checkBet(int userBet, int userCoins, FootballBet fb) { bool typeJackpot = CheckBetType.isJackpot(fb, _context); if (userBet > userCoins) { return(false); } if (typeJackpot && userBet != fb.minBet) { return(false); } if (!typeJackpot && (userBet < fb.minBet || userBet > fb.maxBet)) { return(false); } return(true); }
/// <summary> /// Initializes the pay types /// </summary> /// <param name="context">The database context</param> private static void InitializeTypePay(ApplicationDBContext context) { var types = new TypePay[] { new TypePay { name = CheckBetType.getJackpotExact(), winRate = 0, winLoseCancel = 100, description = "Tiene bote. El bote sera para el participante que acierte el resultado exacto. Si nadie acierta el resultado, todos perderán las monedas apostadas." }, new TypePay { name = CheckBetType.getJackpotCloser(), winRate = 0, winLoseCancel = 100, description = "Tiene bote. El bote será para el participante que se acerque más al resultado exacto." }, new TypePay { name = CheckBetType.getSoloExact(), winRate = 1.5, winLoseCancel = 0.2, description = "Cada participante participa el solo. Las apuestas tienen una cuota. Si el participante no gana perderá sus monedas." }, }; types.Where(t => context.TypePay.All(type => type.name != t.name)).ToList().ForEach(tp => context.Add(tp)); }
/// <summary> /// Return the coins to the users who bet in that fb /// </summary> /// <param name="bet">The fb</param> /// <param name="group">The group where the fb was launched</param> private void getMoneyBackAndLaunchNews(FootballBet bet, Group group) { _context.Entry(bet).Collection("userBets").Load(); _context.Entry(group).Collection("users").Load(); bool isJackpot = CheckBetType.isJackpot(bet, _context); bet.userBets.ToList().ForEach(ub => { _context.Entry(ub).Reference("User").Load(); UserGroup userg = group.users.Where(u => u.userid == ub.userid).First(); int coinsBet = ub.bet; if (isJackpot || ub.valid) { userg.coins += coinsBet; } else { int retCoins = CheckBetType.calculateCancelRate(bet, coinsBet, _context); userg.coins += (coinsBet - retCoins); } _context.UserGroup.Update(userg); _context.SaveChanges(); _context.Entry(userg).Reference("User").Load(); }); _context.UserFootballBet.RemoveRange(bet.userBets.ToList()); _context.SaveChanges(); _context.Entry(group).Collection("users"); group.users.ToList().ForEach(async u => { _context.Entry(u).Reference("role").Load(); _context.Entry(u).Reference("User").Load(); User recv = u.User; Home.Util.GroupNew.launch(recv, group, bet, Home.Models.TypeGroupNew.FOOTBALLBET_CANCELLED_USER, u.role == RoleManager.getGroupMaker(_context), _context); await SendNotification.send(_hub, group.name, recv, Alive.Models.NotificationType.CANCELLED_FOOTBALLBET, _context); }); }