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

Link to this section Functions

Link to this function

add(players, name)

Specs

add(t(), String.t()) :: {:ok, t()} | {:error, :name_already_taken}

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}
Link to this function

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.

Link to this function

kill(players, name)

Specs

kill(t(), String.t()) :: t()

Sets the player with the given name alive field to false.

Specs

names(t()) :: [String.t()]

Returns a list with the player names.

Specs

new() :: t()

Creates a new player list.

Link to this function

remove(players, name)

Specs

remove(t(), String.t()) :: t()

Removes the player with the given name from the list if exists.

Link to this function

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.

Link to this function

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.