예제 #1
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: 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);
        }
예제 #2
0
        //
        // ────────────────────────────────────────────────────────────────────────────────────
        //   :::::: P R I V A T E   F U N C T I O N S : :  :   :    :     :        :          :
        // ────────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Calculate the winner of a fb with a score type
        /// </summary>
        /// <param name="fb">The fb</param>
        /// <param name="time">The time of the fb</param>
        /// <returns>A list of exact winners and closers winners</returns>
        private static List<List<Guid>> calculateTypeScore(FootballBet fb, int time)
        {
            List<List<Guid>> ret = new List<List<Guid>>();
            ret.Add(new List<Guid>());
            ret.Add(new List<Guid>());
            List<Tuple<Guid, int>> diff = new List<Tuple<Guid, int>>();
            int? homeGoals = getGoals(fb.MatchDay, time, true);
            int? awayGoals = getGoals(fb.MatchDay, time, false);
            int closer = 999;
            fb.userBets.Where(b => b.valid).ToList().ForEach(ub =>
            {
                int diffHome = Math.Abs((int)(homeGoals - ub.homeGoals));
                int diffAway = Math.Abs((int)(awayGoals - ub.awayGoals));
                int fullDiff = diffHome + diffAway;
                if (fullDiff < closer) closer = fullDiff;
                diff.Add(new Tuple<Guid, int>(ub.id, diffHome+diffAway));
            });
            diff.ForEach(u =>
            {
                if(u.Item2 == 0)  ret.First().Add(u.Item1); 
                else if(u.Item2 == closer) ret.Last().Add(u.Item1);
            });

            return ret;
        }
예제 #3
0
        //
        // ──────────────────────────────────────────────────────────────────────────
        //   :::::: C O N S T R U C T O R S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="caller">The caller of the function</param>
        /// <param name="bet">The fb that has ended</param>
        /// <param name="_context">The database context</param>
        /// <param name="includeResults">True to include the results of the match, false otherwise</param>
        public EndedFootballBet(User caller, FootballBet bet, ApplicationDBContext _context, bool includeResults)
        {
            _context.Entry(bet).Reference("typePay").Load();
            _context.Entry(bet).Collection("userBets").Load();
            var userBet = bet.userBets.Where(b => b.userid == caller.id);

            this.bet = new GroupBet(bet, _context, includeResults);
            if (userBet.Count() != 0)
            {
                bool theUserHasWin = false;
                this.ownBet = new List <HistoryUserFootballBet>();
                bet.userBets.Where(b => b.userid == caller.id).OrderByDescending(bb => bb.dateDone).ToList().ForEach(bb =>
                {
                    this.ownBet.Add(new HistoryUserFootballBet(bb, _context, true));
                    theUserHasWin = !theUserHasWin && bb.earnings > 0 && bb.valid ? true : theUserHasWin;
                });
                if (bet.ended)
                {
                    this.userWins = theUserHasWin;
                }
            }
            else
            {
                this.ownBet = null;
            }
            if (bet.ended)
            {
                this.users = new List <OtherUserBets>();
                _context.Entry(bet).Reference("Group").Load();
                _context.Entry(bet.Group).Collection("users").Load();
                //See all the users of the group
                bet.Group.users.Where(g => g.userid != caller.id).ToList().ForEach(userGroup =>
                {
                    List <UserFootballBet> anotherUserBets = bet.userBets.Where(b => b.userid == userGroup.userid)
                                                             .OrderByDescending(bb => bb.dateDone).ToList();

                    if (anotherUserBets.Count() != 0)
                    {
                        _context.Entry(userGroup).Reference("User").Load();
                        List <HistoryUserFootballBet> otherUserBetsHistory = new List <HistoryUserFootballBet>();
                        bool winner = false;
                        anotherUserBets.ForEach(ub =>
                        {
                            winner = !winner && ub.earnings > 0 && ub.valid ? true : winner;
                            otherUserBetsHistory.Add(new HistoryUserFootballBet(ub, _context, bet.ended));
                        });
                        this.users.Add(new OtherUserBets {
                            username = userGroup.User.nickname, winner = winner, bets = otherUserBetsHistory
                        });
                    }
                });
            }
            else
            {
                this.users = null;
            }
        }
