Ejemplo n.º 1
0
        public IActionResult GetMatchups(int tournamentId)
        {
            var matchups = roundRepository.GetAllMatchups(tournamentId);

            if (matchups == null || !matchups.Any())
            {
                return(NoContent());
            }


            var currentRound = roundRepository.GetCurrentRound(tournamentId);
            var scores       = roundRepository.GetScoresForTournament(tournamentId);

            return(Ok(matchups.Select(x => new RoundMatchupDto
            {
                RoundNumber = x.Key,
                RoundId = x.Any() ? x.First().RoundId : -1,
                Public = currentRound == null ? false : x.Any() && currentRound.Id == x.First().RoundId ? currentRound.Public : true,
                Matchups = x.Select(y => new MatchupDto
                {
                    Id = y.Id,
                    TableNumber = y.TableNumber,
                    Player1Id = y.Player1Id,
                    Player2Id = y.Player2Id ?? -1,
                    Player1Score = scores.SingleOrDefault(s => s.MatchupId == y.Id && s.PlayerId == y.Player1Id)?.Amount ?? 0,
                    Player2Score = scores.SingleOrDefault(s => s.MatchupId == y.Id && s.PlayerId == y.Player2Id)?.Amount ?? 0
                }).ToList()
            })));
        }
Ejemplo n.º 2
0
        public bool CanGenerateNextRound(int tournamentId)
        {
            var currentRound = roundRepository.GetCurrentRound(tournamentId);

            if (currentRound == null)
            {
                return(true);
            }

            var scores = roundRepository.GetScoresForTournament(tournamentId);
            var scoresForCurrentRound = scores.Where(s => s.MatchupId.HasValue && currentRound.Matchups.Select(m => m.Id).Contains(s.MatchupId.Value)).ToList();

            return(scoresForCurrentRound.Count == currentRound.Matchups.Sum(m => m.Player2Id.HasValue ? 2 : 1));
        }
Ejemplo n.º 3
0
        public async Task <Response> Handle(PlayCard request, CancellationToken cancellationToken)
        {
            var playerInGame = await _playerRepository.GetPlayer(request.GameId, request.PlayerId);

            if (playerInGame == null)
            {
                throw new DomainException(ErrorCode.NotAuthorized);
            }

            var validValues = new [] { 0, 0.5, 1, 2, 3, 5, 8, 13, 20, 50, 100 };
            var validTypes  = new[] { "Infinite", "Break", "Number" };

            if (!validTypes.Contains(request.Type))
            {
                throw new DomainException(ErrorCode.InputNotAllowed, $"Card type is not in allowed range. Must be in [{string.Join(", ", validTypes)}]");
            }

            if (request.Value.HasValue && !validValues.Contains(request.Value.Value))
            {
                throw new DomainException(ErrorCode.InputNotAllowed, $"Card value is not in allowed range. Must be in [{string.Join(", ", validValues)}]");
            }

            var currentRound = await _roundRepository.GetCurrentRound(request.GameId);

            await _playCardRepository.PlayCard(request.GameId, request.PlayerId, currentRound.Id, new PlayedCard
            {
                GameId          = request.GameId,
                PlayerId        = request.PlayerId,
                GameRoundId     = currentRound.Id,
                CardType        = request.Type,
                CardValue       = request.Value,
                PlayedTimestamp = DateTimeOffset.Now
            });

            return(Response.Success);
        }
Ejemplo n.º 4
0
        public async Task <Round> Handle(GetCurrentRound request, CancellationToken cancellationToken)
        {
            var round = await _repo.GetCurrentRound(request.GameId);

            if (round == null)
            {
                return(null);
            }

            return(new Round
            {
                Id = round.Id,
                GameId = request.GameId,
                RoundVersion = round.RoundVersion,
                StartedTimestamp = round.StartedTimestamp,
                EndedTimestamp = round.EndedTimestamp,
                Subject = round.Subject
            });
        }
Ejemplo n.º 5
0
        public async Task <Response> Handle(AddGameRound request, CancellationToken cancellationToken)
        {
            var game = await _gameRepo.GetById(request.GameId);

            if (game == null)
            {
                throw new DomainException(ErrorCode.GameNotFound, $"A game with id '{request.GameId}' was not found");
            }

            var current = await _roundRepo.GetCurrentRound(request.GameId);

            await _roundRepo.UpdateRoundTimestamp(request.GameId, current.Id, DateTimeOffset.Now);

            await _roundRepo.AddRound(request.GameId, new Round
            {
                Id = request.Id,
                StartedTimestamp = DateTimeOffset.Now,
                Subject          = request.Subject,
                RoundVersion     = request.RoundVersion
            });

            return(Response.Success);
        }