lab 04
This commit is contained in:
18 files changed
+614
No files matched your search
@@ -0,0 +1,16 @@
|
||||
defmodule Card.Application do
|
||||
@moduledoc false
|
||||
|
||||
use Application
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
{Card.Store, "cards.db"},
|
||||
Card.Worker
|
||||
]
|
||||
|
||||
opts = [strategy: :one_for_one, name: Card.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,39 @@
|
||||
defmodule Card.Store do
|
||||
use GenServer
|
||||
|
||||
# client
|
||||
def start_link(file) do
|
||||
GenServer.start_link(__MODULE__, file, name: __MODULE__)
|
||||
end
|
||||
|
||||
def put(value) do
|
||||
GenServer.call(__MODULE__, {:put, value})
|
||||
end
|
||||
|
||||
def get() do
|
||||
GenServer.call(__MODULE__, :get)
|
||||
end
|
||||
|
||||
# server
|
||||
@impl true
|
||||
def init(file) do
|
||||
{:ok, file}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:get, _from, file) do
|
||||
reply =
|
||||
case File.read(file) do
|
||||
{:ok, content} -> :erlang.binary_to_term(content)
|
||||
_ -> nil
|
||||
end
|
||||
|
||||
{:reply, reply, file}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call({:put, value}, _from, file) do
|
||||
reply = File.write(file, :erlang.term_to_binary(value))
|
||||
{:reply, reply, file}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,79 @@
|
||||
defmodule Card.Worker do
|
||||
use GenServer
|
||||
|
||||
### Client API ###
|
||||
def start_link(_arg \\ nil) do
|
||||
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
|
||||
end
|
||||
|
||||
def new() do
|
||||
GenServer.cast(__MODULE__, :new)
|
||||
end
|
||||
|
||||
def shuffle() do
|
||||
GenServer.cast(__MODULE__, :shuffle)
|
||||
end
|
||||
|
||||
def count() do
|
||||
GenServer.call(__MODULE__, :count)
|
||||
end
|
||||
|
||||
def deal(n \\ 1) do
|
||||
GenServer.call(__MODULE__, {:deal, n})
|
||||
end
|
||||
|
||||
### Server Impl ###
|
||||
defp fresh() do
|
||||
values = Enum.map(2..10, &Integer.to_string/1) ++ ["J", "Q", "K", "A"]
|
||||
suits = ["♣", "♦", "♥", "♠"]
|
||||
|
||||
for v <- values, f <- suits, do: v <> f
|
||||
end
|
||||
|
||||
defp save(state) do
|
||||
Card.Store.put(state)
|
||||
state
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_arg) do
|
||||
IO.puts("Card.Worker started/re-started")
|
||||
{:ok, Card.Store.get() || save(fresh())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast(:new, _state) do
|
||||
# {:noreply, new_state}
|
||||
{:noreply, save(fresh())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cast(:shuffle, state) do
|
||||
# {:noreply, new_state}
|
||||
{:noreply, save(Enum.shuffle(state))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:count, _from, state) do
|
||||
# {:reply, actual_reply, new_state}
|
||||
{:reply, length(state), state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call({:deal, n}, _from, state) when is_integer(n) do
|
||||
case n do
|
||||
_ when n < 0 ->
|
||||
# {:reply, actual_reply, new_state}
|
||||
{:reply, {:error, "Cannot deal negative number of cards"}, state}
|
||||
|
||||
_ when n > length(state) ->
|
||||
# {:reply, actual_reply, new_state}
|
||||
{:reply, {:error, "Insufficient cards in deck to deal"}, state}
|
||||
|
||||
_ ->
|
||||
{d, r} = Enum.split(state, n)
|
||||
# {:reply, actual_reply, new_state}
|
||||
{:reply, {:ok, d}, save(r)}
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user