Set the Islands
Explore how to mark islands as set by players using Phoenix Channels to broadcast updates and handle replies. Learn to manage real-time state changes in an Elixir OTP backend, ensuring proper in-game communication and state tracking.
We'll cover the following...
Mark the islands as set
Once players are done moving their islands around, they need to mark their islands as a set in place. The Channel only needs to pass an atom that represents the player down to Game.set_islands/2 and pattern match on the result to send the right response.
A player successfully setting his islands is something we want both players to know about, so we use broadcast!/3 to respond when Game.set_islands/2 succeeds. If it fails, we want to let only that player know, so we use a :reply tuple.
To make it easier for any front-end code to display all the islands once they are set successfully, we will also have to send a :reply tuple with the full map of the islands. We send this only to the caller since we would not want the opponent to see this.
def handle_in("set_islands", player, socket) do
player = String.to_existing_atom(player)
case Game.set_islands(via(socket.topic), player) do
{:ok, board} ->
broadcast! socket, "player_set_islands", %{player: player}
{:reply, {:ok, %{board: board}}, socket}
_ -> {:reply, :error, socket}
end
end
Note: Run the app below and open the SPA link in two separate browser tabs. Run the following commands as instructed.
defmodule IslandsInterface.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
children = [
# Start the Telemetry supervisor
IslandsInterfaceWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: IslandsInterface.PubSub},
# Start the Endpoint (http/https)
IslandsInterfaceWeb.Endpoint
# Start a worker by calling: IslandsInterface.Worker.start_link(arg)
# {IslandsInterface.Worker, arg}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: IslandsInterface.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
IslandsInterfaceWeb.Endpoint.config_change(changed, removed)
:ok
end
endTrying it out
We need to do ...