Add a Second Player
Learn how to add a second player.
We'll cover the following...
Add another player
To add a second player, we just need to pass that player’s name to the Game.add_player/2 function along with the via tuple that maps to the game PID.
We want both players to know about successfully adding a second player. However, if we fail to add the second player, only that player needs to know. This is a case where we can use broadcast!/3 on success and :reply tuple if something goes wrong.
Fortunately, we have an easy way to derive the via tuple and address the right game process. The socket.topic will always be a string that begins with “game:” and ends with the first player’s name. We can do a little binary pattern matching to extract just the name. We only need this player’s name to get the via tuple ...