Example #1
0
        private MazeDTO BuildMazeDTOFromMaze(Maze maze, Guid userId)
        {
            int      playersCount = _userMazeRepository.PlayersCountByMazeId(maze.MazeId);
            UserMaze userMaze     = _userMazeRepository.FindOwnByMazeId(maze.MazeId);
            User     user         = _userRepository.FindById(userMaze.UserId);

            UserMaze userMazeSolved = _userMazeRepository.FindById(maze.MazeId, userId);
            bool     solved         = false;

            if (userMazeSolved != null)
            {
                solved = userMazeSolved.Solved;
            }

            return(MazeDTO.Builder()
                   .Id(maze.MazeId)
                   .Name(maze.Name)
                   .OwnerId(user.UserId)
                   .Owner(user.Username)
                   .PlayersCount(playersCount)
                   .Width(maze.Width)
                   .Height(maze.Height)
                   .State(maze.State.DecompressString())
                   .CreationTime(maze.CreationTime)
                   .Solved(solved)
                   .Build());
        }
Example #2
0
        private UserDTO BuildUserDTOFromUser(User user)
        {
            user.UserMazes = _userMazeRepository.FindOwnMazesByUserId(user.UserId);
            List <MazeDTO> ownMazes = new List <MazeDTO>();

            foreach (UserMaze um in user.UserMazes)
            {
                Maze m            = _mazeRepository.FindById(um.MazeId);
                int  playersCount = _userMazeRepository.PlayersCountByMazeId(m.MazeId);

                MazeDTO mDTO = MazeDTO.Builder()
                               .Id(m.MazeId)
                               .Name(m.Name)
                               .PlayersCount(playersCount)
                               .Width(m.Width)
                               .Height(m.Height)
                               .State(m.State.DecompressString())
                               .CreationTime(m.CreationTime)
                               .Build();

                ownMazes.Add(mDTO);
            }

            List <UserMaze> solvedMazes   = _userMazeRepository.FindLockedMazesByUserId(user.UserId);
            int             totalAccuracy = 0;

            foreach (UserMaze um in solvedMazes)
            {
                totalAccuracy += um.Accuracy.GetValueOrDefault();
            }

            int accuracy = 0;

            if (solvedMazes.Count != 0)
            {
                accuracy = totalAccuracy / solvedMazes.Count;
            }

            return(UserDTO.Builder()
                   .Id(user.UserId)
                   .Username(user.Username)
                   .Accuracy(accuracy)
                   .OwnMazes(ownMazes)
                   .SolvedMazesCount(solvedMazes.Count)
                   .Build());
        }
Example #3
0
        private MazeDTO BuildMazeDTOFromMaze(Maze maze)
        {
            int      playersCount = _userMazeRepository.PlayersCountByMazeId(maze.MazeId);
            UserMaze userMaze     = _userMazeRepository.FindOwnByMazeId(maze.MazeId);
            User     user         = _userRepository.FindById(userMaze.UserId);

            return(MazeDTO.Builder()
                   .Id(maze.MazeId)
                   .Name(maze.Name)
                   .OwnerId(user.UserId)
                   .Owner(user.Username)
                   .PlayersCount(playersCount)
                   .Width(maze.Width)
                   .Height(maze.Height)
                   .State(maze.State.DecompressString())
                   .CreationTime(maze.CreationTime)
                   .Build());
        }
Example #4
0
        public MazeDTO CreateMazeByUserId(Guid userId, MazeFE maze)
        {
            User user = _userRepository.FindById(userId);

            if (user == null)
            {
                _logger.LogError("User with id " + userId.ToString() + " not in database\n\n");
                throw new ApiException(404, "User with id " + userId.ToString() + " not in database");
            }

            string solution;

            try
            {
                solution = _bfsService.ValidateMaze(maze);
            }
            catch (ApiException e)
            {
                _logger.LogError(e.Message);
                throw e;
            }

            Maze mazeBE     = BuildMazeFromMazeFE(maze, solution);
            Maze storedMaze = CreateMaze(mazeBE);

            UserMaze userMaze    = UserMaze.Create(user, storedMaze);
            UserMaze retUserMaze = _userMazeRepository.Create(userMaze);

            return(MazeDTO.Builder()
                   .Id(storedMaze.MazeId)
                   .Name(storedMaze.Name)
                   .OwnerId(user.UserId)
                   .Owner(user.Username)
                   .PlayersCount(0)
                   .Width(storedMaze.Width)
                   .Height(storedMaze.Height)
                   .State(storedMaze.State.DecompressString())
                   .CreationTime(storedMaze.CreationTime)
                   .Build());
        }