예제 #4
0
        //
        // ────────────────────────────────────────────────────────────────────────────────────
        //   :::::: P R I V A T E   F U N C T I O N S : :  :   :    :     :        :          :
        // ────────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if the fb can be cancelled
        /// </summary>
        /// <param name="bet">The fb</param>
        /// <returns>True if the fb can be cancelled, false otherwise</returns>
        private bool checkValidBet(FootballBet bet)
        {
            if (bet.cancelled || bet.ended)
            {
                return(false);
            }

            return(true);
        }
예제 #5
0
파일: GroupNew.cs 프로젝트: jagolu/TFG-API
        /// <summary>
        /// Get the match title for a fb
        /// </summary>
        /// <param name="fb">The football bet</param>
        /// <param name="_context">The database context</param>
        /// <returns>The name of the match</returns>
        private static string getMatchTitle(FootballBet fb, ApplicationDBContext _context)
        {
            _context.Entry(fb).Reference("MatchDay").Load();
            MatchDay md = fb.MatchDay;

            _context.Entry(md).Reference("HomeTeam").Load();
            _context.Entry(md).Reference("AwayTeam").Load();

            return(md.HomeTeam.name + " vs " + md.AwayTeam.name);
        }
예제 #6
0
        /// <summary>
        /// Checks if the user has bet on the fb
        /// </summary>
        /// <param name="fb">The fb to check</param>
        /// <param name="caller">The user who can be or not in the fb</param>
        /// <param name="_context">The database context</param>
        /// <returns>True if the user has bet on the fb, false otherwise</returns>
        private static bool checkUserInBet(FootballBet fb, User caller, ApplicationDBContext _context)
        {
            var existBet = _context.UserFootballBet.Where(ufb => ufb.userid == caller.id && ufb.footballBetid == fb.id && ufb.valid);

            if (existBet.Count() != 0)
            {
                return(false);
            }
            return(true);
        }
        /// <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));
            }
        }
예제 #8
0
        //
        // ────────────────────────────────────────────────────────────────────────────────────
        //   :::::: P R I V A T E   F U N C T I O N S : :  :   :    :     :        :          :
        // ────────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if the fb exists or not
        /// </summary>
        /// <param name="fb">A new football bet object, to save on it</param>
        /// <param name="betId">The fb id</param>
        /// <param name="group">The group where the fb belongs to</param>
        /// <param name="_context">The database context</param>
        /// <returns>True if the fb exists, false otherwise</returns>
        private static bool getBet(ref FootballBet fb, string betId, Group group, ApplicationDBContext _context)
        {
            List <FootballBet> fbs = _context.FootballBets
                                     .Where(md => md.id.ToString() == betId && md.groupid == group.id).ToList();

            if (fbs.Count() != 1)
            {
                return(false);
            }

            fb = fbs.First();
            return(true);
        }
예제 #9
0
파일: GroupNew.cs 프로젝트: jagolu/TFG-API
        /// <summary>
        /// Get the new when a fb is cancelled (group new)
        /// </summary>
        /// <param name="group">The group where the bet has been cancelled</param>
        /// <param name="fb">The bet that has been cancelled</param>
        /// <param name="_context">The database context</param>
        /// <returns>The new object</returns>
        private static New cancelFootballBet_group(Group group, FootballBet fb, ApplicationDBContext _context)
        {
            string title   = "Se ha cancelado una apuesta!";
            string message = "Se ha cancelado la apuesta asociada al partido " + getMatchTitle(fb, _context);

            New n = new New
            {
                Group   = group,
                title   = title,
                message = message
            };

            return(n);
        }
예제 #10
0
        /// <summary>
        /// Calculate the winner of a fb with a winner type
        /// </summary>
        /// <param name="fb">The fb</param>
        /// <param name="time">The time of the bet</param>
        /// <returns>A list of exact winners and closers winners</returns>
        private static List<List<Guid>> calculateResult(FootballBet fb, int time)
        {
            List<List<Guid>> ret = new List<List<Guid>>();
            ret.Add(new List<Guid>());
            ret.Add(new List<Guid>());
            int winner = getWinner(fb.MatchDay, time);
            fb.userBets.Where(b => b.valid).ToList().ForEach(ub =>
            {
                if (ub.winner == winner) ret.First().Add(ub.id);
                else ret.Last().Add(ub.id);
            });

            return ret;
        }
