-
Notifications
You must be signed in to change notification settings - Fork 1
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
[FEAT] user stat 결과 경험치를 %로 치환해서 응답 #90
Changes from 4 commits
0102caf
19cce2a
cfc2cf0
0cad753
11179d5
3488ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import com.wakeUpTogetUp.togetUp.api.users.UserAvatarService; | ||
import com.wakeUpTogetUp.togetUp.api.users.UserService; | ||
import com.wakeUpTogetUp.togetUp.api.users.model.User; | ||
import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; | ||
import com.wakeUpTogetUp.togetUp.utils.JwtService; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
@@ -56,7 +57,7 @@ public LoginRes socialLogin(SocialLoginReq socialLoginReq) { | |
.email(socialUserRes.getEmail()) | ||
.accessToken(accessToken) | ||
.avatarId(userAvatarService.getUserAvatarId(user.getId())) | ||
.userStat(userService.getUserStat(user)) | ||
.userStat(UserStat.from(user)) | ||
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. 함수이름을 from이라고 지은 이유가 궁금해요! 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. 정적 팩토리 메소드로 인수인 user에서 정보를 가져와 생성한다는 의미를 담았습니다. 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. 그렇군요 👍 |
||
.build(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,15 @@ | |
import com.google.cloud.vision.v1.Likelihood; | ||
import com.wakeUpTogetUp.togetUp.api.alarm.AlarmRepository; | ||
import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm; | ||
import com.wakeUpTogetUp.togetUp.api.mission.dto.request.MissionLogCreateReq; | ||
import com.wakeUpTogetUp.togetUp.api.mission.dto.request.MissionCompleteReq; | ||
import com.wakeUpTogetUp.togetUp.api.mission.dto.response.MissionCompleteRes; | ||
import com.wakeUpTogetUp.togetUp.api.mission.model.Emotion; | ||
import com.wakeUpTogetUp.togetUp.api.mission.model.MissionLog; | ||
import com.wakeUpTogetUp.togetUp.api.mission.service.AzureAiService; | ||
import com.wakeUpTogetUp.togetUp.api.mission.service.GoogleVisionService; | ||
import com.wakeUpTogetUp.togetUp.api.mission.service.ObjectDetectionService; | ||
import com.wakeUpTogetUp.togetUp.api.notification.NotificationService; | ||
import com.wakeUpTogetUp.togetUp.api.room.RoomRepository; | ||
import com.wakeUpTogetUp.togetUp.api.room.model.Room; | ||
import com.wakeUpTogetUp.togetUp.api.users.UserRepository; | ||
import com.wakeUpTogetUp.togetUp.api.users.UserService; | ||
import com.wakeUpTogetUp.togetUp.api.users.model.User; | ||
|
@@ -22,15 +22,16 @@ | |
import com.wakeUpTogetUp.togetUp.exception.BaseException; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class MissionService { | ||
|
||
private final ObjectDetectionService objectDetectionService; | ||
|
@@ -49,28 +50,26 @@ public List<DetectedObject> getObjectDetectionResult(String object, MultipartFil | |
List<DetectedObject> detectedObjects = azureAiService.detectObjectByVer40(missionImage); | ||
|
||
// TODO: 디버그용 삭제 | ||
System.out.println(); | ||
System.out.println("탐지할 객체 = " + object); | ||
System.out.println("[감지된 객체]"); | ||
log.info("탐지할 객체 = " + object); | ||
log.info("[감지된 객체]"); | ||
Comment on lines
52
to
+54
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. 훨씬 좋네요! |
||
detectedObjects.stream() | ||
.map(DetectedObject::getName) | ||
.forEach(System.out::println); | ||
.forEach(log::info); | ||
|
||
if (detectedObjects.size() == 0) { | ||
if (detectedObjects.isEmpty()) { | ||
throw new BaseException(Status.MISSION_OBJECT_NOT_FOUND); | ||
} | ||
|
||
// todo: 객체 정리하고 비교할 자료구조 찾기 | ||
|
||
List<DetectedObject> highestConfidenceObjects = detectedObjects.stream() | ||
.filter(objetDetected -> objetDetected.getName().toLowerCase().equals(object)) | ||
.sorted(Comparator.comparing(DetectedObject::getConfidence).reversed()) | ||
.limit(3) | ||
.collect(Collectors.toList()); | ||
|
||
// TODO: 디버그용 삭제 | ||
System.out.println("[감지된 객체 중 목표 객체 정확도순 최대 3개]"); | ||
highestConfidenceObjects.forEach(System.out::println); | ||
log.info("[감지된 객체 중 목표 객체 정확도순 최대 3개]"); | ||
highestConfidenceObjects.forEach(obj -> log.info(obj.toString())); | ||
|
||
if (highestConfidenceObjects.isEmpty()) { | ||
throw new BaseException(Status.MISSION_FAILURE); | ||
|
@@ -119,7 +118,7 @@ private Likelihood getLikelihood(Emotion emotion, FaceAnnotation face) { | |
// 모델로 객체 인식하기 | ||
public void recognizeObjectByModel(String object, MultipartFile missionImage) { | ||
for (String objectDetected : objectDetectionService.detectObject(missionImage)) { | ||
System.out.println("objectDetected = " + objectDetected); | ||
log.info("objectDetected = " + objectDetected); | ||
|
||
if (objectDetected.equals(object)) { | ||
return; | ||
|
@@ -130,34 +129,43 @@ public void recognizeObjectByModel(String object, MultipartFile missionImage) { | |
} | ||
|
||
@Transactional | ||
public UserStat afterMissionComplete(int userId, MissionLogCreateReq req) { | ||
createMissionLog(userId, req); | ||
return userService.userProgress(userId); | ||
} | ||
|
||
// TODO : 미션 수행 기록 추가 + REQUEST 수정하기 | ||
public void createMissionLog(int userId, MissionLogCreateReq req) { | ||
public MissionCompleteRes afterMissionComplete(int userId, MissionCompleteReq req) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new BaseException(Status.USER_NOT_FOUND)); | ||
|
||
Alarm alarm = alarmRepository.findById(req.getAlarmId()) | ||
.orElseThrow(() -> new BaseException(Status.ALARM_NOT_FOUND)); | ||
|
||
Room room = Objects.isNull(req.getRoomId()) | ||
? null | ||
: roomRepository.findById(req.getRoomId()) | ||
.orElseThrow(() -> new BaseException(Status.ROOM_NOT_FOUND)); | ||
createMissionLog(user, req); | ||
userService.userProgress(user); | ||
sendNotificationIfRoomExists(alarm, user); | ||
|
||
return new MissionCompleteRes(UserStat.from(user)); | ||
} | ||
|
||
private void sendNotificationIfRoomExists(Alarm alarm, User user) { | ||
if (alarm.isRoomAlarm()) { | ||
// 미션 성공 후 처리 로직과 알림 보내기 로직을 독립적으로 분리 | ||
// 알림을 보내는데 실패해도 모든 과정이 롤백되지 않음 | ||
try { | ||
notificationService.sendNotificationToUsersInRoom(alarm.getId(), user.getId()); | ||
} catch (BaseException e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private void createMissionLog(User user, MissionCompleteReq req) { | ||
Alarm alarm = alarmRepository.findById(req.getAlarmId()) | ||
.orElseThrow(() -> new BaseException(Status.ALARM_NOT_FOUND)); | ||
|
||
MissionLog missionLog = MissionLog.builder() | ||
.alarmName(alarm.getName()) | ||
.missionPicLink(req.getMissionPicLink()) | ||
.user(user) | ||
.room(room) | ||
.room(alarm.getRoom()) | ||
.build(); | ||
|
||
missionLogRepository.save(missionLog); | ||
|
||
|
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,11 @@ | |
import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmToken; | ||
import com.wakeUpTogetUp.togetUp.api.users.fcmToken.FcmTokenRepository; | ||
import com.wakeUpTogetUp.togetUp.api.users.model.User; | ||
import com.wakeUpTogetUp.togetUp.api.users.vo.UserStat; | ||
import com.wakeUpTogetUp.togetUp.common.Status; | ||
import com.wakeUpTogetUp.togetUp.exception.BaseException; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -47,8 +43,8 @@ public User getSignedUser(SocialUserRes socialUserRes, LoginType loginType) { | |
.name(socialUserRes.getName()) | ||
.email(socialUserRes.getEmail()) | ||
.level(DEFAULT_USER_LEVEL) | ||
.experience(DEFAULT_USER_EXPERIENCE) | ||
.point(DEFAULT_USER_POINT) | ||
.expPoint(DEFAULT_USER_EXPERIENCE) | ||
.coin(DEFAULT_USER_POINT) | ||
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. DEFAULT_USER_POINT -> DEFAULT_USER_COIN 부탁해요! 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. 네 고치고 머지하겠습니다! |
||
.build()); | ||
userAvatarService.setUserDefaultAvatar(user); | ||
|
||
|
@@ -97,30 +93,12 @@ public void deleteById(Integer userId) { | |
if (roomUserNumber > 0) { | ||
roomUserRepository.deleteByUserId(userId); | ||
} | ||
|
||
} | ||
|
||
public UserStat userProgress(int userId) { | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new BaseException(Status.USER_NOT_FOUND)); | ||
|
||
user.gainExperience(10); | ||
int threshold = 10 + 16 * (user.getLevel() - 1); | ||
|
||
if (user.checkUserLevelUpAvailable(threshold)) { | ||
user.levelUp(threshold); | ||
} | ||
public void userProgress(User user) { | ||
user.gainExpPoint(10); | ||
user.progress(); | ||
userRepository.save(user); | ||
|
||
return new UserStat(user.getLevel(), user.getExperience(), user.getPoint()); | ||
} | ||
|
||
public UserStat getUserStat(User user) { | ||
return UserStat.builder() | ||
.level(user.getLevel()) | ||
.experience(user.getExperience()) | ||
.point(user.getPoint()) | ||
.build(); | ||
} | ||
|
||
public List<Integer> getAgreedNotiUsersIds() { | ||
|
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.
👍 👍