Example #1
0
        public Game MakeMove(User user, GameAction action)
        {
            Game actualGame = _gameRepository.GetActualByUser(user.Id);

            actualGame.ProceedMapToLastState();

            actualGame.PlayerMoves.Add(action.Normalize(actualGame));
            actualGame.ProceedMapStep(actualGame.PlayerMoves.Last(), PlayerType.Player);

            actualGame.OpponentMoves.Add(AILogic.GenerateAction(actualGame.Map).Normalize(actualGame));
            actualGame.ProceedMapStep(actualGame.OpponentMoves.Last(), PlayerType.AI);

            actualGame.UpdateResources();

            if (actualGame.IsFinished || !actualGame.Map.Fields.Any(x => x.Owner == FieldOwner.AI) || !actualGame.Map.Fields.Any(x => x.Owner == FieldOwner.Player))
            {
                actualGame.IsFinished = true;
                actualGame.Score      = actualGame.ComputeScore();

                _statRepository.Create(new Stats()
                {
                    FinalGold    = (int)actualGame.Map.Money,
                    FinalGummies = (int)actualGame.Map.Fields.Where(x => x.Owner == FieldOwner.Player).Sum(x => x.GummiesNumber),
                    GameId       = actualGame.Id,
                    MapId        = actualGame.MapId,
                    OverallScore = actualGame.Score,
                    UserId       = actualGame.UserId
                });
            }
            else
            {
                ActionsLogic.GetPossibleActions(actualGame.Map);
            }

            _gameRepository.Update(actualGame);

            return(actualGame);
        }