예제 #11
0
        /// <summary>
        /// Launch the news for the group and for the members on it
        /// </summary>
        /// <param name="u">The user who has launch the fb</param>
        /// <param name="group">The group where the new fb has been launched</param>
        /// <param name="fb">The fb that has been launched</param>
        private void launchNews(User u, Group group, FootballBet fb)
        {
            _context.Entry(group).Collection("users").Load();
            Home.Util.GroupNew.launch(null, group, fb, Home.Models.TypeGroupNew.LAUNCH_FOOTBALLBET_GROUP, false, _context);

            group.users.Where(g => !g.blocked).ToList().ForEach(async ug =>
            {
                _context.Entry(ug).Reference("User").Load();
                bool isLauncher = ug.userid == u.id;
                User recv       = ug.User;

                Home.Util.GroupNew.launch(recv, group, fb, Home.Models.TypeGroupNew.LAUNCH_FOOTBALLBET_USER, isLauncher, _context);
                await SendNotification.send(_hub, group.name, recv, Alive.Models.NotificationType.NEW_FOOTBALLBET, _context);
            });
        }
        //
        // ────────────────────────────────────────────────────────────────────────────────────
        //   :::::: P R I V A T E   F U N C T I O N S : :  :   :    :     :        :          :
        // ────────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if the order is correct
        /// </summary>
        /// <param name="userFB">A new UserFootballBet object, to save the user fb in it</param>
        /// <param name="userBet">The id of the user fb</param>
        /// <param name="footballBet">The fb where the user fb belongs to</param>
        /// <param name="caller">The user who wants to cancel the user fb</param>
        /// <returns>True if user fb exists and belongs to the user, false otherwise</returns>
        private bool checkBet(ref UserFootballBet userFB, string userBet, FootballBet footballBet, User caller)
        {
            //User bets by the user with the userBet & valids
            _context.Entry(footballBet).Collection("userBets").Load();
            List <UserFootballBet> bet = footballBet.userBets.Where(ub =>
                                                                    ub.id.ToString() == userBet && ub.userid == caller.id && ub.valid).ToList();

            if (bet.Count() != 1)
            {
                return(false);
            }

            userFB = bet.First();
            return(true);
        }
예제 #13
0
        /// <summary>
        /// Check if the caller can cancel the fb
        /// </summary>
        /// <param name="bet">The bet to cancel</param>
        /// <param name="u">The user who wants to cancel the fb</param>
        /// <returns>Check if the caller is an admin</returns>
        private bool checkAdmin(FootballBet bet, User u)
        {
            _context.Entry(bet).Reference("Group").Load();
            _context.Entry(bet.Group).Collection("users").Load();
            Role maker = RoleManager.getGroupMaker(_context);

            List <UserGroup> admins = bet.Group.users.Where(uu => uu.role == maker && uu.userid == u.id).ToList();

            if (admins.Count() != 1)
            {
                return(false);
            }

            return(true);
        }
예제 #14
0
        /// <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);
        }
예제 #15
0
        /// <summary>
        /// Pay to the winners of a solo fb
        /// </summary>
        /// <param name="fb">The fb</param>
        /// <param name="winners">The exact winners of the fb</param>
        /// <param name="group">The group of the fb</param>
        /// <param name="_context">The database context</param> 
        private static void paySoloBet(FootballBet fb, List<Guid> winners, Group group, ApplicationDBContext _context)
        {
            _context.Entry(fb).Reference("type").Load();
            _context.Entry(fb).Reference("typePay").Load();
            _context.Entry(fb).Collection("userBets").Load();
            _context.Entry(group).Collection("users").Load();

            fb.userBets.Where(ub => winners.Contains(ub.id)).ToList().ForEach(userBet =>
            {
                UserGroup u = group.users.Where(g => g.userid == userBet.userid).First();
                double coinsWin = userBet.bet * fb.winRate;
                u.coins += (int)coinsWin;
                userBet.earnings = (int)coinsWin;
                _context.Update(u);
            });
        }
예제 #16
0
        /// <summary>
        /// Get the coins that the user received when cancels a bet
        /// </summary>
        /// <param name="fb">The fb where the user wants to cancel his bet</param>
        /// <param name="coinsBet">The coins that the user bet</param>
        /// <param name="_context">The database context</param>
        /// <returns>The coins that user will receive when cancel his bet</returns>
        public static int calculateCancelRate(FootballBet fb, int coinsBet, ApplicationDBContext _context)
        {
            _context.Entry(fb).Reference(__typeBet).Load();
            _context.Entry(fb).Reference(__typePay).Load();
            double less1 = fb.type.winLoseCancel;
            double less2 = fb.typePay.winLoseCancel;

            if (less2 == 100)
            {
                return(0);
            }

            double retCoins = coinsBet * (less1 + less2);

            return((int)System.Math.Round(retCoins, System.MidpointRounding.AwayFromZero));
        }
