Packages

An implementation of the Perudo / Dudo / Pirate's Dice game written in Elixir.

Current section

Files

Jump to
perudox lib perudox player.ex
Raw

lib/perudox/player.ex

defmodule Perudox.Player do
@moduledoc """
The Perudo.Player module is in charge of holding hand values for a player.
"""
use GenServer
alias Perudox.{Player, Player.State}
@type t :: pid
# Public API.
@spec new(String.t) :: Player.t
def new(nickname) do
with {:ok, player} <- start_link %State{nickname: nickname} do
player
end
end
@spec start_link(State.t) :: {:ok, Player.t}
def start_link(state) do
GenServer.start_link __MODULE__, state
end
@spec state(Player.t) :: {:ok, State.t}
def state(player), do: GenServer.call player, :state
@spec occurences(Player.t) :: State.occurences
def occurences(player), do: GenServer.call player, :occurences
@spec roll(Player.t) :: :ok
def roll(player), do: GenServer.call player, :roll
@spec set_hand(Player.t, State.hand) :: :ok
def set_hand(player, hand), do: GenServer.call player, {:set_hand, hand}
@spec win_a_dice(Player.t) :: :ok
def win_a_dice(player), do: GenServer.call player, :win_a_dice
@spec lose_a_dice(Player.t) :: :ok
def lose_a_dice(player), do: GenServer.call player, :lose_a_dice
# GenServer callback.
@spec init(State.t) :: {:ok, State.t}
def init(state), do: {:ok, state}
def handle_call(:state, _from, state) do
{:reply, {:ok, state}, state}
end
def handle_call(:occurences, _from, state) do
{:reply, {:ok, State.occurences state}, state}
end
def handle_call(:roll, _from, state) do
state = state |> State.roll
{:reply, {:ok, state}, state}
end
def handle_call({:set_hand, hand}, _hand, state) do
{:reply, :ok, %{state | hand: hand}}
end
def handle_call(:win_a_dice, _from, state) do
state = state |> State.win_a_dice
{:reply, {:ok, state}, state}
end
def handle_call(:lose_a_dice, _from, state) do
state = state |> State.lose_a_dice
{:reply, {:ok, state}, state}
end
end