NotifyByTag() public method

public NotifyByTag ( string message, string tag, Sport.Shared.NotificationPayload payload = null, int badgeCount = null ) : System.Threading.Tasks.Task
message string
tag string
payload Sport.Shared.NotificationPayload
badgeCount int
return System.Threading.Tasks.Task
示例#1
0
        // POST tables/Member
        public async Task <IHttpActionResult> PostMembership(MembershipDto item)
        {
            Membership current;
            var        exists = _context.Memberships.FirstOrDefault(m => m.AthleteId == item.AthleteId && m.LeagueId == item.LeagueId && m.AbandonDate == null);

            if (exists != null)
            {
                //Athlete is already a member of this league
                current = exists;
            }
            else
            {
                var prior = _context.Memberships.Where(m => m.LeagueId == item.LeagueId && m.AbandonDate == null)
                            .OrderByDescending(m => m.CurrentRank).FirstOrDefault();

                if (prior != null)
                {
                    item.CurrentRank = prior.CurrentRank + 1;
                }

                current = await InsertAsync(item.ToMembership());

                if (WebApiConfig.IsDemoMode)
                {
                    //Keep the lists to a reasonable amount
                    var list = _context.Memberships.Where(m => m.LeagueId == item.LeagueId && m.AbandonDate == null).ToList();
                    if (list.Count > WebApiConfig.MaxLeagueMembershipCount)
                    {
                        var diff   = list.Count - WebApiConfig.MaxLeagueMembershipCount;
                        var oldest = list.OrderBy(m => m.CreatedAt).Take(diff);

                        foreach (var m in oldest)
                        {
                            await DeleteMembership(m.Id);
                        }
                    }
                }
            }

            try
            {
                var leagueName = _context.Leagues.Where(l => l.Id == item.LeagueId).Select(l => l.Name).ToList().First();
                var athlete    = _context.Athletes.Where(a => a.Id == item.AthleteId).First();
                var message    = "{0} joined the {1} league".Fmt(athlete.Alias, leagueName);
                await _notificationController.NotifyByTag(message, item.LeagueId);
            }
            catch (Exception e)
            {
                //TODO log to Insights
                Console.WriteLine(e);
            }

            return(CreatedAtRoute("Tables", new { id = current.Id }, current));
        }
示例#2
0
        public DateTime StartLeague(string id)
        {
            _authController.EnsureAdmin(Request);
            var league = _context.Leagues.SingleOrDefault(l => l.Id == id);

            league.HasStarted = true;
            league.StartDate  = DateTime.Now.ToUniversalTime();

            _context.SaveChanges();

            var message = "The {0} league has officially started!".Fmt(league.Name);
            var payload = new NotificationPayload
            {
                Action  = PushActions.LeagueStarted,
                Payload = { { "leagueId", id } }
            };

            _notificationController.NotifyByTag(message, league.Id, payload);
            return(league.StartDate.Value.UtcDateTime);
        }
示例#3
0
        public DateTime StartLeague(string id)
        {
            _authController.EnsureAdmin(Request);
            var league = _context.Leagues.SingleOrDefault(l => l.Id == id);

            league.HasStarted = true;
            league.StartDate  = DateTime.Now.ToUniversalTime();

            var memberships = _context.Memberships.Where(m => m.LeagueId == id && m.AbandonDate == null).ToList();

            if (memberships.Count < 2)
            {
                //TODO Enable this validation
                //return Conflict("Must have at least 2 members before starting a league.");
            }

            memberships.Shuffle();

            //Randomize the athlete rankage when the league kicks off
            var i = 0;

            foreach (var m in memberships)
            {
                m.CurrentRank = i;
                i++;
            }
            _context.SaveChanges();

            var message = "The {0} league has officially started!".Fmt(league.Name);
            var payload = new NotificationPayload
            {
                Action  = PushActions.LeagueStarted,
                Payload = { { "leagueId", id } }
            };

            _notificationController.NotifyByTag(message, league.Id, payload);
            return(league.StartDate.Value.UtcDateTime);
        }
示例#4
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
        }
示例#5
0
        // POST tables/Member
        public async Task <IHttpActionResult> PostMembership(MembershipDto item)
        {
            Membership current = null;
            var        exists  = _context.Memberships.FirstOrDefault(m => m.AthleteId == item.AthleteId && m.LeagueId == item.LeagueId && m.AbandonDate == null);

            if (exists != null)
            {
                //Athlete is already a member of this league
                current = exists;
            }
            else
            {
                try
                {
                    var membership = item.ToMembership();
                    current = await InsertAsync(membership);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

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

            if (Startup.IsDemoMode)
            {
                var tasks = new List <Task>();
                //Keep the lists to a reasonable amount for the public-facing service
                lock (_syncObject)
                {
                    var query = _context.Memberships.Where(m => m.LeagueId == item.LeagueId && m.AbandonDate == null);
                    var list  = ConvertMembershipToDto(query).ToList();

                    if (list.Count > Startup.MaxLeagueMembershipCount)
                    {
                        var diff   = list.Count - Startup.MaxLeagueMembershipCount;
                        var oldest = list.OrderBy(m => m.CreatedAt).Take(diff).Select(m => m.Id).ToList();

                        foreach (var mId in oldest)
                        {
                            if (mId == current.Id)
                            {
                                continue;
                            }

                            tasks.Add(DeleteMembershipInternal(mId));
                        }
                    }
                }

                try
                {
                    await Task.WhenAll(tasks);
                }
                catch (Exception ex)
                {
                    //TODO log to Insights
                    Console.WriteLine(ex);
                }
            }

            try
            {
                var leagueName = _context.Leagues.Where(l => l.Id == item.LeagueId).Select(l => l.Name).ToList().First();
                var athlete    = _context.Athletes.Where(a => a.Id == item.AthleteId).First();
                var message    = "{0} joined the {1} league".Fmt(athlete.Alias, leagueName);
                await _notificationController.NotifyByTag(message, item.LeagueId);
            }
            catch (Exception e)
            {
                //TODO log to Insights
                Console.WriteLine(e);
            }

            return(result);
        }
示例#6
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);
        }