Skip to content

[๐Ÿš€ ์‚ฌ์ดํด2 - ๋ฏธ์…˜ (๊ธฐ๋ฌผ ํ™•์žฅ + DB ์ ์šฉ)] ๋งฅ์Šค ๋ฏธ์…˜ ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค.#349

Open
simhokyung wants to merge 20 commits intowoowacourse:simhokyungfrom
simhokyung:step2

Conversation

@simhokyung
Copy link
Copy Markdown

์ฒดํฌ ๋ฆฌ์ŠคํŠธ

  • ๋ฏธ์…˜์˜ ํ•„์ˆ˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ–ˆ๋‚˜์š”?
  • Gradle test๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ํ…Œ์ŠคํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ํ†ต๊ณผํ–ˆ๋‚˜์š”?
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋‚˜์š”?

H2 DB ์‹คํ–‰ ๋ฐฉ๋ฒ•

1. H2 ์ฝ˜์†” ์ ‘์†

๋ธŒ๋ผ์šฐ์ €์—์„œ ์•„๋ž˜ ์ฃผ์†Œ๋กœ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.
http://localhost:8082

2. DB ์—ฐ๊ฒฐ ์ •๋ณด ์ž…๋ ฅ

  • JDBC URL: jdbc:h2:~/janggi_test;AUTO_SERVER=TRUE
  • User: sa
  • Password: (๋นˆ ๊ฐ’)

3. ๋ฐ์ดํ„ฐ ํ™•์ธ

์ฝ˜์†” ์ ‘์† ํ›„ ์•„๋ž˜ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ €์žฅ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT * FROM game;
SELECT * FROM piece;

์–ด๋–ค ๋ถ€๋ถ„์— ์ง‘์ค‘ํ•˜์—ฌ ๋ฆฌ๋ทฐํ•ด์•ผ ํ• ๊นŒ์š”?

์•ˆ๋…•ํ•˜์„ธ์š” ์•„์„œ! ์ด๋ฒˆ์— ์ฝ˜์†”์— DB๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š”๊ฒƒ๊ณผ JDBC์— ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ํ•™์Šตํ•˜๋ฉด์„œ ์ง„ํ–‰ํ•˜๋А๋ผ ์ œ์ถœ์ด ๋งŽ์ด ๋Šฆ์–ด์กŒ๋„ค์š”..ใ… 
์ด๋ฒˆ ๋ฆฌ๋ทฐ๋„ ์ž˜ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

DB ์—ฐ๊ฒฐ ๊ตฌ์กฐ ๋ฐฉ์‹์ด ์ ์ ˆํ•œ์ง€

์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” ์ฝ˜์†” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— DB๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  JDBC๋กœ ์ €์žฅ/๋ณต์› ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ณผ์ •์ด ์ฒ˜์Œ์ด์–ด์„œ, AI์˜ ๋„์›€์„ ๋งŽ์ด ๋ฐ›์œผ๋ฉด์„œ ๊ตฌํ˜„์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ณผ์ •์—์„œ ๊ตฌ์กฐ์ ์œผ๋กœ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋Š”์ง€ ํ”ผ๋“œ๋ฐฑ ๋ฐ›๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. config๋Š” DB ์—ฐ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™” ์ฑ…์ž„์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  2. repository๋Š” ์ €์žฅ/์กฐํšŒ์˜ ์ฑ…์ž„์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  3. SavedGame, SavedPiece๋Š” DB์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ €์žฅ์šฉ ๊ฐ์ฒด๋กœ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
  4. GameSnapshot, GameRestorer๋Š” ๋„๋ฉ”์ธ ๊ฐ์ฒด์™€ ์ €์žฅ์šฉ ๊ฐ์ฒด๋ฅผ ์„œ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ๋กœ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
  5. controller๋Š” ์ƒˆ ๊ฒŒ์ž„ ์‹œ์ž‘, ์ €์žฅ๋œ ๊ฒŒ์ž„ ๋ณต์›, ๊ฒŒ์ž„ ์ง„ํ–‰, ์ž๋™์ €์žฅ ๊ฐ™์€ ์ „์ฒด ํ๋ฆ„์„ ๋‹ด๋‹นํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋„๋ฉ”์ธ์ด DB ๊ตฌ์กฐ๋ฅผ ์ง์ ‘ ์•Œ์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๊ณ  ์ €์žฅ์šฉ ๋ชจ๋ธ๊ณผ ๋ณ€ํ™˜ ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ์ ์ ˆํ–ˆ๋Š”์ง€, ํ˜น์€ ๋ถ€์กฑํ•˜๊ฑฐ๋‚˜ ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ์™ธ์— ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๊ถ์„ฑ๊ทœ์น™๊ณผ ๊ด€๋ จ๋œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ๋„ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ํ”ผ๋“œ๋ฐฑ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

