-
Notifications
You must be signed in to change notification settings - Fork 412
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, 5단계 - 체스] 라이언(권영훈) 미션 제출합니다. #235
Changes from 125 commits
27eea54
7b41611
9ff0366
612ce53
da59418
fd15b1a
65371aa
e90c792
2e65682
ef48684
f036d6b
2e26da1
2112313
f6b677e
3776fd9
96aa010
a186116
f9ecd41
4dbe3b8
0f5b708
3f4f062
6cb36ce
23e7aef
16c1962
c8680f6
6365cfa
75a0b53
cdaf2ca
101d677
1996682
bdf163c
b3513ec
69b30cc
9282eef
206f0b2
9875051
5238d71
8324ee8
d12ded4
768f509
f776685
7097bb3
c6637dc
d609d9a
83e83df
f1c1002
7dcd632
e7e0fa5
1f8d4c2
3df65d3
d81f786
0dac739
06c9376
f80bf5b
36ff8f4
1713ef7
c34bdd3
af9fd29
57759a8
4bf176c
1c79923
2e380a3
67f5ce5
99f8268
fec2d17
7c07e63
de7e577
15ada50
848b55f
9b29f8f
0537d3f
403c3d2
67e80a6
e35961c
a3826f2
f078317
4d10d37
4ba3daf
963e5b6
eece287
f44a710
b0f1655
dacf443
ac801c1
754b9c0
b1a602f
c43db81
a49374d
f2667a6
2426a5f
d84e0b8
0ef1dba
c055ea6
efd9e9e
78cbe9d
a0f156d
ea7de41
1e3b452
c31e0fd
d472193
7829565
ef4f91a
1378d73
7481be7
b2650d8
4b9b318
719b235
e4b061b
2bbb124
46b1733
7c0dbe2
a9ecf49
61d654e
735ab12
1166ecf
bcd5045
88ef082
d01058e
7fa3488
fb50826
74c4fd9
430598d
f26dc31
9837de7
6523c82
5849615
ad8d890
5c32d38
56d3516
6efff11
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,18 +1,99 @@ | ||
package chess; | ||
|
||
import chess.dao.Chess; | ||
import chess.dao.ChessDAO; | ||
import chess.domain.grid.ChessGame; | ||
import chess.domain.grid.Grid; | ||
import chess.domain.grid.gridStrategy.NormalGridStrategy; | ||
import chess.domain.piece.Color; | ||
import chess.domain.position.Position; | ||
import chess.dto.PositionDTO; | ||
import com.google.gson.Gson; | ||
import spark.ModelAndView; | ||
import spark.template.handlebars.HandlebarsTemplateEngine; | ||
|
||
import java.sql.SQLDataException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static spark.Spark.get; | ||
import static spark.Spark.*; | ||
|
||
public class WebUIChessApplication { | ||
public static final Gson GSON = new Gson(); | ||
|
||
public static void main(String[] args) { | ||
staticFiles.location("/public"); | ||
ChessGame chessGame = new ChessGame(new Grid(new NormalGridStrategy())); | ||
ChessDAO chessDAO = new ChessDAO(); | ||
|
||
get("/", (req, res) -> { | ||
Map<String, Object> model = new HashMap<>(); | ||
return render(model, "index.html"); | ||
return render(new HashMap<>(), "index.html"); | ||
}); | ||
|
||
get("/pieces", (req, res) -> { | ||
return chessGame.pieceMap(); | ||
}, GSON::toJson); | ||
|
||
get("/turn", (req, res) -> { | ||
return chessGame.turn(); | ||
}, GSON::toJson); | ||
|
||
get("/score/white", (req, res) -> { | ||
return chessGame.score(Color.WHITE); | ||
}, GSON::toJson); | ||
|
||
get("/score/black", (req, res) -> { | ||
return chessGame.score(Color.BLACK); | ||
}, GSON::toJson); | ||
|
||
get("/gameover", (req, res) -> { | ||
return Arrays.asList(chessGame.isGameOver(), chessGame.getWinner()); | ||
}, GSON::toJson); | ||
|
||
post("/move", (req, res) -> { | ||
PositionDTO positionDTO = GSON.fromJson(req.body(), PositionDTO.class); | ||
String sourcePosition = positionDTO.getSourcePosition(); | ||
String targetPosition = positionDTO.getTargetPosition(); | ||
try { | ||
chessGame.move(chessGame.grid().piece(new Position(sourcePosition)), | ||
chessGame.grid().piece(new Position(targetPosition))); | ||
return 200; | ||
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. 해당 경우에 HTTP status code가 200일 것 같은데, 꼭 숫자 200을 리턴해야만 할까요? 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. 상수로 랩핑 해서 수정 했는데, 이것이 게이츠님이 의도하신바가 맞나요?? |
||
} catch (IllegalArgumentException error) { | ||
return 400; | ||
} | ||
}); | ||
|
||
post("/start", (req, res) -> { | ||
try { | ||
chessGame.start(); | ||
return 200; | ||
} catch (IllegalArgumentException error) { | ||
return 201; | ||
} | ||
}); | ||
|
||
post("/reset", (req, res) -> { | ||
chessGame.reset(); | ||
return 200; | ||
}); | ||
|
||
post("/save", (req, res) -> { | ||
Chess chess = new Chess("1", chessGame.gridStringify(), chessGame.turn().name()); | ||
if (chessDAO.findByChessId("1") == null) { | ||
chessDAO.addChess(chess); | ||
} | ||
chessDAO.updateChess(chess, chess.getChess(), chess.getTurn()); | ||
return 200; | ||
}); | ||
|
||
post("/load", (req, res) -> { | ||
if (chessDAO.findByChessId("1") == null) { | ||
throw new SQLDataException("저장된 보드가 없습니다."); | ||
} | ||
Chess chess = chessDAO.findByChessId("1"); | ||
chessGame.load(chess); | ||
return 200; | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package chess.dao; | ||
|
||
import java.util.Objects; | ||
|
||
public class Chess { | ||
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. 현재 Chess가 dao패키지에 있는데 DAO라고 볼 수 있을까요?
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. entity 패키지로 분리했어요! 근데 entity를 보면서 다소 이해가 안되는 부분이, 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. Entity(도메인 객체)는 비즈니스 로직을 가진 객체라고 이해하면 좋을 것 같고, DTO는 단지 데이터를 전달하는 객체라고 이해하면 좋을 것 같아요.
|
||
private String chessId; | ||
private String chess; | ||
private String turn; | ||
|
||
public Chess(String chessId, String chess, String turn) { | ||
this.chessId = chessId; | ||
this.chess = chess; | ||
this.turn = turn; | ||
} | ||
|
||
public String getChessId() { | ||
return chessId; | ||
} | ||
|
||
public String getChess() { | ||
return chess; | ||
} | ||
|
||
public String getTurn() { | ||
return turn; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Chess chess1 = (Chess) o; | ||
return Objects.equals(chess, chess1.chess) && Objects.equals(turn, chess1.turn); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(chess, turn); | ||
} | ||
} |
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.
이건 개인적인 궁금함인데요. Jackson이 아닌 Gson 라이브러리를 사용한 이유가 있을까요?
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.
Spring 에서는 Jackson을 기본으로 내장 하는 것으로 알고있는데 spark는 따로 언급 없이 spark documentation에 Gson으로 사용하길래, 따라했어요!
조금 찾아봤는데,
대용량 파일에서는 Jackson이 GSON보다 2배가량 빠르고,
작은 파일에서는 GSON이 Jackson보다 아주 약간? 빠르네요.
하지만, GSON은 maintenance가 더이상 안되는 것으로 보이며, moshi라는 것으로 대체되는 추세인것 같습니다.
google/gson#1821
https://www.overops.com/blog/the-ultimate-json-library-json-simple-vs-gson-vs-jackson-vs-json/
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.
추가 학습까지 하시고 이렇게 공유까지 해주시다니 정말 멋지네요 👍
이런 피드백을 드린 이유는 어떤 기준을 가지고 라이브러리를 선택했는지가 궁금했어요!
그리고 저는 설령 그 기준이 틀렸다하더라도 그런 고민하는 시간이 의미있다고 생각하거든요. ^^