Exemple #1
0
        private void _UpdateUserAchievements(Activity activity, String uid)
        {
            var user        = _uAcc.FindUser(uid);
            var oldDistance = user.LifetimeDistance;
            var oldDuration = user.LifetimeDuration;
            var oldSteps    = user.LifetimeSteps;
            var newDistance = oldDistance + activity.Distance;
            var newDuration = oldDuration + activity.Duration;
            var newSteps    = oldSteps + activity.Steps;

            user.LifetimeDistance = newDistance;
            user.LifetimeDuration = newDuration;
            user.LifetimeSteps    = newSteps;
            _uAcc.UpdateUser(user);

            foreach (Badge b in _bAcc.GetBadges())
            {
                bool justEarned = false;
                switch (b.Field)
                {
                case Field.DISTANCE:
                    justEarned = (oldDistance < b.Threshold && newDistance >= b.Threshold);
                    break;

                case Field.DURATION:
                    justEarned = oldDuration < b.Threshold && newDuration >= b.Threshold;
                    break;

                case Field.STEPS:
                    justEarned = oldSteps < b.Threshold && newSteps >= b.Threshold;
                    break;
                }
                if (justEarned)
                {
                    _ubAcc.CreateUserBadge(new UserBadge()
                    {
                        BadgeID       = b.ID,
                        DateCompleted = DateTime.Now,
                        UserID        = user.Id,
                    });
                }
            }

            foreach (Goal b in _gAcc.GetGoals()) // TODO The duplication here feels bad; is there some clean way to give goals and badges the same interface/prevent this?
            {
                bool justEarned = false;
                switch (b.Field)
                {
                case Field.DISTANCE:
                    justEarned = (oldDistance < b.Threshold && newDistance >= b.Threshold);
                    break;

                case Field.DURATION:
                    justEarned = oldDuration < b.Threshold && newDuration >= b.Threshold;
                    break;

                case Field.STEPS:
                    justEarned = oldSteps < b.Threshold && newSteps >= b.Threshold;
                    break;
                }
                if (justEarned)
                {
                    var ug = _ugAcc.GetUserGoal(b.ID, uid);
                    if (ug != null)
                    {
                        ug.DateCompleted = DateTime.Now;
                        _ugAcc.UpdateUserGoal(ug);
                    }
                }
            }
        }
Exemple #2
0
 public Models.UserGoal UpdateUserGoal(Models.UserGoal ub)
 {
     return(_acc.UpdateUserGoal(ub));
 }