Skip to content

Commit 3ddd037

Browse files
committed
fix: 누적 SR은 세션 종료 후에 반영
1 parent 9d9035b commit 3ddd037

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/main/java/com/climbup/climbup/attempt/service/AttemptServiceImpl.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,32 @@ public CreateAttemptResponse createAttempt(Long userId, CreateAttemptRequest req
170170

171171
activeSession.setAttemptedCount(activeSession.getAttemptedCount() + 1);
172172

173-
174173
Integer srGained = 0;
175174
Integer currentSr = user.getSr();
176175

177176
if (request.getSuccess()) {
178177
activeSession.setCompletedCount(activeSession.getCompletedCount() + 1);
179178
try {
180-
LevelSRReward reward = LevelSRReward.fromLevelName(mission.getDifficulty());
181-
srGained = reward.getSrReward();
182-
183-
Integer srBefore = user.getSr();
184-
Integer srAfter = srBefore + srGained;
179+
int gained = LevelSRReward.fromLevelName(mission.getDifficulty()).getSrReward();
185180

186-
user.updateSr(srAfter);
187-
currentSr = srAfter;
181+
int sessionGain = (activeSession.getSrGained() == null ? 0 : activeSession.getSrGained());
182+
int before = user.getSr() + sessionGain;
183+
int after = before + gained;
188184

189-
activeSession.setSrGained(activeSession.getSrGained() + srGained);
185+
activeSession.setSrGained(sessionGain + gained);
190186

191-
SRHistory srHistory = SRHistory.builder()
187+
srHistoryRepository.save(SRHistory.builder()
192188
.user(user)
193189
.session(activeSession)
194190
.mission(mission)
195-
.srBefore(srBefore)
196-
.srAfter(srAfter)
197-
.build();
191+
.srBefore(before)
192+
.srAfter(after)
193+
.build());
198194

199-
srHistoryRepository.save(srHistory);
195+
srGained = gained;
200196

201-
log.info("SR updated for user: {} from {} to {} (+{}) for difficulty: {}",
202-
userId, srBefore, srAfter, srGained, mission.getDifficulty());
197+
log.info("SR pending (session accumulate): user={}, +{}, difficulty={}, sessionId={}",
198+
userId, srGained, mission.getDifficulty(), activeSession.getId());
203199
} catch (IllegalArgumentException e) {
204200
log.warn("Invalid difficulty level: {} for mission: {}", mission.getDifficulty(), mission.getId());
205201
throw new ValidationException(ErrorCode.INVALID_DIFFICULTY_LEVEL, mission.getDifficulty());

src/main/java/com/climbup/climbup/session/service/UserSessionServiceImpl.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,29 @@ public UserSession getSession(Long userId, Long sessionId) {
7575
@Override
7676
@Transactional
7777
public UserSession finishSession(Long userId, Long sessionId) {
78-
UserSession session = userSessionRepository.findById(sessionId).orElseThrow(UserSessionNotFoundException::new);
78+
UserSession session = userSessionRepository.findById(sessionId)
79+
.orElseThrow(UserSessionNotFoundException::new);
7980

80-
if(!session.getUser().getId().equals(userId)) {
81+
if (!session.getUser().getId().equals(userId))
8182
throw new AccessDeniedException("해당 세션에 접근할 수 없습니다.");
82-
}
83-
84-
if(session.getEndedAt() != null) {
83+
if (session.getEndedAt() != null)
8584
throw new UserSessionAlreadyFinishedException();
86-
}
8785

8886
LocalDateTime endTime = LocalDateTime.now();
8987
session.setEndedAt(endTime);
9088

9189
Duration duration = Duration.between(session.getStartedAt(), endTime);
9290
session.setTotalDuration((int) duration.getSeconds());
93-
94-
userSessionRepository.save(session);
9591

96-
return session;
92+
User user = session.getUser();
93+
int srBefore = user.getSr();
94+
int gained = session.getSrGained() == null ? 0 : session.getSrGained();
95+
int srAfter = srBefore + gained;
96+
97+
user.updateSr(srAfter);
98+
99+
userRepository.save(user);
100+
return userSessionRepository.save(session);
97101
}
98102

99103
@Override

0 commit comments

Comments
 (0)