-
Notifications
You must be signed in to change notification settings - Fork 178
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
[라빈] - 체스 스프링 실습 레벨 1 제출합니다 #30
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db5d477
feat: 기존 체스게임 프로그램 적용을 위한 커밋
toneyparky 7fd6107
refactor: 콘솔 관련 기능 제거 및 1단계 미션을 위한 프로덕션 코드 수정
giantim 49b62c4
docs: README.md 작성
toneyparky de29620
refactor: 1단계 미션을 위한 테스트 코드 수정
toneyparky d9913d6
fix: 정적 패키지 적용이 안되는 문제 해결
toneyparky 7e9db2c
Merge pull request #1 from toneyparky/toney1
giantim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
package wooteco.chess; | ||
|
||
import spark.ModelAndView; | ||
import spark.template.handlebars.HandlebarsTemplateEngine; | ||
import wooteco.chess.controller.SparkChessController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static spark.Spark.get; | ||
import static spark.Spark.staticFiles; | ||
|
||
public class SparkChessApplication { | ||
public static void main(String[] args) { | ||
get("/", (req, res) -> { | ||
Map<String, Object> model = new HashMap<>(); | ||
return render(model, "index.hbs"); | ||
}); | ||
} | ||
staticFiles.location("/templates/public"); | ||
|
||
SparkChessController sparkChessController = new SparkChessController(); | ||
|
||
private static String render(Map<String, Object> model, String templatePath) { | ||
return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath)); | ||
sparkChessController.route(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
src/main/java/wooteco/chess/controller/ChessConsoleController.java
This file was deleted.
Oops, something went wrong.
122 changes: 0 additions & 122 deletions
122
src/main/java/wooteco/chess/controller/ChessWebController.java
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
src/main/java/wooteco/chess/controller/SparkChessController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package wooteco.chess.controller; | ||
|
||
import spark.ModelAndView; | ||
import spark.Request; | ||
import spark.Response; | ||
import spark.template.handlebars.HandlebarsTemplateEngine; | ||
import wooteco.chess.domain.game.NormalStatus; | ||
import wooteco.chess.domain.piece.Color; | ||
import wooteco.chess.domain.position.MovingPosition; | ||
import wooteco.chess.dto.DestinationPositionDto; | ||
import wooteco.chess.dto.MovablePositionsDto; | ||
import wooteco.chess.dto.MoveStatusDto; | ||
import wooteco.chess.service.ChessWebService; | ||
|
||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static spark.Spark.get; | ||
import static spark.Spark.post; | ||
import static wooteco.chess.web.JsonTransformer.json; | ||
|
||
public class SparkChessController { | ||
private ChessWebService chessWebService; | ||
|
||
public SparkChessController() { | ||
this.chessWebService = new ChessWebService(); | ||
} | ||
|
||
public void route() { | ||
get("/", this::index); | ||
|
||
get("/new", this::startNewGame); | ||
|
||
get("/loading", this::loadGame); | ||
|
||
get("/board", (req, res) -> chessWebService.setBoard(), json()); | ||
|
||
post("/board", this::postBoard); | ||
|
||
post("/source", this::getMovablePositions, json()); | ||
|
||
post("/destination", this::move, json()); | ||
} | ||
|
||
private String index(Request req, Response res) { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("normalStatus", NormalStatus.YES.isNormalStatus()); | ||
|
||
return render(model, "index.html"); | ||
} | ||
|
||
private String startNewGame(Request req, Response res) throws SQLException { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("normalStatus", NormalStatus.YES.isNormalStatus()); | ||
|
||
chessWebService.clearHistory(); | ||
|
||
return render(model, "chess.html"); | ||
} | ||
|
||
private String loadGame(Request req, Response res) { | ||
Map<String, Object> model = new HashMap<>(); | ||
model.put("normalStatus", NormalStatus.YES.isNormalStatus()); | ||
|
||
return render(model, "chess.html"); | ||
} | ||
|
||
private String postBoard(Request req, Response res) { | ||
Map<String, Object> model = new HashMap<>(); | ||
|
||
try { | ||
MoveStatusDto moveStatusDto = chessWebService.move(new MovingPosition(req.queryParams("source"), req.queryParams("destination"))); | ||
|
||
model.put("normalStatus", moveStatusDto.getNormalStatus()); | ||
model.put("winner", moveStatusDto.getWinner()); | ||
|
||
if (moveStatusDto.getWinner().isNone()) { | ||
return render(model, "chess.html"); | ||
} | ||
return render(model, "result.html"); | ||
} catch (IllegalArgumentException | UnsupportedOperationException | NullPointerException | SQLException e) { | ||
model.put("normalStatus", NormalStatus.NO.isNormalStatus()); | ||
model.put("exception", e.getMessage()); | ||
model.put("winner", Color.NONE); | ||
return render(model, "chess.html"); | ||
} | ||
} | ||
|
||
private Map<String, Object> getMovablePositions(Request req, Response res) throws SQLException { | ||
Map<String, Object> model = new HashMap<>(); | ||
try { | ||
MovablePositionsDto movablePositionsDto = chessWebService.findMovablePositions(req.queryParams("source")); | ||
|
||
model.put("movable", movablePositionsDto.getMovablePositionNames()); | ||
model.put("position", movablePositionsDto.getPosition()); | ||
model.put("normalStatus", NormalStatus.YES.isNormalStatus()); | ||
|
||
return model; | ||
} catch (IllegalArgumentException | UnsupportedOperationException | NullPointerException e) { | ||
model.put("normalStatus", NormalStatus.NO.isNormalStatus()); | ||
model.put("exception", e.getMessage()); | ||
|
||
return model; | ||
} | ||
} | ||
|
||
private Map<String, Object> move(Request req, Response res) { | ||
Map<String, Object> model = new HashMap<>(); | ||
|
||
DestinationPositionDto destinationPositionDto = chessWebService.chooseDestinationPosition(req.queryParams("destination")); | ||
|
||
model.put("normalStatus", destinationPositionDto.getNormalStatus().isNormalStatus()); | ||
model.put("position", destinationPositionDto.getPosition()); | ||
|
||
return model; | ||
} | ||
|
||
private static String render(Map<String, Object> model, String templatePath) { | ||
return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
메서드의 컨벤션은 동사라는 피드백을 받은 적이 있습니다. 변경해보는 것은 어떨까요?