public override async Task Rematch(RematchRequest request, IServerStreamWriter <RematchResponse> responseStream, ServerCallContext context)
        {
            var username = GetUsername(request.Username, request.TwitterLogged);
            var dto      = await _repository.GetMatch(request.MatchId);

            if (dto.Result.Value == (int)Result.Pending)
            {
                await _repository.SaveMatchChallenger(request.MatchId, username);

                await responseStream.WriteAsync(new RematchResponse { HasStarted = true });

                return;
            }

            await _repository.CreateMatch(request.MatchId, username, UnknownUser);

            await responseStream.WriteAsync(new RematchResponse());

            dto = await _repository.GetMatch(request.MatchId);

            while (!context.CancellationToken.IsCancellationRequested && dto.Challenger.Name == UnknownUser)
            {
                await Task.Delay(_multiplayerSettings.GameStatusUpdateDelay);

                dto = await _repository.GetMatch(request.MatchId);

                if (dto == null)
                {
                    _logger.LogDebug($"{username} -> dto is null");
                    return;
                }

                var matchExpired = DateTime.UtcNow.AddSeconds(-_multiplayerSettings.GameStatusMaxWait) > dto.WhenUtc;
                if (matchExpired)
                {
                    _logger.LogDebug($"{username} -> rematch expired");
                    await _repository.DeleteMatch(request.MatchId);

                    return;
                }

                await responseStream.WriteAsync(new RematchResponse());
            }

            await responseStream.WriteAsync(new RematchResponse { HasStarted = true });
        }
        public override async Task Rematch(RematchRequest request, IServerStreamWriter <RematchResponse> responseStream, ServerCallContext context)
        {
            var apiRequest = new GameApi.Proto.RematchRequest()
            {
                MatchId       = request.MatchId,
                Username      = request.Username,
                TwitterLogged = request.TwitterLogged
            };

            using var stream = _multiplayerGameManagerClient.Rematch(apiRequest);
            while (await stream.ResponseStream.MoveNext(context.CancellationToken))
            {
                var response = stream.ResponseStream.Current;

                await responseStream.WriteAsync(new RematchResponse
                {
                    HasStarted  = response.HasStarted,
                    IsCancelled = response.IsCancelled
                });
            }
        }
Exemplo n.º 3
0
        public async Task <bool> Rematch(string matchId, string username, bool isTwitterUser)
        {
            var request = new RematchRequest()
            {
                MatchId       = matchId,
                Username      = username,
                TwitterLogged = isTwitterUser
            };

            using var stream = _multiplayerGameManagerClient.Rematch(request);
            while (await stream.ResponseStream.MoveNext(CancellationToken.None))
            {
                var response = stream.ResponseStream.Current;
                if (response.HasStarted)
                {
                    return(true);
                }
            }

            return(false);
        }