Add a New Player

Let's learn how to add a new player.

We'll cover the following

Add the second player

Each game needs to add a second player in order to begin play. This really amounts to assigning a value to the :name key for the second player.

We’ll begin with a public client function, add_player/2. This takes the PID of the game process and the second player’s name. We’ll have add_player/2 wrap GenServer.call/2 so it will be synchronous. Then, we need a new clause of handle_call/3 to define the new behavior:

def add_player(game, name) when is_binary(name), do:
  GenServer.call(game, {:add_player, name})

Get hands-on with 1400+ tech skills courses.