Пример #1
0
        private async void TimeToAcceptGame(PotentialMatch potentialMatch)
        {
            await Task.Delay(TimeSpan.FromSeconds(30));

            _matches.TryRemove(potentialMatch.Id, out var result);
            MatchAbandoned?.Invoke(result);
        }
Пример #2
0
        public async Task <bool> AcceptGame(Guid matchId, Guid playerId)
        {
            _matches.TryGetValue(matchId, out var result);

            if (result == null)
            {
                return(false);
            }

            result.PlayersWaitingForGames.ForEach(p =>
            {
                if (p.Id == playerId)
                {
                    p.Accepted = true;
                }
            });


            if (result.PlayersWaitingForGames.Select(p => p.Accepted).Any(a => a == false))
            {
                var updated = new PotentialMatch(result.PlayersWaitingForGames)
                {
                    Id = result.Id,
                };
                _matches.TryUpdate(matchId, updated, result);
                return(true);
            }

            _matches.TryRemove(result.Id, out _);
            MatchAcceptedByBothPlayers?.Invoke(result);
            return(true);
        }
Пример #3
0
        private async void AttemptToMatchPlayers()
        {
            _pollingCancellationToken = new CancellationTokenSource();
            while (!_pollingCancellationToken.IsCancellationRequested)
            {
                if (_queue.Count > 1)
                {
                    _queue.TryRemove(_queue.Keys.ElementAt(0), out var playerOne);
                    _queue.TryRemove(_queue.Keys.ElementAt(0), out var playerTwo);

                    var potentialMatch = new PotentialMatch(new List <PlayerWaitingForGame>
                    {
                        playerOne,
                        playerTwo
                    });
                    MatchFound?.Invoke(potentialMatch);
                    _matches.TryAdd(potentialMatch.Id, potentialMatch);
                    Task.Run(() => TimeToAcceptGame(potentialMatch));
                }

                await Task.Delay(5000);
            }
        }