Skip to content
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

[BE-REFACTOR] 바이옴 포켓몬 티어 정렬 #363

Open
wants to merge 2 commits into
base: be/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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;
import lombok.RequiredArgsConstructor;
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
Expand All @@ -24,14 +28,17 @@ public ApiResponse<List<BiomeResponse>> biomeList() {
}

@GetMapping("/api/v1/biome/{id}")
public ApiResponse<BiomeDetailResponse> biomeDetails(@PathVariable("id") String id) {
public ApiResponse<BiomeDetailResponse> 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}",
id,
Thread.currentThread().getName()
);

return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.", biomeService.findBiome(id));
return new ApiResponse<>("바이옴 정보 불러오기에 성공했습니다.",
biomeService.findBiome(id, bossPokemonOrder, wildPokemonOrder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,4 +25,8 @@ public boolean isWild() {
public boolean isBoss() {
return tier.isBoss();
}

public int getRarity() {
return this.tier.getRarity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,34 @@ public List<BiomeResponse> 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<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getWildPokemons(List<NativePokemon> nativePokemons, String wildPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isWild)
.sorted(NativePokemonComparator.of(wildPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
.toList();
}

private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons) {
private List<BiomeAllPokemonResponse> getBossPokemons(List<NativePokemon> nativePokemons, String bossPokemonOrder) {
return nativePokemons.stream()
.filter(NativePokemon::isBoss)
.sorted(NativePokemonComparator.of(bossPokemonOrder))
.map(nativePokemon -> BiomeAllPokemonResponse.of(
nativePokemon,
getBiomePokemons(nativePokemon.getPokemonIds())))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<NativePokemon> {

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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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"),
Expand All @@ -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("슈퍼 울트라 레어 보스", "슈퍼 레어 보스", "레어 보스", "보스");
});
}
}
Original file line number Diff line number Diff line change
@@ -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<NativePokemon> 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);
}
}
Loading