From 7548b685e601c5341b892cf105cb8e13b8520f28 Mon Sep 17 00:00:00 2001 From: jongmee Date: Sun, 13 Oct 2024 21:06:29 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20NativePokemonComparator=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../helper/biome/data/NativePokemon.java | 8 +++- .../com/pokerogue/helper/biome/data/Tier.java | 22 +++++----- .../service/NativePokemonComparator.java | 34 +++++++++++++++ .../service/NativePokemonComparatorTest.java | 41 +++++++++++++++++++ 4 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/NativePokemonComparator.java create mode 100644 backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/NativePokemonComparatorTest.java diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/NativePokemon.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/NativePokemon.java index d65afa01..7ff6878d 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/NativePokemon.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/NativePokemon.java @@ -2,16 +2,16 @@ import java.util.List; import lombok.AccessLevel; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import org.springframework.data.mongodb.core.mapping.Field; @Getter +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class NativePokemon { - private static final String BOSS = "보스"; - @Field("tier") private Tier tier; @@ -25,4 +25,8 @@ public boolean isWild() { public boolean isBoss() { return tier.isBoss(); } + + public int getRarity() { + return this.tier.getRarity(); + } } diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/Tier.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/Tier.java index 14274634..7b8d198d 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/Tier.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/data/Tier.java @@ -8,21 +8,23 @@ @Getter public enum Tier { - COMMON("보통"), - UNCOMMON("드묾"), - RARE("레어"), - SUPER_RARE("슈퍼 레어"), - ULTRA_RARE("울트라 레어"), - BOSS("보스"), - BOSS_RARE("레어 보스"), - BOSS_SUPER_RARE("슈퍼 레어 보스"), - BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스"), + COMMON("보통", 1), + UNCOMMON("드묾", 2), + RARE("레어", 3), + SUPER_RARE("슈퍼 레어", 4), + ULTRA_RARE("울트라 레어", 5), + BOSS("보스", 6), + BOSS_RARE("레어 보스", 7), + BOSS_SUPER_RARE("슈퍼 레어 보스", 8), + BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스", 9), ; private final String name; + private final int rarity; - Tier(String name) { + Tier(String name, int rarity) { this.name = name; + this.rarity = rarity; } public boolean isWild() { diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/NativePokemonComparator.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/NativePokemonComparator.java new file mode 100644 index 00000000..2ba08a3d --- /dev/null +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/NativePokemonComparator.java @@ -0,0 +1,34 @@ +package com.pokerogue.helper.biome.service; + +import com.pokerogue.helper.biome.data.NativePokemon; +import java.util.Comparator; + +public class NativePokemonComparator implements Comparator { + + public static final String ASCENDING = "asc"; + public static final String DESCENDING = "desc"; + private static final NativePokemonComparator ASCENDING_COMPARATOR = new NativePokemonComparator(ASCENDING); + private static final NativePokemonComparator DESCENDING_COMPARATOR = new NativePokemonComparator(DESCENDING); + + private final String criteria; + + private NativePokemonComparator(String criteria) { + this.criteria = criteria; + } + + public static NativePokemonComparator of(String criteria) { + if (criteria.equals(ASCENDING)) { + return ASCENDING_COMPARATOR; + } + return DESCENDING_COMPARATOR; + } + + @Override + public int compare(NativePokemon firstPokemon, NativePokemon secondPokemon) { + if (this.criteria.equals(ASCENDING)) { + return Integer.compare(firstPokemon.getRarity(), secondPokemon.getRarity()); + } + + return Integer.compare(secondPokemon.getRarity(), firstPokemon.getRarity()); + } +} diff --git a/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/NativePokemonComparatorTest.java b/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/NativePokemonComparatorTest.java new file mode 100644 index 00000000..97db8998 --- /dev/null +++ b/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/NativePokemonComparatorTest.java @@ -0,0 +1,41 @@ +package com.pokerogue.helper.biome.service; + +import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING; +import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING; +import static org.assertj.core.api.Assertions.assertThat; + +import com.pokerogue.helper.biome.data.NativePokemon; +import com.pokerogue.helper.biome.data.Tier; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class NativePokemonComparatorTest { + + private final List nativePokemons = new ArrayList<>(List.of( + new NativePokemon(Tier.RARE, List.of("bulbasaur", "venusaur")), + new NativePokemon(Tier.COMMON, List.of("pikachu", "raichu")), + new NativePokemon(Tier.BOSS_RARE, List.of("charmander")), + new NativePokemon(Tier.BOSS_SUPER_RARE, List.of("roserade")) + )); + + @Test + @DisplayName("바이옴 포켓몬을 티어의 희귀도가 낮은 순으로 정렬한다.") + void sortNativePokemonsAscending() { + Collections.sort(nativePokemons, NativePokemonComparator.of(ASCENDING)); + + assertThat(nativePokemons).extracting(NativePokemon::getTier) + .containsExactly(Tier.COMMON, Tier.RARE, Tier.BOSS_RARE, Tier.BOSS_SUPER_RARE); + } + + @Test + @DisplayName("바이옴 포켓몬을 티어의 희귀도가 높은 순으로 정렬한다.") + void sortNativePokemonsDescending() { + Collections.sort(nativePokemons, NativePokemonComparator.of(DESCENDING)); + + assertThat(nativePokemons).extracting(NativePokemon::getTier) + .containsExactly(Tier.BOSS_SUPER_RARE, Tier.BOSS_RARE, Tier.RARE, Tier.COMMON); + } +} From 2e292e0f777f0a24adaf2a405c5ee5421c0112f4 Mon Sep 17 00:00:00 2001 From: jongmee Date: Sun, 13 Oct 2024 21:06:52 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EB=B0=94=EC=9D=B4=EC=98=B4=20?= =?UTF-8?q?=ED=8F=AC=EC=BC=93=EB=AA=AC=20=EC=A0=95=EB=A0=AC=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20(=EC=BF=BC=EB=A6=AC=20=EC=8A=A4=ED=8A=B8=EB=A7=81?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biome/controller/BiomeController.java | 13 +++++++--- .../helper/biome/service/BiomeService.java | 12 ++++++---- .../biome/service/BiomeServiceTest.java | 24 +++++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/controller/BiomeController.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/controller/BiomeController.java index 11ebd300..6f73c2b3 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/controller/BiomeController.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/controller/BiomeController.java @@ -1,7 +1,10 @@ package com.pokerogue.helper.biome.controller; -import com.pokerogue.helper.biome.dto.BiomeResponse; +import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING; +import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING; + import com.pokerogue.helper.biome.dto.BiomeDetailResponse; +import com.pokerogue.helper.biome.dto.BiomeResponse; import com.pokerogue.helper.biome.service.BiomeService; import com.pokerogue.helper.util.dto.ApiResponse; import java.util.List; @@ -9,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @Slf4j @@ -24,7 +28,9 @@ public ApiResponse> biomeList() { } @GetMapping("/api/v1/biome/{id}") - public ApiResponse biomeDetails(@PathVariable("id") String id) { + public ApiResponse biomeDetails(@PathVariable("id") String id, + @RequestParam(value = "boss", defaultValue = DESCENDING) String bossPokemonOrder, + @RequestParam(value = "wild", defaultValue = ASCENDING) String wildPokemonOrder) { log.info( "---- URI : {}, Param : {}, ThreadName : {}", "/api/v1/biome/{id}", @@ -32,6 +38,7 @@ public ApiResponse biomeDetails(@PathVariable("id") String Thread.currentThread().getName() ); - return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.", biomeService.findBiome(id)); + return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.", + biomeService.findBiome(id, bossPokemonOrder, wildPokemonOrder)); } } diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/BiomeService.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/BiomeService.java index dd5741b9..6eb77185 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/BiomeService.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/biome/service/BiomeService.java @@ -39,32 +39,34 @@ public List findBiomes() { .toList(); } - public BiomeDetailResponse findBiome(String id) { + public BiomeDetailResponse findBiome(String id, String bossPokemonOrder, String wildPokemonOrder) { Biome biome = biomeRepository.findById(id) .orElseThrow(() -> new GlobalCustomException(ErrorMessage.BIOME_NOT_FOUND)); return BiomeDetailResponse.of( biome, s3Service.getBiomeImageFromS3(biome.getId()), - getWildPokemons(biome.getNativePokemons()), - getBossPokemons(biome.getNativePokemons()), + getWildPokemons(biome.getNativePokemons(), wildPokemonOrder), + getBossPokemons(biome.getNativePokemons(), bossPokemonOrder), getTrainerPokemons(biome), getNextBiomes(biome) ); } - private List getWildPokemons(List nativePokemons) { + private List getWildPokemons(List nativePokemons, String wildPokemonOrder) { return nativePokemons.stream() .filter(NativePokemon::isWild) + .sorted(NativePokemonComparator.of(wildPokemonOrder)) .map(nativePokemon -> BiomeAllPokemonResponse.of( nativePokemon, getBiomePokemons(nativePokemon.getPokemonIds()))) .toList(); } - private List getBossPokemons(List nativePokemons) { + private List getBossPokemons(List nativePokemons, String bossPokemonOrder) { return nativePokemons.stream() .filter(NativePokemon::isBoss) + .sorted(NativePokemonComparator.of(bossPokemonOrder)) .map(nativePokemon -> BiomeAllPokemonResponse.of( nativePokemon, getBiomePokemons(nativePokemon.getPokemonIds()))) diff --git a/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/BiomeServiceTest.java b/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/BiomeServiceTest.java index bed2ec00..dcdf43d8 100644 --- a/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/BiomeServiceTest.java +++ b/backend/pokerogue/src/test/java/com/pokerogue/helper/biome/service/BiomeServiceTest.java @@ -1,10 +1,13 @@ package com.pokerogue.helper.biome.service; +import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING; +import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; import com.pokerogue.environment.service.ServiceTest; +import com.pokerogue.helper.biome.dto.BiomeAllPokemonResponse; import com.pokerogue.helper.biome.dto.BiomeDetailResponse; import com.pokerogue.helper.biome.dto.BiomeResponse; import com.pokerogue.helper.global.exception.ErrorMessage; @@ -30,7 +33,7 @@ void findBoimes() { @Test @DisplayName("단일 바이옴 정보를 불러온다") void findBiome() { - BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave"); + BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave", ASCENDING, DESCENDING); assertAll( () -> assertThat(biomeDetailResponse.id()).isEqualTo("fairy_cave"), @@ -45,8 +48,25 @@ void findBiome() { @Test @DisplayName("해당 id의 바이옴이 없는 경우 예외를 발생시킨다") void notExistBiome() { - assertThatThrownBy(() -> biomeService.findBiome("test")) + assertThatThrownBy(() -> biomeService.findBiome("test", ASCENDING, DESCENDING)) .isInstanceOf(GlobalCustomException.class) .hasMessage(ErrorMessage.BIOME_NOT_FOUND.getMessage()); } + + @Test + @DisplayName("바이옴 포켓몬의 티어를 희귀도 순으로 정렬한다.") + void sortBiomeNativePokemons() { + String bossPokemonOrder = DESCENDING; + String wildPokemonOrder = ASCENDING; + + BiomeDetailResponse biomeDetailResponse = biomeService.findBiome("fairy_cave", bossPokemonOrder, + wildPokemonOrder); + + assertAll(() -> { + assertThat(biomeDetailResponse.wildPokemons()).extracting(BiomeAllPokemonResponse::tier) + .containsExactly("보통", "드묾", "레어", "슈퍼 레어", "울트라 레어"); + assertThat(biomeDetailResponse.bossPokemons()).extracting(BiomeAllPokemonResponse::tier) + .containsExactly("슈퍼 울트라 레어 보스", "슈퍼 레어 보스", "레어 보스", "보스"); + }); + } }