More Player/Grain API and Tests

parent 10036200
......@@ -24,6 +24,14 @@ defmodule GrainsEngine.Grain do
Parameter.set_value(parameter, value)
end
def get_params(grain) do
Agent.get(grain, fn state -> state end)
|> Enum.reduce(%{}, fn{k,v}, acc ->
value = Parameter.value(v)
Map.put(acc, k, value)
end)
end
def to_string(grain) do
grain_string =
Agent.get(grain, fn state -> state end)
......
......@@ -3,12 +3,39 @@ defmodule GrainsEngine.Player do
defstruct name: :none, grain: :none
def start_link(name \\ :none) do
{:ok, grain} = Grain.start_link
@doc """
Starts a new player with a `name` and a list of names of `params`
"""
def start_link(name, params) do
{:ok, grain} = Grain.start_link(params)
Agent.start_link(fn -> %Player{name: name,
grain: grain} end)
end
@doc """
Updates the values of the parameters. Takes a `params` map that
has the same keys as the ones specified when the `player` was
created.
"""
def update_parameters(player, params) do
grain = Player.grain(player)
Enum.each(params, fn {k, v} ->
Grain.set_value(grain, k, v)
end)
end
@doc """
Returns a Map in the form %{foo: 1, bar: 2} with the
values and the parameters.
"""
def get_values(player) do
grain = Player.grain(player)
Grain.get_params(grain)
end
def set_name(player, name) do
Agent.update(player, fn state -> Map.put(state, :name, name) end)
end
......@@ -31,10 +58,10 @@ defmodule GrainsEngine.Player do
Grain.replace_parameters(state.grain, params)
state
end)
end
def to_string(player) do
"%Player{" <> string_body(player) <> "}"
end
......
......@@ -29,6 +29,6 @@ defmodule GrainsEngine.Mixfile do
#
# Type "mix help deps" for more examples and options
defp deps do
[]
[{:mix_test_watch, "~> 0.3", only: :dev, runtime: false}]
end
end
......@@ -27,6 +27,12 @@ defmodule GrainTest do
assert Grain.get_value(grain, :foo) == 63
end
test "get all parameters", %{grain: grain} do
Grain.set_value(grain, :foo, 1)
Grain.set_value(grain, :bar, 2)
assert Grain.get_params(grain) == %{bar: 2, foo: 1}
end
end
......@@ -42,7 +48,4 @@ defmodule GrainTest do
end
end
test "the truth" do
assert 1 + 1 == 2
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment