示例#1
0
        private async Task CreateFirstRound(Player player, int gameId)
        {
            var firstRound  = new Round();
            var secondRound = new Round();

            var firstCard  = new Card();
            var secondCard = new Card();

            firstCard = await _cardRepository.GetRandom();

            secondCard = await _cardRepository.GetRandom();

            firstRound.GameId   = gameId;
            firstRound.PlayerId = player.Id;
            firstRound.CardId   = firstCard.Id;
            firstRound.Id       = await _roundRepository.Add(firstRound, gameId);

            secondRound.GameId   = gameId;
            secondRound.PlayerId = player.Id;
            secondRound.CardId   = secondCard.Id;
            secondRound.Id       = await _roundRepository.Add(secondRound, gameId);

            firstRound.Card   = firstCard;
            firstRound.Player = player;
            _rounds.Add(firstRound);

            secondRound.Card   = secondCard;
            secondRound.Player = player;
            _rounds.Add(secondRound);
        }
示例#2
0
        public async Task <DTOs.RoundDetails> Create(DTOs.CreateRound request)
        {
            var band = await bandRepository.Get(request.Band.Id);

            if (band == null)
            {
                throw new NotFoundException("Band", request.Band.Id);
            }

            var sheriff = band.Members
                          .FirstOrDefault(m => m.Bandit.Id == request.Sheriff.Id);

            if (sheriff == null)
            {
                throw new NotFoundException("Bandit", request.Sheriff.Id);
            }

            var members = band.Members
                          .Where(m => request.Members.Any(r => m.Bandit.Id == r.Id));

            var round = sheriff.CreateRound(request.Name, request.Place, request.DateTime, members);

            round = await roundRepository.Add(round);

            return(round.ToDtoDetails());
        }
示例#3
0
        public QuizRoundDto AddRoundToQuiz(QuizRoundDto quizDto, int quizId)
        {
            var round = _roundRepository.Add(new QuizRound {
                QuizId = quizId, RoundName = quizDto.RoundName, MaxRoundScore = quizDto.MaxScorePerRound
            }).First();

            _roundRepository.Save();
            return(new QuizRoundDto {
                QuizId = round.QuizId, RoundName = round.RoundName, MaxScorePerRound = round.MaxRoundScore, Id = round.Id
            });
        }
示例#4
0
        public Round GenerateMatchups(int tournamentId)
        {
            if (!CanGenerateNextRound(tournamentId))
            {
                throw new CantGenerateNewRoundException();
            }

            var previousMatchups = roundRepository.GetAllMatchups(tournamentId);
            var players          = playerRepository.GetAllInTournament(tournamentId);
            var scores           = roundRepository.GetScoresForTournament(tournamentId);

            var contestants = players.Where(p => p.Active).Select(p => new Contestant
            {
                Identifier         = p.Id,
                Affiliation        = p.Affiliation,
                PreviousOpponents  = ExtractPreviousOpponentsForPlayer(p, previousMatchups, players, scores),
                Score              = scores.Where(s => s.PlayerId == p.Id).Sum(s => s.Amount),
                CompensationPoints = p.CompensationPoints
            }).OrderBy(c => c.Score).ToList();

            if (contestants.Count <= 1)
            {
                throw new Exception("Not enough active player in tournament to generate round matchups");
            }

            var engine             = new MatchupEngine();
            var contestantMatchups = engine.GenerateMatchup(contestants.Count % 2 == 0 ? contestants
                                                                                       : contestants.Skip(1).ToList());

            var round = roundRepository.Add(new Round
            {
                RoundNumber  = previousMatchups.Any() ? previousMatchups.Max(x => x.Key) + 1 : 1,
                TournamentId = tournamentId
            });

            roundRepository.AddMatchupsToRound(round.Id, contestantMatchups.Select(x => new Matchup
            {
                RoundId     = round.Id,
                TableNumber = x.Table,
                Player1Id   = x.Contestant1.Identifier,
                Player2Id   = x.Contestant2.Identifier
            }).ToList());

            if (contestants.Count % 2 != 0)
            {
                //The left over player gets a pseudo matchup and score
                var leftOverContestant = contestants.First();
                roundRepository.AddPseudoMatchupToRound(round.Id, leftOverContestant.Identifier);
            }

            return(roundRepository.GetCurrentRound(tournamentId));
        }
        public IHttpActionResult Post([FromBody] Round round)
        {
            var result = new Round();

            if (ModelState.IsValid)
            {
                if (round.Name == null)
                {
                    return(Content(HttpStatusCode.BadRequest, "Round name is empty"));
                }

                result = roundRepository.Add(round);
            }

            return(Json(result));
        }
