예제 #1
0
        public void LocalRefresh(bool refreshLeagues = true)
        {
            if (Athlete != null)
            {
                Athlete.LocalRefresh();
            }

            if (League != null && refreshLeagues)
            {
                League.LocalRefresh();
            }
        }
예제 #2
0
        /// <summary>
        /// This app uses Azure as the backend which utilizes Notifications hubs
        /// </summary>
        /// <returns>The athlete notification hub registration.</returns>
        public Task UpdateAthleteNotificationHubRegistration(Athlete athlete, bool forceSave = false, bool sendTestPush = false)
        {
            return(new Task(() =>
            {
                if (athlete == null)
                {
                    throw new ArgumentNullException("athlete");
                }

                if (athlete.Id == null || athlete.DeviceToken == null)
                {
                    return;
                }

                var tags = new List <string> {
                    App.CurrentAthlete.Id,
                    "All",
                };

                App.CurrentAthlete.LocalRefresh();
                App.CurrentAthlete.Memberships.Select(m => m.LeagueId).ToList().ForEach(tags.Add);
                athlete.DevicePlatform = Xamarin.Forms.Device.OS.ToString();

                var reg = new DeviceRegistration {
                    Handle = athlete.DeviceToken,
                    Platform = athlete.DevicePlatform,
                    Tags = tags.ToArray()
                };

                var registrationId = Client.InvokeApiAsync <DeviceRegistration, string>("registerWithHub", reg, HttpMethod.Put, null).Result;
                athlete.NotificationRegistrationId = registrationId;

                //Used to verify the device is successfully registered with the backend
                if (sendTestPush)
                {
                    var qs = new Dictionary <string, string>();
                    qs.Add("athleteId", athlete.Id);
                    Client.InvokeApiAsync("sendTestPushNotification", null, HttpMethod.Get, qs).Wait();
                }

                if (athlete.IsDirty || forceSave)
                {
                    var task = SaveAthlete(athlete);
                    task.Start();
                    task.Wait();
                }
            }));
        }
예제 #3
0
        /// <summary>
        /// Registers an athlete with the backend and returns the new athlete profile
        /// </summary>
        async Task <Athlete> RegisterAthlete(UserProfile profile)
        {
            AuthenticationStatus = "Registering athlete";
            var athlete = new Athlete(profile);

            var task = AzureService.Instance.SaveAthlete(athlete);

            await RunSafe(task);

            if (task.IsCompleted && task.IsFaulted)
            {
                return(null);
            }

            "You're now an officially registered athlete!".ToToast();
            return(athlete);
        }
예제 #4
0
        public static string GetChallengeConflictReason(this Membership membership, Athlete athlete)
        {
            if (!membership.League.HasStarted)
            {
                return("The league hasn't started yet");
            }

            if (membership.Athlete == null || athlete.Id == membership.Athlete.Id)
            {
                return("You cannot challenge yourself");
            }

            //Check to see if they are part of the same league
            var otherMembership = athlete.Memberships.SingleOrDefault(m => m.LeagueId == membership.LeagueId);

            if (otherMembership != null)
            {
                //Ensure they are within range and lower in rank than the challengee
                var diff = otherMembership.CurrentRank - membership.CurrentRank;
                if (diff <= 0 || diff > membership.League.MaxChallengeRange)
                {
                    return("{0} is not within a valid range of being challenged".Fmt(membership.Athlete.Alias));
                }
            }
            else
            {
                return("{0} is not a member of the {1} league".Fmt(membership.Athlete.Alias, membership.League.Name));
            }

            var challenge = membership.GetOngoingChallenge(membership.Athlete);

            if (challenge != null)
            {
                return("{0} already has an ongoing challenge with {1}".Fmt(membership.Athlete.Alias, challenge.Opponent(membership.Athlete.Id).Alias));
            }

            //Athlete is within range but let's make sure there aren't already challenges out there
            challenge = membership.GetOngoingChallenge(athlete);
            if (challenge != null)
            {
                var player = athlete.Id == App.CurrentAthlete.Id ? "You already have" : athlete.Alias + " already has";
                return("{0} an ongoing challenge with {1}".Fmt(player, challenge.Opponent(athlete.Id).Alias));
            }

            return(null);
        }
예제 #5
0
        /// <summary>
        /// Gets the athlete's profile from the Azure backend
        /// </summary>
        async Task <Athlete> GetAthletesProfile()
        {
            Athlete athlete = null;

            //Let's try to load based on email address
            if (athlete == null && AuthUserProfile != null && !AuthUserProfile.Email.IsEmpty())
            {
                var task = AzureService.Instance.GetAthleteByEmail(AuthUserProfile.Email);
                await RunSafe(task);

                if (task.IsCompleted && !task.IsFaulted)
                {
                    athlete = task.Result;
                }
            }

            return(athlete);
        }
