diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/controller/BattleController.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/controller/BattleController.java index 09c9dcc16..1a46cb5df 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/controller/BattleController.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/controller/BattleController.java @@ -1,6 +1,7 @@ package com.pokerogue.helper.battle.controller; -import com.pokerogue.helper.battle.dto.BattleResultResponse; +import com.pokerogue.helper.battle.dto.BattleResultResponseV1; +import com.pokerogue.helper.battle.dto.BattleResultResponseV2; import com.pokerogue.helper.battle.dto.WeatherResponse; import com.pokerogue.helper.battle.service.BattleService; import com.pokerogue.helper.battle.service.WeatherService; @@ -26,11 +27,20 @@ public ApiResponse> weatherList() { } @GetMapping("/api/v1/battle") - public ApiResponse battleResult(@RequestParam("weather-id") String weatherId, - @RequestParam("my-pokemon-id") String myPokemonId, - @RequestParam("rival-pokemon-id") String rivalPokemonId, - @RequestParam("my-move-id") String myMoveId) { + public ApiResponse battleResultV1(@RequestParam("weather-id") String weatherId, + @RequestParam("my-pokemon-id") String myPokemonId, + @RequestParam("rival-pokemon-id") String rivalPokemonId, + @RequestParam("my-move-id") String myMoveId) { return new ApiResponse<>("배틀 예측 결과 불러오기에 성공했습니다.", - battleService.calculateBattleResult(weatherId, myPokemonId, rivalPokemonId, myMoveId)); + battleService.calculateBattleResultV1(weatherId, myPokemonId, rivalPokemonId, myMoveId)); + } + + @GetMapping("/api/v2/battle") + public ApiResponse battleResultV2(@RequestParam("weather-id") String weatherId, + @RequestParam("my-pokemon-id") String myPokemonId, + @RequestParam("rival-pokemon-id") String rivalPokemonId, + @RequestParam("my-move-id") String myMoveId) { + return new ApiResponse<>("배틀 예측 결과 불러오기에 성공했습니다.", + battleService.calculateBattleResultV2(weatherId, myPokemonId, rivalPokemonId, myMoveId)); } } diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV1.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV1.java new file mode 100644 index 000000000..618b0b3c1 --- /dev/null +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV1.java @@ -0,0 +1,30 @@ +package com.pokerogue.helper.battle.dto; + +import com.pokerogue.helper.move.data.Move; +import com.pokerogue.helper.move.data.MoveCategory; +import com.pokerogue.helper.type.data.Type; + +public record BattleResultResponseV1( + int power, + double multiplier, + double accuracy, + String moveName, + String moveDescription, + String moveType, + String moveCategory +) { + public static BattleResultResponseV1 from(Move move, double multiplier, double accuracy) { + Type moveType = move.getType(); + MoveCategory moveCategory = move.getMoveCategory(); + + return new BattleResultResponseV1( + move.getPower(), + multiplier, + accuracy, + move.getName(), + move.getEffect(), + moveType.getKoName(), + moveCategory.getName() + ); + } +} diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponse.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV2.java similarity index 79% rename from backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponse.java rename to backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV2.java index 25170edab..dabba2a89 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponse.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/dto/BattleResultResponseV2.java @@ -4,7 +4,7 @@ import com.pokerogue.helper.move.data.MoveCategory; import com.pokerogue.helper.type.data.Type; -public record BattleResultResponse( +public record BattleResultResponseV2( int power, double multiplier, double accuracy, @@ -15,11 +15,11 @@ public record BattleResultResponse( boolean isPreemptive ) { - public static BattleResultResponse from(Move move, double multiplier, double accuracy, boolean isPreemptive) { + public static BattleResultResponseV2 from(Move move, double multiplier, double accuracy, boolean isPreemptive) { Type moveType = move.getType(); MoveCategory moveCategory = move.getMoveCategory(); - return new BattleResultResponse( + return new BattleResultResponseV2( move.getPower(), multiplier, accuracy, diff --git a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/service/BattleService.java b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/service/BattleService.java index c307cf505..f167dd941 100644 --- a/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/service/BattleService.java +++ b/backend/pokerogue/src/main/java/com/pokerogue/helper/battle/service/BattleService.java @@ -1,7 +1,8 @@ package com.pokerogue.helper.battle.service; import com.pokerogue.helper.battle.data.Weather; -import com.pokerogue.helper.battle.dto.BattleResultResponse; +import com.pokerogue.helper.battle.dto.BattleResultResponseV1; +import com.pokerogue.helper.battle.dto.BattleResultResponseV2; import com.pokerogue.helper.global.exception.ErrorMessage; import com.pokerogue.helper.global.exception.GlobalCustomException; import com.pokerogue.helper.move.data.Move; @@ -19,11 +20,37 @@ public class BattleService { private final PokemonRepository pokemonRepository; private final BattleCalculator battleCalculator; - public BattleResultResponse calculateBattleResult( + public BattleResultResponseV1 calculateBattleResultV1( String weatherId, String myPokemonId, String rivalPokemonId, String myMoveId) { + BattleResult battleResult = calculateBattleResult(weatherId, myPokemonId, rivalPokemonId, myMoveId); + return BattleResultResponseV1.from(battleResult.move, battleResult.totalMultiplier, battleResult.finalAccuracy); + } + + public BattleResultResponseV2 calculateBattleResultV2( + String weatherId, + String myPokemonId, + String rivalPokemonId, + String myMoveId) { + BattleResult battleResult = calculateBattleResult(weatherId, myPokemonId, rivalPokemonId, myMoveId); + boolean isPreemptive = battleCalculator.decidePreemptiveAttack(battleResult.rivalPokemon, battleResult.myPokemon); + + return BattleResultResponseV2.from( + battleResult.move, + battleResult.totalMultiplier, + battleResult.finalAccuracy, + isPreemptive + ); + } + + private BattleResult calculateBattleResult( + String weatherId, + String myPokemonId, + String rivalPokemonId, + String myMoveId + ) { Weather weather = Weather.findById(weatherId) .orElseThrow(() -> new GlobalCustomException(ErrorMessage.WEATHER_NOT_FOUND)); Pokemon myPokemon = pokemonRepository.findById(myPokemonId) @@ -35,8 +62,32 @@ public BattleResultResponse calculateBattleResult( double finalAccuracy = battleCalculator.calculateAccuracy(move, weather); double totalMultiplier = battleCalculator.calculateTotalMultiplier(move, weather, rivalPokemon, myPokemon); - boolean isPreemptive = battleCalculator.decidePreemptiveAttack(rivalPokemon, myPokemon); - return BattleResultResponse.from(move, totalMultiplier, finalAccuracy, isPreemptive); + return new BattleResult(weather, myPokemon, rivalPokemon, move, finalAccuracy, totalMultiplier); + } + + private static class BattleResult { + Weather weather; + Pokemon myPokemon; + Pokemon rivalPokemon; + Move move; + double finalAccuracy; + double totalMultiplier; + + BattleResult( + Weather weather, + Pokemon myPokemon, + Pokemon rivalPokemon, + Move move, + double finalAccuracy, + double totalMultiplier + ) { + this.weather = weather; + this.myPokemon = myPokemon; + this.rivalPokemon = rivalPokemon; + this.move = move; + this.finalAccuracy = finalAccuracy; + this.totalMultiplier = totalMultiplier; + } } } diff --git a/backend/pokerogue/src/test/java/com/pokerogue/helper/battle/service/BattleServiceTest.java b/backend/pokerogue/src/test/java/com/pokerogue/helper/battle/service/BattleServiceTest.java index 9db55accc..f51234447 100644 --- a/backend/pokerogue/src/test/java/com/pokerogue/helper/battle/service/BattleServiceTest.java +++ b/backend/pokerogue/src/test/java/com/pokerogue/helper/battle/service/BattleServiceTest.java @@ -23,7 +23,7 @@ void findWeatherByIdWhenCalculateBattleResult() { String myMoveId = "ember"; assertThatThrownBy( - () -> battleService.calculateBattleResult(wrongWeatherId, myPokemonId, rivalPokemonId, myMoveId)) + () -> battleService.calculateBattleResultV2(wrongWeatherId, myPokemonId, rivalPokemonId, myMoveId)) .isInstanceOf(GlobalCustomException.class) .hasMessage(ErrorMessage.WEATHER_NOT_FOUND.getMessage()); } @@ -37,7 +37,7 @@ void findPokemonByIdWhenCalculateBattleResult() { String myMoveId = "ember"; assertThatThrownBy( - () -> battleService.calculateBattleResult(weatherId, wrongMyPokemonId, rivalPokemonId, myMoveId)) + () -> battleService.calculateBattleResultV2(weatherId, wrongMyPokemonId, rivalPokemonId, myMoveId)) .isInstanceOf(GlobalCustomException.class) .hasMessage(ErrorMessage.POKEMON_NOT_FOUND.getMessage()); } @@ -51,7 +51,7 @@ void findMoveByIdWhenCalculateBattleResult() { String wrongMyMoveId = "punch"; assertThatThrownBy( - () -> battleService.calculateBattleResult(weatherId, myPokemonId, rivalPokemonId, wrongMyMoveId)) + () -> battleService.calculateBattleResultV2(weatherId, myPokemonId, rivalPokemonId, wrongMyMoveId)) .isInstanceOf(GlobalCustomException.class) .hasMessage(ErrorMessage.MOVE_NOT_FOUND.getMessage()); }