MafiaEngine.Players (mafia_engine v0.1.1)
This module defines the type for a player list and functions to handle it.
Examples
iex> p = MafiaEngine.Players.new()
[]
iex> {:ok, p} = MafiaEngine.Players.add(p, "Abed")
{:ok, [%MafiaEngine.Player{alive: true, name: "Abed", role: :unknown}]}
iex> {:ok, p} = MafiaEngine.Players.add(p, "Jeff")
{:ok,
[%MafiaEngine.Player{alive: true, name: "Jeff", role: :unknown},
%MafiaEngine.Player{alive: true, name: "Abed", role: :unknown}]
}
iex> MafiaEngine.Players.names(p)
["Jeff", "Abed"]
iex> p = MafiaEngine.Players.remove(p, "Abed")
[%MafiaEngine.Player{alive: true, name: "Jeff", role: :unknown}]
iex> MafiaEngine.Players.set_roles(p, [:townie])
[%MafiaEngine.Player{alive: true, name: "Jeff", role: :townie}]
Link to this section Summary
Functions
Adds a new player with the given name
unless name
is taken.
Returns the player with the given name
from the list.
Sets the player with the given name
alive field to false
.
Returns a list with the player names.
Creates a new player list.
Removes the player with the given name
from the list if exists.
Sets the player with the given name
role to role
.
Gives a role from role_list
at random to each player.
Link to this section Types
Specs
t() :: [MafiaEngine.Player.t()]
Link to this section Functions
add(players, name)
Specs
Adds a new player with the given name
unless name
is taken.
Examples
iex> p = MafiaEngine.Players.new()
[]
iex> {:ok, p} = MafiaEngine.Players.add(p, "Abed")
{:ok, [%MafiaEngine.Player{alive: true, name: "Abed", role: :unknown}]}
iex> MafiaEngine.Players.add(p, "Abed")
{:error, :name_already_taken}
get(players, name)
Specs
get(t(), String.t()) :: MafiaEngine.Player.t() | :none
Returns the player with the given name
from the list.
If the player does not exist it returns none instead.
kill(players, name)
Specs
Sets the player with the given name
alive field to false
.
names(players)
Specs
Returns a list with the player names.
new()
Specs
new() :: t()
Creates a new player list.
remove(players, name)
Specs
Removes the player with the given name
from the list if exists.
set_role(players, name, role)
Specs
set_role(t(), String.t(), MafiaEngine.Role.t()) :: t()
Sets the player with the given name
role to role
.
set_roles(players, role_list)
Specs
set_roles(t(), [MafiaEngine.Role.t()]) :: t()
Gives a role from role_list
at random to each player.
The role_list
should have the same lenght as the player list.