コード例 #1
0
        public async Task <ContestLearnerUpdated> ExecuteAsync(
            ContestLearnerUpdate contestLearnerUpdate)
        {
            var contest =
                await _contestRepository.FetchByIdAsync(
                    contestLearnerUpdate.ContestId);

            if (contest == null)
            {
                throw new NullReferenceException($"Contest {contestLearnerUpdate.ContestId} not found");
            }

            var contestLearner =
                await _contestLearnerRepository.FetchByIdAsync(
                    contestLearnerUpdate.ContestId,
                    contestLearnerUpdate.Id);

            if (contestLearner == null)
            {
                throw new ArgumentOutOfRangeException(
                          $"Contest learner {contestLearnerUpdate.UserName} not found");
            }

            contestLearner.UserName =
                contestLearnerUpdate.UserName.ToLower();
            contestLearner.StartValue =
                contestLearnerUpdate.StartValue;

            var microsoftProfile =
                await _microsoftProfileRepository.FetchProfileAsync(
                    contestLearner.UserName);

            contestLearner =
                _contestLearnerTargetValueUpdater.Update(
                    contest,
                    contestLearner);

            if (contest.IsStatus(ContestStatus.InProgress))
            {
                contestLearner =
                    _contestLearnerCurrentValueUpdater.Update(
                        contest,
                        contestLearner,
                        microsoftProfile);
            }

            contestLearner =
                await _contestLearnerRepository.UpdateByIdAsync(
                    contestLearnerUpdate.ContestId,
                    contestLearnerUpdate.Id,
                    contestLearner);

            var contestLearnerUpdated =
                _mapper.Map(contestLearner, new ContestLearnerUpdated());

            await _eventPublisher.PublishAsync("contestlearner.updated", contestLearnerUpdated);

            return(contestLearnerUpdated);
        }
コード例 #2
0
        public async Task <ContestLearnerStarted> ExecuteAsync(
            ContestLearnerStart contestLearnerStart)
        {
            var contestLearner =
                await _contestLearnerRepository.FetchByIdAsync(
                    contestLearnerStart.ContestId,
                    contestLearnerStart.Id);

            if (contestLearner == null)
            {
                throw new ArgumentOutOfRangeException($"Contest learner {contestLearnerStart.Id} not found.");
            }

            var contest =
                await _contestRepository.FetchByIdAsync(
                    contestLearner.ContestId);

            var contestLearnerStarted =
                new ContestLearnerStarted();

            try
            {
                var microsoftProfile =
                    await _microsoftProfileRepository.FetchProfileAsync(
                        contestLearner.UserName);

                contestLearner.Level =
                    microsoftProfile.GameStatus.Level.LevelNumber;
                contestLearner.Points =
                    _experiencePointsCalculator.Calculate(
                        microsoftProfile.GameStatus.Level.LevelNumber,
                        microsoftProfile.GameStatus.CurrentLevelPointsEarned);

                contestLearner =
                    _contestLearnerStartValueUpdater.Update(
                        contest,
                        contestLearner,
                        microsoftProfile,
                        true);

                contestLearner =
                    _contestLearnerCurrentValueUpdater.Update(
                        contest,
                        contestLearner,
                        microsoftProfile);

                contestLearner.LastProgressUpdateOn =
                    DateTime.UtcNow;

                contestLearner =
                    await _contestLearnerRepository.UpdateByIdAsync(
                        contestLearnerStart.ContestId,
                        contestLearnerStart.Id,
                        contestLearner);

                contestLearnerStarted =
                    _mapper.Map(contestLearner, contestLearnerStarted);

                return(contestLearnerStarted);
            }
            catch (Exception ex)
            {
                throw new MicrosoftProfileException(contestLearner, ex);
            }
        }
コード例 #3
0
        public async Task <ContestLearnerProgressUpdated> ExecuteAsync(
            ContestLearnerProgressUpdate contestLearnerProgressUpdate)
        {
            var contest =
                await _contestRepository.FetchByIdAsync(
                    contestLearnerProgressUpdate.ContestId);

            if (contest == null)
            {
                throw new NullReferenceException($"Contest {contestLearnerProgressUpdate.ContestId} not found");
            }

            var contestLearner =
                await _contestLearnerRepository.FetchByIdAsync(
                    contestLearnerProgressUpdate.ContestId,
                    contestLearnerProgressUpdate.Id);

            var contestLearnerProgressUpdated =
                new ContestLearnerProgressUpdated();

            if (contest.Status != ContestStatus.InProgress ||
                string.IsNullOrWhiteSpace(contestLearner.DisplayName))
            {
                contestLearnerProgressUpdated =
                    _mapper.Map(contestLearner, contestLearnerProgressUpdated);

                return(contestLearnerProgressUpdated);
            }

            try
            {
                var microsoftProfile =
                    await _microsoftProfileRepository.FetchProfileAsync(
                        contestLearner.UserName);

                contestLearner.Level =
                    microsoftProfile.GameStatus.Level.LevelNumber;
                contestLearner.Points =
                    _experiencePointsCalculator.Calculate(
                        microsoftProfile.GameStatus.Level.LevelNumber,
                        microsoftProfile.GameStatus.CurrentLevelPointsEarned);

                contestLearner =
                    _contestLearnerTargetValueUpdater.Update(
                        contest,
                        contestLearner);

                contestLearner =
                    _contestLearnerCurrentValueUpdater.Update(
                        contest,
                        contestLearner,
                        microsoftProfile);

                contestLearner.LastProgressUpdateOn =
                    DateTime.UtcNow;

                contestLearner =
                    await _contestLearnerRepository.UpdateByIdAsync(
                        contestLearnerProgressUpdate.ContestId,
                        contestLearnerProgressUpdate.Id,
                        contestLearner);

                contestLearnerProgressUpdated =
                    _mapper.Map(contestLearner, contestLearnerProgressUpdated);

                return(contestLearnerProgressUpdated);
            }
            catch (Exception ex)
            {
                throw new MicrosoftProfileException(contestLearner, ex);
            }
        }