Skip to content

Commit

Permalink
[BE-REFACTOR] 배틀 api v1, v2 분리 (#469)
Browse files Browse the repository at this point in the history
* refactor: 배틀 api v1, v2 분리

* refactor: BattleResultResponseV1

* [BE-REFACTOR] cloudfront 주소를 변경한다 (#465)

* refactor: cloudfront url 수정

* refactor: 이미지 url 한 곳에서 관리

---------

Co-authored-by: unifolio0 <[email protected]>

---------

Co-authored-by: unifolio0 <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and unifolio0 authored Nov 29, 2024
1 parent 809f584 commit 5062795
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,11 +27,20 @@ public ApiResponse<List<WeatherResponse>> weatherList() {
}

@GetMapping("/api/v1/battle")
public ApiResponse<BattleResultResponse> 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<BattleResultResponseV1> 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<BattleResultResponseV2> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down

0 comments on commit 5062795

Please sign in to comment.