Ejemplo n.º 1
0
        public void FetchChallengeMatches_ShouldBeOfTypeFetchChallengeMatchesResponse()
        {
            // 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 FetchChallengeMatchesRequest
            {
                Game         = EnumGame.LeagueOfLegends,
                EndedAt      = DateTime.UtcNow.ToTimestamp(),
                StartedAt    = (DateTime.UtcNow - TimeSpan.FromDays(1)).ToTimestamp(),
                Participants =
                {
                    new ChallengeParticipantDto
                    {
                        ChallengeId    = new ChallengeId(),
                        GamePlayerId   = "testID1",
                        Id             = new Guid().ToString(),
                        Score          = 10,
                        SynchronizedAt = DateTime.UtcNow.ToTimestamp(),
                        UserId         = new UserId()
                    },
                    new ChallengeParticipantDto
                    {
                        ChallengeId    = new ChallengeId(),
                        GamePlayerId   = "testID2",
                        Id             = new Guid().ToString(),
                        Score          = 20,
                        SynchronizedAt = DateTime.UtcNow.ToTimestamp(),
                        UserId         = new UserId()
                    },
                    new ChallengeParticipantDto
                    {
                        ChallengeId    = new ChallengeId(),
                        GamePlayerId   = "testID3",
                        Id             = new Guid().ToString(),
                        Score          = 50,
                        SynchronizedAt = DateTime.UtcNow.ToTimestamp(),
                        UserId         = new UserId()
                    }
                }
            };

            var client = new GameService.GameServiceClient(host.CreateChannel());

            // Act
            var response = client.FetchChallengeMatches(request);

            //Assert
            response.Should().BeOfType <AsyncServerStreamingCall <FetchChallengeMatchesResponse> >();
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        public override async Task FetchChallengeMatches(
            FetchChallengeMatchesRequest request,
            IServerStreamWriter <FetchChallengeMatchesResponse> responseStream,
            ServerCallContext context
            )
        {
            var game = request.Game.ToEnumeration <Game>();

            var startedAt = request.StartedAt.ToDateTime();

            var endedAt = request.EndedAt.ToDateTime();

            foreach (var participant in request.Participants)
            {
                var participantId = participant.Id.ParseEntityId <ParticipantId>();

                var gamePlayerId = participant.GamePlayerId.ParseStringId <PlayerId>();

                var matchIds = participant.Matches.Select(match => match.Id).ToImmutableHashSet();

                try
                {
                    var matches = await _challengeService.GetMatchesAsync(
                        game,
                        gamePlayerId,
                        startedAt,
                        endedAt,
                        matchIds);

                    var response = new FetchChallengeMatchesResponse
                    {
                        GamePlayerId = gamePlayerId,
                        Matches      =
                        {
                            matches.Select(
                                match => new GameMatchDto
                            {
                                GameUuid      = match.GameUuid,
                                GameCreatedAt = match.GameCreatedAt.ToTimestampUtc(),
                                GameDuration  = match.GameDuration.ToDuration(),
                                Stats         =
                                {
                                    match.Stats
                                }
                            })
                        }
                    };

                    await responseStream.WriteAsync(response);
                }
                catch (Exception exception)
                {
                    _logger.LogCritical(exception, $"Failed to fetch {game} matches for the participant '{participantId}'. (gamePlayerId=\"{gamePlayerId}\")");

                    _logger.LogCritical(
                        JsonConvert.SerializeObject(
                            new
                    {
                        ParticipantId = participantId,
                        Game          = game,
                        GamePlayerId  = gamePlayerId,
                        StartedAt     = startedAt,
                        EndedAt       = endedAt,
                        MatchIds      = matchIds.ToArray()
                    },
                            Formatting.Indented));

                    var response = new FetchChallengeMatchesResponse
                    {
                        GamePlayerId = gamePlayerId
                    };

                    await responseStream.WriteAsync(response);
                }
            }
        }