Packages

A project to explore mars field using probes

Current section

Files

Jump to
mars_explorer lib explorations_controller.ex
Raw

lib/explorations_controller.ex

defmodule MarsExplorer.ExplorationsController do
@moduledoc """
Module responsible for managing multiple explorations state
"""
use Agent
defstruct grid: nil, running_exploration: nil, explorations: []
def start_link(_opts) do
Agent.start_link(fn -> %__MODULE__{} end, name: __MODULE__)
end
@doc """
Get the explorations grid
"""
@spec get_grid() :: MarsExplorer.HighlandGrid.t()
def get_grid() do
Agent.get(__MODULE__, &Map.get(&1, :grid))
end
@doc """
Set the explorations grid
"""
@spec set_grid(MarsExplorer.HighlandGrid.t()) :: :ok
def set_grid(grid) do
Agent.update(__MODULE__, &Map.put(&1, :grid, grid))
end
@doc """
Set the running exploration
"""
@spec set_running_exploration(MarsExplorer.Exploration.t()) :: :ok
def set_running_exploration(exploration) do
running_exploration = get_running_exploration()
if running_exploration do
add_exploration_to_explorations_list(running_exploration)
end
update_running_exploration(exploration)
end
@doc """
Add a exploration to the concluded explorations list
"""
@spec add_exploration_to_explorations_list(MarsExplorer.Exploration.t()) :: :ok
def add_exploration_to_explorations_list(exploration) do
Agent.update(
__MODULE__,
&Map.update(&1, :explorations, [], fn explorations ->
explorations ++ [exploration]
end)
)
end
@doc """
Cleans the concluded explorations list
"""
def clean_explorations() do
Agent.update(__MODULE__, &Map.put(&1, :explorations, []))
end
@doc """
Get the running exploration
"""
@spec get_running_exploration() :: MarsExplorer.Exploration.t()
def get_running_exploration() do
Agent.get(__MODULE__, &Map.get(&1, :running_exploration))
end
@doc """
Updates the running exploration
"""
@spec update_running_exploration(MarsExplorer.Exploration.t()) :: :ok
def update_running_exploration(new_exploration) do
Agent.update(__MODULE__, &Map.put(&1, :running_exploration, new_exploration))
end
@doc """
Get the concluded explorations list
"""
@spec get_explorations() :: [MarsExplorer.Exploration.t()]
def get_explorations() do
Agent.get(__MODULE__, &Map.get(&1, :explorations))
end
end