示例#6
0
        public async Task AddAsync(Round round)
        {
            var rounds = await GetAllAsync();

            if (rounds.Any(x => x.Description == round.Description && x.RankingId == round.RankingId))
            {
                AddNotification("Já existem outra rodada cadastrada com essa descrição.");
            }

            if (await ValidateRoundAsync(round))
            {
                _roundRepository.Add(round);

                if (!await CommitAsync())
                {
                    AddNotification("Não foi possível cadastrar a rodada.");
                }
            }
        }
 public Player AddRound([FromBody] Round round)
 {
     _roundRepository.Add(round);
     return(_roundRepository.NextRound(round.GameId));
 }
        public async Task <IActionResult> Round(RoundViewModel newRound)
        {
            IActionResult   _result          = new ObjectResult(false);
            NextRoundResult _nextRoundResult = null;
            GenericResult   _genericResult   = null;

            try
            {
                if (ModelState.IsValid)
                {
                    Game game = _gameRepository.GetSingle(newRound.GameId);
                    if (game != null)
                    {
                        if (game.ValidateMove(newRound.Move1) && game.ValidateMove(newRound.Move2))
                        {
                            Round lastRound = _gameRepository.GetLastRound(game.Id);
                            int   roundNo   = lastRound != null ? lastRound.RoundNo + 1 : 1;

                            Round roundToAdd = new Round()
                            {
                                Move1   = newRound.Move1,
                                Move2   = newRound.Move2,
                                Game    = game,
                                RoundNo = roundNo
                            };

                            bool gameOver = PlayRound(roundToAdd);

                            _roundRepository.Add(roundToAdd);
                            _roundRepository.Commit();

                            if (gameOver == true && game.WinnerId == game.Player1Id)
                            {
                                _nextRoundResult = new NextRoundResult()
                                {
                                    Succeded = true,
                                    GameOver = true,
                                    Winner   = game.Player1,
                                    Round    = roundToAdd
                                };
                            }
                            else if (gameOver == true && game.WinnerId == game.Player2Id)
                            {
                                _nextRoundResult = new NextRoundResult()
                                {
                                    Succeded = true,
                                    GameOver = true,
                                    Winner   = game.Player2,
                                    Round    = roundToAdd
                                };
                            }
                            else
                            {
                                _nextRoundResult = new NextRoundResult()
                                {
                                    Succeded = true,
                                    GameOver = false,
                                    Winner   = roundToAdd.Winner,
                                    Round    = roundToAdd
                                };
                            }

                            _result = new ObjectResult(_nextRoundResult);
                            return(_result);
                        }
                        else
                        {
                            var json = JsonConvert.SerializeObject(newRound);
                            _genericResult = new GenericResult()
                            {
                                Succeeded = false,
                                Message   = "Move1 " + newRound.Move1 + " or Move2 " + newRound.Move2 + " are invalid for the game settings"
                            };

                            _loggingRepository.Add(new Error()
                            {
                                Message = "Invalid Move1 or Move2 POST api/game/round Move1 " + newRound.Move1 + " Move2 " + newRound.Move2, DateCreated = DateTime.Now
                            });
                            _loggingRepository.Commit();
                            _result = new ObjectResult(_genericResult);
                            return(_result);
                        }
                    }
                    else
                    {
                        var json = JsonConvert.SerializeObject(newRound);
                        _genericResult = new GenericResult()
                        {
                            Succeeded = false,
                            Message   = "Game with id " + newRound.GameId + " was not found"
                        };

                        _loggingRepository.Add(new Error()
                        {
                            Message = "Invalid Model POST api/game/round " + json, DateCreated = DateTime.Now
                        });
                        _loggingRepository.Commit();
                        _result = new ObjectResult(_genericResult);
                        return(_result);
                    }
                }
                else
                {
                    var json = JsonConvert.SerializeObject(newRound);
                    _genericResult = new GenericResult()
                    {
                        Succeeded = false,
                        Message   = "Invalid parameter fields " + json
                    };

                    _loggingRepository.Add(new Error()
                    {
                        Message = "Invalid Model POST api/game/round " + json, DateCreated = DateTime.Now
                    });
                    _loggingRepository.Commit();
                    _result = new ObjectResult(_genericResult);
                    return(_result);
                }
            }
            catch (Exception ex)
            {
                _genericResult = new GenericResult()
                {
                    Succeeded = false,
                    Message   = ex.Message
                };

                _loggingRepository.Add(new Error()
                {
                    Message = ex.Message, StackTrace = ex.StackTrace, DateCreated = DateTime.Now
                });
                _loggingRepository.Commit();
                _result = new ObjectResult(_genericResult);
                return(_result);
            }
        }
示例#9
0
 public void Add(Round entity)
 {
     _roundRepository.Add(entity);
 }