-
Notifications
You must be signed in to change notification settings - Fork 3
[feat] 게임 시작 후 게임 진행 + 타이머 기능 추가 #94
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
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aee9991
:wrench: 최신 dev 브랜치와 충돌 해결..
silver-eunjoo e9a5a38
:sparkles: feat : 게임 시작, 진행, 타이머 기능 추가
silver-eunjoo 0b2689e
chore: Java 스타일 수정
de8b3b2
:sparkles: feat : Room에 타이머 업데이트 해주기
silver-eunjoo 9c4a793
chore: Java 스타일 수정
e090bd8
:recycle: refactor : PR 리뷰 반영 + chat으로 정답 맞혔을 시, Question_start 누락 해결
silver-eunjoo 56e0010
chore: Java 스타일 수정
a59060d
:wrench: chore: 사용하지 않는 상수 삭제
silver-eunjoo 37c7a65
:recycle: refactor : getDestination() util 메서드로 분리
silver-eunjoo d8f0d62
chore: Java 스타일 수정
f33974b
:wrench: chore : PR 리뷰 반영
silver-eunjoo 4031963
chore: Java 스타일 수정
65629e2
:recycle: refactor : SystemNotice enum으로 분리
silver-eunjoo 6444663
chore: Java 스타일 수정
d4e8f43
Merge branch 'dev' into feat/79
silver-eunjoo 257f09c
chore: Java 스타일 수정
d046325
:wrench: chore: cancelTimer 위치 변경
silver-eunjoo 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
91 changes: 91 additions & 0 deletions
91
backend/src/main/java/io/f1/backend/domain/game/app/TimerService.java
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,91 @@ | ||
| package io.f1.backend.domain.game.app; | ||
|
|
||
| import static io.f1.backend.domain.game.mapper.RoomMapper.ofPlayerEvent; | ||
| import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionResultResponse; | ||
| import static io.f1.backend.domain.game.mapper.RoomMapper.toQuestionStartResponse; | ||
| 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.RoomEventType; | ||
| import io.f1.backend.domain.game.model.Room; | ||
| import io.f1.backend.domain.game.websocket.MessageSender; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class TimerService { | ||
|
|
||
| private final MessageSender messageSender; | ||
|
|
||
| private static final String NONE_CORRECT_USER = ""; | ||
| private static final int CONTINUE_DELAY = 3; | ||
|
|
||
| public void startTimer(Room room, int delaySec) { | ||
| cancelTimer(room); | ||
|
|
||
| ScheduledFuture<?> timer = | ||
| room.getScheduler() | ||
| .schedule( | ||
| () -> { | ||
| handleTimeout(room); | ||
| }, | ||
| delaySec + room.getGameSetting().getTimeLimit(), | ||
| TimeUnit.SECONDS); | ||
|
|
||
| room.updateTimer(timer); | ||
| } | ||
|
|
||
| private void handleTimeout(Room room) { | ||
| String destination = getDestination(room.getId()); | ||
|
|
||
| messageSender.send( | ||
| destination, | ||
| MessageType.QUESTION_RESULT, | ||
| toQuestionResultResponse(NONE_CORRECT_USER, room.getCurrentQuestion().getAnswer())); | ||
| messageSender.send( | ||
| destination, | ||
| MessageType.SYSTEM_NOTICE, | ||
| ofPlayerEvent(NONE_CORRECT_USER, RoomEventType.TIMEOUT)); | ||
|
|
||
| // TODO : 게임 종료 로직 | ||
| if (!validateCurrentRound(room)) { | ||
| // 게임 종료 로직 | ||
| // GAME_SETTING, PLAYER_LIST, GAME_RESULT, ROOM_SETTING | ||
| return; | ||
| } | ||
|
|
||
| // 다음 문제 출제 | ||
| room.increaseCurrentRound(); | ||
|
|
||
| startTimer(room, CONTINUE_DELAY); | ||
| messageSender.send( | ||
| destination, | ||
| MessageType.QUESTION_START, | ||
| toQuestionStartResponse(room, CONTINUE_DELAY)); | ||
| } | ||
|
|
||
| public boolean validateCurrentRound(Room room) { | ||
| if (room.getGameSetting().getRound() != room.getCurrentRound()) { | ||
| return true; | ||
| } | ||
| cancelTimer(room); | ||
| room.getScheduler().shutdown(); | ||
| return false; | ||
| } | ||
|
|
||
| public boolean cancelTimer(Room room) { | ||
| // 정답 맞혔어요 ~ 타이머 캔슬 부탁 | ||
| ScheduledFuture<?> timer = room.getTimer(); | ||
| if (timer != null && !timer.isDone()) { | ||
| timer.cancel(false); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
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. 여기도 있군요. |
||
| } | ||
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/io/f1/backend/domain/game/dto/GameEventType.java
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,6 @@ | ||
| package io.f1.backend.domain.game.dto; | ||
|
|
||
| public enum GameEventType { | ||
| START, | ||
| CONTINUE | ||
| } |
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 |
|---|---|---|
|
|
@@ -9,4 +9,5 @@ public enum MessageType { | |
| CHAT, | ||
| QUESTION_RESULT, | ||
| RANK_UPDATE, | ||
| QUESTION_START | ||
| } | ||
25 changes: 21 additions & 4 deletions
25
backend/src/main/java/io/f1/backend/domain/game/dto/RoomEventType.java
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 |
|---|---|---|
| @@ -1,8 +1,25 @@ | ||
| package io.f1.backend.domain.game.dto; | ||
|
|
||
| public enum RoomEventType { | ||
| ENTER, | ||
| EXIT, | ||
| START, | ||
| END, | ||
| ENTER(SystemNoticeMessage.ENTER), | ||
| EXIT(SystemNoticeMessage.EXIT), | ||
| START(null), | ||
| END(null), | ||
| CORRECT_ANSWER(SystemNoticeMessage.CORRECT_ANSWER), | ||
| TIMEOUT(SystemNoticeMessage.TIMEOUT); | ||
|
|
||
| private final SystemNoticeMessage systemMessage; | ||
|
|
||
| RoomEventType(SystemNoticeMessage systemMessage) { | ||
| this.systemMessage = systemMessage; | ||
| } | ||
|
|
||
| public String getMessage(String nickname) { | ||
|
|
||
| if (this == TIMEOUT) { | ||
| return systemMessage.getMessage(); | ||
| } | ||
|
|
||
| return nickname + systemMessage.getMessage(); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/io/f1/backend/domain/game/dto/SystemNoticeMessage.java
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,18 @@ | ||
| package io.f1.backend.domain.game.dto; | ||
|
|
||
| public enum SystemNoticeMessage { | ||
| ENTER(" 님이 입장하셨습니다"), | ||
| EXIT(" 님이 퇴장하셨습니다"), | ||
| CORRECT_ANSWER(" 님 정답입니다 !"), | ||
| TIMEOUT("땡 ~ ⏰ 제한 시간 초과!"); | ||
|
|
||
| private final String message; | ||
|
|
||
| SystemNoticeMessage(String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/io/f1/backend/domain/game/dto/response/QuestionResultResponse.java
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package io.f1.backend.domain.game.dto.response; | ||
|
|
||
| public record QuestionResultResponse(Long questionId, String correctUser, String answer) {} | ||
| public record QuestionResultResponse(String correctUser, String answer) {} |
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/io/f1/backend/domain/game/dto/response/QuestionStartResponse.java
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,5 @@ | ||
| package io.f1.backend.domain.game.dto.response; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| public record QuestionStartResponse(Long questionId, int round, Instant timestamp) {} |
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 |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ public class GameSetting { | |
|
|
||
| private Long quizId; | ||
| private Integer round; // 게임 변경 시 해당 게임의 총 문제 수로 설정 | ||
| private int timeLimit = 60; | ||
|
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. 이부분 전 캐치 못하고있었는뎁 감사합니다 ~! |
||
| private int timeLimit; | ||
|
|
||
| public boolean validateQuizId(Long quizId) { | ||
| return Objects.equals(this.quizId, quizId); | ||
|
|
||
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.
오홍.. 재연결 유예시간 로직 구현할 때 참고해서 구현해봐야겠군요 👀