Exemplo n.º 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);
        }
Exemplo n.º 2
0
        // Jeżeli użytkownik ma jakąś nieukończoną grę, to jest ona zwracana
        public Game StartGame(User user, int mapId)
        {
            Game actualGame = _gameRepository.GetActualByUser(user.Id);

            if (actualGame != null)
            {
                actualGame = actualGame.ProceedMapToLastState();
                ActionsLogic.GetPossibleActions(actualGame.Map);
                return(actualGame);
            }

            Game game = new Game()
            {
                MapId = mapId, UserId = user.Id
            };

            game.Id  = _gameRepository.Create(game);
            game.Map = _mapRepository.GetById(mapId);

            ActionsLogic.GetPossibleActions(game.Map);

            return(game);
        }