- ๊ถ์„ฑ์—์„œ ํ™”์‚ดํ‘œ ๋ฐฉํ–ฅ์— ๋งž๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋Œ€๊ฐ์„  ์ด๋™ ๊ฐ€๋Šฅ
- ๊ฒŒ์ž„์˜ ์ง„ํ–‰/์ข…๋ฃŒ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” GameStatus ๊ตฌํ˜„
- PieceType์— ๊ธฐ๋ฌผ๋ณ„ ์ ์ˆ˜ ์ถ”๊ฐ€
- Board์—์„œ ์ ์ˆ˜๊ณ„์‚ฐ ๋กœ์ง ๊ตฌํ˜„
- SavedGame์˜ ํ„ด, ์ข…๋ฃŒ์—ฌ๋ถ€, ์Šน์ž๋ฅผ game ํ…Œ์ด๋ธ”์— ์ €์žฅ
Copy link
Copy Markdown

@Hyunta Hyunta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•ˆ๋…•ํ•˜์„ธ์š” ๋งฅ์Šค,
์žฅ๊ธฐ 2๋‹จ๊ณ„ ๋ฏธ์…˜ ๊ตฌํ˜„ํ•˜์‹œ๋А๋ผ ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.
๊ด€๋ จํ•ด์„œ ๋ช‡๊ฐ€์ง€ ์ปค๋ฉ˜ํŠธ ๋‚จ๊ฒจ๋“œ๋ ธ๋Š”๋ฐ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

