diff --git a/src/main/java/wooteco/chess/SparkChessApplication.java b/src/main/java/wooteco/chess/SparkChessApplication.java index 18bf52c233..970faef274 100644 --- a/src/main/java/wooteco/chess/SparkChessApplication.java +++ b/src/main/java/wooteco/chess/SparkChessApplication.java @@ -4,9 +4,9 @@ import wooteco.chess.controller.SparkChessController; import wooteco.chess.repository.DataSource; -import wooteco.chess.repository.GameDAO; +import wooteco.chess.repository.GameDao; import wooteco.chess.repository.MySQLDataSource; -import wooteco.chess.repository.jdbc.JDBCGameDAO; +import wooteco.chess.repository.jdbc.JDBCGameDao; import wooteco.chess.repository.jdbc.JDBCTemplate; import wooteco.chess.service.GameService; @@ -15,7 +15,7 @@ public static void main(String[] args) { port(8080); staticFiles.location("/templates"); DataSource dataSource = new MySQLDataSource(); - GameDAO gameDAO = new JDBCGameDAO(new JDBCTemplate(dataSource)); + GameDao gameDAO = new JDBCGameDao(new JDBCTemplate(dataSource)); GameService gameService = new GameService(gameDAO); SparkChessController controller = new SparkChessController(gameService); controller.run(); diff --git a/src/main/java/wooteco/chess/controller/SparkChessController.java b/src/main/java/wooteco/chess/controller/SparkChessController.java index be9e5c5f8e..a8ba62ec52 100644 --- a/src/main/java/wooteco/chess/controller/SparkChessController.java +++ b/src/main/java/wooteco/chess/controller/SparkChessController.java @@ -11,13 +11,13 @@ import spark.ModelAndView; import spark.template.handlebars.HandlebarsTemplateEngine; import wooteco.chess.service.GameService; -import wooteco.chess.view.dto.requestdto.PositionRequestDTO; -import wooteco.chess.view.response.StandardResponse; +import wooteco.chess.view.dto.requestdto.PositionRequestDto; +import wooteco.chess.view.response.ResponseDto; public class SparkChessController { private static final HandlebarsTemplateEngine TEMPLATE_ENGINE = new HandlebarsTemplateEngine(); private static final ModelAndView DEFAULT_MODEL_AND_VIEW = new ModelAndView(new HashMap(), - "index.html"); + "index.hbs"); private final GameService gameService; @@ -30,61 +30,61 @@ public void run() { get("/chess/state", (req, res) -> makeResponse(() -> - new StandardResponse(SUCCESS, toJsonTree(gameService.getCurrentState()))), + new ResponseDto(SUCCESS, toJsonTree(gameService.getCurrentState()))), json() ); post("/chess/state", (req, res) -> makeResponse(() -> { gameService.changeState(req.body()); - return new StandardResponse(SUCCESS); + return new ResponseDto(SUCCESS); }), json() ); get("/chess/pieces", (req, res) -> makeResponse(() -> - new StandardResponse(SUCCESS, toJsonTree(this.gameService.findAllPiecesOnBoard())) + new ResponseDto(SUCCESS, toJsonTree(this.gameService.findAllPiecesOnBoard())) ), json() ); get("/chess/record", (req, res) -> makeResponse(() -> - new StandardResponse(SUCCESS, toJsonTree(this.gameService.calculateScore())) + new ResponseDto(SUCCESS, toJsonTree(this.gameService.calculateScore())) ), json() ); post("/chess/move", (req, res) -> makeResponse(() -> { - PositionRequestDTO requestDTO = fromJson(req.body(), PositionRequestDTO.class); + PositionRequestDto requestDTO = fromJson(req.body(), PositionRequestDto.class); this.gameService.move(requestDTO); - return new StandardResponse(SUCCESS, toJsonTree(gameService.findChangedPiecesOnBoard(requestDTO))); + return new ResponseDto(SUCCESS, toJsonTree(gameService.findChangedPiecesOnBoard(requestDTO))); }), json() ); get("/chess/isnotfinish", (req, res) -> makeResponse(() -> - new StandardResponse(SUCCESS, toJsonTree(this.gameService.isNotFinish())) + new ResponseDto(SUCCESS, toJsonTree(this.gameService.isNotFinish())) ), json() ); get("/chess/result", (req, res) -> makeResponse(() -> - new StandardResponse(SUCCESS, toJsonTree(this.gameService.getWinner())) + new ResponseDto(SUCCESS, toJsonTree(this.gameService.getWinner())) ), json() ); } - private StandardResponse makeResponse(Supplier responseGenerator) { + private ResponseDto makeResponse(Supplier responseGenerator) { try { return responseGenerator.get(); } catch (RuntimeException e) { - return new StandardResponse(ERROR, e.getMessage()); + return new ResponseDto(ERROR, e.getMessage()); } } } diff --git a/src/main/java/wooteco/chess/controller/ChessController.java b/src/main/java/wooteco/chess/controller/SpringChessController.java similarity index 85% rename from src/main/java/wooteco/chess/controller/ChessController.java rename to src/main/java/wooteco/chess/controller/SpringChessController.java index b63fe770ce..f1004d3c73 100644 --- a/src/main/java/wooteco/chess/controller/ChessController.java +++ b/src/main/java/wooteco/chess/controller/SpringChessController.java @@ -4,7 +4,7 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller -public class ChessController { +public class SpringChessController { @GetMapping("/") public String index() { return "index"; diff --git a/src/main/java/wooteco/chess/controller/SpringChessRestController.java b/src/main/java/wooteco/chess/controller/SpringChessRestController.java new file mode 100644 index 0000000000..6a7a06140a --- /dev/null +++ b/src/main/java/wooteco/chess/controller/SpringChessRestController.java @@ -0,0 +1,56 @@ +package wooteco.chess.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import wooteco.chess.service.GameService; +import wooteco.chess.view.dto.requestdto.PositionRequestDto; +import wooteco.chess.view.response.ResponseDto; + +@RestController +@RequestMapping("/chess") +public class SpringChessRestController { + private final GameService gameService; + + public SpringChessRestController(GameService gameService) { + this.gameService = gameService; + } + + @GetMapping("/state") + public ResponseDto findCurrentState() { + return gameService.getCurrentState(); + } + + @PostMapping("/state") + public ResponseDto changeState(@RequestBody String request) { + return gameService.changeState(request); + } + + @GetMapping("/pieces") + public ResponseDto findAllPiecesOnBoard() { + return gameService.findAllPiecesOnBoard(); + } + + @GetMapping("/record") + public ResponseDto calculateScore() { + return gameService.calculateScore(); + } + + @PostMapping("/move") + public ResponseDto move(@RequestBody PositionRequestDto requestDTO) { + return gameService.move(requestDTO); + } + + @GetMapping("/isnotfinish") + public ResponseDto isNotFinish() { + return gameService.isNotFinish(); + } + + @GetMapping("/result") + public ResponseDto findWinner() { + return gameService.getWinner(); + } +} diff --git a/src/main/java/wooteco/chess/domain/board/BoardFactory.java b/src/main/java/wooteco/chess/domain/board/BoardFactory.java index cbd1cb130c..54089e9f82 100644 --- a/src/main/java/wooteco/chess/domain/board/BoardFactory.java +++ b/src/main/java/wooteco/chess/domain/board/BoardFactory.java @@ -11,6 +11,9 @@ public class BoardFactory { private static final Pattern PIECES_PATTERN = Pattern.compile("([\\.pPbBrRkKnNqQ]{8}\\n){8}"); private static final String ILLEGAL_BOARD_REGEX_EXCEPTION_MESSAGE = "문자열의 형태가 체스판의 형식이 아닙니다."; + private BoardFactory() { + } + public static Board create(String board) { validateBoardRegex(Objects.requireNonNull(board)); Map pieces = BoardParser.parsePieces(board); diff --git a/src/main/java/wooteco/chess/domain/board/BoardParser.java b/src/main/java/wooteco/chess/domain/board/BoardParser.java index 4e4c73b356..41fb4b14bf 100644 --- a/src/main/java/wooteco/chess/domain/board/BoardParser.java +++ b/src/main/java/wooteco/chess/domain/board/BoardParser.java @@ -14,6 +14,9 @@ public class BoardParser { private static final String COLUMN_SEPARATOR = ""; private static final String EMPTY_ZONE = "."; + private BoardParser() { + } + public static Map parsePieces(String board) { Map pieces = new HashMap<>(); String[] rowBoards = board.split(ROW_SEPARATOR); diff --git a/src/main/java/wooteco/chess/domain/game/Game.java b/src/main/java/wooteco/chess/domain/game/Game.java index e260147189..61e7e71e43 100644 --- a/src/main/java/wooteco/chess/domain/game/Game.java +++ b/src/main/java/wooteco/chess/domain/game/Game.java @@ -1,7 +1,5 @@ package wooteco.chess.domain.game; -import static wooteco.chess.domain.piece.Team.*; - import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -17,6 +15,7 @@ public abstract class Game { private static final Map> CHANGE_STATE_FUNCTIONS; private static final String START_REQUEST = "start"; private static final String END_REQUEST = "end"; + private static final int DEFAULT_GAME_ID = 1; static { CHANGE_STATE_FUNCTIONS = new HashMap<>(); @@ -28,12 +27,8 @@ public abstract class Game { protected final Board board; protected Team turn; - public Game(Board board) { - this(1, board, WHITE); - } - public Game(Board board, Team turn) { - this(1, board, turn); + this(DEFAULT_GAME_ID, board, turn); } public Game(int id, Board board, Team turn) { diff --git a/src/main/java/wooteco/chess/domain/piece/Empty.java b/src/main/java/wooteco/chess/domain/piece/Empty.java index fbbe6823e6..572af968dc 100644 --- a/src/main/java/wooteco/chess/domain/piece/Empty.java +++ b/src/main/java/wooteco/chess/domain/piece/Empty.java @@ -8,9 +8,10 @@ public class Empty extends Piece { public static final Empty EMPTY = new Empty(); + private static final String INITIAL_CHARACTER = "."; - public Empty() { - super(NONE, "."); + private Empty() { + super(NONE, INITIAL_CHARACTER); } @Override @@ -20,7 +21,7 @@ public List findMoveModeTrace(Position from, Position to) { @Override protected String getInitialCharacter() { - return "."; + return INITIAL_CHARACTER; } @Override diff --git a/src/main/java/wooteco/chess/domain/piece/Pawn.java b/src/main/java/wooteco/chess/domain/piece/Pawn.java index 285a5e4d66..ceeebda8e9 100644 --- a/src/main/java/wooteco/chess/domain/piece/Pawn.java +++ b/src/main/java/wooteco/chess/domain/piece/Pawn.java @@ -7,6 +7,9 @@ public class Pawn extends Piece { private static final String INITIAL_CHARACTER = "P"; + private static final int INITIAL_MAX_STEP = 2; + private static final int REVERSE_FACTOR = -1; + private static final int DEFAULT_STEP = 1; public Pawn(Team team) { super(team, INITIAL_CHARACTER); @@ -18,7 +21,7 @@ public List findMoveModeTrace(Position from, Position to) { throw new IllegalArgumentException("해당 위치로 이동할 수 없습니다."); } - if ((Math.abs(to.getRankNumber() - from.getRankNumber()) == 2)) { + if ((Math.abs(to.getRankNumber() - from.getRankNumber()) == INITIAL_MAX_STEP)) { return Position.findMultipleStepTrace(from, to); } return Collections.emptyList(); @@ -37,13 +40,20 @@ private boolean isNotMovable(Position start, Position end) { } private boolean isMovable(Position start, Position end) { + int rankMoveDistance = calculateMoveDistance(start, end); + return rankMoveDistance >= DEFAULT_STEP && rankMoveDistance <= INITIAL_MAX_STEP; + } + + private int calculateMoveDistance(Position start, Position end) { int rankMoveDistance = end.getRankNumber() - start.getRankNumber(); - rankMoveDistance *= team.isBlack() ? -1 : 1; - return rankMoveDistance > 0 && rankMoveDistance <= 2; + if (team.isBlack()) { + return rankMoveDistance * REVERSE_FACTOR; + } + return rankMoveDistance; } private boolean isAbleToMoveDoubleSquare(Position start, Position end) { - return start.isInitialPawnPosition(team) || (Math.abs(end.getRankNumber() - start.getRankNumber()) != 2); + return start.isInitialPawnPosition(team) || (Math.abs(end.getRankNumber() - start.getRankNumber()) != INITIAL_MAX_STEP); } private boolean isNotAbleToMoveDoubleSquare(Position start, Position end) { diff --git a/src/main/java/wooteco/chess/domain/piece/PieceFactory.java b/src/main/java/wooteco/chess/domain/piece/PieceFactory.java index e7731f9302..df46536ec5 100644 --- a/src/main/java/wooteco/chess/domain/piece/PieceFactory.java +++ b/src/main/java/wooteco/chess/domain/piece/PieceFactory.java @@ -3,34 +3,33 @@ import static wooteco.chess.domain.piece.Team.*; import java.util.Arrays; -import java.util.function.Supplier; public enum PieceFactory { - BLACK_PAWN("P", () -> new Pawn(BLACK)), - BLACK_BISHOP("B", () -> new Bishop(BLACK)), - BLACK_KNIGHT("N", () -> new Knight(BLACK)), - BLACK_ROOK("R", () -> new Rook(BLACK)), - BLACK_QUEEN("Q", () -> new Queen(BLACK)), - BLACK_KING("K", () -> new King(BLACK)), - WHITE_PAWN("p", () -> new Pawn(WHITE)), - WHITE_BISHOP("b", () -> new Bishop(WHITE)), - WHITE_KNIGHT("n", () -> new Knight(WHITE)), - WHITE_ROOK("r", () -> new Rook(WHITE)), - WHITE_QUEEN("q", () -> new Queen(WHITE)), - WHITE_KING("k", () -> new King(WHITE)); + BLACK_PAWN("P", new Pawn(BLACK)), + BLACK_BISHOP("B", new Bishop(BLACK)), + BLACK_KNIGHT("N", new Knight(BLACK)), + BLACK_ROOK("R", new Rook(BLACK)), + BLACK_QUEEN("Q", new Queen(BLACK)), + BLACK_KING("K", new King(BLACK)), + WHITE_PAWN("p", new Pawn(WHITE)), + WHITE_BISHOP("b", new Bishop(WHITE)), + WHITE_KNIGHT("n", new Knight(WHITE)), + WHITE_ROOK("r", new Rook(WHITE)), + WHITE_QUEEN("q", new Queen(WHITE)), + WHITE_KING("k", new King(WHITE)); private final String symbol; - private final Supplier pieceSupplier; + private final Piece piece; - PieceFactory(String symbol, Supplier pieceSupplier) { + PieceFactory(String symbol, Piece piece) { this.symbol = symbol; - this.pieceSupplier = pieceSupplier; + this.piece = piece; } public static Piece of(String symbol) { return Arrays.stream(values()) .filter(val -> val.symbol.equals(symbol)) - .map(val -> val.pieceSupplier.get()) + .map(val -> val.piece) .findFirst() .orElseThrow(() -> new IllegalArgumentException("유효한 심볼이 아닙니다. : " + symbol)); } diff --git a/src/main/java/wooteco/chess/domain/position/Position.java b/src/main/java/wooteco/chess/domain/position/Position.java index 73fa541746..a880b1a148 100644 --- a/src/main/java/wooteco/chess/domain/position/Position.java +++ b/src/main/java/wooteco/chess/domain/position/Position.java @@ -14,14 +14,14 @@ public class Position { private static final Map CACHE = new HashMap<>(); static { - initPositionCache(); + for (int col = MIN_POSITION_NUMBER; col <= MAX_POSITION_NUMBER; col++) { + initColumnPositionCache(col); + } } - private static void initPositionCache() { - for (int col = MIN_POSITION_NUMBER; col <= MAX_POSITION_NUMBER; col++) { - for (int row = MIN_POSITION_NUMBER; row <= MAX_POSITION_NUMBER; row++) { - CACHE.put(getKey(col, row), new Position(new Cell(col), new Cell(row))); - } + private static void initColumnPositionCache(int col) { + for (int row = MIN_POSITION_NUMBER; row <= MAX_POSITION_NUMBER; row++) { + CACHE.put(getKey(col, row), new Position(new Cell(col), new Cell(row))); } } @@ -46,7 +46,11 @@ public static Position of(Cell file, Cell rank) { } private static String getKey(int file, int rank) { - return (char)('a' + file - 1) + String.valueOf(rank); + return convertColumnChar(file) + String.valueOf(rank); + } + + private static char convertColumnChar(int file) { + return (char)('a' + file - 1); } public static List findMultipleStepTrace(Position from, Position to) { @@ -100,6 +104,6 @@ public int getRankNumber() { @Override public String toString() { - return String.valueOf((char)('a' + col.getNumber() - 1)) + row.getNumber(); + return String.valueOf(convertColumnChar(col.getNumber())) + row.getNumber(); } } diff --git a/src/main/java/wooteco/chess/domain/result/Result.java b/src/main/java/wooteco/chess/domain/result/Result.java index 7650f8e394..dd62de3eba 100644 --- a/src/main/java/wooteco/chess/domain/result/Result.java +++ b/src/main/java/wooteco/chess/domain/result/Result.java @@ -26,7 +26,7 @@ private Result(Map status) { } public static Result from(Board board) { - Map collect = findDafaultScores(board); + Map collect = findDefaultScores(board); for (int col = MIN_POSITION_NUMBER; col <= MAX_POSITION_NUMBER; col++) { inputNthColumnPawnBonus(board, collect, col); } @@ -38,7 +38,7 @@ private static void inputNthColumnPawnBonus(Board board, Map colle addPawnBonusScore(collect, cnt); } - private static HashMap findDafaultScores(Board board) { + private static Map findDefaultScores(Board board) { HashMap collect = board.getPieces().values().stream() .filter(Piece::isNotBlank) .collect(groupingBy(Piece::getTeam, HashMap::new, summingDouble(Piece::getScore))); diff --git a/src/main/java/wooteco/chess/exception/SQLConnectionException.java b/src/main/java/wooteco/chess/exception/SQLConnectionException.java deleted file mode 100644 index 12b3b7bcc1..0000000000 --- a/src/main/java/wooteco/chess/exception/SQLConnectionException.java +++ /dev/null @@ -1,9 +0,0 @@ -package wooteco.chess.exception; - -public class SQLConnectionException extends RuntimeException { - private static final String message = "데이터베이스 연결에 실패했습니다."; - - public SQLConnectionException() { - super(message); - } -} diff --git a/src/main/java/wooteco/chess/repository/DataSource.java b/src/main/java/wooteco/chess/repository/DataSource.java index f7acf5a46c..104b6d9403 100644 --- a/src/main/java/wooteco/chess/repository/DataSource.java +++ b/src/main/java/wooteco/chess/repository/DataSource.java @@ -3,6 +3,9 @@ import java.sql.Connection; import java.sql.SQLException; +import org.springframework.stereotype.Component; + +@Component public interface DataSource { Connection getConnection() throws SQLException; } diff --git a/src/main/java/wooteco/chess/repository/GameDAO.java b/src/main/java/wooteco/chess/repository/GameDao.java similarity index 65% rename from src/main/java/wooteco/chess/repository/GameDAO.java rename to src/main/java/wooteco/chess/repository/GameDao.java index 483f5e874f..c9863e9c28 100644 --- a/src/main/java/wooteco/chess/repository/GameDAO.java +++ b/src/main/java/wooteco/chess/repository/GameDao.java @@ -2,9 +2,12 @@ import java.util.Optional; +import org.springframework.stereotype.Repository; + import wooteco.chess.domain.game.Game; -public interface GameDAO { +@Repository +public interface GameDao { Optional findById(int gameId); void update(Game game); diff --git a/src/main/java/wooteco/chess/repository/MySQLDataSource.java b/src/main/java/wooteco/chess/repository/MySQLDataSource.java index e87ee739a3..b061fd3774 100644 --- a/src/main/java/wooteco/chess/repository/MySQLDataSource.java +++ b/src/main/java/wooteco/chess/repository/MySQLDataSource.java @@ -4,6 +4,9 @@ import java.sql.DriverManager; import java.sql.SQLException; +import org.springframework.stereotype.Component; + +@Component public class MySQLDataSource implements DataSource { private static final String SERVER = "localhost:13306"; private static final String DATABASE = "chess"; diff --git a/src/main/java/wooteco/chess/repository/jdbc/JDBCGameDAO.java b/src/main/java/wooteco/chess/repository/jdbc/JDBCGameDao.java similarity index 79% rename from src/main/java/wooteco/chess/repository/jdbc/JDBCGameDAO.java rename to src/main/java/wooteco/chess/repository/jdbc/JDBCGameDao.java index eac87c235a..5d8c34a08b 100644 --- a/src/main/java/wooteco/chess/repository/jdbc/JDBCGameDAO.java +++ b/src/main/java/wooteco/chess/repository/jdbc/JDBCGameDao.java @@ -3,15 +3,20 @@ import java.util.Objects; import java.util.Optional; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Repository; + import wooteco.chess.domain.board.BoardParser; import wooteco.chess.domain.game.Game; import wooteco.chess.domain.game.GameFactory; -import wooteco.chess.repository.GameDAO; +import wooteco.chess.repository.GameDao; -public class JDBCGameDAO implements GameDAO { +@Primary +@Repository +public class JDBCGameDao implements GameDao { private final JDBCTemplate jdbcTemplate; - public JDBCGameDAO(JDBCTemplate jdbcTemplate) { + public JDBCGameDao(JDBCTemplate jdbcTemplate) { this.jdbcTemplate = Objects.requireNonNull(jdbcTemplate); } diff --git a/src/main/java/wooteco/chess/repository/jdbc/JDBCTemplate.java b/src/main/java/wooteco/chess/repository/jdbc/JDBCTemplate.java index f020afa08b..b1caffae3c 100644 --- a/src/main/java/wooteco/chess/repository/jdbc/JDBCTemplate.java +++ b/src/main/java/wooteco/chess/repository/jdbc/JDBCTemplate.java @@ -6,11 +6,14 @@ import java.sql.SQLException; import java.util.Optional; +import org.springframework.stereotype.Component; + import wooteco.chess.exception.SQLAccessException; import wooteco.chess.repository.DataSource; import wooteco.chess.repository.PrepareStatementSetter; import wooteco.chess.repository.RowMapper; +@Component public class JDBCTemplate { private final DataSource dataSource; @@ -18,7 +21,7 @@ public JDBCTemplate(DataSource dataSource) { this.dataSource = dataSource; } - public Optional executeQuery(String query, RowMapper mapper, PrepareStatementSetter setter) { + Optional executeQuery(String query, RowMapper mapper, PrepareStatementSetter setter) { try (Connection con = dataSource.getConnection(); PreparedStatement preparedStatement = con.prepareStatement(query)) { setter.set(preparedStatement); @@ -32,7 +35,7 @@ public Optional executeQuery(String query, RowMapper mapper, PrepareSt } } - public int executeUpdate(String query, PrepareStatementSetter setter) { + int executeUpdate(String query, PrepareStatementSetter setter) { try (Connection con = dataSource.getConnection(); PreparedStatement preparedStatement = con.prepareStatement(query)) { setter.set(preparedStatement); diff --git a/src/main/java/wooteco/chess/service/GameService.java b/src/main/java/wooteco/chess/service/GameService.java index 8dc610897f..b4b6fda0b8 100644 --- a/src/main/java/wooteco/chess/service/GameService.java +++ b/src/main/java/wooteco/chess/service/GameService.java @@ -1,95 +1,135 @@ package wooteco.chess.service; +import static wooteco.chess.view.response.ResponseStatus.*; + import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; -import java.util.Objects; +import java.util.function.Supplier; import java.util.stream.Collectors; +import org.springframework.stereotype.Service; + import wooteco.chess.domain.board.Board; import wooteco.chess.domain.game.Game; import wooteco.chess.domain.position.Position; import wooteco.chess.domain.result.Result; -import wooteco.chess.repository.GameDAO; -import wooteco.chess.view.dto.requestdto.PositionRequestDTO; -import wooteco.chess.view.dto.responsedto.BoardDTO; -import wooteco.chess.view.dto.responsedto.GameDTO; -import wooteco.chess.view.dto.responsedto.ScoreDTO; - +import wooteco.chess.repository.GameDao; +import wooteco.chess.view.dto.requestdto.PositionRequestDto; +import wooteco.chess.view.dto.responsedto.BoardDto; +import wooteco.chess.view.dto.responsedto.GameDto; +import wooteco.chess.view.dto.responsedto.ScoreDto; +import wooteco.chess.view.response.ResponseDto; + +@Service public class GameService { private static final String NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE = "조건에 해당하는 요소가 없습니다."; private static final int DEFAULT_USER_ID = 1; - private GameDAO gameDAO; + private final GameDao gameDao; - public GameService(GameDAO gameDAO) { - this.gameDAO = Objects.requireNonNull(gameDAO); + public GameService(GameDao gameDao) { + this.gameDao = gameDao; } - public List calculateScore() { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - Result status = game.status(); - return status.getStatus().entrySet().stream() - .map(entry -> new ScoreDTO(entry.getKey().getTeam(), entry.getValue())) - .collect(Collectors.toList()); + public ResponseDto calculateScore() { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + Result status = game.status(); + List scores = status.getStatus().entrySet().stream() + .map(entry -> new ScoreDto(entry.getKey().getTeam(), entry.getValue())) + .collect(Collectors.toList()); + return new ResponseDto(SUCCESS, scores); + }); } - public void changeState(String request) { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - game = game.changeState(request); - gameDAO.update(game); + public ResponseDto changeState(String request) { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + game = game.changeState(request); + gameDao.update(game); + return new ResponseDto(SUCCESS); + } + ); } - public List findAllPiecesOnBoard() { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - Board board = game.getBoard(); - - return board.getPieces().entrySet().stream() - .map(entry -> new BoardDTO(entry.getKey().toString(), entry.getValue().getSymbol())) - .collect(Collectors.toList()); + public ResponseDto findAllPiecesOnBoard() { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + Board board = game.getBoard(); + + List pieces = board.getPieces().entrySet().stream() + .map(entry -> new BoardDto(entry.getKey().toString(), entry.getValue().getSymbol())) + .collect(Collectors.toList()); + return new ResponseDto(SUCCESS, pieces); + }); } - public void move(PositionRequestDTO positionRequestDTO) { - Position from = Position.of(positionRequestDTO.getFrom()); - Position to = Position.of(positionRequestDTO.getTo()); - - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - game = game.move(from, to); - gameDAO.update(game); + public ResponseDto move(PositionRequestDto positionRequestDTO) { + return makeResponse(() -> { + Position from = Position.of(positionRequestDTO.getFrom()); + Position to = Position.of(positionRequestDTO.getTo()); + + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + game = game.move(from, to); + gameDao.update(game); + Board board = game.getBoard(); + List result = new ArrayList<>(); + result.add(new BoardDto(from.toString(), board.findSymbol(from))); + result.add(new BoardDto(to.toString(), board.findSymbol(to))); + return new ResponseDto(SUCCESS, result); + } + ); } - public List findChangedPiecesOnBoard(PositionRequestDTO positionRequestDTO) { - Game game = gameDAO.findById(DEFAULT_USER_ID) + public List findChangedPiecesOnBoard(PositionRequestDto positionRequestDTO) { + Game game = gameDao.findById(DEFAULT_USER_ID) .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); Position from = Position.of(positionRequestDTO.getFrom()); Position to = Position.of(positionRequestDTO.getTo()); Board board = game.getBoard(); - List result = new ArrayList<>(); - result.add(new BoardDTO(from.toString(), board.findSymbol(from))); - result.add(new BoardDTO(to.toString(), board.findSymbol(to))); + List result = new ArrayList<>(); + result.add(new BoardDto(from.toString(), board.findSymbol(from))); + result.add(new BoardDto(to.toString(), board.findSymbol(to))); return result; } - public GameDTO getCurrentState() { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - return GameDTO.of(game.getTurn(), game.getStateType()); + public ResponseDto getCurrentState() { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + return new ResponseDto(SUCCESS, GameDto.of(game.getTurn(), game.getStateType())); + }); } - public String getWinner() { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - return game.getWinner().name().toLowerCase(); + public ResponseDto getWinner() { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + String winner = game.getWinner().name().toLowerCase(); + return new ResponseDto(SUCCESS, winner); + }); } - public boolean isNotFinish() { - Game game = gameDAO.findById(DEFAULT_USER_ID) - .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); - return game.isNotFinished(); + public ResponseDto isNotFinish() { + return makeResponse(() -> { + Game game = gameDao.findById(DEFAULT_USER_ID) + .orElseThrow(() -> new NoSuchElementException(NONE_ELEMENT_QUERY_RESULT_EXCEPTION_MESSAGE)); + return new ResponseDto(SUCCESS, game.isNotFinished()); + } + ); + } + + private ResponseDto makeResponse(Supplier responseGenerator) { + try { + return responseGenerator.get(); + } catch (RuntimeException e) { + return new ResponseDto(ERROR, e.getMessage()); + } } } \ No newline at end of file diff --git a/src/main/java/wooteco/chess/utils/json/JsonUtil.java b/src/main/java/wooteco/chess/utils/json/JsonUtil.java index 078c4df22a..344d4de7fc 100644 --- a/src/main/java/wooteco/chess/utils/json/JsonUtil.java +++ b/src/main/java/wooteco/chess/utils/json/JsonUtil.java @@ -7,6 +7,9 @@ public class JsonUtil { private static final Gson GSON = new Gson(); + private JsonUtil() { + } + public static String toJson(Object object) { return GSON.toJson(object); } diff --git a/src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDTO.java b/src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDto.java similarity index 65% rename from src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDTO.java rename to src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDto.java index de3b388727..c277f05c14 100644 --- a/src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDTO.java +++ b/src/main/java/wooteco/chess/view/dto/requestdto/PositionRequestDto.java @@ -1,10 +1,13 @@ package wooteco.chess.view.dto.requestdto; -public class PositionRequestDTO { - private final String from; - private final String to; +public class PositionRequestDto { + private String from; + private String to; - public PositionRequestDTO(String from, String to) { + public PositionRequestDto() { + } + + public PositionRequestDto(String from, String to) { this.from = from; this.to = to; } diff --git a/src/main/java/wooteco/chess/view/dto/responsedto/BoardDTO.java b/src/main/java/wooteco/chess/view/dto/responsedto/BoardDto.java similarity index 87% rename from src/main/java/wooteco/chess/view/dto/responsedto/BoardDTO.java rename to src/main/java/wooteco/chess/view/dto/responsedto/BoardDto.java index 6d71d4aebb..4bcf069abe 100644 --- a/src/main/java/wooteco/chess/view/dto/responsedto/BoardDTO.java +++ b/src/main/java/wooteco/chess/view/dto/responsedto/BoardDto.java @@ -2,11 +2,11 @@ import java.util.Objects; -public class BoardDTO { +public class BoardDto { private final String position; private final String symbol; - public BoardDTO(String position, String symbol) { + public BoardDto(String position, String symbol) { this.position = position; this.symbol = symbol; } @@ -25,7 +25,7 @@ public boolean equals(Object o) { return true; if (o == null || getClass() != o.getClass()) return false; - BoardDTO boardDTO = (BoardDTO)o; + BoardDto boardDTO = (BoardDto)o; return Objects.equals(position, boardDTO.position) && Objects.equals(symbol, boardDTO.symbol); } diff --git a/src/main/java/wooteco/chess/view/dto/responsedto/GameDTO.java b/src/main/java/wooteco/chess/view/dto/responsedto/GameDto.java similarity index 75% rename from src/main/java/wooteco/chess/view/dto/responsedto/GameDTO.java rename to src/main/java/wooteco/chess/view/dto/responsedto/GameDto.java index 468f42e56a..2ae448b5e8 100644 --- a/src/main/java/wooteco/chess/view/dto/responsedto/GameDTO.java +++ b/src/main/java/wooteco/chess/view/dto/responsedto/GameDto.java @@ -4,17 +4,17 @@ import wooteco.chess.domain.piece.Team; -public class GameDTO { +public class GameDto { private final String turn; private final String gameState; - private GameDTO(String turn, String gameState) { + private GameDto(String turn, String gameState) { this.turn = turn; this.gameState = gameState; } - public static GameDTO of(Team turn, String stateType) { - return new GameDTO(turn.getTeam(), stateType); + public static GameDto of(Team turn, String stateType) { + return new GameDto(turn.getTeam(), stateType); } public String getTurn() { @@ -31,7 +31,7 @@ public boolean equals(Object o) { return true; if (o == null || getClass() != o.getClass()) return false; - GameDTO gameDTO = (GameDTO)o; + GameDto gameDTO = (GameDto)o; return Objects.equals(turn, gameDTO.turn) && Objects.equals(gameState, gameDTO.gameState); } diff --git a/src/main/java/wooteco/chess/view/dto/responsedto/ScoreDTO.java b/src/main/java/wooteco/chess/view/dto/responsedto/ScoreDto.java similarity index 75% rename from src/main/java/wooteco/chess/view/dto/responsedto/ScoreDTO.java rename to src/main/java/wooteco/chess/view/dto/responsedto/ScoreDto.java index 7fce1f4e52..cb2da9c063 100644 --- a/src/main/java/wooteco/chess/view/dto/responsedto/ScoreDTO.java +++ b/src/main/java/wooteco/chess/view/dto/responsedto/ScoreDto.java @@ -2,22 +2,30 @@ import java.util.Objects; -public class ScoreDTO { +public class ScoreDto { private final String team; private final double score; - public ScoreDTO(String team, double score) { + public ScoreDto(String team, double score) { this.team = team; this.score = score; } + public String getTeam() { + return team; + } + + public double getScore() { + return score; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - ScoreDTO scoreDTO = (ScoreDTO)o; + ScoreDto scoreDTO = (ScoreDto)o; return Double.compare(scoreDTO.score, score) == 0 && Objects.equals(team, scoreDTO.team); } diff --git a/src/main/java/wooteco/chess/view/response/ResponseDto.java b/src/main/java/wooteco/chess/view/response/ResponseDto.java new file mode 100644 index 0000000000..84863f8162 --- /dev/null +++ b/src/main/java/wooteco/chess/view/response/ResponseDto.java @@ -0,0 +1,28 @@ +package wooteco.chess.view.response; + +public class ResponseDto { + private ResponseStatus status; + private Object data; + + public ResponseDto(ResponseStatus status) { + this.status = status; + } + + public ResponseDto(ResponseStatus status, String data) { + this.status = status; + this.data = data; + } + + public ResponseDto(ResponseStatus status, Object data) { + this.status = status; + this.data = data; + } + + public ResponseStatus getStatus() { + return status; + } + + public Object getData() { + return data; + } +} diff --git a/src/main/java/wooteco/chess/view/response/StandardResponse.java b/src/main/java/wooteco/chess/view/response/StandardResponse.java deleted file mode 100644 index 16628afd55..0000000000 --- a/src/main/java/wooteco/chess/view/response/StandardResponse.java +++ /dev/null @@ -1,23 +0,0 @@ -package wooteco.chess.view.response; - -import com.google.gson.JsonElement; - -public class StandardResponse { - private ResponseStatus status; - private String message; - private JsonElement data; - - public StandardResponse(ResponseStatus status) { - this.status = status; - } - - public StandardResponse(ResponseStatus status, String message) { - this.status = status; - this.message = message; - } - - public StandardResponse(ResponseStatus status, JsonElement data) { - this.status = status; - this.data = data; - } -} diff --git a/src/main/resources/templates/chess-img/bishop_black.png b/src/main/resources/static/chess-img/bishop_black.png similarity index 100% rename from src/main/resources/templates/chess-img/bishop_black.png rename to src/main/resources/static/chess-img/bishop_black.png diff --git a/src/main/resources/templates/chess-img/bishop_white.png b/src/main/resources/static/chess-img/bishop_white.png similarity index 100% rename from src/main/resources/templates/chess-img/bishop_white.png rename to src/main/resources/static/chess-img/bishop_white.png diff --git a/src/main/resources/templates/chess-img/king_black.png b/src/main/resources/static/chess-img/king_black.png similarity index 100% rename from src/main/resources/templates/chess-img/king_black.png rename to src/main/resources/static/chess-img/king_black.png diff --git a/src/main/resources/templates/chess-img/king_white.png b/src/main/resources/static/chess-img/king_white.png similarity index 100% rename from src/main/resources/templates/chess-img/king_white.png rename to src/main/resources/static/chess-img/king_white.png diff --git a/src/main/resources/templates/chess-img/knight_black.png b/src/main/resources/static/chess-img/knight_black.png similarity index 100% rename from src/main/resources/templates/chess-img/knight_black.png rename to src/main/resources/static/chess-img/knight_black.png diff --git a/src/main/resources/templates/chess-img/knight_white.png b/src/main/resources/static/chess-img/knight_white.png similarity index 100% rename from src/main/resources/templates/chess-img/knight_white.png rename to src/main/resources/static/chess-img/knight_white.png diff --git a/src/main/resources/templates/chess-img/pawn_black.png b/src/main/resources/static/chess-img/pawn_black.png similarity index 100% rename from src/main/resources/templates/chess-img/pawn_black.png rename to src/main/resources/static/chess-img/pawn_black.png diff --git a/src/main/resources/templates/chess-img/pawn_white.png b/src/main/resources/static/chess-img/pawn_white.png similarity index 100% rename from src/main/resources/templates/chess-img/pawn_white.png rename to src/main/resources/static/chess-img/pawn_white.png diff --git a/src/main/resources/templates/chess-img/queen_black.png b/src/main/resources/static/chess-img/queen_black.png similarity index 100% rename from src/main/resources/templates/chess-img/queen_black.png rename to src/main/resources/static/chess-img/queen_black.png diff --git a/src/main/resources/templates/chess-img/queen_white.png b/src/main/resources/static/chess-img/queen_white.png similarity index 100% rename from src/main/resources/templates/chess-img/queen_white.png rename to src/main/resources/static/chess-img/queen_white.png diff --git a/src/main/resources/templates/chess-img/rook_black.png b/src/main/resources/static/chess-img/rook_black.png similarity index 100% rename from src/main/resources/templates/chess-img/rook_black.png rename to src/main/resources/static/chess-img/rook_black.png diff --git a/src/main/resources/templates/chess-img/rook_white.png b/src/main/resources/static/chess-img/rook_white.png similarity index 100% rename from src/main/resources/templates/chess-img/rook_white.png rename to src/main/resources/static/chess-img/rook_white.png diff --git a/src/main/resources/templates/css/index.css b/src/main/resources/static/css/index.css similarity index 100% rename from src/main/resources/templates/css/index.css rename to src/main/resources/static/css/index.css diff --git a/src/main/resources/templates/js/index.js b/src/main/resources/static/js/index.js similarity index 96% rename from src/main/resources/templates/js/index.js rename to src/main/resources/static/js/index.js index 29b696f390..13cd152a33 100644 --- a/src/main/resources/templates/js/index.js +++ b/src/main/resources/static/js/index.js @@ -74,7 +74,7 @@ const updateGameState = function updateGameState() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } renderCurrentGameState(data['data']); @@ -90,7 +90,7 @@ const requestMovePieces = function requestMovePieces(data) { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } updatePieceOnBoard(data['data']); @@ -102,6 +102,7 @@ const requestMovePieces = function requestMovePieces(data) { } }; xhttp.open("POST", "/chess/move", true); + xhttp.setRequestHeader("Content-type", "application/json"); xhttp.send(data); }; @@ -111,7 +112,7 @@ const checkWhetherGameIsFinished = function checkWhetherGameIsFinished() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } if (!data['data']) { @@ -129,7 +130,7 @@ const requestEndGame = function requestEndGame() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } updateGameState(); @@ -146,7 +147,7 @@ const requestWinner = function requestWinner() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } alert("승리 : " + data['data']); @@ -163,7 +164,7 @@ const requestNewGame = function requestNewGame() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } requestAllPieces(); @@ -270,7 +271,7 @@ const requestRecord = function requestRecord() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } updateRecord(data['data']); @@ -298,7 +299,7 @@ const requestAllPieces = function requestAllPieces() { if (this.readyState === 4 && this.status === 200) { const data = JSON.parse(xhttp.responseText); if (data['status'] === 'ERROR') { - alert(data['message']); + alert(data['data']); return; } updatePieceOnBoard(data['data']); diff --git a/src/main/resources/templates/index.hbs b/src/main/resources/templates/index.hbs index 011c8cb828..48a7f63fa2 100644 --- a/src/main/resources/templates/index.hbs +++ b/src/main/resources/templates/index.hbs @@ -1,9 +1,39 @@ - - + + - 모두의 체스 + + CHESS_GAME + -

Hello World

+
+
+
+ 우 아 한 체 스 코 스 +
+
+ + + +
+
+
현재턴 :
+
게임상태 :
+
+
+
+
점수
+
+ + + + +
+
+ + +
+
+ \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html deleted file mode 100644 index 4f040c73a4..0000000000 --- a/src/main/resources/templates/index.html +++ /dev/null @@ -1,39 +0,0 @@ - - - - - CHESS_GAME - - - -
-
-
- 우 아 한 체 스 코 스 -
-
- - - -
-
-
현재턴 :
-
게임상태 :
-
-
-
-
점수
-
- - - - -
-
- - -
-
- - - \ No newline at end of file diff --git a/src/test/java/wooteco/chess/repository/BlackKingCatchedGameDAO.java b/src/test/java/wooteco/chess/repository/BlackKingCatchedGameDao.java similarity index 93% rename from src/test/java/wooteco/chess/repository/BlackKingCatchedGameDAO.java rename to src/test/java/wooteco/chess/repository/BlackKingCatchedGameDao.java index aad422a6ad..329a0cc880 100644 --- a/src/test/java/wooteco/chess/repository/BlackKingCatchedGameDAO.java +++ b/src/test/java/wooteco/chess/repository/BlackKingCatchedGameDao.java @@ -12,7 +12,7 @@ import wooteco.chess.domain.piece.Team; import wooteco.chess.domain.position.Position; -public class BlackKingCatchedGameDAO implements GameDAO { +public class BlackKingCatchedGameDao implements GameDao { @Override public Optional findById(int gameId) { diff --git a/src/test/java/wooteco/chess/repository/FinishedDrawGameDAO.java b/src/test/java/wooteco/chess/repository/FinishedDrawGameDao.java similarity index 88% rename from src/test/java/wooteco/chess/repository/FinishedDrawGameDAO.java rename to src/test/java/wooteco/chess/repository/FinishedDrawGameDao.java index 8bad0f00e4..9a245a93ac 100644 --- a/src/test/java/wooteco/chess/repository/FinishedDrawGameDAO.java +++ b/src/test/java/wooteco/chess/repository/FinishedDrawGameDao.java @@ -6,7 +6,7 @@ import wooteco.chess.domain.game.Game; import wooteco.chess.domain.game.Started; -public class FinishedDrawGameDAO implements GameDAO { +public class FinishedDrawGameDao implements GameDao { @Override public Optional findById(int gameId) { diff --git a/src/test/java/wooteco/chess/repository/ReadyGameDAO.java b/src/test/java/wooteco/chess/repository/ReadyGameDao.java similarity index 87% rename from src/test/java/wooteco/chess/repository/ReadyGameDAO.java rename to src/test/java/wooteco/chess/repository/ReadyGameDao.java index 4ca517aac2..5bd1bf0d78 100644 --- a/src/test/java/wooteco/chess/repository/ReadyGameDAO.java +++ b/src/test/java/wooteco/chess/repository/ReadyGameDao.java @@ -6,7 +6,7 @@ import wooteco.chess.domain.game.Game; import wooteco.chess.domain.game.Ready; -public class ReadyGameDAO implements GameDAO { +public class ReadyGameDao implements GameDao { @Override public Optional findById(int gameId) { diff --git a/src/test/java/wooteco/chess/repository/StartedGameDAO.java b/src/test/java/wooteco/chess/repository/StartedGameDao.java similarity index 84% rename from src/test/java/wooteco/chess/repository/StartedGameDAO.java rename to src/test/java/wooteco/chess/repository/StartedGameDao.java index 2382099bd5..74696b4613 100644 --- a/src/test/java/wooteco/chess/repository/StartedGameDAO.java +++ b/src/test/java/wooteco/chess/repository/StartedGameDao.java @@ -6,10 +6,10 @@ import wooteco.chess.domain.game.Game; import wooteco.chess.domain.game.Started; -public class StartedGameDAO implements GameDAO { +public class StartedGameDao implements GameDao { private Game game; - public StartedGameDAO() { + public StartedGameDao() { this.game = new Started(new Board()); this.game = game.start(); } diff --git a/src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDAO.java b/src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDao.java similarity index 93% rename from src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDAO.java rename to src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDao.java index 52adc5fef8..e8b5ad655c 100644 --- a/src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDAO.java +++ b/src/test/java/wooteco/chess/repository/WhiteMoreScoreGameDao.java @@ -12,7 +12,7 @@ import wooteco.chess.domain.piece.Team; import wooteco.chess.domain.position.Position; -public class WhiteMoreScoreGameDAO implements GameDAO { +public class WhiteMoreScoreGameDao implements GameDao { @Override public Optional findById(int gameId) { diff --git a/src/test/java/wooteco/chess/service/GameServiceTest.java b/src/test/java/wooteco/chess/service/GameServiceTest.java index 3c1eb971b6..a1542136b2 100644 --- a/src/test/java/wooteco/chess/service/GameServiceTest.java +++ b/src/test/java/wooteco/chess/service/GameServiceTest.java @@ -14,246 +14,248 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.MethodSource; -import wooteco.chess.repository.BlackKingCatchedGameDAO; -import wooteco.chess.repository.FinishedDrawGameDAO; -import wooteco.chess.repository.GameDAO; -import wooteco.chess.repository.ReadyGameDAO; -import wooteco.chess.repository.StartedGameDAO; -import wooteco.chess.repository.WhiteMoreScoreGameDAO; -import wooteco.chess.view.dto.requestdto.PositionRequestDTO; -import wooteco.chess.view.dto.responsedto.BoardDTO; -import wooteco.chess.view.dto.responsedto.ScoreDTO; +import wooteco.chess.repository.BlackKingCatchedGameDao; +import wooteco.chess.repository.FinishedDrawGameDao; +import wooteco.chess.repository.GameDao; +import wooteco.chess.repository.ReadyGameDao; +import wooteco.chess.repository.StartedGameDao; +import wooteco.chess.repository.WhiteMoreScoreGameDao; +import wooteco.chess.view.dto.requestdto.PositionRequestDto; +import wooteco.chess.view.dto.responsedto.BoardDto; +import wooteco.chess.view.dto.responsedto.GameDto; +import wooteco.chess.view.dto.responsedto.ScoreDto; +import wooteco.chess.view.response.ResponseDto; +import wooteco.chess.view.response.ResponseStatus; class GameServiceTest { private GameService service; - private GameDAO gameDAO; + private GameDao gameDAO; private static Stream gameStateSet() { return Stream.of( - Arguments.of(new ReadyGameDAO(), new PositionRequestDTO("a1", "a3")), - Arguments.of(new FinishedDrawGameDAO(), new PositionRequestDTO("a1", "a3")) + Arguments.of(new ReadyGameDao(), new PositionRequestDto("a1", "a3")), + Arguments.of(new FinishedDrawGameDao(), new PositionRequestDto("a1", "a3")) ); } private static Stream initialPositionSet() { return Stream.of( - Arguments.arguments(new PositionRequestDTO("a1", "a2"), - asList(new BoardDTO("a1", "r"), - new BoardDTO("a2", "p")) + Arguments.arguments(new PositionRequestDto("a1", "a2"), + asList(new BoardDto("a1", "r"), + new BoardDto("a2", "p")) ), - Arguments.arguments(new PositionRequestDTO("c1", "c5"), - asList(new BoardDTO("c1", "b"), - new BoardDTO("c5", ".")) + Arguments.arguments(new PositionRequestDto("c1", "c5"), + asList(new BoardDto("c1", "b"), + new BoardDto("c5", ".")) ), - Arguments.arguments(new PositionRequestDTO("b1", "b8"), - asList(new BoardDTO("b1", "n"), - new BoardDTO("b8", "N")) + Arguments.arguments(new PositionRequestDto("b1", "b8"), + asList(new BoardDto("b1", "n"), + new BoardDto("b8", "N")) ) ); } - @DisplayName("게임 시작전 점수 계산 서비스 실행시, USO 예외 발생") + private static Stream stateAndIsNotFinishExpectedSets() { + return Stream.of( + Arguments.of(new ReadyGameDao(), true), + Arguments.of(new StartedGameDao(), true), + Arguments.of(new FinishedDrawGameDao(), false) + ); + } + + @DisplayName("게임 시작전 점수 계산 서비스 실행시, Error status 반환") @Test void calculateScore_Ready_state_exception_test() { - gameDAO = new ReadyGameDAO(); + gameDAO = new ReadyGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.calculateScore()).isInstanceOf(UnsupportedOperationException.class); + ResponseDto response = service.calculateScore(); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } @DisplayName("게임중 점수 계산 서비스 실행시, 정상적으로 DTO 리스트 반환") @Test void calculateScore_Started_state_normal_test() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThat(service.calculateScore()).containsExactlyInAnyOrder( - new ScoreDTO("black", 38.0), - new ScoreDTO("white", 38.0) + assertThat((List)service.calculateScore().getData()).containsExactlyInAnyOrder( + new ScoreDto("black", 38.0), + new ScoreDto("white", 38.0) ); } @DisplayName("게임 종료후 점수 계산 서비스 실행시, 정상적으로 DTO 리스트 반환") @Test void calculateScore_Finished_state_normal_test() { - gameDAO = new FinishedDrawGameDAO(); + gameDAO = new FinishedDrawGameDao(); service = new GameService(gameDAO); - assertThat(service.calculateScore()).containsExactlyInAnyOrder( - new ScoreDTO("black", 38.0), - new ScoreDTO("white", 38.0) + assertThat((List)service.calculateScore().getData()).containsExactlyInAnyOrder( + new ScoreDto("black", 38.0), + new ScoreDto("white", 38.0) ); } @DisplayName("비어있는 보드 판에서 기물 DTO 목록 가져올시, 빈 리스트 반환한다.") @Test void findAllPiecesOnEmptyBoardTest() { - gameDAO = new ReadyGameDAO(); + gameDAO = new ReadyGameDao(); service = new GameService(gameDAO); - assertThat(service.findAllPiecesOnBoard()).isEqualTo(Collections.emptyList()); + assertThat((List)service.findAllPiecesOnBoard().getData()).isEqualTo(Collections.emptyList()); } - @DisplayName("게임 중이 아닌 경우, move 실행시 예외 발생한다.") + @DisplayName("게임 중이 아닌 경우, move 실행시 error status 반환") @ParameterizedTest @MethodSource("gameStateSet") - void moveExceptionTest(GameDAO notPlayingStateDAO, PositionRequestDTO position) { + void moveExceptionTest(GameDao notPlayingStateDAO, PositionRequestDto position) { service = new GameService(notPlayingStateDAO); - assertThatThrownBy(() -> service.move(position)) - .isInstanceOf(UnsupportedOperationException.class); + ResponseDto response = service.move(position); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } @DisplayName("게임 중인 상태에서 올바른 차례의 말 움직이면 예외 없이 정상 실행") @Test void startedGameMoveTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); assertThatCode(() -> { - service.move(new PositionRequestDTO("a2", "a4")); - service.move(new PositionRequestDTO("a7", "a5")); + service.move(new PositionRequestDto("a2", "a4")); + service.move(new PositionRequestDto("a7", "a5")); }).doesNotThrowAnyException(); } - @DisplayName("게임 중인 상태에서 현재 차례 말을 이동하지 못하는 경로로 움직이면 illegal argument 예외 발생") + @DisplayName("게임 중인 상태에서 현재 차례 말을 이동하지 못하는 경로로 움직이면 error status 반환") @Test void startedGameMoveWrongWayTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> { - service.move(new PositionRequestDTO("b2", "b4")); - service.move(new PositionRequestDTO("b7", "b5")); - service.move(new PositionRequestDTO("a1", "c3")); - }).isInstanceOf(IllegalArgumentException.class); + service.move(new PositionRequestDto("b2", "b4")); + service.move(new PositionRequestDto("b7", "b5")); + ResponseDto response = service.move(new PositionRequestDto("a1", "c3")); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } - @DisplayName("게임 중인 상태에서 현재 차례 말을 이동하려는 경로 사이 장애물이 있는 경우 illegal argument 예외 발생") + @DisplayName("게임 중인 상태에서 현재 차례 말을 이동하려는 경로 사이 장애물이 있는 경우 error status 반환") @Test void startedGameMoveWithObstacleTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.move(new PositionRequestDTO("a1", "a5"))) - .isInstanceOf(IllegalArgumentException.class); + ResponseDto response = service.move(new PositionRequestDto("a1", "a5")); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } - @DisplayName("게임 중인 상태에서 현재 차례가 아닌 말을 움직이면 illegal argument 예외 발생") + @DisplayName("게임 중인 상태에서 현재 차례가 아닌 말을 움직이면 error status 빈허ㅣ") @Test void startedGameMoveWrongTurnTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> { - service.move(new PositionRequestDTO("a2", "a4")); - service.move(new PositionRequestDTO("a4", "a5")); - }).isInstanceOf(IllegalArgumentException.class); + service.move(new PositionRequestDto("a2", "a4")); + ResponseDto response = service.move(new PositionRequestDto("a4", "a5")); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } - @DisplayName("게임 중인 상태에서 현재 차례가 아닌 말을 움직이면 illegal argument 예외 발생") + @DisplayName("게임 중인 상태에서 현재 차례가 아닌 말을 움직이면 error status 빈환") @Test void startedGameMoveWrongTurnTest2() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.move(new PositionRequestDTO("a7", "a5"))) - .isInstanceOf(IllegalArgumentException.class); + ResponseDto response = service.move(new PositionRequestDto("a7", "a5")); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } - @DisplayName("게임 중인 상태에서 빈공간을 출발지로 move 시 illegal argument 예외 발생") + @DisplayName("게임 중인 상태에서 빈공간을 출발지로 move 시 error status 반환") @Test void startedGameMoveEmptySpaceTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.move(new PositionRequestDTO("a4", "a5"))) - .isInstanceOf(IllegalArgumentException.class); + ResponseDto response = service.move(new PositionRequestDto("a4", "a5")); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } @DisplayName("초기화된 보드 판에서 기물 DTO 목록 가져올시, 보드위 모든 기물 DTO 목록 반환한다.") @Test void findAllPiecesOnInitialBoardTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThat(service.findAllPiecesOnBoard()).contains( - new BoardDTO("a2", "p"), - new BoardDTO("b2", "p"), - new BoardDTO("c2", "p"), - new BoardDTO("d2", "p"), - new BoardDTO("e2", "p"), - new BoardDTO("f2", "p"), - new BoardDTO("g2", "p"), - new BoardDTO("h2", "p") + assertThat((List)service.findAllPiecesOnBoard().getData()).contains( + new BoardDto("a2", "p"), + new BoardDto("b2", "p"), + new BoardDto("c2", "p"), + new BoardDto("d2", "p"), + new BoardDto("e2", "p"), + new BoardDto("f2", "p"), + new BoardDto("g2", "p"), + new BoardDto("h2", "p") ); } @DisplayName("인자로 받는 두 위치에 해당하는 좌표, 기물 정보가 담긴 DTO 목록을 가져온다.") @ParameterizedTest @MethodSource("initialPositionSet") - void findChangedPiecesOnBoardTest(PositionRequestDTO positionDTO, List expected) { - gameDAO = new StartedGameDAO(); + void findChangedPiecesOnBoardTest(PositionRequestDto positionDTO, List expected) { + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - List actual = service.findChangedPiecesOnBoard(positionDTO); + List actual = service.findChangedPiecesOnBoard(positionDTO); assertThat(actual).isEqualTo(expected); } @DisplayName("게임 상태 변경이 올바르게 이뤄졌는지 확인한다.") @ParameterizedTest @CsvSource({"start,started", "end,suspendFinish"}) - void changeStateTest(String request, String expeected) { - gameDAO = new StartedGameDAO(); + void changeStateTest(String request, String expected) { + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); service.changeState(request); - assertThat(service.getCurrentState().getGameState()).isEqualTo(expeected); + assertThat(((GameDto)service.getCurrentState().getData()).getGameState()).isEqualTo(expected); } @DisplayName("게임이 끝난 상태가 아니면 true 반환한다.") @ParameterizedTest @MethodSource("stateAndIsNotFinishExpectedSets") - void isNotFinishTest(GameDAO gameDAO, boolean expected) { + void isNotFinishTest(GameDao gameDAO, boolean expected) { service = new GameService(gameDAO); - boolean actual = service.isNotFinish(); + boolean actual = (boolean)service.isNotFinish().getData(); assertThat(actual).isEqualTo(expected); } - private static Stream stateAndIsNotFinishExpectedSets() { - return Stream.of( - Arguments.of(new ReadyGameDAO(), true), - Arguments.of(new StartedGameDAO(), true), - Arguments.of(new FinishedDrawGameDAO(), false) - ); - } - - @DisplayName("게임이 준비 상태인 경우, 승자를 구하고자 할때 USO 예외 발생한다.") + @DisplayName("게임이 준비 상태인 경우, 승자를 구하고자 할때 error status 반환") @Test void getWinnerExceptionTest() { - gameDAO = new ReadyGameDAO(); + gameDAO = new ReadyGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.getWinner()) - .isInstanceOf(UnsupportedOperationException.class); + ResponseDto response = service.getWinner(); + assertThat(response.getStatus()).isEqualTo(ResponseStatus.ERROR); } - @DisplayName("게임이 진행중인 상태인 경우, 승자를 구하고자 할때 USO 예외 발생한다.") + @DisplayName("게임이 진행중인 상태인 경우, 승자를 구하고자 할때 Error status를 반환한다.") @Test void getWinnerWithStartedStateExceptionTest() { - gameDAO = new StartedGameDAO(); + gameDAO = new StartedGameDao(); service = new GameService(gameDAO); - assertThatThrownBy(() -> service.getWinner()) - .isInstanceOf(UnsupportedOperationException.class); + ResponseDto winner = service.getWinner(); + assertThat(winner.getStatus()).isEqualTo(ResponseStatus.ERROR); } @DisplayName("게임이 동점으로 끝난 상태인 경우, 승자가 없다.") @Test void getWinnerWithFinishedStateTest() { - gameDAO = new FinishedDrawGameDAO(); + gameDAO = new FinishedDrawGameDao(); service = new GameService(gameDAO); - assertThat(service.getWinner()).isEqualTo("none"); + assertThat((String)service.getWinner().getData()).isEqualTo("none"); } @DisplayName("검정 왕이 잡힌 경우, 하얀 팀이 이긴다.") @Test void getWinnerWithFinishedStateTest2() { - gameDAO = new BlackKingCatchedGameDAO(); + gameDAO = new BlackKingCatchedGameDao(); service = new GameService(gameDAO); - assertThat(service.getWinner()).isEqualTo("white"); + assertThat((String)service.getWinner().getData()).isEqualTo("white"); } @DisplayName("하얀 팀이 기물 점수가 더 높은 경우, 하얀 팀이 이긴다.") @Test void getWinnerWithFinishedStateTest3() { - gameDAO = new WhiteMoreScoreGameDAO(); + gameDAO = new WhiteMoreScoreGameDao(); service = new GameService(gameDAO); - assertThat(service.getWinner()).isEqualTo("white"); + assertThat((String)service.getWinner().getData()).isEqualTo("white"); } } \ No newline at end of file