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] Biome, Move 정보 MongoDB 사용하도록 리팩토링 #335

Merged
merged 36 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c617f66
refactor: Biome MongoDB를 사용하도록 리팩토링
unifolio0 Sep 25, 2024
8461dba
refactor: Move MongoDB를 사용하도록 리팩토링
unifolio0 Sep 25, 2024
d46830f
refactor: Biome 관련 사용하지 않는 클래스 삭제
unifolio0 Sep 25, 2024
3cfeae4
refactor: 테스트 통과하도록 수정
unifolio0 Sep 25, 2024
958915d
refactor: 로그 추가
unifolio0 Sep 25, 2024
4e644e1
test: 테스트 작성
unifolio0 Sep 25, 2024
db2053c
refactor: 불필요한 필드 및 메소드 삭제
unifolio0 Sep 25, 2024
40b1803
refactor: 일단 테스트 삭제
unifolio0 Sep 25, 2024
305c4d3
refactor: 일단 테스트
unifolio0 Sep 25, 2024
b3a91dc
refactor: 테스트 통과하도록 수정
unifolio0 Sep 25, 2024
6e9e8f5
refactor: getter 삭제
unifolio0 Sep 25, 2024
d1f3456
refactor: BiomePokemonType 삭제
unifolio0 Sep 26, 2024
df2e8a7
refactor: 상수화
unifolio0 Sep 26, 2024
346a2f6
refactor: 메시지 수정
unifolio0 Sep 26, 2024
9588078
refactor: AllArgsConstructor 삭제
unifolio0 Sep 26, 2024
24f4738
refactor: 메소드명 수정
unifolio0 Sep 26, 2024
e188470
refactor: 로그에 스레드 정보 포함
unifolio0 Sep 26, 2024
82d4bef
refactor: 반환값 확인
unifolio0 Sep 26, 2024
b311da2
refactor: 메소드 순서 변경
unifolio0 Sep 26, 2024
9479e9d
refactor: 캐싱 삭제
unifolio0 Sep 26, 2024
653d516
Merge branch 'be/develop' of https://github.com/woowacourse-teams/202…
unifolio0 Sep 26, 2024
08bf2c2
refactor: 캐싱 삭제
unifolio0 Sep 26, 2024
e8b2876
refactor: 테스트 복구
unifolio0 Sep 26, 2024
e6a433c
refactor: notExistMove 테스트 작성
unifolio0 Sep 26, 2024
b1619a0
refactor: notExistBiome 테스트 작성
unifolio0 Sep 26, 2024
c52c55c
refactor: ErrorMessage 수정
unifolio0 Sep 26, 2024
83909de
refactor: 개행 추가
unifolio0 Sep 26, 2024
469e8d9
refactor: findAllById 적용
unifolio0 Sep 26, 2024
46f5ad9
refactor: 테스트 깨지는 오류 수정
unifolio0 Sep 26, 2024
2c069be
refactor: 오타 수정
unifolio0 Sep 27, 2024
f4fe902
refactor: 에러 처리
unifolio0 Sep 27, 2024
9721101
refactor: Backend-CI.yml 수정
unifolio0 Sep 27, 2024
93736f3
refactor: Backend-CI.yml 수정
unifolio0 Sep 27, 2024
450c4b1
refactor: conflict 해결
unifolio0 Sep 27, 2024
fa15573
refactor: conflict로 인한 오류 해결
unifolio0 Sep 27, 2024
c1f6973
refactor: 중복 클래스 삭제
unifolio0 Sep 27, 2024
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
Expand Up @@ -20,11 +20,6 @@ public ApiResponse<List<WeatherResponse>> weatherList() {
return new ApiResponse<>("날씨 리스트 불러오기에 성공했습니다.", battleService.findWeathers());
}

@GetMapping("/api/v1/moves")
public ApiResponse<List<MoveResponse>> moveByPokemonList(@RequestParam("pokedex-number") Integer pokedexNumber) {
return new ApiResponse<>("포켓몬의 기술 리스트 불러오기에 성공했습니다.", battleService.findMovesByPokemon(pokedexNumber));
}

