Skip to content

Commit

Permalink
feat : 게임방 기능 구현
Browse files Browse the repository at this point in the history
- 저장시 DB에 저장
- 불러오기 시 DB 로부터 게임 load
- 진행중인 게임을 저장하지 않고 종료시 DB 삭제
- 진행중인 게임에서 새 게임 플레이 가능
  • Loading branch information
fucct committed Apr 8, 2020
1 parent b2ea5e9 commit 2754a72
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 91 deletions.
173 changes: 101 additions & 72 deletions src/main/java/chess/controller/ChessWebController.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -21,84 +23,111 @@ public void run() {
get("/", (req, res) -> {
Map<String, Object> model = new HashMap<>();
ResponseDto responseDto = chessService.getRoomId();
List<Long> roomId = responseDto.getRoomId();
List<WebDto> roomIdDto = roomId.stream().map(id -> new WebDto(id.toString(), id)).collect(Collectors.toList());
List<WebDto> roomIdDto = makeRoomIdDto(responseDto);
model.put("roomId", roomIdDto);
return render(model, "index.html");
});

post("/createChessGame", (req, res) -> {
ResponseDto responseDto = chessService.start(null);
Map<String, Object> 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<WebDto> boardDto = makeBoardDto(responseDto);
List<WebDto> scoreDto = makeScoreDto(responseDto);
WebDto turnDto = makeTurnDto(responseDto);
Map<String, Object> 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<WebDto> boardDto = makeBoardDto(responseDto);
List<WebDto> scoreDto = makeScoreDto(responseDto);
WebDto turnDto = makeTurnDto(responseDto);
Map<String, Object> 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<String, Object> model = new HashMap<>();
RequestDto requestDto = makeRequestDto(req);
ResponseDto responseDto = chessService.move(requestDto);
List<WebDto> boardDto = makeBoardDto(responseDto);
List<WebDto> 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<WebDto> roomDto = makeRoomIdDto(responseDto);
Map<String, Object> 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<WebDto> 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<WebDto> makeScoreDto(final ResponseDto responseDto) {
return responseDto.getStatus().entrySet().stream().map(entry ->
new WebDto(entry.getKey().toString(), entry.getValue().toString()))
.collect(Collectors.toList());
}

private List<WebDto> 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<String, Object> 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<WebDto> boardDto = getBoardDto(responseDto.getBoard());
// List<WebDto> scoreDto = getScoreDto(responseDto.getScores());
// WebDto turnDto = getTurnDto(responseDto.getTurn());
// Map<String, Object> 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<WebDto> boardDto = getBoardDto(responseDto.getBoard());
// List<WebDto> scoreDto = getScoreDto(responseDto.getScores());
// WebDto turnDto = getTurnDto(responseDto.getTurn());
// Map<String, Object> 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<String, Object> model = new HashMap<>();
// String number = req.queryParams("id").trim();
// Long id = Long.valueOf(number);
// List<String> 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<WebDto> boardDto = getBoardDto(responseDto.getBoard());
// List<WebDto> 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<String> parameters = new ArrayList<>(Arrays.asList(req.queryParams("parameter").split("_")));
// chessService.end(id, parameters);
// List<WebDto> roomDto = getRoomDto(chessService.getRoomId());
// Map<String, Object> model = new HashMap<>();
// model.put("roomId", roomDto);
// return render(model, "index.html");
// });
//
// get("/", (req, res) -> {
// List<WebDto> roomDto = getRoomDto(chessService.getRoomId());
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/chess/controller/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,14 +15,18 @@ public class ResponseDto {
private Map<Position, String> board;
private Status status;
private String message;
private Turn turn;
private Player winner;
private long id;

public ResponseDto(final List<Long> roomId) {
this.roomId = roomId;
}

public ResponseDto(Map<Position, String> board, long id) {
public ResponseDto(Map<Position, String> board, Turn turn, Status status, long id) {
this.board = board;
this.turn = turn;
this.status = status;
this.id = id;
}

Expand Down Expand Up @@ -52,6 +57,15 @@ public String getMessage() {
return message;
}

public Turn getTurn() {
return turn;
}

public long getId() {

return id;
}

public void setBoard(final Map<Position, String> board) {
this.board = board;
}
Expand All @@ -64,11 +78,19 @@ 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<Long> getRoomId() {
return roomId;
}

public void setRoomId(final List<Long> roomId) {
this.roomId = roomId;
}
}
2 changes: 1 addition & 1 deletion src/main/java/chess/domain/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Map<Position, String> getBoardAndString() {
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> entry.getValue().getFigure()
entry -> entry.getValue().getPlayer().toString() + entry.getValue().toString()
));
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/chess/domain/game/ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/chess/domain/game/Turn.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public void switchTurn() {
public boolean isSamePlayer(Player player) {
return turn.equals(player);
}

@Override
public String toString() {
return turn.toString();
}
}
Loading

0 comments on commit 2754a72

Please sign in to comment.