Skip to content

Commit

Permalink
Merge pull request #481 from Team-WeQuiz/feat/#455
Browse files Browse the repository at this point in the history
[BE] 0์ดˆ์—์„œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ ์ค„์ด๊ธฐ
  • Loading branch information
cherry031 authored May 19, 2024
2 parents a9337d4 + 77bbc48 commit 57df49b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.chatty.chatty.game.controller.dto.ChatRequest;
import com.chatty.chatty.game.controller.dto.ChatResponse;
import com.chatty.chatty.game.controller.dto.QuizResponse;
import com.chatty.chatty.game.controller.dto.ScoreResponse;
import com.chatty.chatty.game.controller.dto.SubmitAnswerRequest;
import com.chatty.chatty.game.service.GameService;
import com.chatty.chatty.player.controller.dto.NicknameRequest;
Expand Down Expand Up @@ -48,7 +47,7 @@ public ChatResponse chat(
@MessageMapping("/rooms/{roomId}/join")
@SendTo("/sub/rooms/{roomId}/status")
public PlayersStatusDTO joinRoom(@DestinationVariable Long roomId, SimpMessageHeaderAccessor headerAccessor,
NicknameRequest request) {
NicknameRequest request) {
headerAccessor.getSessionAttributes().put("roomId", roomId);
PlayersStatusDTO response = gameService.joinRoom(roomId, getUserIdFromHeader(headerAccessor), request);
quizRoomService.broadcastUpdatedRoomList();
Expand All @@ -68,27 +67,20 @@ public PlayersStatusDTO toggleReady(@DestinationVariable Long roomId, SimpMessag
@MessageMapping("/rooms/{roomId}/quiz")
public void sendQuiz(@DestinationVariable Long roomId, SimpMessageHeaderAccessor headerAccessor) {
QuizResponse quizResponse = gameService.sendQuiz(roomId);
if (quizResponse == null) {
return;
}
template.publishQuiz(getUserIdFromHeader(headerAccessor), roomId, quizResponse);
}

@MessageMapping("/rooms/{roomId}/submit")
public void submitAnswer(@DestinationVariable Long roomId, SubmitAnswerRequest request,
SimpMessageHeaderAccessor headerAccessor) {
SimpMessageHeaderAccessor headerAccessor) {
log.info("Submit request: {}", request);
log.info("Submit userId: {}", getUserIdFromHeader(headerAccessor));
gameService.addPlayerAnswer(roomId, request, getUserIdFromHeader(headerAccessor));
}

/*
publication URL : /pub/rooms/{roomId}/score
subscription URL : /user/{userId}/queue/rooms/{roomId}/score
*/
@MessageMapping("/rooms/{roomId}/score")
public void sendScore(@DestinationVariable Long roomId, SimpMessageHeaderAccessor headerAccessor) {
ScoreResponse scoreResponse = gameService.sendScore(roomId);
template.publishScore(roomId, scoreResponse);
}

@MessageMapping("/rooms/{roomId}/phase")
@SendTo("/sub/rooms/{roomId}/phase")
public void getPhase(@DestinationVariable Long roomId, SimpMessageHeaderAccessor headerAccessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,25 @@ public Boolean checkSubmitStatus() {
}
return false;
}

public AnswerData clone() {
AnswerData clonedAnswerData = AnswerData.builder()
.playerNum(playerNum)
.majorityNum(majorityNum)
.quizId(quizId)
.quizNum(quizNum)
.quizType(quizType)
.correct(correct)
.startedTime(startedTime)
.build();

for (Map.Entry<Long, PlayerAnswerData> entry : playerAnswers.entrySet()) {
PlayerAnswerData clonedData = PlayerAnswerData.builder()
.playerAnswer(entry.getValue().playerAnswer())
.submittedTime(entry.getValue().submittedTime())
.build();
clonedAnswerData.playerAnswers.put(entry.getKey(), clonedData);
}
return clonedAnswerData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Getter
@Slf4j
public class ScoreData {

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ private AnswerData initAnswerData(Long roomId) {
QuizRoom quizRoom = quizRoomRepository.findById(roomId)
.orElseThrow(() -> new QuizRoomException(ROOM_NOT_FOUND));
QuizData quizData = gameRepository.getQuizData(roomId);
int majorityNum = (quizRoom.getPlayerNum() == 2) ? 1 : (quizRoom.getPlayerNum() / 2 + 1);

return AnswerData.builder()
.playerNum(quizRoom.getPlayerNum())
.majorityNum((quizRoom.getPlayerNum() + 1) / 2)
.majorityNum(majorityNum)
.quizId(quizData.getQuiz().id())
.quizNum(quizData.getQuiz().questionNumber())
.quizType(quizData.getQuiz().type())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private synchronized void initQuiz(QuizData quizData) {
}

public QuizResponse sendQuiz(Long roomId) {
if (phaseRepository.getPhase(roomId) == RESULT) {
return null;
}
QuizData quizData = gameRepository.getQuizData(roomId);
initQuiz(quizData);
log.info("Send: Quiz: {}", gameRepository.getQuizData(roomId).getQuiz());
Expand Down Expand Up @@ -187,11 +190,50 @@ public void addPlayerAnswer(Long roomId, SubmitAnswerRequest request, Long userI
if (submitCount == answerData.getMajorityNum()) {
phaseRepository.update(roomId, COUNTDOWN);
log.info("Phase UPDATED: COUNTDOWN");
quizCountDown(roomId, userId);
quizCountDown(roomId);
}
}

private void quizCountDown(Long roomId) {
Integer seconds = QUIZ_COUNT_SECONDS;
while (seconds >= 0) {
log.info("Countdown: {}", seconds);
template.publishQuizCount(roomId, buildCountDownResponse(seconds));
ThreadSleep.sleep(1000L);
seconds--;
}
// seconds == -1
// ์ œ์ถœ ์•ˆ ํ•œ์• ๋“ค ๋นˆ๊ฐ’์œผ๋กœ ์ œ์ถœ ์ฒ˜๋ฆฌ
AnswerData answerData = answerRepository.getAnswerData(roomId);
UsersSubmitStatus usersSubmitStatus = userSubmitStatusRepository.findByRoomId(roomId);
usersSubmitStatus.usersSubmitStatus().stream()
.filter(userSubmitStatus -> !userSubmitStatus.isSolved())
.forEach(userSubmitStatus -> {
answerData.addAnswer(userSubmitStatus.userId(),
SubmitAnswerRequest.builder().playerAnswer("").build());
userSubmitStatus.submit();
});
log.info("Answer All Submitted: PlayerAnswers: {}", answerData.getPlayerAnswers());
// ํ€ด์ฆˆ ๋๋‚ฌ์œผ๋ฉด ๋‹ค์Œ ํ€ด์ฆˆ ๋ฐ˜ํ™˜ ์ค€๋น„
AnswerData clonedAnswerData = answerData.clone();
resetState(roomId);
// ๋ผ์šด๋“œ ๋ณ„ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ์ธ์ง€ ํ™•์ธ
if (gameRepository.getQuizData(roomId).isQueueEmpty()) {
markAndUpdateScore(roomId, clonedAnswerData);
phaseRepository.update(roomId, RESULT);
log.info("Phase UPDATED: RESULT");
template.publishQuizCount(roomId, buildCountDownResponse(seconds));
sendScore(roomId);
} else {
phaseRepository.update(roomId, Phase.QUIZ_SOLVING);
log.info("Phase UPDATED: QUIZ_SOLVING");
template.publishQuizCount(roomId, buildCountDownResponse(seconds));
markAndUpdateScore(roomId, clonedAnswerData);
}
log.info("Quiz Countdown: {}", seconds);
}

private void markAndUpdateScore(Long roomId, Long userId, AnswerData answerData) {
private void markAndUpdateScore(Long roomId, AnswerData answerData) {
// ML์— ํ”Œ๋ ˆ์ด์–ด๋“ค ๋‹ต์•ˆ ๋„˜๊ฒจ์„œ ์ฑ„์  ์š”์ฒญ ํ›„ ์ฑ„์  ๋ฌธ์„œ id ์ €์žฅ
QuizRoom quizRoom = quizRoomRepository.findById(roomId).get();
MarkResponse markResponse = modelService.requestMark(MarkRequest.builder()
Expand All @@ -208,22 +250,11 @@ private void markAndUpdateScore(Long roomId, Long userId, AnswerData answerData)
ScoreData scoreData = scoreRepository.getScoreData(roomId);
scoreData.addScore(answerData, markResponse.marked_answers());
log.info("Updated Score");

// ์ง์ „ ๋ฌธ์ œ ์‚ญ์ œ
removeQuiz(roomId);
QuizData quizData = gameRepository.getQuizData(roomId);
if (quizData.isQueueEmpty()) {
phaseRepository.update(roomId, RESULT);
log.info("Phase UPDATED: RESULT");
getPhase(roomId, userId);
scoreCountDown(roomId);
} else {
phaseRepository.update(roomId, Phase.QUIZ_SOLVING);
log.info("Phase UPDATED: QUIZ_SOLVING");
}
}

private void resetState(Long roomId) {
// ์ง์ „ ๋ฌธ์ œ ์‚ญ์ œ
removeQuiz(roomId);
// ํ”Œ๋ ˆ์ด์–ด ์ œ์ถœ ์ƒํƒœ, ๋‹ต์•ˆ ๋งต ์ดˆ๊ธฐํ™”
PlayersStatus players = playersStatusRepository.findByRoomId(roomId).get();
userSubmitStatusRepository.init(players, roomId);
Expand All @@ -232,41 +263,24 @@ private void resetState(Long roomId) {
log.info("Reset");
}

private void quizCountDown(Long roomId, Long userId) {
Integer seconds = QUIZ_COUNT_SECONDS;
while (seconds >= 0) {
log.info("Countdown: {}", seconds);
template.publishQuizCount(roomId, buildCountDownResponse(seconds));
ThreadSleep.sleep(1000L);
seconds--;
}
// seconds == -1
// ์ œ์ถœ ์•ˆ ํ•œ์• ๋“ค ๋นˆ๊ฐ’์œผ๋กœ ์ œ์ถœ ์ฒ˜๋ฆฌ
AnswerData answerData = answerRepository.getAnswerData(roomId);
UsersSubmitStatus usersSubmitStatus = userSubmitStatusRepository.findByRoomId(roomId);
usersSubmitStatus.usersSubmitStatus().stream()
.filter(userSubmitStatus -> !userSubmitStatus.isSolved())
.forEach(userSubmitStatus -> {
answerData.addAnswer(userSubmitStatus.userId(),
SubmitAnswerRequest.builder().playerAnswer("").build());
userSubmitStatus.submit();
});
// ํ€ด์ฆˆ ๋๋‚ฌ์œผ๋ฉด ๋‹ค์Œ ํ€ด์ฆˆ ๋ฐ˜ํ™˜ ์ค€๋น„
log.info("Answer All Submitted: PlayerAnswers: {}", answerData.getPlayerAnswers());
markAndUpdateScore(roomId, userId, answerData);
resetState(roomId);
template.publishQuizCount(roomId, buildCountDownResponse(seconds));
log.info("Countdown: {}", seconds);
private void sendScore(Long roomId) {
ScoreResponse scoreResponse = convertScoreDTO(roomId);
template.publishScore(roomId, scoreResponse);
scoreCountDown(roomId);
}

private void scoreCountDown(Long roomId) {
Integer seconds = SCORE_COUNT_SECONDS;
do {
int seconds = SCORE_COUNT_SECONDS;
while (seconds >= 0) {
template.publishScoreCount(roomId, buildCountDownResponse(seconds));
ThreadSleep.sleep(1000L);
seconds--;
}
while (seconds >= -1);
if (seconds == -1) {
phaseRepository.update(roomId, Phase.QUIZ_SOLVING);
log.info("Phase UPDATED: QUIZ_SOLVING");
template.publishScoreCount(roomId, buildCountDownResponse(seconds));
}
}

private CountDownResponse buildCountDownResponse(Integer second) {
Expand All @@ -292,7 +306,7 @@ private List<AnswerDTO> getAnswers(Map<Long, PlayerAnswerData> playerAnswers) {
.toList();
}

public ScoreResponse sendScore(Long roomId) {
public ScoreResponse convertScoreDTO(Long roomId) {
ScoreData scoreData = scoreRepository.getScoreData(roomId);
List<PlayerScoreDTO> scores = scoreData.getPlayersScore().entrySet().stream()
.map(entry -> PlayerScoreDTO.builder()
Expand Down Expand Up @@ -320,7 +334,7 @@ public void getPhase(Long roomId, Long userId) {
sendPlayersSubmitStatus(roomId, isMajority, userSubmitStatusRepository.findByRoomId(roomId));
}
case RESULT -> {
ScoreResponse scoreResponse = sendScore(roomId);
ScoreResponse scoreResponse = convertScoreDTO(roomId);
template.publishScore(roomId, scoreResponse);
}
}
Expand Down

0 comments on commit 57df49b

Please sign in to comment.