예제 #17
0
        /// <summary>
        /// Pay to the winners of a closer jackpot fb
        /// </summary>
        /// <param name="fb">The fb</param>
        /// <param name="winners">The closer winners of the fb</param>
        /// <param name="group">The group of the fb</param>
        /// <param name="_context">The database context</param>
        private static void payJackpotCloser(FootballBet fb, List<List<Guid>> winners, Group group, ApplicationDBContext _context)
        {
            _context.Entry(group).Collection("users").Load();
            _context.Entry(fb).Collection("userBets").Load();
            List<Guid> toPay = winners.First().Count() > 0 ? winners.First() : winners.Last();
            int jackpot = fb.usersJoined * fb.minBet;
            int div_jack = toPay.Count()>0 ? jackpot / toPay.Count() : 0;

            fb.userBets.Where(ub => toPay.Contains(ub.id)).ToList().ForEach(userBet =>
            {
                userBet.earnings = div_jack;
                UserGroup u = group.users.Where(g => g.userid == userBet.userid).First();
                u.coins += div_jack;
                _context.Update(u);
            });
        }
예제 #18
0
파일: GroupNew.cs 프로젝트: jagolu/TFG-API
        /// <summary>
        /// Get the new when the bets has been paid (user new)
        /// </summary>
        /// <param name="user">The user who will receive the new</param>
        /// <param name="group">The group where the bets has been paid</param>
        /// <param name="fb">The fb that has been paid</param>
        /// <param name="_context">The database context</param>
        /// <returns>The new object</returns>
        private static New pay_bets_user(User user, Group group, FootballBet fb, ApplicationDBContext _context)
        {
            _context.Entry(fb).Reference("type").Load();
            _context.Entry(fb).Reference("typePay").Load();
            string title   = "Se han pagado los resultados de las apuestas";
            string message = "Se han pagado los resultados de las apuestas del grupo \"" + group.name + "\" asociadas al partido "
                             + getMatchTitle(fb, _context) + " del tipo " + fb.type.name + " y premio " + fb.typePay.name;

            New n = new New
            {
                User    = user,
                title   = title,
                message = message
            };

            return(n);
        }
예제 #19
0
파일: GroupNew.cs 프로젝트: jagolu/TFG-API
        /// <summary>
        /// Get the new when a fb is cancelled (user new)
        /// </summary>
        /// <param name="user">The user who will receive the new</param>
        /// <param name="group">The group where the bet has been cancelled</param>
        /// <param name="fb">The bet that has been cancelled</param>
        /// <param name="isLauncher">True if the user is the launcher of the bet, false otherwise</param>
        /// <param name="_context">The database context</param>
        /// <returns>The new object</returns>
        private static New cancelFootballBet_user(User user, Group group, FootballBet fb, bool isLauncher, ApplicationDBContext _context)
        {
            string title = !isLauncher ? "Se ha cancelado una apuesta en uno de tus grupos" :
                           "Has cancelado una apuesta";
            string message = !isLauncher ? "Se ha cancelado una apuesta en el grupo \"" + group.name + "\". " :
                             "Has cancelado una apuesta en el grupo \"" + group.name + "\". ";

            message += "El partido asociado a la apuesta era el " + getMatchTitle(fb, _context);

            New n = new New
            {
                User    = user,
                title   = title,
                message = message
            };

            return(n);
        }
예제 #20
0
        //
        // ────────────────────────────────────────────────────────────────────────────────────
        //   :::::: 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);
        }
예제 #21
0
        /// <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);
            });
        }
