minecraft server
All checks were successful
Build and Deploy / Build & Push Image (push) Successful in 42s
All checks were successful
Build and Deploy / Build & Push Image (push) Successful in 42s
This commit is contained in:
74
lib/cobblemon_ui/battles_api.ex
Normal file
74
lib/cobblemon_ui/battles_api.ex
Normal file
@@ -0,0 +1,74 @@
|
||||
defmodule CobblemonUi.BattlesApi do
|
||||
@moduledoc """
|
||||
Fetches active battle data from the Cobblemon Minecraft server.
|
||||
"""
|
||||
|
||||
def server_url do
|
||||
Application.get_env(:cobblemon_ui, :minecraft_server_url, "http://localhost:80")
|
||||
end
|
||||
|
||||
def list_battles do
|
||||
url = "#{server_url()}/battles"
|
||||
|
||||
case Req.get(url) do
|
||||
{:ok, %Req.Response{status: 200, body: body}} ->
|
||||
battles = parse_battles(body)
|
||||
{:ok, battles}
|
||||
|
||||
{:ok, %Req.Response{status: status}} ->
|
||||
{:error, "Server returned status #{status}"}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, "Request failed: #{inspect(reason)}"}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_battles(%{"battles" => battles}) when is_list(battles) do
|
||||
Enum.map(battles, &parse_battle/1)
|
||||
end
|
||||
|
||||
defp parse_battles(_), do: []
|
||||
|
||||
defp parse_battle(battle) do
|
||||
%{
|
||||
battle_id: battle["battleId"],
|
||||
actors: Enum.map(battle["actors"] || [], &parse_actor/1)
|
||||
}
|
||||
end
|
||||
|
||||
defp parse_actor(actor) do
|
||||
%{
|
||||
name: actor["name"],
|
||||
type: actor["type"],
|
||||
player_id: actor["playerId"],
|
||||
active_pokemon: Enum.map(actor["activePokemon"] || [], &parse_active_pokemon/1),
|
||||
party: Enum.map(actor["party"] || [], &parse_party_pokemon/1)
|
||||
}
|
||||
end
|
||||
|
||||
defp parse_active_pokemon(p) do
|
||||
%{
|
||||
uuid: p["uuid"],
|
||||
species: p["species"],
|
||||
level: p["level"],
|
||||
hp: p["hp"],
|
||||
max_hp: p["maxHp"]
|
||||
}
|
||||
end
|
||||
|
||||
defp parse_party_pokemon(p) do
|
||||
%{
|
||||
uuid: p["uuid"],
|
||||
species: p["species"],
|
||||
level: p["level"],
|
||||
hp: p["hp"],
|
||||
max_hp: p["maxHp"],
|
||||
ability: p["ability"],
|
||||
nature: p["nature"],
|
||||
shiny: p["shiny"],
|
||||
moves: Enum.map(p["moves"] || [], fn m ->
|
||||
%{name: m["name"], pp: m["pp"], max_pp: m["maxPp"]}
|
||||
end)
|
||||
}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user