Pokex v0.2.0 Table View Source

The Table struct and functions to manipulate the spots.

A struct of the players seats. There are two to six spots in this version of the poker game for players to “seat”.

Each table contains spot_one, spot_two, spot_three, spot_four, spot_five, spot_six.

At each spot only a %Player{} struct can be placed.

It cannot have less than two spots filled.

Link to this section Summary

Functions

Add a player at a certain spot

Invoked in order to access the value stored under key in the given term term

Returns the filled spot names only, in order

Invoked in order to access the value stored under key in the given term term, defaulting to default if not present

Invoked in order to access the value under key and update it at the same time

Initializes players to consecutive spots as given in list

Invoked to “pop” the value under key out of the given data structure

Returns the number of filled spots in the Table

Checks if the Table has an empty free spot at the place specified

Applies a function on every spot of the Table (Player) and returns a list of tuples containing the spot name and the function result

Returns the spot names of the Table struct in order

Link to this section Functions

Link to this function add_player!(table, spot, player) View Source

Add a player at a certain spot

The spot must be empty or it raises an exception.

Invoked in order to access the value stored under key in the given term term.

This function should return {:ok, value} where value is the value under key if the key exists in the term, or :error if the key does not exist in the term.

Many of the functions defined in the Access module internally call this function. This function is also used when the square-brackets access syntax (structure[key]) is used: the fetch/2 callback implemented by the module that defines the structure struct is invoked and if it returns {:ok, value} then value is returned, or if it returns :error then nil is returned.

See the Map.fetch/2 and Keyword.fetch/2 implementations for examples of how to implement this callback.

Callback implementation for Access.fetch/2.

Returns the filled spot names only, in order

Examples

iex> Table.filled_spots(%Table{})
[]

iex> Table.filled_spots(%Table{
...>       spot_one: %Player{name: "Vicky"},
...>       spot_two: nil,
...>       spot_three: nil,
...>       spot_four: %Player{name: "Paul", wallet: 100},
...>       spot_five: nil,
...>       spot_six: nil
...>     })
[:spot_one, :spot_four]
Link to this function get(table, key, default) View Source

Invoked in order to access the value stored under key in the given term term, defaulting to default if not present.

This function should return the value under key in term if there’s such key, otherwise default.

For most data structures, this can be implemented using fetch/2 internally; for example:

def get(structure, key, default) do
  case fetch(structure, key) do
    {:ok, value} -> value
    :error       -> default
  end
end

See the Map.get/3 and Keyword.get/3 implementations for examples of how to implement this callback.

Callback implementation for Access.get/3.

Link to this function get_and_update(table, key, func) View Source

Invoked in order to access the value under key and update it at the same time.

The implementation of this callback should invoke fun with the value under key in the passed structure data, or with nil if key is not present in it. This function must return either {get_value, update_value} or :pop.

If the passed function returns {get_value, update_value}, the return value of this callback should be {get_value, new_data}, where:

  • get_value is the retrieved value (which can be operated on before being returned)
  • update_value is the new value to be stored under key
  • new_data is data after updating the value of key with update_value.

If the passed function returns :pop, the return value of this callback must be {value, new_data} where value is the value under key (or nil if not present) and new_data is data without key.

See the implementations of Map.get_and_update/3 or Keyword.get_and_update/3 for more examples.

Callback implementation for Access.get_and_update/3.

Link to this function init_players(table, player_list) View Source

Initializes players to consecutive spots as given in list.

Because the table cannot contain less than two players the list cannot contain less than two players.

Invoked to “pop” the value under key out of the given data structure.

When key exists in the given structure data, the implementation should return a {value, new_data} tuple where value is the value that was under key and new_data is term without key.

When key is not present in the given structure, a tuple {value, data} should be returned, where value is implementation-defined.

See the implementations for Map.pop/3 or Keyword.pop/3 for more examples.

Callback implementation for Access.pop/2.

Returns the number of filled spots in the Table.

The actual size is always 6 spots

Link to this function spot_empty?(table, spot) View Source

Checks if the Table has an empty free spot at the place specified

Applies a function on every spot of the Table (Player) and returns a list of tuples containing the spot name and the function result.

## Examples

#iex> Table.spotify(%Table{}, )
#[:spot_one, :spot_two, :spot_three, :spot_four, :spot_five, :spot_six]

Returns the spot names of the Table struct in order

Examples

iex> Table.spots
[:spot_one, :spot_two, :spot_three, :spot_four, :spot_five, :spot_six]