LoginSignup
2
1

More than 1 year has passed since last update.

【spring boot】PokeAPIでポケモンの種族値を取得するWebAPIを作成してみた。

Posted at

ポケモンのガチ対戦をしていて、どちらの素早さが高いのか分からなくなった経験はありませんか?
そんな自分の悩みを解決すべく、PokeAPIを使用してポケモンの種族値を取得するWebAPIを作成してみました。
PokeAPIとは、ポケモンの情報を取得することが出来るAPIです。

↓↓↓↓pokeAPI↓↓↓↓
https://github.com/oscar0812/pokeapi-v2-java

分かりやすいように先に完成品をお見せ致します。
検索フォーム欄にポケモンの名前を入力すると、ポケモンの種族値を返却してくれます。
スクリーンショット (10).png
スクリーンショット (11).png

流れのイメージとしては、下記画像のような感じです。
フロントから値を渡して、値に対応する結果を返却する簡単な処理になっています。
無題.png

クラス一覧

search.html
<html>
<head>
<meta charset="UTF-8">
<title>種族値検索</title>
</head>
  <body>
    <form action="/pokemon" method="get">
        <p>自分のポケモンを入力してください</p>
        <input type="text" name="myPokemon">
        <br></br>
        <p>相手のポケモンを入力してください</p>
        <input type="text" name="enemyPokemon">
        <br></br>
        <input type="submit" value="検索">
    </form>
  </body>
</html>
result.html
<html>
<head>
<meta charset="UTF-8">
<title>種族値結果</title>
</head>
<body>
	<p th:text="${myPokemonName}"></p>
	<table>
		<tr>
			<th>HP</th>
			<td th:text="${myH}"></td>
		</tr>
		<tr>
			<th>こうげき</th>
			<td th:text="${myA}"></td>
		</tr>
		<tr>
			<th>ぼうぎょ</th>
			<td th:text="${myB}"></td>
		</tr>
		<tr>
			<th>とくこう</th>
			<td th:text="${myC}"></td>
		</tr>
		<tr>
			<th>とくぼう</th>
			<td th:text="${myD}"></td>
		</tr>
		<tr>
			<th>すばやさ</th>
			<td th:text="${myS}"></td>
		</tr>
	</table>
	<br></br>
	<p th:text="${enemyPokemonName}"></p>
	<table>
		<tr>
			<th>HP</th>
			<td th:text="${enemyH}"></td>
		</tr>
		<tr>
			<th>こうげき</th>
			<td th:text="${enemyA}"></td>
		</tr>
		<tr>
			<th>ぼうぎょ</th>
			<td th:text="${enemyB}"></td>
		</tr>
		<tr>
			<th>とくこう</th>
			<td th:text="${enemyC}"></td>
		</tr>
		<tr>
			<th>とくぼう</th>
			<td th:text="${enemyD}"></td>
		</tr>
		<tr>
			<th>すばやさ</th>
			<td th:text="${enemyS}"></td>
		</tr>
	</table>
	<br></br>
      <button type="button" onclick="history.back()">戻る</button>
</body>
</html>
PokemonController.java
@Controller
@RequiredArgsConstructor
public class PokemonController {
	private final PokemonSevice pokemonService;

	@RequestMapping("/")
	public String home() {
		return "search";
	}

	@GetMapping("pokemon")
	String get1(@RequestParam("myPokemon") String myPokemonName, @RequestParam("enemyPokemon") String enemyPokemonName,  Model model) {
		// ポケモンのステータスを取得
		PokemonStatus myPokemon = this.pokemonService.findPokemonStatusByPokemonName(myPokemonName);
		PokemonStatus enemyPokemon = this.pokemonService.findPokemonStatusByPokemonName(enemyPokemonName);
		this.pokemonService.setMyPokemon(myPokemon, model);
		this.pokemonService.setEnemyPokemon(enemyPokemon, model);
		return "result";
	}
}
PokemonSevice.java
@Service
public class PokemonSevice {

