Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -18,4 +20,6 @@ public class UserGameInfoResponse {
private Long chatRoomsCreated;

private Long levelCompleted;

private List<Boolean> weeklyQuizStatus;
}
94 changes: 87 additions & 7 deletions src/main/java/com/_data/_data/game/entity/UserGameInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,103 @@ public class UserGameInfo {
private Users user;

@Builder.Default
private Long totalQuizzesSolved = 0L;
private Long totalQuizzesSolved = 0L;

@Builder.Default
private Long quizzesSolvedToday = 0L;
private Long quizzesSolvedToday = 0L;

@Builder.Default
private Long chatRoomsCreated = 0L;
private Long chatRoomsCreated = 0L;

@Builder.Default
private Long levelCompleted = 0L;

@Builder.Default
private Long currentCountInLevel = 0L;

// 주간 퀴즈 풀이 여부 추적 (월요일=0, 화요일=1, ..., 일요일=6)
@Builder.Default
@Column(name = "monday_solved", nullable = false)
private Boolean mondaySolved = false;

@Builder.Default
@Column(name = "tuesday_solved", nullable = false)
private Boolean tuesdaySolved = false;

@Builder.Default
@Column(name = "wednesday_solved", nullable = false)
private Boolean wednesdaySolved = false;

@Builder.Default
@Column(name = "thursday_solved", nullable = false)
private Boolean thursdaySolved = false;

@Builder.Default
@Column(name = "friday_solved", nullable = false)
private Boolean fridaySolved = false;

@Builder.Default
@Column(name = "saturday_solved", nullable = false)
private Boolean saturdaySolved = false;

@Builder.Default
@Column(name = "sunday_solved", nullable = false)
private Boolean sundaySolved = false;

// 주간 퀴즈 풀이 여부 업데이트 메서드
public void updateWeeklyQuizStatus(int dayOfWeek, boolean solved) {
switch (dayOfWeek) {
case 1: // 월요일
this.mondaySolved = solved;
break;
case 2: // 화요일
this.tuesdaySolved = solved;
break;
case 3: // 수요일
this.wednesdaySolved = solved;
break;
case 4: // 목요일
this.thursdaySolved = solved;
break;
case 5: // 금요일
this.fridaySolved = solved;
break;
case 6: // 토요일
this.saturdaySolved = solved;
break;
case 7: // 일요일
this.sundaySolved = solved;
break;
}
}

// 주간 퀴즈 풀이 여부 초기화 메서드
public void resetWeeklyQuizStatus() {
this.mondaySolved = false;
this.tuesdaySolved = false;
this.wednesdaySolved = false;
this.thursdaySolved = false;
this.fridaySolved = false;
this.saturdaySolved = false;
this.sundaySolved = false;
}

// 특정 요일의 퀴즈 풀이 여부 확인 메서드
public boolean isQuizSolvedOnDay(int dayOfWeek) {
switch (dayOfWeek) {
case 1: return this.mondaySolved;
case 2: return this.tuesdaySolved;
case 3: return this.wednesdaySolved;
case 4: return this.thursdaySolved;
case 5: return this.fridaySolved;
case 6: return this.saturdaySolved;
case 7: return this.sundaySolved;
default: return false;
}
}

// 추후 추가 정보
// private Integer currentStreak; // 연속 학습 횟수
// private Integer level;
// private LocalDate lastQuizDate;
// private Integer currentStreak; // 연속 학습 횟수
// private Integer level;
// private LocalDate lastQuizDate;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -51,4 +52,31 @@ public interface UserGameInfoRepository extends JpaRepository<UserGameInfo, Long
void updateLevelCompleted(Long userId, Long level);

void deleteByUser(Users user);

// 주간 퀴즈 풀이 여부 업데이트
@Modifying
@Transactional
@Query("UPDATE UserGameInfo u SET " +
"u.mondaySolved = CASE WHEN :dayOfWeek = 1 THEN true ELSE u.mondaySolved END, " +
"u.tuesdaySolved = CASE WHEN :dayOfWeek = 2 THEN true ELSE u.tuesdaySolved END, " +
"u.wednesdaySolved = CASE WHEN :dayOfWeek = 3 THEN true ELSE u.wednesdaySolved END, " +
"u.thursdaySolved = CASE WHEN :dayOfWeek = 4 THEN true ELSE u.thursdaySolved END, " +
"u.fridaySolved = CASE WHEN :dayOfWeek = 5 THEN true ELSE u.fridaySolved END, " +
"u.saturdaySolved = CASE WHEN :dayOfWeek = 6 THEN true ELSE u.saturdaySolved END, " +
"u.sundaySolved = CASE WHEN :dayOfWeek = 7 THEN true ELSE u.sundaySolved END " +
"WHERE u.user.id = :userId")
void updateWeeklyQuizStatus(@Param("userId") Long userId, @Param("dayOfWeek") int dayOfWeek);

// 주간 퀴즈 풀이 여부 초기화 (모든 사용자)
@Modifying
@Transactional
@Query("UPDATE UserGameInfo u SET " +
"u.mondaySolved = false, " +
"u.tuesdaySolved = false, " +
"u.wednesdaySolved = false, " +
"u.thursdaySolved = false, " +
"u.fridaySolved = false, " +
"u.saturdaySolved = false, " +
"u.sundaySolved = false")
void resetAllWeeklyQuizStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -115,6 +116,12 @@ public void quizComplete(Long quizId) {

userGameInfoRepository.incrementCurrentCountInLevel(user.getId());

// 현재 요일 확인 (월요일=1, 일요일=7)
int currentDayOfWeek = LocalDate.now().getDayOfWeek().getValue();

// 주간 퀴즈 풀이 여부 업데이트
userGameInfoRepository.updateWeeklyQuizStatus(user.getId(), currentDayOfWeek);

Long currentCountInLevel = userGameInfoRepository.getCurrentCountInLevelByUserId(user.getId());
Long countByLevel = quizRepository.countByLevel(quiz.getLevel());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -30,12 +33,23 @@ public UserGameInfoResponse getUserGameInfo() {

UserGameInfo userGameInfo = userGameInfoRepository.findByUser(user);

List<Boolean> weeklyStatus = Arrays.asList(
userGameInfo.getMondaySolved(),
userGameInfo.getTuesdaySolved(),
userGameInfo.getWednesdaySolved(),
userGameInfo.getThursdaySolved(),
userGameInfo.getFridaySolved(),
userGameInfo.getSaturdaySolved(),
userGameInfo.getSundaySolved()
);

UserGameInfoResponse userGameInfoResponse = new UserGameInfoResponse();
userGameInfoResponse.setUserName(user.getName());
userGameInfoResponse.setChatRoomsCreated(userGameInfo.getChatRoomsCreated());
userGameInfoResponse.setQuizzesSolvedToday(userGameInfo.getQuizzesSolvedToday());
userGameInfoResponse.setTotalQuizzesSolved(userGameInfo.getTotalQuizzesSolved());
userGameInfoResponse.setLevelCompleted(userGameInfo.getLevelCompleted());
userGameInfoResponse.setWeeklyQuizStatus(weeklyStatus);

return userGameInfoResponse;
}
Expand All @@ -45,4 +59,15 @@ public void resetDailyCounters() {
userGameInfoRepository.resetDailyQuizzes();
log.info("Reset daily quizzes counter done.");
}

@Scheduled(cron = "0 0 0 * * MON", zone = "Asia/Seoul")
public void resetWeeklyQuizStatus() {
try {
log.info("주간 퀴즈 풀이 여부 초기화 시작");
userGameInfoRepository.resetAllWeeklyQuizStatus();
log.info("주간 퀴즈 풀이 여부 초기화 완료");
} catch (Exception e) {
log.error("주간 퀴즈 풀이 여부 초기화 중 오류 발생", e);
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ spring:

sql:
init:
mode: never
# data-locations: classpath:dummy-data/*.sql
mode: always
data-locations: classpath:dummy-data/*.sql

jpa:
defer-datasource-initialization: true
Expand Down
36 changes: 17 additions & 19 deletions src/main/resources/dummy-data/user-game-info.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
UPDATE user_game_info
SET total_quizzes_solved = 0
WHERE total_quizzes_solved IS NULL;

UPDATE user_game_info
SET quizzes_solved_today = 0
WHERE quizzes_solved_today IS NULL;

UPDATE user_game_info
SET chat_rooms_created = 0
WHERE chat_rooms_created IS NULL;

UPDATE user_game_info
SET level_completed = 0
WHERE level_completed IS NULL;

UPDATE user_game_info
SET current_count_in_level = 0
WHERE current_count_in_level IS NULL;
UPDATE user_game_info
SET
monday_solved = COALESCE(monday_solved, false),
tuesday_solved = COALESCE(tuesday_solved, false),
wednesday_solved = COALESCE(wednesday_solved, false),
thursday_solved = COALESCE(thursday_solved, false),
friday_solved = COALESCE(friday_solved, false),
saturday_solved = COALESCE(saturday_solved, false),
sunday_solved = COALESCE(sunday_solved, false)
WHERE
monday_solved IS NULL OR
tuesday_solved IS NULL OR
wednesday_solved IS NULL OR
thursday_solved IS NULL OR
friday_solved IS NULL OR
saturday_solved IS NULL OR
sunday_solved IS NULL;