Skip to content

Commit

Permalink
Merge branch 'feature/supervisor'
Browse files Browse the repository at this point in the history
  • Loading branch information
jrowah committed Nov 4, 2024
2 parents 57e84ee + 7b8f5be commit 0072f14
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/battle_ship/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule BattleShip.Application do
children = [
# Starts a worker by calling: BattleShip.Worker.start_link(arg)
# {BattleShip.Worker, arg}
{Registry, keys: :unique, name: Registry.Game}
{Registry, keys: :unique, name: Registry.Game},
{BattleShip.GameSupervisor, []}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
14 changes: 10 additions & 4 deletions lib/battle_ship/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ defmodule BattleShip.Game do

use GenServer,
start: {__MODULE__, :start_link, []},
restart: :transient,
restart: :permanent,
shutdown: 5000,
type: :worker

alias __MODULE__
alias BattleShip.Board
alias BattleShip.Coordinate
alias BattleShip.Guesses
alias BattleShip.Rules
alias BattleShip.Ship

@players [:player1, :player2]
@timeout 60 * 60 * 24 * 1000

@spec via_tuple(String.t()) :: {:via, Registry, {Registry.Game, String.t()}}
def via_tuple(name), do: {:via, Registry, {Registry.Game, name}}

@spec start_link(binary()) :: {:ok, pid()} | :ignore
def start_link(name) when is_binary(name) do
case GenServer.start_link(__MODULE__, name, name: via_tuple(name)) do
case GenServer.start_link(Game, name, name: via_tuple(name)) do
{:ok, pid} ->
{:ok, pid}

Expand All @@ -39,7 +41,7 @@ defmodule BattleShip.Game do
def init(name) do
player1 = %{name: name, board: Board.new(), guesses: Guesses.new()}
player2 = %{name: nil, board: Board.new(), guesses: Guesses.new()}
{:ok, %{player1: player1, player2: player2, rules: %Rules{}}}
{:ok, %{player1: player1, player2: player2, rules: %Rules{}}, @timeout}
end

@spec add_player(pid(), binary()) :: :error | {:reply, :ok, %{}}
Expand Down Expand Up @@ -135,11 +137,15 @@ defmodule BattleShip.Game do
end
end

def handle_info(:timeout, state_data) do
{:stop, {:shutdown, :timeout}, state_data}
end

defp update_player2_name(state_data, name), do: put_in(state_data.player2.name, name)

defp update_rules(state_data, rules), do: %{state_data | rules: rules}

defp reply_success(state_data, reply), do: {:reply, reply, state_data}
defp reply_success(state_data, reply), do: {:reply, reply, state_data, @timeout}

defp update_board(state_data, player, board),
do: Map.update!(state_data, player, fn player -> %{player | board: board} end)
Expand Down
23 changes: 23 additions & 0 deletions lib/battle_ship/game_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule BattleShip.GameSupervisor do
@moduledoc false
use Supervisor

alias __MODULE__
alias BattleShip.Game

def start_link(_options), do: Supervisor.start_link(GameSupervisor, :ok, name: GameSupervisor)

def init(:ok), do: Supervisor.init([Game], strategy: :simple_one_for_one)

def start_game(name), do: Supervisor.start_child(GameSupervisor, [name])

def stop_game(name) do
Supervisor.terminate_child(GameSupervisor, pid_from_name(name))
end

defp pid_from_name(name) do
name
|> Game.via_tuple()
|> GenServer.whereis()
end
end

0 comments on commit 0072f14

Please sign in to comment.