lab 04
This commit is contained in:
18 files changed
+614
No files matched your search
@@ -0,0 +1,4 @@
|
||||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
||||
@@ -0,0 +1,28 @@
|
||||
# The directory Mix will write compiled artifacts to.
|
||||
/_build/
|
||||
|
||||
# If you run "mix test --cover", coverage assets end up here.
|
||||
/cover/
|
||||
|
||||
# The directory Mix downloads your dependencies sources to.
|
||||
/deps/
|
||||
|
||||
# Where third-party dependencies like ExDoc output generated docs.
|
||||
/doc/
|
||||
|
||||
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||
/.fetch
|
||||
|
||||
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||
erl_crash.dump
|
||||
|
||||
# Also ignore archive artifacts (built via "mix archive.build").
|
||||
*.ez
|
||||
|
||||
# Ignore package tarball (built via "mix hex.build").
|
||||
card-*.tar
|
||||
|
||||
# Temporary files, for example, from tests.
|
||||
/tmp/
|
||||
|
||||
cards.db
|
||||
@@ -0,0 +1,21 @@
|
||||
# Card
|
||||
|
||||
**TODO: Add description**
|
||||
|
||||
## Installation
|
||||
|
||||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||
by adding `card` to your list of dependencies in `mix.exs`:
|
||||
|
||||
```elixir
|
||||
def deps do
|
||||
[
|
||||
{:card, "~> 0.1.0"}
|
||||
]
|
||||
end
|
||||
```
|
||||
|
||||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||
be found at <https://hexdocs.pm/card>.
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,29 @@
|
||||
defmodule Card.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :card,
|
||||
version: "0.1.0",
|
||||
elixir: "~> 1.17",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps()
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help compile.app" to learn about applications.
|
||||
def application do
|
||||
[
|
||||
extra_applications: [:logger],
|
||||
mod: {Card.Application, []}
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
ExUnit.start()
|
||||
Reference in new issue
Block a user