From 2f4375bf6ef7a77c59a4ff69aeb34782886b3dc3 Mon Sep 17 00:00:00 2001 From: dd Date: Wed, 8 Apr 2020 16:26:04 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/{service => command}/Command.java | 2 +- .../chess/controller/ChessController.java | 2 +- .../chess/controller/ChessWebController.java | 10 ++--- .../java/chess/controller/dto/RequestDto.java | 15 ++++--- .../chess/controller/dto/ResponseDto.java | 42 ++++++++----------- src/main/java/chess/domain/piece/Pawn.java | 22 +++++----- src/main/java/chess/service/ChessService.java | 31 +++++++------- src/main/java/chess/view/InputView.java | 2 +- src/main/resources/templates/chessGame.html | 2 + src/main/resources/templates/create.html | 4 +- 10 files changed, 64 insertions(+), 68 deletions(-) rename src/main/java/chess/{service => command}/Command.java (92%) diff --git a/src/main/java/chess/service/Command.java b/src/main/java/chess/command/Command.java similarity index 92% rename from src/main/java/chess/service/Command.java rename to src/main/java/chess/command/Command.java index 1fc3e0870a3..388d049da71 100644 --- a/src/main/java/chess/service/Command.java +++ b/src/main/java/chess/command/Command.java @@ -1,4 +1,4 @@ -package chess.service; +package chess.command; public enum Command { START, diff --git a/src/main/java/chess/controller/ChessController.java b/src/main/java/chess/controller/ChessController.java index 1df4664bf0a..281c0b5d965 100644 --- a/src/main/java/chess/controller/ChessController.java +++ b/src/main/java/chess/controller/ChessController.java @@ -11,7 +11,7 @@ public class ChessController { public void run() { OutputView.printInitialMessage(); - while (!chessService.isEnd()) { + while (!chessService.isEnd(0)) { RequestDto requestDto = InputView.inputRequest(); ResponseDto responseDto = chessService.run(requestDto); OutputView.printResponse(responseDto); diff --git a/src/main/java/chess/controller/ChessWebController.java b/src/main/java/chess/controller/ChessWebController.java index 07c4041adc2..0486244c987 100644 --- a/src/main/java/chess/controller/ChessWebController.java +++ b/src/main/java/chess/controller/ChessWebController.java @@ -1,10 +1,10 @@ package chess.controller; +import chess.command.Command; import chess.controller.dto.RequestDto; import chess.controller.dto.ResponseDto; import chess.controller.dto.WebDto; import chess.service.ChessService; -import chess.service.Command; import spark.ModelAndView; import spark.Request; import spark.template.handlebars.HandlebarsTemplateEngine; @@ -19,6 +19,10 @@ public class ChessWebController { private ChessService chessService = new ChessService(); + private static String render(Map model, String templatePath) { + return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath)); + } + public void run() { get("/", (req, res) -> { Map model = new HashMap<>(); @@ -123,10 +127,6 @@ private List makeBoardDto(final ResponseDto responseDto) { new WebDto(entry.getKey().getName(), entry.getValue())) .collect(Collectors.toList()); } - - private static String render(Map model, String templatePath) { - return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath)); - } // // // get("/", (req, res) -> { diff --git a/src/main/java/chess/controller/dto/RequestDto.java b/src/main/java/chess/controller/dto/RequestDto.java index 5e86f0147d2..386ae331806 100644 --- a/src/main/java/chess/controller/dto/RequestDto.java +++ b/src/main/java/chess/controller/dto/RequestDto.java @@ -1,6 +1,6 @@ package chess.controller.dto; -import chess.service.Command; +import chess.command.Command; import java.util.List; @@ -9,19 +9,18 @@ public class RequestDto { private List parameter; private long id; - public RequestDto(Command command, List parameter) { + public RequestDto(Command command) { this.command = command; - this.parameter = parameter; } - public RequestDto(final Command command, final List parameter, final long id) { - this.command = command; + public RequestDto(Command command, List parameter) { + this(command); this.parameter = parameter; - this.id = id; } - public RequestDto(Command command) { - this.command = command; + public RequestDto(final Command command, final List parameter, final long id) { + this(command, parameter); + this.id = id; } public Command getCommand() { diff --git a/src/main/java/chess/controller/dto/ResponseDto.java b/src/main/java/chess/controller/dto/ResponseDto.java index 9a1cb8863c9..e81e5abadf3 100644 --- a/src/main/java/chess/controller/dto/ResponseDto.java +++ b/src/main/java/chess/controller/dto/ResponseDto.java @@ -30,10 +30,6 @@ public ResponseDto(Map board, Turn turn, Status status, long i this.id = id; } - public ResponseDto(Status status) { - this.status = status; - } - public ResponseDto(final String message) { this.message = message; } @@ -42,6 +38,10 @@ public Map getBoard() { return board; } + public void setBoard(final Map board) { + this.board = board; + } + public Map getStatus() { if (Objects.nonNull(status)) { return status.getStatus(); @@ -49,14 +49,26 @@ public Map getStatus() { return null; } + public void setStatus(final Status status) { + this.status = status; + } + public Player getWinner() { return status.getWinner(); } + public void setWinner(final Player winner) { + this.winner = winner; + } + public String getMessage() { return message; } + public void setMessage(final String message) { + this.message = message; + } + public Turn getTurn() { return turn; } @@ -66,26 +78,6 @@ public long getId() { return id; } - public void setBoard(final Map board) { - this.board = board; - } - - public void setStatus(final Status status) { - this.status = status; - } - - public void setMessage(final String message) { - this.message = message; - } - - public void setWinner(final Player winner) { - this.winner = winner; - } - - public void setId(final long id) { - this.id = id; - } - public List getRoomId() { return roomId; } @@ -93,4 +85,4 @@ public List getRoomId() { public void setRoomId(final List roomId) { this.roomId = roomId; } -} +} \ No newline at end of file diff --git a/src/main/java/chess/domain/piece/Pawn.java b/src/main/java/chess/domain/piece/Pawn.java index afdd378cbfd..95e49c19b40 100644 --- a/src/main/java/chess/domain/piece/Pawn.java +++ b/src/main/java/chess/domain/piece/Pawn.java @@ -39,6 +39,17 @@ protected Pawn(Position position, Player player) { super(PieceType.PAWN, position, player); } + public static PieceState of(final Position position, final Player player) { + Position whitePawnPosition = Position.of("a2"); + Position blackPawnPosition = Position.of("a7"); + + if (whitePawnPosition.getRankDifference(position) == 0 && player.equals(Player.WHITE) || + blackPawnPosition.getRankDifference(position) == 0 && player.equals(Player.BLACK)) { + return new NotMovedPawn(position, player); + } + return new MovedPawn(position, player); + } + @Override protected void validateMovingPolicy(Position target, Map boardDto) { MovingDirection movingDirection = MovingDirection.getDirection(position, target); @@ -70,15 +81,4 @@ public String getFigure() { public String toString() { return pieceType.toString(); } - - public static PieceState of(final Position position, final Player player) { - Position whitePawnPosition = Position.of("a2"); - Position blackPawnPosition = Position.of("a7"); - - if (whitePawnPosition.getRankDifference(position) == 0 && player.equals(Player.WHITE) || - blackPawnPosition.getRankDifference(position) == 0 && player.equals(Player.BLACK)) { - return new NotMovedPawn(position, player); - } - return new MovedPawn(position, player); - } } diff --git a/src/main/java/chess/service/ChessService.java b/src/main/java/chess/service/ChessService.java index 5e770370711..79f19f5c85c 100644 --- a/src/main/java/chess/service/ChessService.java +++ b/src/main/java/chess/service/ChessService.java @@ -1,5 +1,6 @@ package chess.service; +import chess.command.Command; import chess.controller.dto.RequestDto; import chess.controller.dto.ResponseDto; import chess.dao.ChessDAO; @@ -69,12 +70,8 @@ public ResponseDto end(RequestDto requestDto) { List parameter = requestDto.getParameter(); ResponseDto responseDto = new ResponseDto(""); try { - if ("save".equals(parameter.get(0))) { - chessDAO.addBoard(id, chessGame); - } - if ("".equals(parameter.get(0))) { - chessDAO.deleteGame(id); - } + saveGame(id, chessGame, parameter); + endGame(id, parameter); chessGames.remove(id); responseDto.setRoomId(chessDAO.getRoomId()); } catch (SQLException e) { @@ -83,19 +80,25 @@ public ResponseDto end(RequestDto requestDto) { return responseDto; } -// private ResponseDto status(RequestDto requestDto) { -//// chessGame.status(); -//// return new ResponseDto(chessGame.getStatus()); -// return null; -// } + private void endGame(final long id, final List parameter) throws SQLException { + if ("".equals(parameter.get(0))) { + chessDAO.deleteGame(id); + } + } + + private void saveGame(final long id, final ChessGame chessGame, final List parameter) throws SQLException { + if ("save".equals(parameter.get(0))) { + chessDAO.addBoard(id, chessGame); + } + } + private ResponseDto unknown(RequestDto requestDto) { return new ResponseDto("알 수 없는 명령어 입니다."); } - public boolean isEnd() { -// return chessGame.isEnd(); - return false; + public boolean isEnd(long id) { + return chessGames.get(id).isEnd(); } public ResponseDto getRoomId() { diff --git a/src/main/java/chess/view/InputView.java b/src/main/java/chess/view/InputView.java index 375f38e9d86..7e4fcaee4ed 100644 --- a/src/main/java/chess/view/InputView.java +++ b/src/main/java/chess/view/InputView.java @@ -1,7 +1,7 @@ package chess.view; +import chess.command.Command; import chess.controller.dto.RequestDto; -import chess.service.Command; import java.util.Arrays; import java.util.List; diff --git a/src/main/resources/templates/chessGame.html b/src/main/resources/templates/chessGame.html index aa2b601c268..7e2ca0ba9dc 100644 --- a/src/main/resources/templates/chessGame.html +++ b/src/main/resources/templates/chessGame.html @@ -194,6 +194,8 @@ document.getElementById("{{turn.key}}-player-turn").innerText = '현재 턴' + +