-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] GameSetting 변경 기능 추가 #107
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 14 commits
be3f034
e1d79fc
214bc54
ccac167
9af131a
2fb0b8f
a7ead79
6981459
fdaaee7
655929e
39d9f54
80101d0
db62cbc
181f4ab
c7506d9
b6ae413
476ceea
b7852c2
2db2e95
7c6515e
482bc72
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.websocket.MessageSender; | ||
| import io.f1.backend.domain.quiz.app.QuizService; | ||
|
|
||
| public interface GameSettingChanger { | ||
|
|
||
| boolean change(Room room, QuizService quizService); | ||
|
|
||
| void afterChange(Room room, MessageSender messageSender); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import static io.f1.backend.domain.game.mapper.RoomMapper.toPlayerListResponse; | ||
| import static io.f1.backend.domain.game.websocket.WebSocketUtils.getDestination; | ||
|
|
||
| import io.f1.backend.domain.game.dto.MessageType; | ||
| import io.f1.backend.domain.game.dto.response.PlayerListResponse; | ||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.websocket.MessageSender; | ||
| import io.f1.backend.domain.quiz.app.QuizService; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Slf4j | ||
| public record QuizChangeRequest(Long quizId) implements GameSettingChanger { | ||
dlsrks1021 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public boolean change(Room room, QuizService quizService) { | ||
| if (Objects.equals(room.getQuizId(), quizId)) { | ||
| return false; // 동일하면 무시 | ||
| } | ||
| Quiz quiz = quizService.getQuizWithQuestionsById(quizId); | ||
| int questionSize = quiz.getQuestions().size(); | ||
| room.changeQuiz(quiz); | ||
| // 퀴즈의 문제 갯수로 변경 | ||
| room.changeRound(questionSize, questionSize); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterChange(Room room, MessageSender messageSender) { | ||
| room.resetAllPlayerReadyStates(); | ||
|
|
||
| String destination = getDestination(room.getId()); | ||
| PlayerListResponse response = toPlayerListResponse(room); | ||
|
|
||
| log.info(response.toString()); | ||
| messageSender.send(destination, MessageType.PLAYER_LIST, response); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.websocket.MessageSender; | ||
| import io.f1.backend.domain.quiz.app.QuizService; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public record RoundChangeRequest(Integer round) implements GameSettingChanger { | ||
|
|
||
| @Override | ||
| public boolean change(Room room, QuizService quizService) { | ||
| if (Objects.equals(room.getRound(), round)) { | ||
| return false; // 동일하면 무시 | ||
| } | ||
|
|
||
| Quiz quiz = quizService.findQuizById(room.getQuizId()); | ||
| int questionSize = quiz.getQuestions().size(); | ||
|
|
||
| room.changeRound(round, questionSize); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterChange(Room room, MessageSender messageSender) { | ||
| // 고유한 후처리 동작 없음 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import io.f1.backend.global.exception.CustomException; | ||
| import io.f1.backend.global.exception.errorcode.GameErrorCode; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| @Getter | ||
| public enum TimeLimit { | ||
| FIFTEEN(15), | ||
| THIRTY(30), | ||
| FORTY_FIVE(45), | ||
| SIXTY(60); | ||
|
|
||
| private final int value; | ||
|
|
||
| TimeLimit(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public static TimeLimit from(int value) { | ||
| return Arrays.stream(values()) | ||
| .filter(t -> t.value == value) | ||
| .findFirst() | ||
| .orElseThrow(() -> new CustomException(GameErrorCode.GAME_SETTING_CONFLICT)); | ||
|
Collaborator
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. 에러 내용을 조금 세분화해서 유효하지 않은 타이머 설정입니다.
Collaborator
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. 게임 내에서 일반 사용자가 유효하지 않은 타이머를 보낼일은 없다고 생각해서, 비정상적인 요청에 대해서 만들어놓은 방어적 예외라 예외 내용을 구체화하지는 않았습니다! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package io.f1.backend.domain.game.dto.request; | ||
|
|
||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.websocket.MessageSender; | ||
| import io.f1.backend.domain.quiz.app.QuizService; | ||
|
|
||
| public record TimeLimitChangeRequest(int timeLimit) implements GameSettingChanger { | ||
|
|
||
| @Override | ||
| public boolean change(Room room, QuizService quizService) { | ||
| if (room.getTimeLimit() == timeLimit) { | ||
| return false; // 동일하면 무시 | ||
| } | ||
| room.changeTimeLimit(TimeLimit.from(timeLimit)); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterChange(Room room, MessageSender messageSender) { | ||
| // 고유한 후처리 동작 없음 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,34 @@ | ||
| package io.f1.backend.domain.game.model; | ||
|
|
||
| import io.f1.backend.domain.game.dto.request.TimeLimit; | ||
| import io.f1.backend.domain.quiz.entity.Quiz; | ||
| import io.f1.backend.global.exception.CustomException; | ||
| import io.f1.backend.global.exception.errorcode.GameErrorCode; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class GameSetting { | ||
|
|
||
| private Long quizId; | ||
| private Integer round; // 게임 변경 시 해당 게임의 총 문제 수로 설정 | ||
| private Integer round; | ||
| private int timeLimit; | ||
|
|
||
| public boolean validateQuizId(Long quizId) { | ||
| return Objects.equals(this.quizId, quizId); | ||
| public void changeQuiz(Quiz quiz) { | ||
| quizId = quiz.getId(); | ||
| round = quiz.getQuestions().size(); // 라운드를 바꾼 퀴즈의 문제 수로 동기화 | ||
| } | ||
|
|
||
| public void changeTimeLimit(TimeLimit timeLimit) { | ||
| this.timeLimit = timeLimit.getValue(); | ||
| } | ||
|
|
||
| public void changeRound(int round, int questionsCount) { | ||
| if (round > questionsCount) { | ||
| throw new CustomException(GameErrorCode.ROUND_EXCEEDS_QUESTION_COUNT); | ||
| } | ||
| this.round = round; | ||
| } | ||
| } |
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.
이 부분도 Room의 메서드로 분리할 수 있을 것 같습니다 !
Uh oh!
There was an error while loading. Please reload this page.
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.
Room클래스 안에, getPlayerBySessionId 만들어서 교체했습니다!