	public PokemonStatus findPokemonStatusByPokemonName(String pokemonName) {
		// ポケモンの英語名を取得
		String englishName = PokemonNameEnum.getEnglishName(pokemonName);
		// ポケモンのデータを取得
		Pokemon pokemon = Client.getPokemonByName(englishName);
		// ポケモンの種族値を取得
		PokemonStatus status = PokemonStatus.adaptToPokemonStatus(pokemon, pokemonName);
		return status;
	}

	public void setMyPokemon(PokemonStatus myPokemon, Model model) {
		model.addAttribute("myPokemonName", myPokemon.getPokemonName() + "の種族値");
		model.addAttribute("myH", myPokemon.getH());
		model.addAttribute("myA", myPokemon.getA());
		model.addAttribute("myB", myPokemon.getB());
		model.addAttribute("myC", myPokemon.getC());
		model.addAttribute("myD", myPokemon.getD());
		model.addAttribute("myS", myPokemon.getS());
	}

	public void setEnemyPokemon(PokemonStatus enemyPokemon, Model model) {
		model.addAttribute("enemyPokemonName", enemyPokemon.getPokemonName() + "の種族値");
		model.addAttribute("enemyH", enemyPokemon.getH());
		model.addAttribute("enemyA", enemyPokemon.getA());
		model.addAttribute("enemyB", enemyPokemon.getB());
		model.addAttribute("enemyC", enemyPokemon.getC());
		model.addAttribute("enemyD", enemyPokemon.getD());
		model.addAttribute("enemyS", enemyPokemon.getS());
	}
}
PokemonNameEnum.java
public enum PokemonNameEnum {
    // ここにポケモンを追加することで取得できるポケモンが増える
	Pikachu("ピカチュウ", "pikachu"),
	Venusaur("フシギバナ", "venusaur"),
	Charizard("リザードン", "charizard"),
	Blastoise("カメックス", "blastoise"),
	Dialga("ディアルガ", "dialga"),
	Palkia("パルキア", "palkia");

	private final String japaneseName;
	private final String englishName;

	PokemonNameEnum(String japaneseName, String englishName) {
		this.japaneseName = japaneseName;
		this.englishName = englishName;
	}

	public static String getEnglishName(String japaneseName) {
		return Arrays.stream(values())
				.filter(
						pokemonEnum ->
						Objects.equals(japaneseName, pokemonEnum.japaneseName)
						).map(pokemonEnum -> pokemonEnum.englishName).findFirst().orElse(null);
	}
}
PokemonStatus.java
@Getter
@ToString
@EqualsAndHashCode
public class PokemonStatus {
	private final String pokemonName;
	private final int h;
	private final int a;
	private final int b;
	private final int c;
	private final int d;
	private final int s;

	public PokemonStatus(
			String pokemonName,
			int h, 
			int a, 
			int b, 
			int c, 
			int d, 
			int s) {
		this.pokemonName = pokemonName;
		this.h = h;
		this.a = a;
		this.b = b;
		this.c = c;
		this.d = d;
		this.s = s;
	}

	public static PokemonStatus adaptToPokemonStatus(Pokemon pokemon, String pokemonName) {
		int h = 0;
		int a = 0;
		int b = 0;
		int c = 0;
		int d = 0;
		int s = 0;

		for (PokemonStat status : pokemon.getStats()) {
			if (Objects.equals("hp", status.getStat().getName())) {
				h = status.getBaseStat();
			}
			if (Objects.equals("attack", status.getStat().getName())) {
				a = status.getBaseStat();
			}
			if (Objects.equals("defense", status.getStat().getName())) {
				b = status.getBaseStat();
			}
			if (Objects.equals("special-attack", status.getStat().getName())) {
				c = status.getBaseStat();
			}
			if (Objects.equals("special-defense", status.getStat().getName())) {
				d = status.getBaseStat();
			}
			if (Objects.equals("speed", status.getStat().getName())) {
				s = status.getBaseStat();
			}
		}
		return new PokemonStatus(pokemonName, h, a, b, c, d, s);
	}
}

練習がてらPokeAPIを触ってWebAPIを作成してみました。
なにかの参考になれば・・・

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1