-
Notifications
You must be signed in to change notification settings - Fork 166
[π μ¬μ΄ν΄2 - λ―Έμ (κΈ°λ¬Ό νμ₯ + DB μ μ©)] λ°λ λ―Έμ μ μΆν©λλ€. #352
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
Changes from all commits
6278db2
4cf8291
6e60e02
2c0427f
01cdf1b
8b5d5ec
65a3b8c
7255106
a042190
b339224
381c6ba
904384c
2cf87fd
8b2d866
1f90bb1
dc6802d
24398fa
39923da
0be6dac
51b9556
bc5c954
b1fea2b
21a517b
84a331d
9fba4ff
acf1053
3b2fd5d
b2fc894
2edf16f
4760bab
96e082e
50547ba
ada1c4c
a8e2db8
4d62e04
6ea62fb
dff9109
b51395e
13ea920
7e5c554
e8966f5
ee2e010
ae1aa9a
9323c36
f3b90d3
0a22c7b
8e3360f
d55e7df
ced9270
f8bb9d0
db4d1df
d3f9819
688ac17
42b00e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,71 @@ | ||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import controller.GameController; | ||
| import data.BoardDto; | ||
| import data.BoardRepository; | ||
| import data.TransactionManager; | ||
| import domain.board.Board; | ||
| import domain.board.BoardInitializer; | ||
| import domain.piece.Camp; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Board board = new Board(BoardInitializer.init(InputView.readBoardSetting())); | ||
| GameController gameController = new GameController(board); | ||
| try (HikariDataSource hikariDataSource = createDataSource()) { | ||
| TransactionManager transactionManager = new TransactionManager(hikariDataSource); | ||
|
|
||
| BoardRepository boardRepository = new BoardRepository(); | ||
|
|
||
| Board board = initBoard(transactionManager, boardRepository); | ||
|
|
||
| GameController gameController = new GameController(board, boardRepository, transactionManager); | ||
| gameController.run(); | ||
|
|
||
| while (!board.isGameOver()) { | ||
| gameController.printBoard(); | ||
| gameController.move(); | ||
| OutputView.printBoard(board); | ||
| OutputView.printWinner(board.winner(), board.score(Camp.CHO), board.score(Camp.HAN)); | ||
|
|
||
| transactionManager.executeTransaction(connection -> { | ||
| boardRepository.delete(connection, board); | ||
| return null; | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static HikariDataSource createDataSource() { | ||
| HikariConfig hikariConfig = new HikariConfig(); | ||
| hikariConfig.setJdbcUrl("jdbc:h2:~/janggi;INIT=RUNSCRIPT FROM 'src/main/resources/create_tables.sql'"); | ||
| hikariConfig.setDriverClassName("org.h2.Driver"); | ||
| hikariConfig.setUsername("sa"); | ||
| hikariConfig.setPassword(""); | ||
|
|
||
| return new HikariDataSource(hikariConfig); | ||
| } | ||
|
|
||
| private static Board initBoard(TransactionManager transactionManager, BoardRepository boardRepository) { | ||
| printSavedBoards(transactionManager, boardRepository); | ||
| int boardId = InputView.readBoardNumber(); | ||
|
|
||
| Board board; | ||
| if (boardId == 0) { | ||
| board = Board.from(BoardInitializer.init(InputView.readBoardSetting())); | ||
| transactionManager.executeTransaction(connection -> { | ||
| boardRepository.save(connection, board); | ||
| return null; | ||
| }); | ||
| return board; | ||
| } | ||
| return transactionManager.executeTransaction(connection -> boardRepository.findById(connection, (long) boardId)); | ||
| } | ||
|
|
||
| gameController.printBoard(); | ||
| gameController.printWinner(); | ||
| private static void printSavedBoards(TransactionManager transactionManager, BoardRepository boardRepository) { | ||
| transactionManager.executeTransaction(connection -> { | ||
| List<BoardDto> boards = boardRepository.findAll(connection); | ||
| OutputView.printBoards(boards); | ||
| return null; | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package data; | ||
|
|
||
| import domain.piece.Camp; | ||
|
|
||
| import java.sql.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class BoardDao { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ€μ¬μ°κΈ°κ° 2λ¨κ³λ₯Ό λμ΄κ°λ λ‘μ§μ΄ λ§μ κ² κ°λ€μ. λ©μλ μΆμΆλ‘ μ 리ν΄λ³΄λ©΄ μ’μ κ² κ°μμ~
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄ λΆλΆλ λ©μλ μΆμΆλ‘ μ 리ν΄λ³΄κ² μ΅λλ€! |
||
| public Long insertBoard(Connection connection, boolean gameInProgress, String turn) { | ||
| String sql = "INSERT INTO boards (`game_in_progress`, `turn`) VALUES (?, ?)"; | ||
| try ( | ||
| PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) | ||
| ) { | ||
| preparedStatement.setBoolean(1, gameInProgress); | ||
| preparedStatement.setString(2, turn); | ||
|
|
||
| preparedStatement.executeUpdate(); | ||
|
|
||
| return getGenerateId(preparedStatement); | ||
|
|
||
| } catch (SQLException e) { | ||
| throw new IllegalStateException("λ°μ΄ν° μ½μ μ μ€ν¨νμ΅λλ€.", e); | ||
| } | ||
| } | ||
|
|
||
| public Optional<BoardDto> getBoard(Connection connection, Long boardId) { | ||
| String sql = "SELECT `id`, `game_in_progress`, `turn` FROM boards WHERE `id` = (?)"; | ||
| try ( | ||
| PreparedStatement preparedStatement = connection.prepareStatement(sql) | ||
| ) { | ||
| preparedStatement.setLong(1, boardId); | ||
|
|
||
| return getBoardDto(preparedStatement); | ||
| } catch (SQLException e) { | ||
| throw new IllegalStateException("λ°μ΄ν° μ‘°νμ μ€ν¨νμ΅λλ€.", e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public List<BoardDto> getAllBoards(Connection connection) { | ||
| String sql = "SELECT `id`, `game_in_progress`, `turn` FROM boards WHERE `game_in_progress` = true ORDER BY `id`"; | ||
| List<BoardDto> boards = new ArrayList<>(); | ||
|
|
||
| try (PreparedStatement preparedStatement = connection.prepareStatement(sql); | ||
| ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| while (resultSet.next()) { | ||
| boards.add(mapRow(resultSet)); | ||
| } | ||
| return boards; | ||
| } catch (SQLException e) { | ||
| throw new IllegalStateException("λ°μ΄ν° μ‘°νμ μ€ν¨νμ΅λλ€.", e); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void deleteBoard(Connection connection, Long boardId) { | ||
| String sql = "DELETE FROM boards WHERE `id`=(?)"; | ||
| try ( | ||
| PreparedStatement preparedStatement = connection.prepareStatement(sql) | ||
| ) { | ||
| preparedStatement.setLong(1, boardId); | ||
|
|
||
| preparedStatement.executeUpdate(); | ||
| } catch (SQLException e) { | ||
| throw new IllegalStateException("λ°μ΄ν° μμ μ μ€ν¨νμ΅λλ€.", e); | ||
| } | ||
| } | ||
|
|
||
| public void updateBoard(Connection connection, Long boardId, boolean gameInProgress, String turn) { | ||
| String sql = "UPDATE boards SET `game_in_progress` = (?), `turn` = (?) WHERE `id` = (?)"; | ||
| try ( | ||
| PreparedStatement preparedStatement = connection.prepareStatement(sql) | ||
| ) { | ||
| preparedStatement.setBoolean(1, gameInProgress); | ||
| preparedStatement.setString(2, turn); | ||
| preparedStatement.setLong(3, boardId); | ||
|
|
||
| preparedStatement.executeUpdate(); | ||
| } catch (SQLException e) { | ||
| throw new IllegalStateException("λ°μ΄ν° μμ μ μ€ν¨νμ΅λλ€.", e); | ||
| } | ||
| } | ||
|
|
||
| private Long getGenerateId(PreparedStatement preparedStatement) throws SQLException { | ||
| try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { | ||
| if (resultSet.next()) { | ||
| return resultSet.getLong(1); | ||
| } | ||
| throw new IllegalArgumentException("IDλ₯Ό μ‘°νν μ μμ΅λλ€."); | ||
| } | ||
| } | ||
|
|
||
| private Optional<BoardDto> getBoardDto(PreparedStatement preparedStatement) throws SQLException { | ||
| try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
| if (!resultSet.next()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(mapRow(resultSet)); | ||
| } | ||
| } | ||
|
|
||
| private BoardDto mapRow(ResultSet resultSet) throws SQLException { | ||
| return new BoardDto( | ||
| resultSet.getLong("id"), | ||
| resultSet.getBoolean("game_in_progress"), | ||
| Camp.valueOf(resultSet.getString("turn")) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package data; | ||
|
|
||
| import domain.piece.Camp; | ||
|
|
||
| public record BoardDto( | ||
| Long id, | ||
| boolean gameInProgress, | ||
| Camp turn | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package data; | ||
|
|
||
| import domain.board.Board; | ||
| import domain.board.Position; | ||
| import domain.piece.Camp; | ||
| import domain.piece.Piece; | ||
| import domain.piece.PieceType; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class BoardRepository { | ||
| private final PieceDao pieceDao = new PieceDao(); | ||
| private final BoardDao boardDao = new BoardDao(); | ||
|
|
||
| public void save(Connection connection, Board board) { | ||
| Map<Position, Piece> pieces = board.pieces(); | ||
|
|
||
| Long boardId; | ||
| if (board.id() == null) { | ||
| boardId = boardDao.insertBoard(connection, board.isGameInProgress(), board.turn().name()); | ||
| board.assignId(boardId); | ||
| } else { | ||
| boardId = board.id(); | ||
| boardDao.updateBoard(connection, boardId, board.isGameInProgress(), board.turn().name()); | ||
| pieceDao.deleteAllByBoard(connection, boardId); | ||
| } | ||
|
|
||
| pieces.forEach((position, piece) -> | ||
| pieceDao.insertPiece(connection, boardId, piece.pieceType().name(), | ||
| piece.camp().name(), position.column(), position.row()) | ||
| ); | ||
| } | ||
|
|
||
| public Board findById(Connection connection, Long boardId) { | ||
| BoardDto boardDto = boardDao.getBoard(connection, boardId).orElseThrow(() -> | ||
| new IllegalArgumentException("μ‘΄μ¬νμ§ μλ μ₯κΈ°νμ λλ€.")); | ||
| List<PieceDto> piecesInfo = pieceDao.getAllPieceByBoard(connection, boardId); | ||
|
|
||
| Map<Position, Piece> pieces = new HashMap<>(); | ||
| for (PieceDto piece : piecesInfo) { | ||
| Camp camp = piece.camp(); | ||
| PieceType pieceType = piece.type(); | ||
| pieces.put(new Position(piece.column(), piece.row()), | ||
| Piece.of(camp, pieceType)); | ||
| } | ||
|
|
||
| Board board = new Board(pieces, boardDto.gameInProgress(), boardDto.turn()); | ||
| board.assignId(boardDto.id()); | ||
| return board; | ||
| } | ||
|
|
||
| public List<BoardDto> findAll(Connection connection) { | ||
| return boardDao.getAllBoards(connection); | ||
| } | ||
|
|
||
| public void delete(Connection connection, Board board) { | ||
| Long boardId = board.id(); | ||
|
|
||
| if (boardId == null) { | ||
| throw new IllegalStateException("μ‘΄μ¬νμ§ μλ κ²μμ λλ€."); | ||
| } | ||
|
|
||
| pieceDao.deleteAllByBoard(connection, boardId); | ||
| boardDao.deleteBoard(connection, boardId); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ©μλκ° λ무 κΉλλ€~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄κΈ°μ step2 λΈλμΉμμ μμ νμ΄μΌ νλ λ΄μ©μ μ€μλ‘ frombunny λΈλμΉ κΈ°μ€μΌλ‘ PRμ μ¬λ¦¬κ² λμμ΅λλ€. μ£μ‘ν©λλ€ π
μ΄ν 리뷰 λ°μ λ° μ»€λ° νμ€ν 리 μ 리λ₯Ό μν΄, frombunny κΈ°μ€μΌλ‘ μλ‘μ΄ λΈλμΉμμ 컀λ°μ μ¬κ΅¬μ±ν λ€ step2 λΈλμΉμ λ°μνμ΅λλ€..!