Comment on lines +37 to +55
@Override
public void save(SavedGame savedGame) {
Connection connection = null;
try {
connection = DatabaseConfig.getConnection();
connection.setAutoCommit(false);

deleteAll(connection);
long gameId = insertGame(connection, savedGame);
insertPieces(connection, gameId, savedGame);

connection.commit();
} catch (SQLException exception) {
rollback(connection);
throw new IllegalStateException("[ERROR] ๊ฒŒ์ž„ ์ €์žฅ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.", exception);
} finally {
close(connection);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-with-resources๋ฅผ ํ•™์Šตํ•˜๊ณ  ํ•œ๋ฒˆ ์ ์šฉํ•ด๋ณด์„ธ์š”.

Comment on lines +75 to +91
@Override
public void clear() {
try (Connection connection = DatabaseConfig.getConnection()) {
deleteAll(connection);
} catch (SQLException exception) {
throw new IllegalStateException("[ERROR] ์ €์žฅ ๋ฐ์ดํ„ฐ ์‚ญ์ œ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.", exception);
}
}

private void deleteAll(Connection connection) throws SQLException {
try (PreparedStatement pieceStatement = connection.prepareStatement(DELETE_PIECE);
PreparedStatement gameStatement = connection.prepareStatement(DELETE_GAME);
) {
pieceStatement.executeUpdate();
gameStatement.executeUpdate();
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‚˜์ค‘์— ์žฅ๊ธฐ๋ฅผ ์—ฌ๋Ÿฌ๋ช…์ด์„œ ์‚ฌ์šฉํ•˜๋ฉด deleteAll์ด ์น˜๋ช…์ ์ผ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”.
Game์— Id ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์„œ, ์›ํ•˜๋Š” ๊ฐ’๋งŒ ์‚ญ์ œํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?

Comment on lines +29 to +47
private boolean isInsidePalace(Move move, Country country) {
Palace palace = Palace.from(country);
return palace.contains(move.from()) && palace.contains(move.to());
}

private boolean isMovable(Move move, Country country){
if(isStraightOneStep(move)){
return true;
}
Palace palace = Palace.from(country);
return palace.isDiagonalMove(move);
}

private boolean isStraightOneStep(Move move){
if(!move.isStraight()){
return false;
}
return move.distance() == 1;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeneralMoveRule๊ณผ GuardMoveRule ๋ฉ”์„œ๋“œ๊ฐ€ ์™„์ „ํžˆ ๋™์ผํ•˜๋„ค์š”.
์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

Comment on lines +59 to +73
try (Connection connection = DatabaseConfig.getConnection();
PreparedStatement gameStatement = connection.prepareStatement(SELECT_GAME);
ResultSet gameResultSet = gameStatement.executeQuery();

) {
if (!gameResultSet.next()) {
return Optional.empty();
}

return findSavedGame(gameResultSet, connection);

} catch (SQLException exception) {
throw new IllegalStateException("[ERROR] ์ €์žฅ๋œ ๊ฒŒ์ž„ ์กฐํšŒ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.", exception);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-with-resources๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์…จ๋„ค์š”, ๋‹ค๋ฅธ ๋ถ€๋ถ„๋„ ๊ฐ™์ด ํ†ต์ผํ•ด์ฃผ์„ธ์š”

CONSTRAINT fk_piece_game
FOREIGN KEY (game_id) REFERENCES game(id)
ON DELETE CASCADE
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CASCADE๋ฅผ ์„ค์ •ํ•ด์ฃผ์…จ๋„ค์š”

Comment on lines +33 to +40
private boolean isPalaceDiagonal(Move move){
Palace choPalace = Palace.from(Country.CHO);
if(choPalace.isDiagonalMove(move)){
return true;
}
Palace hanPalace = Palace.from(Country.HAN);
return hanPalace.isDiagonalMove(move);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannon, Chariot, Soldier ๋ชจ๋‘ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋„ค์š”.
์ค‘๋ณต์„ ์—†์• ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”?

Comment on lines +52 to +61
private List<PalaceRoute> routes(){
return List.of(
new PalaceRoute(Move.of(topLeft(), center())),
new PalaceRoute(Move.of(center(), bottomRight())),
new PalaceRoute(Move.of(topRight(), center())),
new PalaceRoute(Move.of(center(), bottomLeft())),
new PalaceRoute(Move.of(topLeft(), bottomRight())),
new PalaceRoute(Move.of(topRight(), bottomLeft()))
);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋–„๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
์ƒ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด๋ณด๋ฉด ์–ด๋–ค๊ฐ€์š”?

Comment on lines +63 to +67
private Position center(){
if(country==Country.CHO){
return Position.of(9, 5);
}
return Position.of(2, 5);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ๋‹จ์— ์ƒ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด์žˆ๋Š”๋ฐ ํ•ด๋‹น ๊ฐ’์„ ์“ฐ๋Š”๊ฒŒ ์ฝ๋Š” ์‚ฌ๋žŒํ•œํ…Œ ๋ฌธ๋งฅ ์ „๋‹ฌ์ด ์ž˜ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Comment on lines +19 to +23
public static JanggiGame restore(Board board, Country turn, boolean finished, Country winner) {
JanggiGame game = new JanggiGame(board);
game.turn = turn;
game.status = GameStatus.restore(finished, winner);
return game;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๋’ค์— ๋ฎ์–ด์“ฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ ์ƒ์„ฑํ•  ๋•Œ ์ฃผ์ž…ํ•ด์ฃผ๋ฉด ์•ˆ๋˜๋‚˜์š”?

private final String title;
private final Direction forward;
private final String color;
private final double bonusScore;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double ๊ณผ Double์€ ๊ฐ™์€ ๊ฐ’์ผ๊นŒ์š”? bonusscore() ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋Š” Bonus๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๋„ค์š”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants