Skip to content

Commit

Permalink
Update CharacterService.java
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkimswe authored Nov 30, 2024
1 parent 738e5e1 commit 9ffc938
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ public CharacterService(CharacterRepository characterRepository, UserRepository
* @throws IllegalArgumentException 사용자가 존재하지 않을 경우
*/
public List<CharacterResponse> getAvailableCharacters(String deviceId) {
// 사용자 확인
User user = userRepository.findByDeviceId(deviceId)
.orElseThrow(() -> new IllegalArgumentException("User not found for this device."));

List<Character> availableCharacters = characterRepository.findAll().stream()
// 기본 캐릭터(ID 1번) 조회
Character defaultCharacter = characterRepository.findById(1L)
.orElseThrow(() -> new IllegalStateException("Default character (ID 1) not found"));

// 사용자가 클리어한 지역에 따라 잠금 해제된 캐릭터 필터링
List<Character> unlockedCharacters = characterRepository.findAll().stream()
.filter(character -> user.hasClearedRegion(character.getRegion().getId()))
.collect(Collectors.toList());

if (availableCharacters.isEmpty()) {
throw new IllegalArgumentException("No available characters for this device.");
// 기본 캐릭터(ID 1번)를 추가
if (unlockedCharacters.stream().noneMatch(character -> character.getId().equals(defaultCharacter.getId()))) {
unlockedCharacters.add(defaultCharacter);
}

return availableCharacters.stream()
// 캐릭터 리스트를 DTO로 변환하여 반환
return unlockedCharacters.stream()
.map(character -> new CharacterResponse(
character.getId(),
character.getName(),
Expand Down Expand Up @@ -114,4 +122,4 @@ public CharacterResponse getSelectedCharacter(String deviceId) {
character.getRegion().getName()
);
}
}
}

0 comments on commit 9ffc938

Please sign in to comment.