예제 #6
0
        public Task UnregisterAthleteForPush(Athlete athlete)
        {
            return(new Task(() =>
            {
                if (athlete == null || athlete.NotificationRegistrationId == null)
                {
                    return;
                }

                var values = new Dictionary <string, string> {
                    {
                        "id",
                        athlete.NotificationRegistrationId
                    }
                };
                var registrationId = Client.InvokeApiAsync <string>("unregister", HttpMethod.Delete, values).Result;
            }));
        }
예제 #7
0
        public Task SaveAthlete(Athlete athlete)
        {
            return(new Task(() =>
            {
                athlete.UserId = AzureService.Instance.Client.CurrentUser.UserId;

                if (athlete.Id == null)
                {
                    Client.GetTable <Athlete>().InsertAsync(athlete).Wait();
                }
                else
                {
                    Client.GetTable <Athlete>().UpdateAsync(athlete).Wait();
                }

                DataManager.Instance.Athletes.AddOrUpdate(athlete);
            }));
        }
예제 #8
0
        async public Task GetLeagues(bool forceRefresh = false)
        {
            if (_hasLoadedLeaguesBefore && !forceRefresh)
            {
                Athlete.LocalRefresh();
                return;
            }

            using (new Busy(this))
            {
                await AthleteViewModel.GetLeagues(forceRefresh);

                LocalRefresh();

                //Settings.Instance.LeagueColors.Clear();
                DataManager.Instance.Leagues.Values.ToList().EnsureLeaguesThemed();
            }

            _hasLoadedLeaguesBefore = true;
        }
예제 #9
0
        public Task SaveAthlete(Athlete athlete)
        {
            return(new Task(() =>
            {
                if (athlete.Id == null)
                {
                    if (athlete.Email == "*****@*****.**")
                    {
                        athlete.IsAdmin = true;
                    }

                    Client.GetTable <Athlete>().InsertAsync(athlete).Wait();
                }
                else
                {
                    Client.GetTable <Athlete>().UpdateAsync(athlete).Wait();
                }

                DataManager.Instance.Athletes.AddOrUpdate(athlete);
            }));
        }
예제 #10
0
        public Task <Athlete> GetAthleteById(string id, bool force = false)
        {
            return(new Task <Athlete>(() =>
            {
                Athlete a = null;

                if (!force)
                {
                    DataManager.Instance.Athletes.TryGetValue(id, out a);
                }

                a = a ?? Client.GetTable <Athlete>().LookupAsync(id).Result;

                if (a != null)
                {
                    a.IsDirty = false;
                    DataManager.Instance.Athletes.AddOrUpdate(a);
                }

                return a;
            }));
        }
예제 #11
0
        public void CreateChallenge(Athlete challenger, Athlete challengee, League league)
        {
            var time = TimeSpan.FromTicks(DateTime.Now.AddMinutes(60).Subtract(DateTime.Today).Ticks);

            if (time.Ticks > TimeSpan.TicksPerDay)
            {
                time = time.Subtract(TimeSpan.FromTicks(TimeSpan.TicksPerDay));
            }

            SelectedTime = time;
            SelectedDate = DateTime.Today;

            var membership = league.Memberships.SingleOrDefault(m => m.AthleteId == challengee.Id);

            Challenge = new Challenge {
                BattleForRank       = membership.CurrentRank,
                ChallengerAthleteId = challenger.Id,
                ChallengeeAthleteId = challengee.Id,
                ProposedTime        = SelectedDateTime,
                LeagueId            = league.Id,
            };
        }
예제 #12
0
 public bool HasExistingChallengeWithAthlete(Athlete athlete)
 {
     return(GetOngoingChallenge(athlete) != null);
 }
예제 #13
0
 public AthleteEditViewModel(Athlete athlete = null)
 {
     Athlete = athlete ?? new Athlete();
 }
예제 #14
0
 public AthleteEditViewModel()
 {
     Athlete = new Athlete();
 }
예제 #15
0
        public override void NotifyPropertiesChanged()
        {
            _athlete = null;

            base.NotifyPropertiesChanged();
        }
예제 #16
0
 public static bool CanChallengeAthlete(this Membership membership, Athlete athlete)
 {
     return(membership.GetChallengeConflictReason(athlete) == null);
 }