From 2754a7246ca705e1fca951168b648d388c1e984d Mon Sep 17 00:00:00 2001 From: dd Date: Wed, 8 Apr 2020 16:19:55 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=9E=84=EB=B0=A9=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 저장시 DB에 저장 - 불러오기 시 DB 로부터 게임 load - 진행중인 게임을 저장하지 않고 종료시 DB 삭제 - 진행중인 게임에서 새 게임 플레이 가능 --- .../chess/controller/ChessWebController.java | 173 ++++++++++-------- .../chess/controller/dto/ResponseDto.java | 24 ++- src/main/java/chess/domain/board/Board.java | 2 +- .../java/chess/domain/game/ChessGame.java | 7 + src/main/java/chess/domain/game/Turn.java | 5 + src/main/java/chess/service/ChessService.java | 87 +++++++-- src/main/resources/templates/chessGame.html | 3 +- 7 files changed, 210 insertions(+), 91 deletions(-) diff --git a/src/main/java/chess/controller/ChessWebController.java b/src/main/java/chess/controller/ChessWebController.java index 1e3ed181a9d..07c4041adc2 100644 --- a/src/main/java/chess/controller/ChessWebController.java +++ b/src/main/java/chess/controller/ChessWebController.java @@ -1,17 +1,19 @@ package chess.controller; +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; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import static spark.Spark.get; +import static spark.Spark.post; public class ChessWebController { @@ -21,84 +23,111 @@ public void run() { get("/", (req, res) -> { Map model = new HashMap<>(); ResponseDto responseDto = chessService.getRoomId(); - List roomId = responseDto.getRoomId(); - List roomIdDto = roomId.stream().map(id -> new WebDto(id.toString(), id)).collect(Collectors.toList()); + List roomIdDto = makeRoomIdDto(responseDto); model.put("roomId", roomIdDto); return render(model, "index.html"); }); + + post("/createChessGame", (req, res) -> { + ResponseDto responseDto = chessService.start(null); + Map model = new HashMap<>(); + model.put("id", responseDto.getId()); + return render(model, "create.html"); + }); + + post("/loadChessGame", (req, res) -> { + RequestDto requestDto = makeRequestDto(req); + ResponseDto responseDto = chessService.load(requestDto); + List boardDto = makeBoardDto(responseDto); + List scoreDto = makeScoreDto(responseDto); + WebDto turnDto = makeTurnDto(responseDto); + Map model = new HashMap<>(); + model.put("board", boardDto); + model.put("score", scoreDto); + model.put("turn", turnDto); + model.put("id", responseDto.getId()); + return render(model, "chessGame.html"); + }); + + post("/restartChessGame", (req, res) -> { + RequestDto requestDto = makeRequestDto(req); + ResponseDto responseDto = chessService.restart(requestDto); + List boardDto = makeBoardDto(responseDto); + List scoreDto = makeScoreDto(responseDto); + WebDto turnDto = makeTurnDto(responseDto); + Map model = new HashMap<>(); + model.put("board", boardDto); + model.put("score", scoreDto); + model.put("turn", turnDto); + model.put("id", requestDto.getId()); + return render(model, "chessGame.html"); + }); + + + post("/move", (req, res) -> { + Map model = new HashMap<>(); + RequestDto requestDto = makeRequestDto(req); + ResponseDto responseDto = chessService.move(requestDto); + List boardDto = makeBoardDto(responseDto); + List scoreDto = makeScoreDto(responseDto); + WebDto winnerDto = makeWinnerDto(responseDto); + WebDto turnDto = makeTurnDto(responseDto); + model.put("board", boardDto); + model.put("score", scoreDto); + model.put("turn", turnDto); + model.put("winner", winnerDto); + model.put("id", responseDto.getId()); + model.put("message", responseDto.getMessage()); + return render(model, "chessGame.html"); + }); + + post("/end", (req, res) -> { + RequestDto requestDto = makeRequestDto(req); + ResponseDto responseDto = chessService.end(requestDto); + List roomDto = makeRoomIdDto(responseDto); + Map model = new HashMap<>(); + model.put("roomId", roomDto); + return render(model, "index.html"); + }); + } + + private WebDto makeWinnerDto(final ResponseDto responseDto) { + return new WebDto(responseDto.getWinner().toString(), responseDto.getWinner().toString()); + } + + private RequestDto makeRequestDto(final Request req) { + return new RequestDto(Command.of(req.queryParams("command")), + new ArrayList<>(Arrays.asList(req.queryParams("parameter").split("_"))), + Long.valueOf(req.queryParams("id").trim())); + } + + private List makeRoomIdDto(final ResponseDto responseDto) { + return responseDto.getRoomId() + .stream() + .map(id -> new WebDto(id.toString(), id)) + .collect(Collectors.toList()); + } + + private WebDto makeTurnDto(final ResponseDto responseDto) { + return new WebDto(responseDto.getTurn().toString(), responseDto.getTurn().toString()); + } + + private List makeScoreDto(final ResponseDto responseDto) { + return responseDto.getStatus().entrySet().stream().map(entry -> + new WebDto(entry.getKey().toString(), entry.getValue().toString())) + .collect(Collectors.toList()); + } + + private List makeBoardDto(final ResponseDto responseDto) { + return responseDto.getBoard().entrySet().stream().map(entry -> + 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)); } // -// post("/restartChessGame", (req, res) -> { -// String number = req.queryParams("id").trim(); -// Long id = Long.valueOf(number); -// chessService.restart(id); -// ResponseDto responseDto = chessService.getResponseDto(id); -// List boardDto = getBoardDto(responseDto.getBoard()); -// List scoreDto = getScoreDto(responseDto.getScores()); -// WebDto turnDto = getTurnDto(responseDto.getTurn()); -// Map model = new HashMap<>(); -// model.put("board", boardDto); -// model.put("score", scoreDto); -// model.put("turn", turnDto); -// model.put("id", id); -// return render(model, "chessGame.html"); -// }); -// -// post("/loadChessGame", (req, res) -> { -// String number = req.queryParams("id").trim(); -// Long id = Long.valueOf(number); -// chessService.load(id); -// ResponseDto responseDto = chessService.getResponseDto(id); -// List boardDto = getBoardDto(responseDto.getBoard()); -// List scoreDto = getScoreDto(responseDto.getScores()); -// WebDto turnDto = getTurnDto(responseDto.getTurn()); -// Map model = new HashMap<>(); -// model.put("board", boardDto); -// model.put("score", scoreDto); -// model.put("turn", turnDto); -// model.put("id", id); -// return render(model, "chessGame.html"); -// }); -// -// post("/move", (req, res) -> { -// String message = null; -// Map model = new HashMap<>(); -// String number = req.queryParams("id").trim(); -// Long id = Long.valueOf(number); -// List parameters = new ArrayList<>(Arrays.asList(req.queryParams("parameter").split("_"))); -// try { -// chessService.move(id, parameters); -// } catch (IllegalArgumentException | UnsupportedOperationException e) { -// model.put("error-message", e.getMessage()); -// } -// ResponseDto responseDto = chessService.getResponseDto(id); -// List boardDto = getBoardDto(responseDto.getBoard()); -// List scoreDto = getScoreDto(responseDto.getScores()); -// WebDto winnerDto = getTurnDto(responseDto.getWinner()); -// WebDto turnDto = getTurnDto(responseDto.getTurn()); -// model.put("board", boardDto); -// model.put("score", scoreDto); -// model.put("turn", turnDto); -// model.put("message", responseDto.getMessage()); -// model.put("winner", winnerDto); -// model.put("id", id); -// return render(model, "chessGame.html"); -// }); -// -// post("/end", (req, res) -> { -// String number = req.queryParams("id").trim(); -// Long id = Long.valueOf(number); -// List parameters = new ArrayList<>(Arrays.asList(req.queryParams("parameter").split("_"))); -// chessService.end(id, parameters); -// List roomDto = getRoomDto(chessService.getRoomId()); -// Map model = new HashMap<>(); -// model.put("roomId", roomDto); -// return render(model, "index.html"); -// }); // // get("/", (req, res) -> { // List roomDto = getRoomDto(chessService.getRoomId()); diff --git a/src/main/java/chess/controller/dto/ResponseDto.java b/src/main/java/chess/controller/dto/ResponseDto.java index d7ae7429803..9a1cb8863c9 100644 --- a/src/main/java/chess/controller/dto/ResponseDto.java +++ b/src/main/java/chess/controller/dto/ResponseDto.java @@ -1,5 +1,6 @@ package chess.controller.dto; +import chess.domain.game.Turn; import chess.domain.player.Player; import chess.domain.position.Position; import chess.domain.status.Status; @@ -14,14 +15,18 @@ public class ResponseDto { private Map board; private Status status; private String message; + private Turn turn; + private Player winner; private long id; public ResponseDto(final List roomId) { this.roomId = roomId; } - public ResponseDto(Map board, long id) { + public ResponseDto(Map board, Turn turn, Status status, long id) { this.board = board; + this.turn = turn; + this.status = status; this.id = id; } @@ -52,6 +57,15 @@ public String getMessage() { return message; } + public Turn getTurn() { + return turn; + } + + public long getId() { + + return id; + } + public void setBoard(final Map board) { this.board = board; } @@ -64,6 +78,10 @@ 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; } @@ -71,4 +89,8 @@ public void setId(final long id) { public List getRoomId() { return roomId; } + + public void setRoomId(final List roomId) { + this.roomId = roomId; + } } diff --git a/src/main/java/chess/domain/board/Board.java b/src/main/java/chess/domain/board/Board.java index ae7f8a3b98c..542a3c67d97 100644 --- a/src/main/java/chess/domain/board/Board.java +++ b/src/main/java/chess/domain/board/Board.java @@ -64,7 +64,7 @@ public Map getBoardAndString() { .stream() .collect(Collectors.toMap( Map.Entry::getKey, - entry -> entry.getValue().getFigure() + entry -> entry.getValue().getPlayer().toString() + entry.getValue().toString() )); } diff --git a/src/main/java/chess/domain/game/ChessGame.java b/src/main/java/chess/domain/game/ChessGame.java index 3c34657f31d..2f02f8c7c1e 100644 --- a/src/main/java/chess/domain/game/ChessGame.java +++ b/src/main/java/chess/domain/game/ChessGame.java @@ -63,6 +63,13 @@ public Status getStatus() { return status; } + public Player getWinner() { + if (board.isLost(Player.BLACK)) { + return Player.WHITE; + } + return Player.BLACK; + } + public boolean isEnd() { return isEnd; } diff --git a/src/main/java/chess/domain/game/Turn.java b/src/main/java/chess/domain/game/Turn.java index b0069871929..137b88e67de 100644 --- a/src/main/java/chess/domain/game/Turn.java +++ b/src/main/java/chess/domain/game/Turn.java @@ -21,4 +21,9 @@ public void switchTurn() { public boolean isSamePlayer(Player player) { return turn.equals(player); } + + @Override + public String toString() { + return turn.toString(); + } } diff --git a/src/main/java/chess/service/ChessService.java b/src/main/java/chess/service/ChessService.java index 43b2ec93722..5e770370711 100644 --- a/src/main/java/chess/service/ChessService.java +++ b/src/main/java/chess/service/ChessService.java @@ -4,9 +4,11 @@ import chess.controller.dto.ResponseDto; import chess.dao.ChessDAO; import chess.domain.game.ChessGame; +import chess.domain.game.MoveParameter; import java.sql.SQLException; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.function.Function; @@ -19,7 +21,6 @@ public ChessService() { commands.put(Command.START, this::start); commands.put(Command.MOVE, this::move); commands.put(Command.END, this::end); - commands.put(Command.STATUS, this::status); commands.put(Command.UNKNOWN, this::unknown); } @@ -27,35 +28,67 @@ public ResponseDto run(RequestDto requestDto) { return commands.get(requestDto.getCommand()).apply(requestDto); } - private ResponseDto start(final RequestDto requestDto) { + public ResponseDto start(RequestDto requestDto) { try { ChessGame chessGame = ChessGame.start(); long id = chessDAO.createChessGame(chessGame); - return new ResponseDto(chessGame.getBoardAndString(), id); + return new ResponseDto(chessGame.getBoardAndString(), chessGame.getTurn(), + chessGame.getStatus(), id); } catch (SQLException | IllegalArgumentException | UnsupportedOperationException e) { return new ResponseDto(e.getMessage()); } } - private ResponseDto move(final RequestDto requestDto) { -// long id = Long.valueOf(parameters.get(1)); -// chessGames.get(id).move(MoveParameter.of(parameters)); -// return new ResponseDto(chessGame.getBoardAndString()); - return null; + public ResponseDto move(final RequestDto requestDto) { + long id = Long.valueOf(requestDto.getId()); + ChessGame chessGame = chessGames.get(id); + ResponseDto responseDto = new ResponseDto(chessGame.getBoardAndString(), chessGame.getTurn(), + chessGame.getStatus(), id); + responseDto.setStatus(chessGame.getStatus()); + try { + chessGame.move(MoveParameter.of(requestDto.getParameter())); + responseDto = new ResponseDto(chessGame.getBoardAndString(), chessGame.getTurn(), + chessGame.getStatus(), id); + responseDto.setStatus(chessGame.getStatus()); + setWinner(chessGame, responseDto); + } catch (UnsupportedOperationException | IllegalArgumentException e) { + responseDto.setMessage(e.getMessage()); + } + return responseDto; } - private ResponseDto end(RequestDto requestDto) { -// chessGame.end(); -// return new ResponseDto(chessGame.getBoardAndString()); - return null; + private void setWinner(final ChessGame chessGame, final ResponseDto responseDto) { + if (chessGame.isEnd()) { + responseDto.setWinner(chessGame.getWinner()); + } } - private ResponseDto status(RequestDto requestDto) { -// chessGame.status(); -// return new ResponseDto(chessGame.getStatus()); - return null; + public ResponseDto end(RequestDto requestDto) { + long id = requestDto.getId(); + ChessGame chessGame = chessGames.get(id); + 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); + } + chessGames.remove(id); + responseDto.setRoomId(chessDAO.getRoomId()); + } catch (SQLException e) { + responseDto.setMessage(e.getMessage()); + } + return responseDto; } +// private ResponseDto status(RequestDto requestDto) { +//// chessGame.status(); +//// return new ResponseDto(chessGame.getStatus()); +// return null; +// } + private ResponseDto unknown(RequestDto requestDto) { return new ResponseDto("알 수 없는 명령어 입니다."); } @@ -72,4 +105,26 @@ public ResponseDto getRoomId() { return new ResponseDto(e.getMessage()); } } + + public ResponseDto load(final RequestDto requestDto) { + ResponseDto responseDto = new ResponseDto(""); + long id = requestDto.getId(); + try { + responseDto.setRoomId(chessDAO.getRoomId()); + ChessGame chessGame = chessDAO.findGameById(id); + chessGames.put(id, chessGame); + responseDto = new ResponseDto(chessGame.getBoardAndString(), chessGame.getTurn(), + chessGame.getStatus(), id); + } catch (SQLException e) { + responseDto.setMessage(e.getMessage()); + } + return responseDto; + } + + public ResponseDto restart(final RequestDto requestDto) { + ChessGame chessGame = ChessGame.start(); + chessGames.put(requestDto.getId(), chessGame); + return new ResponseDto(chessGame.getBoardAndString(), chessGame.getTurn(), + chessGame.getStatus(), requestDto.getId()); + } } diff --git a/src/main/resources/templates/chessGame.html b/src/main/resources/templates/chessGame.html index 2f17dfd54b0..aa2b601c268 100644 --- a/src/main/resources/templates/chessGame.html +++ b/src/main/resources/templates/chessGame.html @@ -2,7 +2,7 @@ - 체스게임 {{id}}게임 + 체스게임 {{id}}번방 @@ -193,6 +193,7 @@ {{/if}} document.getElementById("{{turn.key}}-player-turn").innerText = '현재 턴' +