basics being displayed
This commit is contained in:
30
lib/cobblemon_ui/cobblemon_fs/party_store.ex
Normal file
30
lib/cobblemon_ui/cobblemon_fs/party_store.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule CobblemonUi.CobblemonFS.PartyStore do
|
||||
@moduledoc """
|
||||
Parses a player's party storage `.dat` file.
|
||||
|
||||
Party layout:
|
||||
Root -> party -> slot0..slot5
|
||||
"""
|
||||
|
||||
alias CobblemonUi.CobblemonFS.{NBT, Pokemon}
|
||||
|
||||
@party_slots 6
|
||||
|
||||
@doc """
|
||||
Reads and parses a party `.dat` file at the given path.
|
||||
|
||||
Returns `{:ok, [pokemon | nil]}` or `{:error, reason}`.
|
||||
"""
|
||||
@spec parse(String.t()) :: {:ok, list(map() | nil)} | {:error, term()}
|
||||
def parse(path) do
|
||||
with {:ok, data} <- File.read(path),
|
||||
{:ok, {_name, root}} <- NBT.decode(data) do
|
||||
party = Map.get(root, "party", %{})
|
||||
slots = for i <- 0..(@party_slots - 1), do: Pokemon.normalize(Map.get(party, "slot#{i}"))
|
||||
{:ok, slots}
|
||||
else
|
||||
{:error, :enoent} -> {:error, :not_found}
|
||||
{:error, reason} -> {:error, {:corrupt_data, reason}}
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user