@GetMapping("/api/v1/battle")
public ApiResponse<BattleResultResponse> battleResult(@RequestParam("weather-id") String weatherId,
@RequestParam("my-pokemon-id") String myPokemonId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pokerogue.helper.battle;

import com.pokerogue.helper.move.data.MoveCategory;
import com.pokerogue.helper.type.data.Type;

public record BattleMove(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,30 @@

import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.move.data.MoveCategory;
import com.pokerogue.helper.pokemon.data.InMemoryPokemon;
import com.pokerogue.helper.pokemon.repository.InMemoryPokemonRepository;
import com.pokerogue.helper.type.data.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BattleService {

private final BattleMoveRepository battleMoveRepository;
private final BattleMoveRepository moveRepository;
private final InMemoryPokemonRepository inMemoryPokemonRepository;
private final TypeMatchingRepository typeMatchingRepository;

private Map<Integer, List<MoveResponse>> findByDexnumberCache = new HashMap<>();

public List<WeatherResponse> findWeathers() {
return Arrays.stream(Weather.values())
.map(WeatherResponse::from)
.toList();
}

public List<MoveResponse> findMovesByPokemon(String pokemonId) {
List<String> allMoveIds = new ArrayList<>();
InMemoryPokemon inMemoryPokemon = inMemoryPokemonRepository.findById(pokemonId)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));

List<String> moves = new ArrayList<>();
for (int i = 0; i < inMemoryPokemon.moves().size(); i++) {
if (i % 2 == 0) {
moves.add(inMemoryPokemon.moves().get(i));
}
}
allMoveIds.addAll(moves);
allMoveIds.addAll(inMemoryPokemon.technicalMachineMoves());
allMoveIds.addAll(inMemoryPokemon.eggMoves());
List<BattleMove> battleMoves = allMoveIds.stream()
.distinct()
.map(this::findMoveById)
.toList();

return battleMoves.stream()
.map(MoveResponse::from)
.toList();
}

public List<MoveResponse> findMovesByPokemon(Integer pokedexNumber) {
if (findByDexnumberCache.isEmpty()) {
initFindByDexnumberCache();
}

return findByDexnumberCache.get(pokedexNumber);
}

private void initFindByDexnumberCache() {
for (InMemoryPokemon inMemoryPokemon : inMemoryPokemonRepository.findAll().values()) {
int pokemonId = Integer.parseInt(inMemoryPokemon.speciesId());
if (!findByDexnumberCache.containsKey(pokemonId)) {
findByDexnumberCache.put(pokemonId, makeMoveResponse(inMemoryPokemon));
}
}
}

private List<MoveResponse> makeMoveResponse(InMemoryPokemon inMemoryPokemon) {
List<String> allMoveIds = new ArrayList<>();
List<String> moves = new ArrayList<>();
for (int i = 0; i < inMemoryPokemon.moves().size(); i++) {
if (i % 2 == 0) {
moves.add(inMemoryPokemon.moves().get(i));
}
}
allMoveIds.addAll(moves);
allMoveIds.addAll(inMemoryPokemon.technicalMachineMoves());
allMoveIds.addAll(inMemoryPokemon.eggMoves());
List<BattleMove> battleMoves = allMoveIds.stream()
.distinct()
.map(this::findMoveById)
.toList();

return battleMoves.stream()
.map(MoveResponse::from)
.toList();
}

private BattleMove findMoveById(String id) {
return battleMoveRepository.findById(id)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.MOVE_NOT_FOUND));
}

public BattleResultResponse calculateBattleResult(
String weatherId,
String myPokemonId,
Expand All @@ -107,21 +37,23 @@ public BattleResultResponse calculateBattleResult(
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));
InMemoryPokemon rivalInMemoryPokemon = inMemoryPokemonRepository.findById(rivalPokemonId)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.POKEMON_NOT_FOUND));
BattleMove move = battleMoveRepository.findById(myMoveId)
BattleMove move = moveRepository.findById(myMoveId)
.orElseThrow(() -> new GlobalCustomException(ErrorMessage.MOVE_CATEGORY_NOT_FOUND));
Type moveType = move.type();

double finalAccuracy = calculateAccuracy(move, weather);
double totalMultiplier = getTotalMultiplier(move, weather, rivalInMemoryPokemon, myInMemoryPokemon);

MoveCategory moveCategory = move.category();

return new BattleResultResponse(
move.power(),
totalMultiplier,
finalAccuracy,
move.name(),
move.effect(),
moveType.getKoName(),
move.category().getName()
moveCategory.getName()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.pokerogue.helper.global.exception.ErrorMessage;
import com.pokerogue.helper.global.exception.GlobalCustomException;
import com.pokerogue.helper.move.data.MoveCategory;
import com.pokerogue.helper.type.data.Type;
import java.io.BufferedReader;
import java.io.IOException;
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading