Repository navigation
[FEAT] 게임 시작 전 방장·인원 상태 검증 및 실시간 UI 반영 #104
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
Merged
The head ref may contain hidden characters: "FEAT-\uBC29\uC7A5-\uAC8C\uC784\uC2DC\uC791-\uD45C\uC2DC"
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e34381
feat:방장 조회 DAO 구현 (#99)
gaeunnlee 09e0b18
feat: 방 생성 시 방장 ID 세션 저장 및 시작 버튼 표시 (#99)
gaeunnlee 78e7ef8
feat: 방장 변경 웹소켓 이벤트 처리 (#99)
gaeunnlee a392470
feat: 게임 시작 전 방 인원 검증 로직 추가 (#99)
gaeunnlee b306014
refactor: Room Controller 인원 조회 주석 수정
gaeunnlee 17c2998
fix: hostUserId, session nullPointer 오류 수정 (#99)
gaeunnlee 30e8502
refactor: 중복 null 체크 제거 및 hostUserId 검증 추가 (#99)
gaeunnlee 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 hidden or 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 contains hidden or 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 contains hidden or 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,144 @@ | ||
| package room.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
|
|
||
| import com.google.gson.Gson; | ||
|
|
||
| import room.dao.RoomPlayerDAO; | ||
| import room.dao.RoomPlayerDAOImpl; | ||
| import room.dto.RoomActiveCountDTO; | ||
|
|
||
| @WebServlet("/room/*") | ||
| public class RoomController extends HttpServlet { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final Gson gson = new Gson(); | ||
| private final RoomPlayerDAO roomPlayerDAO = new RoomPlayerDAOImpl(); | ||
|
|
||
| @FunctionalInterface | ||
| private interface Handler { | ||
| void handle(HttpServletRequest req, HttpServletResponse res, String userId) throws Exception; | ||
| } | ||
|
|
||
| private final Map<String, Handler> getHandlers = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public void init() { | ||
| getHandlers.put("count", this::handleGetCount); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse res) | ||
| throws ServletException, IOException { | ||
|
|
||
| String action = getAction(req); | ||
| if (action == null) { | ||
| sendJson(res, 400, ApiError.of("잘못된 요청입니다.")); | ||
| return; | ||
| } | ||
|
|
||
| String userId = getLoginUserId(req); | ||
| if (userId == null || userId.isBlank()) { | ||
| sendJson(res, 401, ApiError.of("로그인이 필요합니다.")); | ||
| return; | ||
| } | ||
|
|
||
| Handler handler = getHandlers.get(action); | ||
| if (handler == null) { | ||
| sendJson(res, 404, ApiError.of("존재하지 않는 API입니다.")); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| handler.handle(req, res, userId); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| sendJson(res, 500, ApiError.of("서버 오류가 발생했습니다.")); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * GET /room/count?roomId= | ||
| * 응답: { ok:true, data:{ roomId, activeCount } } | ||
| */ | ||
| private void handleGetCount(HttpServletRequest req, HttpServletResponse res, String userId) throws Exception { | ||
| String roomId = req.getParameter("roomId"); | ||
| if (roomId == null || roomId.isBlank()) { | ||
| sendJson(res, 400, ApiError.of("roomId가 필요합니다.")); | ||
| return; | ||
| } | ||
| int activeCount = roomPlayerDAO.countActivePlayers(roomId); | ||
| RoomActiveCountDTO dto = new RoomActiveCountDTO(roomId, activeCount); | ||
| sendJson(res, 200, ApiSuccess.of(dto)); | ||
| } | ||
|
|
||
| private String getAction(HttpServletRequest req) { | ||
| String pathInfo = req.getPathInfo(); | ||
| if (pathInfo == null || "/".equals(pathInfo)) | ||
| return null; | ||
| return pathInfo.substring(1); | ||
| } | ||
|
|
||
| private String getLoginUserId(HttpServletRequest req) { | ||
| HttpSession session = req.getSession(false); | ||
| return (session == null) ? null : (String)session.getAttribute("loginUserId"); | ||
| } | ||
|
|
||
| private void sendJson(HttpServletResponse res, int status, Object body) throws IOException { | ||
| res.setStatus(status); | ||
| res.setContentType("application/json; charset=UTF-8"); | ||
| res.getWriter().write(gson.toJson(body)); | ||
| } | ||
|
|
||
| private static class ApiSuccess<T> { | ||
| private final boolean ok = true; | ||
| private final T data; | ||
|
|
||
| private ApiSuccess(T data) { | ||
| this.data = data; | ||
| } | ||
|
|
||
| public static <T> ApiSuccess<T> of(T data) { | ||
| return new ApiSuccess<>(data); | ||
| } | ||
|
|
||
| public boolean isOk() { | ||
| return ok; | ||
| } | ||
|
|
||
| public T getData() { | ||
| return data; | ||
| } | ||
| } | ||
|
|
||
| private static class ApiError { | ||
| private final boolean ok = false; | ||
| private final String message; | ||
|
|
||
| private ApiError(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public static ApiError of(String message) { | ||
| return new ApiError(message); | ||
| } | ||
|
|
||
| public boolean isOk() { | ||
| return ok; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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,11 @@ | ||
| package room.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class RoomActiveCountDTO { | ||
| private String roomId; | ||
| private int activeCount; | ||
| } |
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.