-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from SWM-M3PRO/feature/M3-415-updateAchievement
M3-415 업적 진행 기능 구현
- Loading branch information
Showing
12 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/m3pro/groundflip/enums/AchievementCategoryId.java
This file contains 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,16 @@ | ||
package com.m3pro.groundflip.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum AchievementCategoryId { | ||
EXPLORER(1L), | ||
CONQUEROR(2L), | ||
RANKER(3L), | ||
STEADY(4L), | ||
SPECIAL(5L); | ||
|
||
private final Long categoryId; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/m3pro/groundflip/enums/SpecialAchievement.java
This file contains 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,12 @@ | ||
package com.m3pro.groundflip.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum SpecialAchievement { | ||
JOIN_GROUP(27L); | ||
|
||
private final Long achievementId; | ||
} |
This file contains 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 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 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
94 changes: 94 additions & 0 deletions
94
src/main/java/com/m3pro/groundflip/service/AchievementManager.java
This file contains 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,94 @@ | ||
package com.m3pro.groundflip.service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.m3pro.groundflip.domain.entity.Achievement; | ||
import com.m3pro.groundflip.domain.entity.UserAchievement; | ||
import com.m3pro.groundflip.enums.AchievementCategoryId; | ||
import com.m3pro.groundflip.enums.SpecialAchievement; | ||
import com.m3pro.groundflip.exception.AppException; | ||
import com.m3pro.groundflip.exception.ErrorCode; | ||
import com.m3pro.groundflip.repository.AchievementRepository; | ||
import com.m3pro.groundflip.repository.UserAchievementRepository; | ||
import com.m3pro.groundflip.repository.UserRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AchievementManager { | ||
private final AchievementRepository achievementRepository; | ||
private final UserAchievementRepository userAchievementRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public void updateAccumulateAchievement(Long userId, AchievementCategoryId categoryId) { | ||
UserAchievement achievementToUpdate = getAchievementToUpdate(categoryId.getCategoryId(), userId); | ||
|
||
achievementToUpdate.increaseCurrentValue(); | ||
|
||
if (Objects.equals(achievementToUpdate.getCurrentValue(), | ||
achievementToUpdate.getAchievement().getCompletionGoal())) { | ||
completeAchievement(achievementToUpdate, userId); | ||
} | ||
} | ||
|
||
private void completeAchievement(UserAchievement achievementToUpdate, Long userId) { | ||
achievementToUpdate.setObtainedAt(); | ||
Long nextAchievementId = achievementToUpdate.getAchievement().getNextAchievementId(); | ||
if (nextAchievementId != null) { | ||
UserAchievement nextAchievement = UserAchievement.builder() | ||
.achievement(achievementRepository.getReferenceById(nextAchievementId)) | ||
.user(userRepository.getReferenceById(userId)) | ||
.currentValue(achievementToUpdate.getCurrentValue()) | ||
.isRewardReceived(false) | ||
.build(); | ||
userAchievementRepository.save(nextAchievement); | ||
} | ||
} | ||
|
||
private UserAchievement getAchievementToUpdate(Long categoryId, Long userId) { | ||
List<UserAchievement> userAchievements = userAchievementRepository | ||
.findAllByUserIdAndCategoryId(userId, categoryId); | ||
|
||
if (userAchievements.isEmpty()) { | ||
Achievement achievement = achievementRepository | ||
.findByCategoryId(categoryId).orElseThrow(() -> new AppException(ErrorCode.ACHIEVEMENT_NOT_FOUND)); | ||
|
||
UserAchievement nextAchievement = UserAchievement.builder() | ||
.achievement(achievement) | ||
.user(userRepository.getReferenceById(userId)) | ||
.currentValue(0) | ||
.isRewardReceived(false) | ||
.build(); | ||
return userAchievementRepository.save(nextAchievement); | ||
} else { | ||
return userAchievements.get(userAchievements.size() - 1); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void updateSpecialAchievement(Long userId, SpecialAchievement specialAchievement) { | ||
Optional<UserAchievement> userAchievement = userAchievementRepository.findByAchievementAndUserId( | ||
achievementRepository.getReferenceById(specialAchievement.getAchievementId()), userId); | ||
|
||
if (userAchievement.isEmpty()) { | ||
UserAchievement newUserAchievement = UserAchievement.builder() | ||
.achievement(achievementRepository.getReferenceById(specialAchievement.getAchievementId())) | ||
.user(userRepository.getReferenceById(userId)) | ||
.currentValue(1) | ||
.obtainedAt(LocalDateTime.now()) | ||
.isRewardReceived(false) | ||
.build(); | ||
userAchievementRepository.save(newUserAchievement); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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