107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
package SuMoC.backend.user.stats;
|
|
|
|
import SuMoC.backend.gamification.Timeframe;
|
|
import SuMoC.backend.gamification.badges.BadgeService;
|
|
import SuMoC.backend.gamification.challenges.ChallengeService;
|
|
import SuMoC.backend.user.dtos.StatScope;
|
|
import SuMoC.backend.user.models.User;
|
|
import SuMoC.backend.user.repository.UserRepository;
|
|
import SuMoC.backend.user.service.CurrentUserService;
|
|
import jakarta.transaction.Transactional;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
import java.util.Set;
|
|
|
|
@Service
|
|
public class StatsService {
|
|
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
private final BadgeService badgeService;
|
|
private final ChallengeService challengeService;
|
|
private final CurrentUserService currentUserService;
|
|
|
|
public StatsService(CurrentUserService currentUserService,
|
|
UserRepository userRepository,
|
|
BadgeService badgeService,
|
|
ChallengeService challengeService) {
|
|
this.currentUserService = currentUserService;
|
|
this.userRepository = userRepository;
|
|
this.badgeService = badgeService;
|
|
this.challengeService = challengeService;
|
|
}
|
|
|
|
@Scheduled(cron = "0 0 0 ? * MON")
|
|
public void resetWeeklyStats() {
|
|
userRepository.findAll().forEach( user -> {
|
|
user.getStats().resetStats(Timeframe.WEEKLY);
|
|
});
|
|
|
|
System.out.println("###### Weekly stats have been reset. ######");
|
|
}
|
|
|
|
@Scheduled(cron = "0 0 0 1 * ?")
|
|
public void resetMonthlyStats() {
|
|
userRepository.findAll().forEach( user -> {
|
|
user.getStats().resetStats(Timeframe.MONTHLY);
|
|
});
|
|
|
|
System.out.println("###### Monthly stats have been reset. ######");
|
|
}
|
|
|
|
private static final Set<String> ALLOWED_STATS = Set.of(
|
|
"XP",
|
|
"CO2_SAVED",
|
|
"KM_CYCLED",
|
|
"KM_WALKED",
|
|
"KM_PUBLIC_TRANSPORT",
|
|
"KM_DRIVEN",
|
|
"MOBILITY_SCORE"
|
|
);
|
|
|
|
@Transactional
|
|
public void addStatsToCurrentUser(String statField, long value, StatScope scope, boolean awardBadges, boolean awardChallenges) {
|
|
if (!ALLOWED_STATS.contains(statField)) {
|
|
throw new ResponseStatusException(
|
|
HttpStatus.BAD_REQUEST,
|
|
"Unknown stat field: " + statField + " (allowed: " + ALLOWED_STATS + ")"
|
|
);
|
|
}
|
|
|
|
User user = currentUserService.get();
|
|
|
|
|
|
if (user.getStats() == null) {
|
|
user.setStats(new StatsList(user));
|
|
}
|
|
|
|
StatScope effectiveScope = (scope == null) ? StatScope.ALL : scope;
|
|
|
|
switch (effectiveScope) {
|
|
case ALL -> user.getStats().addToAllStats(statField, value);
|
|
case TOTAL -> user.getStats().addToStat(Timeframe.TOTAL, statField, value);
|
|
case MONTHLY -> user.getStats().addToStat(Timeframe.MONTHLY, statField, value);
|
|
case WEEKLY -> user.getStats().addToStat(Timeframe.WEEKLY, statField, value);
|
|
}
|
|
|
|
userRepository.save(user);
|
|
|
|
if (awardBadges) {
|
|
badgeService.checkAndAwardBadges();
|
|
}
|
|
|
|
if (awardChallenges) {
|
|
challengeService.checkAndAwardChallenges(statField, value);
|
|
}
|
|
}
|
|
|
|
public StatsList getStats() {
|
|
User user = currentUserService.get();
|
|
return user.getStats();
|
|
}
|
|
}
|