Exemple #1
0
        // POST tables/Challenge
        public async Task <IHttpActionResult> PostChallenge(ChallengeDto item)
        {
            var challenger = _context.Athletes.SingleOrDefault(a => a.Id == item.ChallengerAthleteId);
            var challengee = _context.Athletes.SingleOrDefault(a => a.Id == item.ChallengeeAthleteId);

            _authController.EnsureHasPermission(challenger, Request);

            if (challenger == null || challengee == null)
            {
                throw "The opponent in this challenge no longer belongs to this league.".ToException(Request);
            }

            var challengerMembership = _context.Memberships.SingleOrDefault(m => m.AbandonDate == null && m.AthleteId == challenger.Id && m.LeagueId == item.LeagueId);
            var challengeeMembership = _context.Memberships.SingleOrDefault(m => m.AbandonDate == null && m.AthleteId == challengee.Id && m.LeagueId == item.LeagueId);

            if (challengerMembership == null || challengeeMembership == null)
            {
                throw "The opponent in this challenge no longer belongs to this league.".ToException(Request);
            }

            //Check to see if there are any ongoing challenges between both athletes
            var challengeeOngoing = _context.Challenges.Where(c => ((c.ChallengeeAthleteId == item.ChallengeeAthleteId || c.ChallengerAthleteId == item.ChallengeeAthleteId) &&
                                                                    (c.ChallengeeAthleteId == item.ChallengerAthleteId || c.ChallengerAthleteId == item.ChallengerAthleteId) &&
                                                                    c.LeagueId == item.LeagueId && c.DateCompleted == null) && !c.Deleted);

            if (challengeeOngoing.Count() > 0)
            {
                throw "{0} already has an existing challenge underway with {1}.".Fmt(challengee.Alias, challenger.Alias).ToException(Request);
            }

            var league = _context.Leagues.SingleOrDefault(l => l.Id == item.LeagueId);

            try
            {
                var challenge = item.ToChallenge();
                var json      = Newtonsoft.Json.JsonConvert.SerializeObject(challenge);

                Challenge current = await InsertAsync(challenge);

                var result = CreatedAtRoute("Tables", new { id = current.Id }, current.ToChallengeDto());

                var message = "{0}: You have been challenged to a duel by {1}!".Fmt(league.Name, challenger.Alias);
                var payload = new NotificationPayload
                {
                    Action  = PushActions.ChallengePosted,
                    Payload = { { "challengeId", current.Id }, { "leagueId", current.LeagueId } }
                };
                _notificationController.NotifyByTag(message, current.ChallengeeAthleteId, payload);
                return(result);
            }
            catch (Exception e)
            {
                return(null);
            }

            //Not awaiting so the user's result is not delayed
        }
Exemple #2
0
        // POST tables/Challenge
        public async Task <IHttpActionResult> PostChallenge(ChallengeDto item)
        {
            var challenger = _context.Athletes.SingleOrDefault(a => a.Id == item.ChallengerAthleteId);
            var challengee = _context.Athletes.SingleOrDefault(a => a.Id == item.ChallengeeAthleteId);

            _authController.EnsureHasPermission(new Athlete[] { challenger, challengee }, Request);

            if (challenger == null || challengee == null)
            {
                throw "The opponent in this challenge no longer belongs to this league".ToException(Request);
            }

            var challengerMembership = _context.Memberships.SingleOrDefault(m => m.AbandonDate == null && m.AthleteId == challenger.Id && m.LeagueId == item.LeagueId);
            var challengeeMembership = _context.Memberships.SingleOrDefault(m => m.AbandonDate == null && m.AthleteId == challengee.Id && m.LeagueId == item.LeagueId);

            if (challengerMembership == null || challengeeMembership == null)
            {
                throw "The opponent in this challenge no longer belongs to this league".ToException(Request);
            }

            //Check to see if there are any ongoing challenges between either athlete
            var challengeeOngoing = _context.Challenges.Where(c => (c.ChallengeeAthleteId == item.ChallengeeAthleteId || c.ChallengerAthleteId == item.ChallengeeAthleteId) &&
                                                              c.LeagueId == item.LeagueId && c.DateCompleted == null);

            if (challengeeOngoing.Count() > 0)
            {
                throw "{0} already has an existing challenge underway.".Fmt(challengee.Alias).ToException(Request);
            }

            var challengerOngoing = _context.Challenges.Where(c => (c.ChallengerAthleteId == item.ChallengerAthleteId || c.ChallengeeAthleteId == item.ChallengerAthleteId) &&
                                                              c.LeagueId == item.LeagueId && c.DateCompleted == null);

            if (challengerOngoing.Count() > 0)
            {
                throw "You already have an existing challenge underway.".ToException(Request);
            }

            //Check to see if there is already a challenge between the two athletes for this league
            var history = _context.Challenges.Where(c => ((c.ChallengeeAthleteId == item.ChallengeeAthleteId && c.ChallengerAthleteId == item.ChallengerAthleteId) ||
                                                          (c.ChallengeeAthleteId == item.ChallengerAthleteId && c.ChallengerAthleteId == item.ChallengeeAthleteId)) &&
                                                    c.LeagueId == item.LeagueId).OrderByDescending(c => c.DateCompleted);

            var league        = _context.Leagues.SingleOrDefault(l => l.Id == item.LeagueId);
            var lastChallenge = history.FirstOrDefault();

            if (lastChallenge != null && lastChallenge.DateCompleted != null &&
                lastChallenge.ChallengerAthleteId == item.ChallengerAthleteId &&                 //is it the same athlete challenging again
                lastChallenge.GetChallengerWinningGames().Count() < lastChallenge.GetChallengeeWinningGames().Count() &&                 //did the challenger lose the previous match
                DateTime.UtcNow.Subtract(lastChallenge.DateCompleted.Value.UtcDateTime).TotalHours < league.MinHoursBetweenChallenge)                    //has enough time passed
            {
                throw "You must wait at least {0} hours before challenging again".Fmt(league.MinHoursBetweenChallenge).ToException(Request);
            }

            Challenge current = await InsertAsync(item.ToChallenge());

            var result = CreatedAtRoute("Tables", new { id = current.Id }, current.ToChallengeDto());

            var message = "{0}: You have been challenged to a duel by {1}!".Fmt(league.Name, challenger.Alias);
            var payload = new NotificationPayload
            {
                Action  = PushActions.ChallengePosted,
                Payload = { { "challengeId", current.Id }, { "leagueId", current.LeagueId } }
            };

            //Not awaiting so the user's result is not delayed
            _notificationController.NotifyByTag(message, current.ChallengeeAthleteId, payload);
            return(result);
        }