Add multiple drone functionalities

This commit is contained in:
Liru 2019-05-06 15:35:12 -04:00
parent eb9bf1980a
commit 213587a889
4 changed files with 89 additions and 3 deletions

View File

@ -16,6 +16,12 @@ defmodule Aife.Drone do
""" """
@moduledoc since: "0.1.0" @moduledoc since: "0.1.0"
alias Aife.Drone
use GenServer
defstruct enabled?: false, human: %{traits: %{}}, drone: %{traits: %{}}, values: %{}
@doc """ @doc """
Sends a command to a drone. Sends a command to a drone.
@ -23,6 +29,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def command(drone, cmd) do def command(drone, cmd) do
GenServer.cast(drone, {:command, cmd})
end end
@doc """ @doc """
@ -34,6 +41,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def enable(drone) do def enable(drone) do
GenServer.cast(drone, :enable)
end end
@doc """ @doc """
@ -43,6 +51,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def disable(drone) do def disable(drone) do
GenServer.cast(drone, :disable)
end end
@doc """ @doc """
@ -50,6 +59,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def traits(drone) do def traits(drone) do
GenServer.call(drone, :traits)
end end
@doc """ @doc """
@ -69,6 +79,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def alter_traits(drone, form \\ :current, values \\ []) do def alter_traits(drone, form \\ :current, values \\ []) do
GenServer.cast(drone, {:alter, form, values})
end end
@doc """ @doc """
@ -82,10 +93,11 @@ defmodule Aife.Drone do
Aife.Drone.get_values(drone, :fetishes) Aife.Drone.get_values(drone, :fetishes)
#=> [fetishes: [:maid, :latex, :hypnosis]] #=> [fetishes: [:maid, :latex, :hypnosis]]
Aife.Drone.insert_value(drone, :fetishes, :haigure) Aife.Drone.insert_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :haigure, :hypnosis]]} #=> [fetishes: [:maid, :latex, :haigure, :hypnosis]]
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def insert_value(drone, key, value) do def insert_value(drone, key, value) do
GenServer.call(drone, {:insert_value, key, value})
end end
@doc """ @doc """
@ -101,12 +113,13 @@ defmodule Aife.Drone do
Aife.Drone.get_values(drone, :fetishes) Aife.Drone.get_values(drone, :fetishes)
#=> [fetishes: [:maid, :latex, :haigure, :hypnosis]] #=> [fetishes: [:maid, :latex, :haigure, :hypnosis]]
Aife.Drone.remove_value(drone, :fetishes, :haigure) Aife.Drone.remove_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :hypnosis]]} #=> [fetishes: [:maid, :latex, :hypnosis]]
Aife.Drone.remove_value(drone, :fetishes, :haigure) Aife.Drone.remove_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :hypnosis]]} #=> [fetishes: [:maid, :latex, :hypnosis]]
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def remove_value(drone, key, value) do def remove_value(drone, key, value) do
GenServer.call(drone, {:remove_value, key, value})
end end
@doc """ @doc """
@ -116,6 +129,7 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def say(drone, text) do def say(drone, text) do
GenServer.cast(drone, {:say, text})
end end
@doc """ @doc """
@ -132,5 +146,45 @@ defmodule Aife.Drone do
""" """
@doc since: "0.1.0" @doc since: "0.1.0"
def think(drone, thought) do def think(drone, thought) do
GenServer.cast(drone, {:think, thought})
end end
## GenServer calls.
def start_link() do
GenServer.start_link(__MODULE__, [])
end
@impl GenServer
def init(_) do
{:ok, %Drone{}}
end
@impl GenServer
def handle_call(:traits, _from, state) do
form =
case state.enabled? do
true -> :drone
false -> :human
end
{:reply, state[form].traits, state}
end
def handle_call({:insert_value, key, value}, _from, state) do
# TODO
{:reply, state, state}
end
def handle_call({:remove_value, key, value}, _from, state) do
# TODO
{:reply, state, state}
end
@impl GenServer
def handle_cast({:command, _cmd}, state), do: {:noreply, state}
def handle_cast(:disable, state), do: {:noreply, %{state | enabled?: false}}
def handle_cast(:enable, state), do: {:noreply, %{state | enabled?: true}}
def handle_cast({:say, _text}, state), do: {:noreply, state}
def handle_cast({:think, _thought}, state), do: {:noreply, state}
end end

View File

@ -0,0 +1,12 @@
defmodule Aife.Drone.DynamicSupervisor do
use DynamicSupervisor
def start_link(_) do
DynamicSupervisor.start_link(__MODULE__, nil, name: __MODULE__)
end
@impl DynamicSupervisor
def init(_init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end
end

View File

@ -0,0 +1,16 @@
defmodule Aife.Drone.Supervisor do
use Supervisor
def start_link(_) do
Supervisor.start_link(__MODULE__, [])
end
def init(_) do
children = [
{Registry, name: Aife.Drone.Registry, keys: :unique},
{DynamicSupervisor, name: Aife.Drone.DynamicSupervisor, strategy: :one_for_one}
]
Supervisor.init(children, strategy: :one_for_one)
end
end

4
test/aife/drone_test.exs Normal file
View File

@ -0,0 +1,4 @@
defmodule Aife.DroneTest do
use ExUnit.Case
doctest Aife.Drone
end