-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE-FEAT] 특성 api txt 파싱 정보로 변환 (#275)
* chore: 서브모듈 커밋 시점 변경 * feat: 특성 txt파일로 파싱 * chore: 서버모듈 커밋 시점 변경 * refactor: 출력문 제거 * refactor: 타입 이미지 가져오는 로직 수정 * refactor: 안쓰는 부분 제거
- Loading branch information
Showing
13 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...erogue/src/main/java/com/pokerogue/helper/ability2/config/AbilityDatabaseInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.pokerogue.helper.ability2.config; | ||
|
||
import com.pokerogue.helper.ability2.data.Ability; | ||
import com.pokerogue.helper.ability2.data.AbilityInfo; | ||
import com.pokerogue.helper.ability2.data.AbilityPokemon; | ||
import com.pokerogue.helper.ability2.data.AbilityPokemonInfo; | ||
import com.pokerogue.helper.ability2.repository.AbilityRepository; | ||
import com.pokerogue.helper.biome.data.BiomePokemonType; | ||
import com.pokerogue.helper.global.exception.ErrorMessage; | ||
import com.pokerogue.helper.global.exception.GlobalCustomException; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AbilityDatabaseInitializer implements ApplicationRunner { | ||
|
||
private final AbilityRepository abilityRepository; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
List<AbilityInfo> abilityInfos = new ArrayList<>(); | ||
List<AbilityPokemonInfo> abilityPokemonInfos = new ArrayList<>(); | ||
|
||
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("ability.txt"); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { | ||
while (true) { | ||
String abilityInfo = bufferedReader.readLine(); | ||
if (abilityInfo == null) { | ||
break; | ||
} | ||
abilityInfos.add(new AbilityInfo(abilityInfo)); | ||
} | ||
} catch (IOException e) { | ||
log.error("error message : {}", e.getStackTrace()[0]); | ||
} | ||
|
||
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("ability-pokemon.txt"); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { | ||
while (true) { | ||
String abilityPokemonInfo = bufferedReader.readLine(); | ||
if (abilityPokemonInfo == null) { | ||
break; | ||
} | ||
abilityPokemonInfos.add(new AbilityPokemonInfo(abilityPokemonInfo)); | ||
} | ||
} catch (IOException e) { | ||
log.error("error message : {}", e.getStackTrace()[0]); | ||
} | ||
|
||
abilityInfos.stream() | ||
.map(abilityInfo -> new Ability( | ||
abilityInfo.getId(), | ||
abilityInfo.getName(), | ||
abilityInfo.getDescription(), | ||
getAbilityPokemon(abilityPokemonInfos, abilityInfo.getPokemons(), abilityInfo.getPokedexNumbers())) | ||
).forEach(abilityRepository::save); | ||
} | ||
|
||
private List<AbilityPokemon> getAbilityPokemon(List<AbilityPokemonInfo> abilityPokemonInfos, List<String> pokemons, List<String> pokedexNumbers) { | ||
List<AbilityPokemon> abilityPokemons = new ArrayList<>(); | ||
for (int i = 0; i < pokemons.size(); i++) { | ||
String pokemonId = pokemons.get(i); | ||
|
||
AbilityPokemonInfo abilityPokemon = abilityPokemonInfos.stream() | ||
.filter(abilityPokemonInfo -> abilityPokemonInfo.getId().equals(pokemonId)) | ||
.findFirst() | ||
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND)); | ||
|
||
abilityPokemons.add(new AbilityPokemon( | ||
pokemons.get(i), | ||
Long.parseLong(pokedexNumbers.get(i)), | ||
abilityPokemon.getName(), | ||
abilityPokemon.getType1(), | ||
abilityPokemon.getType2()) | ||
); | ||
} | ||
|
||
return abilityPokemons; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...d/pokerogue/src/main/java/com/pokerogue/helper/ability2/controller/AbilityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.pokerogue.helper.ability2.controller; | ||
|
||
import com.pokerogue.helper.ability2.dto.AbilityDetailResponse; | ||
import com.pokerogue.helper.ability2.dto.AbilityResponse2; | ||
import com.pokerogue.helper.ability2.service.AbilityService; | ||
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.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class AbilityController { | ||
|
||
private final AbilityService abilityService; | ||
|
||
@GetMapping("/api/v1/abilities2") | ||
public ApiResponse<List<AbilityResponse2>> abilityList() { | ||
return new ApiResponse<>("특성 리스트 불러오기에 성공했습니다.", abilityService.findAbilities()); | ||
} | ||
|
||
@GetMapping("/api/v1/ability2/{id}") | ||
public ApiResponse<AbilityDetailResponse> abilityDetails(@PathVariable("id") String id) { | ||
log.info("---- URI : {}, Param : {}", "/api/v1/ability/{id}", id); | ||
|
||
return new ApiResponse<>("특성 정보 불러오기에 성공했습니다.", abilityService.findAbilityDetails(id)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/data/Ability.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.pokerogue.helper.ability2.data; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class Ability { | ||
|
||
private final String id; | ||
private final String name; | ||
private final String description; | ||
private final List<AbilityPokemon> pokemons; | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/data/AbilityInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.pokerogue.helper.ability2.data; | ||
|
||
import java.util.List; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
public class AbilityInfo { | ||
|
||
private final String id; | ||
private final String name; | ||
private final String description; | ||
private final List<String> pokemons; | ||
private final List<String> pokedexNumbers; | ||
|
||
public AbilityInfo(String abilityInfo) { | ||
String[] abilityInfos = abilityInfo.split(" / "); | ||
this.id = abilityInfos[0]; | ||
this.name = abilityInfos[1]; | ||
this.description = abilityInfos[2]; | ||
this.pokemons = List.of(abilityInfos[4].split(" , ")); | ||
this.pokedexNumbers = List.of(abilityInfos[5].split(" , ")); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/data/AbilityPokemon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.pokerogue.helper.ability2.data; | ||
|
||
import com.pokerogue.helper.biome.data.BiomePokemonType; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class AbilityPokemon { | ||
|
||
private final String id; | ||
private final Long pokedexNumber; | ||
private final String name; | ||
private final String type1; | ||
private final String type2; | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/data/AbilityPokemonInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.pokerogue.helper.ability2.data; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
public class AbilityPokemonInfo { | ||
|
||
private final String id; | ||
private final String type1; | ||
private final String type2; | ||
private final String name; | ||
|
||
public AbilityPokemonInfo(String abilityPokemonInfo) { | ||
String[] abilityPokemonInfos = abilityPokemonInfo.split(" / "); | ||
this.id = abilityPokemonInfos[0]; | ||
this.type1 = abilityPokemonInfos[1]; | ||
this.type2 = abilityPokemonInfos[2]; | ||
this.name = abilityPokemonInfos[3]; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/dto/AbilityDetailResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.pokerogue.helper.ability2.dto; | ||
|
||
import com.pokerogue.helper.ability2.data.Ability; | ||
import java.util.List; | ||
|
||
public record AbilityDetailResponse( | ||
String koName, | ||
String description, | ||
List<AbilityPokemonResponse> pokemons | ||
) { | ||
|
||
public static AbilityDetailResponse of(Ability ability, List<AbilityPokemonResponse> pokemons) { | ||
return new AbilityDetailResponse(ability.getName(), ability.getDescription(), pokemons); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...end/pokerogue/src/main/java/com/pokerogue/helper/ability2/dto/AbilityPokemonResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.pokerogue.helper.ability2.dto; | ||
|
||
import java.util.List; | ||
|
||
public record AbilityPokemonResponse( | ||
String id, | ||
Long pokedexNumber, | ||
String koName, | ||
String image, | ||
List<AbilityTypeResponse> pokemonTypeResponses | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/dto/AbilityResponse2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.pokerogue.helper.ability2.dto; | ||
|
||
import com.pokerogue.helper.ability2.data.Ability; | ||
|
||
public record AbilityResponse2(String id, String koName, String description) { | ||
|
||
public static AbilityResponse2 from(Ability ability) { | ||
return new AbilityResponse2(ability.getId(), ability.getName(), ability.getDescription()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/dto/AbilityTypeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.pokerogue.helper.ability2.dto; | ||
|
||
public record AbilityTypeResponse(String typeLogo, String typeName) { | ||
} |
35 changes: 35 additions & 0 deletions
35
...d/pokerogue/src/main/java/com/pokerogue/helper/ability2/repository/AbilityRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.pokerogue.helper.ability2.repository; | ||
|
||
import com.pokerogue.helper.ability2.data.Ability; | ||
import com.pokerogue.helper.biome.data.Biome; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.aop.target.LazyInitTargetSource; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class AbilityRepository { | ||
|
||
private final Map<String, Ability> abilities; | ||
|
||
public AbilityRepository() { | ||
this.abilities = new HashMap<>(); | ||
} | ||
|
||
public void save(Ability ability) { | ||
abilities.put(ability.getId(), ability); | ||
} | ||
|
||
public List<Ability> findAll() { | ||
return abilities.values().stream() | ||
.toList(); | ||
} | ||
|
||
public Optional<Ability> findById(String id) { | ||
return Optional.ofNullable(abilities.get(id)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
backend/pokerogue/src/main/java/com/pokerogue/helper/ability2/service/AbilityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.pokerogue.helper.ability2.service; | ||
|
||
import com.pokerogue.external.s3.service.S3Service; | ||
import com.pokerogue.helper.ability2.data.Ability; | ||
import com.pokerogue.helper.ability2.dto.AbilityDetailResponse; | ||
import com.pokerogue.helper.ability2.dto.AbilityPokemonResponse; | ||
import com.pokerogue.helper.ability2.dto.AbilityResponse2; | ||
import com.pokerogue.helper.ability2.dto.AbilityTypeResponse; | ||
import com.pokerogue.helper.ability2.repository.AbilityRepository; | ||
import com.pokerogue.helper.battle.Type; | ||
import com.pokerogue.helper.global.exception.ErrorMessage; | ||
import com.pokerogue.helper.global.exception.GlobalCustomException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AbilityService { | ||
|
||
private final S3Service s3Service; | ||
private final AbilityRepository abilityRepository; | ||
|
||
public List<AbilityResponse2> findAbilities() { | ||
return abilityRepository.findAll().stream() | ||
.map(AbilityResponse2::from) | ||
.toList(); | ||
} | ||
|
||
public AbilityDetailResponse findAbilityDetails(String id) { | ||
Ability ability = abilityRepository.findById(id) | ||
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_ABILITY_NOT_FOUND)); | ||
List<AbilityPokemonResponse> abilityPokemonResponses = ability.getPokemons().stream() | ||
.map(abilityPokemon -> new AbilityPokemonResponse( | ||
abilityPokemon.getId(), | ||
abilityPokemon.getPokedexNumber(), | ||
abilityPokemon.getName(), | ||
s3Service.getPokemonImageFromS3(abilityPokemon.getId()), | ||
getAbilityTypeResponses(abilityPokemon.getType1(), abilityPokemon.getType2()) | ||
)) | ||
.toList(); | ||
|
||
return AbilityDetailResponse.of(ability, abilityPokemonResponses); | ||
} | ||
|
||
private List<AbilityTypeResponse> getAbilityTypeResponses(String type1, String type2) { | ||
List<AbilityTypeResponse> abilityTypeResponses = new ArrayList<>(); | ||
if (!type1.equals("Type.undefined")) { | ||
abilityTypeResponses.add(new AbilityTypeResponse(Type.findByName(type1) | ||
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_TYPE_NOT_FOUND)).getImage(), | ||
type1)); | ||
} | ||
if (!type2.equals("Type.undefined")) { | ||
abilityTypeResponses.add(new AbilityTypeResponse(Type.findByName(type2) | ||
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_TYPE_NOT_FOUND)).getImage(), | ||
type2)); | ||
} | ||
|
||
return abilityTypeResponses; | ||
} | ||
} |
Submodule resources
updated
from c27fde to ad022f