Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[스티치] 체스 스프링 실습 4단계 미션 제출입니다 #209

Merged
merged 2 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/wooteco/chess/ConsoleChessApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import wooteco.chess.controller.ChessController;
import wooteco.chess.controller.ConsoleChessController;
import wooteco.chess.domain.chessBoard.ChessBoard;
import wooteco.chess.domain.chessBoard.ChessBoardInitializer;
import wooteco.chess.domain.chessGame.ChessCommand;
Expand All @@ -16,11 +16,11 @@ public class ConsoleChessApplication {
public static void main(String[] args) {
ChessBoard chessBoard = new ChessBoard(ChessBoardInitializer.create());
ChessGame chessGame = ChessGame.from(chessBoard);
ChessController chessController = new ChessController(chessGame);
ConsoleChessController consoleChessController = new ConsoleChessController(chessGame);

ConsoleOutputView.printChessStart();
if (isStartChessCommand()) {
chessController.run();
consoleChessController.run();
}
ConsoleOutputView.printChessEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import wooteco.chess.domain.chessGame.ChessGame;
import wooteco.chess.util.ChessBoardRenderer;

public class ChessController {
public class ConsoleChessController {

private final ChessGame chessGame;

public ChessController(ChessGame chessGame) {
public ConsoleChessController(ChessGame chessGame) {
Objects.requireNonNull(chessGame, "체스 게임이 null입니다.");
this.chessGame = chessGame;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SpringChessController(final ChessService chessService) {

@GetMapping("/")
public String index(Model model) {
model.addAttribute("gameNames", chessService.showAllGames());
model.addAttribute("games", chessService.showAllGames());
return "index";
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/wooteco/chess/database/GameRoomRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wooteco.chess.database;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -14,6 +15,6 @@ public interface GameRoomRepository extends CrudRepository<GameRoom, Long> {
List<GameRoom> findAll();

@Query("SELECT * FROM game_room WHERE name = :name")
GameRoom findByName(@Param("name") final String name);
Optional<GameRoom> findByName(@Param("name") final String name);

}
17 changes: 8 additions & 9 deletions src/main/java/wooteco/chess/service/ChessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import wooteco.chess.entity.GameHistory;
import wooteco.chess.entity.GameRoom;
import wooteco.chess.service.dto.ChessGameDto;
import wooteco.chess.service.dto.GameRoomDto;

@Service
public class ChessService {
Expand All @@ -30,7 +31,8 @@ public ChessService(final GameRoomRepository gameRoomRepository) {
}

public ChessGameDto loadChessGameByName(String name) {
GameRoom gameRoom = gameRoomRepository.findByName(name);
GameRoom gameRoom = gameRoomRepository.findByName(name)
.orElseThrow(() -> new NoSuchElementException("해당 이름의 방이 존재하지 않습니다."));
return ChessGameDto.of(gameRoom.getId(), initChessGameOf(gameRoom));
}

Expand All @@ -52,17 +54,14 @@ public ChessGameDto playChessGame(final Long gameId, final String sourcePosition
}

private ChessGameDto moveChessPiece(final Long gameId, final String sourcePosition, final String targetPosition) {
GameRoom gameRoom = gameRoomRepository.findById(gameId)
final GameRoom gameRoom = gameRoomRepository.findById(gameId)
.orElseThrow(() -> new NoSuchElementException("게임이 존재하지 않습니다."));

final ChessGame chessGame = initChessGameOf(gameRoom);
final ChessCommand chessCommand = ChessCommand.of(Arrays.asList(MOVE_COMMAND, sourcePosition, targetPosition));

chessGame.move(chessCommand);
gameRoom.addGameHistory(new GameHistory(sourcePosition, targetPosition, gameId));

gameRoomRepository.save(gameRoom);

gameRoomRepository.save(new GameRoom(gameRoom, chessGame.isEndState()));
return ChessGameDto.of(gameRoom.getId(), chessGame);
}

Expand All @@ -77,7 +76,7 @@ public ChessGameDto endChessGame(final Long gameId) {
final ChessGame chessGame = initChessGameOf(gameRoom);

chessGame.end();
gameRoomRepository.save(new GameRoom(gameRoom, true));
gameRoomRepository.save(new GameRoom(gameRoom, chessGame.isEndState()));
return ChessGameDto.of(gameRoom.getId(), chessGame);
}

Expand All @@ -87,11 +86,11 @@ public boolean isEndGame(final Long gameId) {
return gameRoom.getState();
}

public List<String> showAllGames() {
public List<GameRoomDto> showAllGames() {
List<GameRoom> gameRooms = gameRoomRepository.findAll();

return gameRooms.stream()
.map(GameRoom::getName)
.map(GameRoomDto::of)
.collect(toList());
}

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/wooteco/chess/service/dto/ChessGameDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

public class ChessGameDto {

private final Long id;
private final ChessBoardDto chessBoardDto;
private final PieceColorDto pieceColorDto;
private final ChessStatusDtos chessStatusDtos;
private final boolean isEndState;
private final boolean isKingCaught;

private ChessGameDto(final ChessBoardDto chessBoardDto, final PieceColorDto pieceColorDto,
private ChessGameDto(final Long id, final ChessBoardDto chessBoardDto, final PieceColorDto pieceColorDto,
final ChessStatusDtos chessStatusDtos, final boolean isEndState, final boolean isKingCaught) {
this.id = id;
this.chessBoardDto = chessBoardDto;
this.pieceColorDto = pieceColorDto;
this.chessStatusDtos = chessStatusDtos;
this.isEndState = isEndState;
this.isKingCaught = isKingCaught;
}

public static ChessGameDto of(final ChessGame chessGame) {
// NOTE: 2020/04/28 DTO가 관리해야하는 필드의 종류
public static ChessGameDto of(final Long id, final ChessGame chessGame) {
Objects.requireNonNull(chessGame, "체스 게임이 null입니다.");

final ChessBoardDto chessBoardDto = ChessBoardDto.of(chessGame.getChessBoard());
Expand All @@ -30,7 +33,11 @@ public static ChessGameDto of(final ChessGame chessGame) {
final boolean isEndStatus = chessGame.isEndState();
final boolean isKingCaught = chessGame.isKingCaught();

return new ChessGameDto(chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught);
return new ChessGameDto(id, chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught);
}

public Long getId() {
return id;
}

public ChessBoardDto getChessBoardDto() {
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/wooteco/chess/service/dto/GameRoomDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wooteco.chess.service.dto;

import java.util.Objects;

import wooteco.chess.entity.GameRoom;

public class GameRoomDto {

private Long id;
private String name;
private Boolean state;

private GameRoomDto(final Long id, final String name, final Boolean state) {
this.id = id;
this.name = name;
this.state = state;
}

public static GameRoomDto of(final GameRoom gameRoom) {
Objects.requireNonNull(gameRoom, "게임이 null입니다.");
return new GameRoomDto(gameRoom.getId(), gameRoom.getName(), gameRoom.getState());
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Boolean getState() {
return state;
}

}
2 changes: 1 addition & 1 deletion src/main/java/wooteco/chess/web/PieceNameConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum PieceNameConverter {
BLACK_PAWN("P", "black-pawn"),
WHITE_PAWN("p", "white-pawn");

private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"./images/%s.png\">";
private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"/images/%s.png\">";

private final String chessPieceName;
private final String imageFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@ article > form {
text-align: center;
}

article.game__start {
text-align: center;
line-height: 1.6;
font-size: 2.5rem;
}

#game__room__list {
display: inline-block;
margin: 0.5rem auto;
padding: 0.8rem;
width: 12rem;
height: 3rem;
text-align: center;
font-size: 2rem;
border: 3px solid #333;
background-color: #777;
color: #fff;
border-radius: 10px;
text-decoration: none;
cursor: pointer;
}

#game__room__new {
display: inline-block;
margin: 0.5rem auto;
padding: 0.8rem;
width: 12rem;
height: 3rem;
text-align: center;
font-size: 2rem;
border: 3px solid #333;
background-color: #777;
color: #fff;
border-radius: 10px;
text-decoration: none;
cursor: pointer;
}

#game__room__new::placeholder {
color: #999;
}

#start__button {
display: inline-block;
padding: 15px 0;
Expand All @@ -33,6 +75,7 @@ article > form {
border-radius: 10px;
text-decoration: none;
cursor: pointer;
vertical-align: middle;
}

#start__button:hover {
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/templates/chess.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="./style.css" rel="stylesheet"/>
<link href="/style.css" rel="stylesheet"/>
<title>스티치의 체스 게임</title>
</head>
<body>
Expand Down Expand Up @@ -102,12 +102,12 @@
<article class="chess__status">
<div id="chess__status__area">
{{#status}} {{pieceColor}}의 점수 : {{score}}
<br/>
<br/>
{{/status}}
</div>
</article>
<article class="move__command">
<form action="/chess_play" method="POST">
<form action="/game/play" method="POST">
이동시킬 피스 :&nbsp;
<input class="move__command__area" name="sourcePosition" placeholder="b1" type="text"/>
<br/>
Expand All @@ -118,10 +118,10 @@
</form>
</article>
<article class="chess_command">
<form action="/chess_new" method="POST">
<input class="restart__command__submit" type="submit" value="다시시작"/>
<form action="/" method="get">
<input class="restart__command__submit" type="submit" value="처음으로"/>
</form>
<form action="/chess_end" method="POST">
<form action="/game/end" method="POST">
<input class="end__command__submit" type="submit" value="게임종료"/>
</form>
</article>
Expand Down
19 changes: 16 additions & 3 deletions src/main/resources/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
<head>
<meta charset="UTF-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="./style.css" rel="stylesheet"/>
<link href="/style.css" rel="stylesheet"/>
<title>스티치의 체스 게임</title>
</head>
<body>
<header>
<h1>Chess Game</h1>
</header>
<article>
<form action="/chess" method="get">
<article class="game__start">
<form action="/game" method="POST">
게임 찾기 :&nbsp;
<select id="game__room__list" name="name">
{{#games}}
<option value="{{name}}">{{name}}{{#state}}&nbsp;(종료){{/state}}</option>
{{/games}}
</select>
<input id="start__button" type="submit" value="게임 시작"/>
</form>
<form action="/game/new" method="POST">
새로운 게임 :&nbsp;
<input id="game__room__new" name="name" placeholder="게임방 이름" type="text"/>
<input id="start__button" type="submit" value="게임 시작"/>
</form>


</article>
</body>
</html>
10 changes: 5 additions & 5 deletions src/main/resources/templates/result.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="./style.css" rel="stylesheet"/>
<link href="/style.css" rel="stylesheet"/>
<title>스티치의 체스 게임</title>
</head>
<body>
Expand Down Expand Up @@ -100,14 +100,14 @@
<article class="chess__status">
<div id="chess__status__area">
{{#status}}
{{pieceColor}}의 점수 : {{score}}
<br/>
{{pieceColor}}의 점수 : {{score}}
<br/>
{{/status}}
</div>
</article>
<article class="chess_command">
<form action="/chess_new" method="POST">
<input class="restart__command__submit" type="submit" value="다시시작"/>
<form action="/" method="get">
<input class="restart__command__submit" type="submit" value="처음으로"/>
</form>
</article>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.*;

import java.util.NoSuchElementException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -27,7 +29,8 @@ void findByName() {
String gameRoomName = "test";
GameRoom savedGameRoom = gameRoomRepository.save(new GameRoom(gameRoomName));

GameRoom expected = gameRoomRepository.findByName(gameRoomName);
GameRoom expected = gameRoomRepository.findByName(gameRoomName)
.orElseThrow(() -> new NoSuchElementException("해당 이름의 방이 존재하지 않습니다."));
assertThat(savedGameRoom.getName()).isEqualTo(expected.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class ChessBoardDtoTest {

private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"./images/%s.png\">";
private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"/images/%s.png\">";

@ParameterizedTest
@NullSource
Expand Down
Loading