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

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

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

            var isMaster   = dto.PlayerName == username;
            var gameStatus = isMaster ? CreateGameStatusForMaster(dto) : CreateGameStatusForOpponent(dto);
            await responseStream.WriteAsync(gameStatus);

            _logger.LogDebug($"{username} -> Updated {gameStatus.User} vs {gameStatus.Challenger} /{gameStatus.UserPick}-{gameStatus.ChallengerPick}/");
            while (!context.CancellationToken.IsCancellationRequested && gameStatus.Result == Result.Pending)
            {
                await Task.Delay(_multiplayerSettings.GameStatusUpdateDelay);

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

                if (dto == null)
                {
                    _logger.LogDebug($"{username} -> dto is null");
                    await responseStream.WriteAsync(_cancelledMatch);

                    return;
                }

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

                    await responseStream.WriteAsync(_cancelledMatch);

                    return;
                }

                gameStatus = isMaster ? CreateGameStatusForMaster(dto) : CreateGameStatusForOpponent(dto);
                _logger.LogDebug($"{username} -> Updated {gameStatus.User} vs {gameStatus.Challenger} /{gameStatus.UserPick}-{gameStatus.ChallengerPick}/");
                await responseStream.WriteAsync(gameStatus);
            }
        }
Beispiel #2
0
        public async Task UnmatchProfile(string profileId, string matchId)
        {
            var match = await _matchesRepository.GetMatch(matchId);

            var matchProfileId = match.ProfileMatches.FirstOrDefault(pm => pm.ProfileId != profileId)?.ProfileId ?? string.Empty;

            var pairing = await _pairingsRepository.GetPairing(profileId, matchProfileId);

            if (pairing != null)
            {
                pairing.IsLiked   = false;
                pairing.UpdatedAt = DateTime.UtcNow;
            }

            await _matchesRepository.DeleteMatch(matchId);

            await _pairingsRepository.UpsertPairingData(pairing);
        }