Exemple #1
0
        public RegisterTagResponse RegisterTag(RegisterTagRequest request)
        {
            var tag = TagRepository.RetrieveTag(request.BeaconId,
                                                request.BeaconMajorVersion,
                                                request.BeaconMinorVersion);

            if (tag == null || !tag.Active)
            {
                return(CreateFailedTagResponse("Tag not active"));
            }

            var user = UserRepository.RetrieveUser(request.SessionId);

            if (user == null)
            {
                return(CreateFailedTagResponse("Session not Valid"));
            }

            var tagType = TagTypeRepository.RetrieveTagType(tag.TagTypeId);

            if (tagType == null)
            {
                return(CreateFailedTagResponse("Invalid Tag Type"));
            }

            if (TagRepository.CheckTagIsNotWithinTimeout(user.UserId, tag.TagId, tagType.LockoutTimePeriod))
            {
                return(CreateFailedTagResponse("Tag seen within Timeout"));
            }

            var pointsScored = tagType.Points + tag.AdditionalPoints;

            var achievements = AchievementRepository.RetrieveAvailableAchievements(user.UserId, tag.TagId);

            var achievementNames = new List <string>();

            if (achievements != null)
            {
                foreach (var achievement in achievements)
                {
                    achievementNames.Add(achievement.AchievementName);
                    if (achievement.RewardPoints > 0)
                    {
                        TransactionRepository.CreateTransaction(user.UserId, achievement.RewardPoints, TransactionType.AchievementBonus);
                    }
                }
            }

            TransactionRepository.CreateTransactionForUserAndTag(user.UserId, pointsScored, tag.TagId, TransactionType.Tag);

            user = UserRepository.UpdateUser(user);

            var top = UserRepository.RetrieveTopLeaderboard(3, user.UserId);

            return(new RegisterTagResponse
            {
                PointsScored = pointsScored,
                NewPointsTotal = TransactionRepository.GetUsersPoints(user.UserId),
                UsablePoints = TransactionRepository.GetUsersUsablePoints(user.UserId),
                Achievements = achievementNames.ToArray(),
                Top10 = top.Select(LeaderboardResponseEntry.FromModel).ToArray(),
                Ranking = 0,
                LockoutTime = tagType.LockoutTimePeriod,
                Success = true,
                ErrorMessage = string.Empty
            });
        }