예제 #22
0
        /// <summary>
        /// Launch the news and notifications for the group and users of the fb
        /// </summary>
        /// <param name="fb">The fb</param>
        /// <param name="_context">The database context</param>
        /// <param name="hub">The notification hub</param>
        private static void launchNews(FootballBet fb, ApplicationDBContext _context, IHubContext<NotificationHub> hub)
        {
            _context.Entry(fb).Reference("Group").Load();
            Group group = fb.Group;
            List<User> newGroups = new List<User>();

            Areas.Home.Util.GroupNew.launch(null, group, fb, Areas.Home.Models.TypeGroupNew.PAID_BETS_GROUP, false, _context);

            _context.Entry(fb).Collection("userBets").Load();
            fb.userBets.ToList().ForEach(u => 
            {
                _context.Entry(u).Reference("User").Load();
                if (newGroups.All(uu => uu.id != u.userid)) newGroups.Add(u.User);
            });

            newGroups.ForEach(async u =>
            {
                Areas.Home.Util.GroupNew.launch(u, group, fb, Areas.Home.Models.TypeGroupNew.PAID_BETS_USER, false, _context);
                await SendNotification.send(hub, group.name, u, Areas.Alive.Models.NotificationType.PAID_BETS, _context);
            });
        }
예제 #23
0
        /// <summary>
        /// Do a user fb on a fb
        /// </summary>
        /// <param name="order">The info to do the user fb</param>
        /// See <see cref="Areas.Bet.Models.DoFootballBet"/> to know the param structure
        /// <returns>IActionResult of the do user fb action</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public IActionResult doFootballBet([FromBody] DoFootballBet 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 fb       = new FootballBet();

            if (!UserInFootballBet.check(caller, ref group, order.groupName, ref ugCaller, ref fb, order.footballbet, _context))
            {
                return(BadRequest());
            }
            if (fb.cancelled)
            {
                return(BadRequest(new { error = "BetCancelled" }));
            }
            if (fb.ended)
            {
                return(BadRequest(new { error = "BetEnded" }));
            }
            if (fb.dateLastBet < DateTime.Now)
            {
                return(BadRequest(new { error = "BetLastBetPassed" }));
            }
            if (!checkBet(order.bet, ugCaller.coins, fb))
            {
                return(BadRequest());
            }
            if (!checkTypePriceWithBet(fb, order.homeGoals, order.awayGoals, order.winner))
            {
                return(BadRequest());
            }
            try
            {
                _context.Add(new UserFootballBet
                {
                    FootballBet = fb,
                    User        = caller,
                    bet         = order.bet,
                    winner      = order.winner,
                    homeGoals   = order.homeGoals,
                    awayGoals   = order.awayGoals
                });

                fb.usersJoined++;
                ugCaller.coins -= order.bet;
                _context.Update(ugCaller);
                _context.Update(fb);

                _context.SaveChanges();

                return(Ok(GroupPageManager.GetPage(caller, group, _context)));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
예제 #24
0
        /// <summary>
        /// Launchs a new fb
        /// </summary>
        /// <param name="order">The info to launch a new fb</param>
        /// See <see cref="Areas.Bet.Models.LaunchFootballBet"/> to know the param structure
        /// <returns>IActionResult of the launch fb action</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public IActionResult launchBet([FromBody] LaunchFootballBet order)
        {
            User caller = TokenUserManager.getUserFromToken(HttpContext, _context);

            if (!caller.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(caller, _context))
            {
                return(BadRequest("notAllowed"));
            }
            Group           group   = new Group();
            MatchDay        match   = new MatchDay();
            TypeFootballBet typeBet = new TypeFootballBet();
            TypePay         typePay = new TypePay();

            if (!GroupMakerFuncionlities.checkFuncionality(caller, ref group, order.groupName, GroupMakerFuncionality.STARTCREATE_FOOTBALL_BET, _context))
            {
                return(BadRequest());
            }
            if (!getMatchDay(ref match, order.matchday))
            {
                return(BadRequest());
            }
            if (!checkMaxBetAllowed(group))
            {
                return(BadRequest());
            }
            if (!checkParams(ref typeBet, order.typeBet, ref typePay, order.typePay))
            {
                return(BadRequest());
            }
            if (order.lastBetTime > match.date)
            {
                return(BadRequest());
            }
            if (!checkMaxMin(order.minBet, order.maxBet))
            {
                return(BadRequest());
            }
            try
            {
                FootballBet fb = new FootballBet
                {
                    MatchDay    = match,
                    Group       = group,
                    type        = typeBet,
                    typePay     = typePay,
                    minBet      = order.minBet,
                    maxBet      = order.maxBet,
                    winRate     = typeBet.winRate + typePay.winRate,
                    dateLastBet = order.lastBetTime,
                    dateEnded   = match.date
                };
                _context.Add(fb);
                _context.SaveChanges();

                launchNews(caller, group, fb);

                return(Ok(GroupPageManager.GetPage(caller, group, _context)));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
예제 #25
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Check if a user has bet in a fb
        /// </summary>
        /// <param name="caller">The user who wants do/cancel a user fb</param>
        /// <param name="group">A new group object, to save on it</param>
        /// <param name="groupName">The name of the group</param>
        /// <param name="ugCaller">A new UserGroup object, to save on it</param>
        /// <param name="footballBet">A new FootballBet object, to save on it</param>
        /// <param name="footballBetId">The id of the football bet</param>
        /// <param name="_context">The datbase context</param>
        /// <param name="checkOnlyBet">True to check if the user has bet on the fb, false otherwise</param>
        /// <returns></returns>
        public static bool check(User caller, ref Group group, string groupName, ref UserGroup ugCaller, ref FootballBet footballBet, string footballBetId, ApplicationDBContext _context, bool checkOnlyBet = true)
        {
            if (!UserFromGroup.isOnIt(caller.id, ref group, groupName, ref ugCaller, _context))
            {
                return(false);
            }
            if (!getBet(ref footballBet, footballBetId, group, _context))
            {
                return(false);
            }
            if (checkOnlyBet && !checkUserInBet(footballBet, caller, _context))
            {
                return(false);
            }

            return(true);
        }
예제 #26
0
        /// <summary>
        /// Checks if the type is a winner type with the fb object
        /// </summary>
        /// <param name="bet">The football bet</param>
        /// <param name="_context">The database context</param>
        /// <returns>true if the fb is a winner fb, false otherwise</returns>
        public static bool isWinner(FootballBet bet, ApplicationDBContext _context)
        {
            _context.Entry(bet).Reference(__typeBet).Load();

            return(bet.type.name.Contains(_winner));
        }
예제 #27
0
파일: GroupNew.cs 프로젝트: jagolu/TFG-API
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Launchs a new
        /// </summary>
        /// <param name="user">The user who will receive the new (null for nobody)</param>
        /// <param name="group">The group which will receive the new (null for none)</param>
        /// <param name="fb">The FootballBet extra info (null for none)</param>
        /// <param name="type">The type of the new</param>
        /// <param name="makeUnmake">Bool extra info</param>
        /// <param name="context">The database context</param>
        public static void launch(User user, Group group, FootballBet fb, TypeGroupNew type, bool makeUnmake, ApplicationDBContext context)
        {
            New whatsGoingOn = new New();

            if (type == TypeGroupNew.BLOCK_USER_USER)
            {
                whatsGoingOn = blockUser_user(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.BLOCK_USER_GROUP)
            {
                whatsGoingOn = blockUser_group(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.JOIN_LEFT_GROUP)
            {
                whatsGoingOn = join_left_group(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.JOIN_LEFT_USER)
            {
                whatsGoingOn = join_left_user(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.MAKE_ADMIN_USER)
            {
                whatsGoingOn = makeAdmin_user(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.MAKE_ADMIN_GROUP)
            {
                whatsGoingOn = makeAdmin_group(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.MAKE_PRIVATE)
            {
                whatsGoingOn = makePrivate(group, makeUnmake);
            }
            else if (type == TypeGroupNew.REMOVE_GROUP)
            {
                whatsGoingOn = removeGroup(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.KICK_USER_USER)
            {
                whatsGoingOn = kickUser_user(user, group);
            }
            else if (type == TypeGroupNew.KICK_USER_GROUP)
            {
                whatsGoingOn = kickUser_group(user, group);
            }
            else if (type == TypeGroupNew.CREATE_GROUP_USER)
            {
                whatsGoingOn = createGroup_user(user, group);
            }
            else if (type == TypeGroupNew.CREATE_GROUP_GROUP)
            {
                whatsGoingOn = createGroup_group(user, group);
            }
            else if (type == TypeGroupNew.MAKE_MAKER_GROUP)
            {
                whatsGoingOn = makeMaker_group(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.MAKE_MAKER_USER)
            {
                whatsGoingOn = makeMaker_user(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.BAN_GROUP)
            {
                whatsGoingOn = ban_group(user, group, makeUnmake);
            }

            else if (type == TypeGroupNew.LAUNCH_FOOTBALLBET_USER)
            {
                whatsGoingOn = launchFootballBet_user(user, group, fb, makeUnmake, context);
            }
            else if (type == TypeGroupNew.LAUNCH_FOOTBALLBET_GROUP)
            {
                whatsGoingOn = launchFootballBet_group(group, fb, context);
            }
            else if (type == TypeGroupNew.PAID_BETS_USER)
            {
                whatsGoingOn = pay_bets_user(user, group, fb, context);
            }
            else if (type == TypeGroupNew.PAID_BETS_GROUP)
            {
                whatsGoingOn = pay_bets_group(group, fb, context);
            }
            else if (type == TypeGroupNew.FOOTBALLBET_CANCELLED_GROUP)
            {
                whatsGoingOn = cancelFootballBet_group(group, fb, context);
            }
            else if (type == TypeGroupNew.FOOTBALLBET_CANCELLED_USER)
            {
                whatsGoingOn = cancelFootballBet_user(user, group, fb, makeUnmake, context);
            }


            else if (type == TypeGroupNew.PAID_PLAYERS_USER)
            {
                whatsGoingOn = pay_players_user(user, group);
            }
            else if (type == TypeGroupNew.PAID_PLAYERS_GROUPS)
            {
                whatsGoingOn = pay_players_group(group);
            }


            else if (type == TypeGroupNew.CHANGE_WEEKLYPAY_USER)
            {
                whatsGoingOn = change_weeklyPay_user(user, group, makeUnmake);
            }
            else if (type == TypeGroupNew.CHANGE_WEEKLYPAY_GROUP)
            {
                whatsGoingOn = change_weeklyPay_group(group);
            }


            else if (type == TypeGroupNew.WELCOME)
            {
                whatsGoingOn = getWelcomeMessage(user);
            }
            else if (type == TypeGroupNew.WELCOMEBACK)
            {
                whatsGoingOn = getWelcomeBackMessage(user);
            }

            try
            {
                context.Add(whatsGoingOn);
                context.SaveChanges();
            }
            catch (Exception) {}
        }
예제 #28
0
        //
        // ──────────────────────────────────────────────────────────────────────────────────
        //   :::::: P U B L I C   F U N C T I O N S : :  :   :    :     :        :          :
        // ──────────────────────────────────────────────────────────────────────────────────
        //

        /// <summary>
        /// Checks if the type is a jackpot type
        /// </summary>
        /// <param name="bet">The football bet</param>
        /// <param name="_context">The database context</param>
        /// <returns>true if the fb is a jackpot fb, false otherwise</returns>
        public static bool isJackpot(FootballBet bet, ApplicationDBContext _context)
        {
            _context.Entry(bet).Reference(__typePay).Load();

            return(bet.typePay.name.Contains(_jackpot));
        }
예제 #29
0
        /// <summary>
        /// Cancel a fb
        /// </summary>
        /// <param name="betId">The id of the fb</param>
        /// <returns>IActionResult of the cancel action</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public IActionResult cancel([Required] string betId)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context);

            if (!user.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(user, _context))
            {
                return(BadRequest("notAllowed"));
            }

            List <FootballBet> bets = _context.FootballBets.Where(g => g.id.ToString() == betId).ToList();

            if (bets.Count() != 1)
            {
                return(BadRequest());
            }

            FootballBet bet = bets.First();

            _context.Entry(bet).Reference("Group").Load();
            Group group = bet.Group;

            if (!checkValidBet(bet))
            {
                return(BadRequest());
            }
            if (!checkAdmin(bet, user))
            {
                return(BadRequest());
            }
            if (bet.dateEnded < DateTime.Now)
            {
                return(BadRequest(new { error = "CantCancelTheFootballBet" }));
            }

            try
            {
                Home.Util.GroupNew.launch(null, group, bet, Home.Models.TypeGroupNew.FOOTBALLBET_CANCELLED_GROUP, false, _context);
                getMoneyBackAndLaunchNews(bet, group);

                bet.cancelled     = true;
                bet.dateCancelled = DateTime.Now;

                _context.SaveChanges();
                using (var scope = _scopeFactory.CreateScope())
                {
                    var   dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>();
                    Group dbgroup   = dbContext.Group.Where(g => g.name == group.name).First();
                    User  dbUser    = dbContext.User.Where(u => u.id == user.id).First();

                    return(Ok(GroupPageManager.GetPage(dbUser, dbgroup, dbContext)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }