Example #1
0
        public IActionResult AddRound(GameViewModel model)
        {
            try
            {
                if (!ModelState.IsValid || model == null)
                {
                    return(ErrorResponse("Invalid model"));
                }

                var round = new Round
                {
                    Id     = 0,
                    GameId = model.Id
                };

                var index     = _roundService.GetWinnerMoveIndex(model.CurrentRound.PlayersMoves[0].MoveName, model.CurrentRound.PlayersMoves[1].MoveName);
                var lastRound = model.CurrentRound;

                if (index == 0)
                {
                    lastRound.WinnerMove = lastRound.PlayersMoves[0].MoveName; // Tied
                }
                else
                {
                    round.WinnerId = model.Players[index - 1].Id;
                    _roundService.InsertRound(round);
                    lastRound.WinnerName = lastRound.PlayersMoves[index - 1].PlayerName;
                    lastRound.WinnerMove = lastRound.PlayersMoves[index - 1].MoveName;
                }

                model.Rounds.Add(lastRound);
                // Prepare another round
                var nextRoundNumber = lastRound.Number + 1;

                if (nextRoundNumber > 3)
                {
                    var winnerId = 0;

                    if (_roundService.GameRoundsWonByPlayer(model.Id, model.Players[0].Id) == 3)
                    {
                        winnerId         = model.Players[0].Id;
                        model.WinnerName = model.Players[0].Name;
                    }
                    else if (_roundService.GameRoundsWonByPlayer(model.Id, model.Players[1].Id) == 3)
                    {
                        winnerId         = model.Players[1].Id;
                        model.WinnerName = model.Players[1].Name;
                    }

                    if (!string.IsNullOrEmpty(model.WinnerName))
                    {
                        model.ShowWinner = true;
                        var game = _gameService.GetGameById(model.Id);

                        if (game != null)
                        {
                            game.WinnerId = winnerId;
                            _gameService.UpdateGame(game);
                        }
                    }
                }

                if (!model.ShowWinner)
                {
                    model.CurrentRound = new RoundViewModel
                    {
                        Id           = 0,
                        GameId       = lastRound.GameId,
                        Number       = nextRoundNumber,
                        WinnerName   = "",
                        WinnerMove   = "",
                        ShowScore    = nextRoundNumber > 1,
                        PlayersMoves = new List <PlayerMoveViewModel>(new[]
                        {
                            new PlayerMoveViewModel
                            {
                                MoveName   = "",
                                PlayerName = model.Players[0].Name,
                                Selected   = false
                            },
                            new PlayerMoveViewModel
                            {
                                MoveName   = "",
                                PlayerName = model.Players[1].Name,
                                Selected   = false
                            }
                        })
                    };
                }

                return(Json(model));
            }
            catch (Exception ex)
            {
                var message = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                SaveActivity("Add Round", "ERROR", message);

                return(ErrorResponse(message));
            }
        }