Пример #1
0
        public async Task SnapshotChallengeParticipant_ShouldThrowFailedPreconditionRpcException()
        {
            // Arrange
            var          userId      = new UserId();
            const string email       = "*****@*****.**";
            var          challengeId = new ChallengeId();

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            await host.Server.UsingScopeAsync(
                async scope =>
            {
                var challengeRepository = scope.GetRequiredService <IChallengeRepository>();

                var challenge = new Challenge(
                    challengeId,
                    new ChallengeName("test"),
                    Game.LeagueOfLegends,
                    BestOf.Five,
                    Entries.Two,
                    new ChallengeTimeline(new UtcNowDateTimeProvider(), ChallengeDuration.FiveDays),
                    new Scoring());

                challenge.Register(
                    new Participant(
                        new ParticipantId(),
                        new UserId(),
                        new PlayerId(),
                        new UtcNowDateTimeProvider()));

                challenge.Register(
                    new Participant(
                        new ParticipantId(),
                        new UserId(),
                        new PlayerId(),
                        new UtcNowDateTimeProvider()));

                challengeRepository.Create(challenge);
                await challengeRepository.CommitAsync(false);
            });

            var request = new SnapshotChallengeParticipantRequest
            {
                ChallengeId  = challengeId,
                GamePlayerId = new PlayerId()
            };

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act Assert
            var func = new Func <Task>(async() => await client.SnapshotChallengeParticipantAsync(request));

            func.Should().Throw <RpcException>();
        }
Пример #2
0
        public override async Task <SnapshotChallengeParticipantResponse> SnapshotChallengeParticipant(
            SnapshotChallengeParticipantRequest request,
            ServerCallContext context
            )
        {
            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

            if (!await _challengeService.ChallengeExistsAsync(challengeId))
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var challenge = await _challengeService.FindChallengeAsync(challengeId);

            var result = await _challengeService.SnapshotChallengeParticipantAsync(
                challenge,
                request.GamePlayerId.ParseStringId <PlayerId>(),
                new UtcNowDateTimeProvider(),
                scoring => request.Matches.Select(
                    match => new Match(
                        new GameUuid(match.GameUuid),
                        new DateTimeProvider(match.GameCreatedAt.ToDateTime()),
                        match.GameDuration.ToTimeSpan(),
                        scoring.Map(match.Stats),
                        new UtcNowDateTimeProvider()))
                .ToImmutableHashSet());

            if (result.IsValid)
            {
                var response = new SnapshotChallengeParticipantResponse
                {
                    Participant = ChallengeProfile.Map(challenge, result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Пример #3
0
        public async Task SynchronizeChallengesAsync(EnumGame game)
        {
            foreach (var challenge in await this.FetchChallengesAsync(game))
            {
                var fetchChallengeMatchesRequest = new FetchChallengeMatchesRequest
                {
                    Game         = game,
                    StartedAt    = challenge.Timeline.StartedAt,
                    EndedAt      = challenge.Timeline.EndedAt,
                    Participants =
                    {
                        challenge.Participants
                    }
                };

                await foreach (var fetchChallengeMatchesResponse in _gameServiceClient.FetchChallengeMatches(fetchChallengeMatchesRequest)
                               .ResponseStream.ReadAllAsync())
                {
                    var snapshotChallengeParticipantRequest = new SnapshotChallengeParticipantRequest
                    {
                        ChallengeId  = challenge.Id,
                        GamePlayerId = fetchChallengeMatchesResponse.GamePlayerId,
                        Matches      =
                        {
                            fetchChallengeMatchesResponse.Matches
                        }
                    };

                    await _challengeServiceClient.SnapshotChallengeParticipantAsync(snapshotChallengeParticipantRequest);
                }

                var synchronizeChallengeRequest = new SynchronizeChallengeRequest
                {
                    ChallengeId = challenge.Id
                };

                await _challengeServiceClient.SynchronizeChallengeAsync(synchronizeChallengeRequest);
            }
        }
Пример #4
0
        public void SnapshotChallengeParticipant_ShouldThrowNotFoundRpcException()
        {
            // Arrange
            var          userId = new UserId();
            const string email  = "*****@*****.**";

            var claims = new[] { new Claim(JwtClaimTypes.Subject, userId.ToString()), new Claim(JwtClaimTypes.Email, email) };
            var host   = TestHost.WithClaimsFromBearerAuthentication(claims);

            host.Server.CleanupDbContext();

            var request = new SnapshotChallengeParticipantRequest
            {
                ChallengeId  = new ChallengeId(),
                GamePlayerId = new PlayerId()
            };

            var client = new ChallengeService.ChallengeServiceClient(host.CreateChannel());

            // Act Assert
            var func = new Func <Task>(async() => await client.SnapshotChallengeParticipantAsync(request));

            func.Should().Throw <RpcException>();
        }