Skip to content

Commit

Permalink
Removed channels from game struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bigardone committed May 18, 2016
1 parent 417f8e9 commit cd4177c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
18 changes: 5 additions & 13 deletions lib/battleship/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ defmodule Battleship.Game do
"""
use GenServer
require Logger
alias Battleship.{Game}
alias Battleship.Game.Board
alias Battleship.Game.Supervisor, as: GameSupervisor

defstruct [
id: nil,
attacker: nil,
defender: nil,
channels: [],
turns: [],
over: false,
winner: nil
Expand Down Expand Up @@ -75,22 +72,19 @@ defmodule Battleship.Game do
{:ok, board_pid} = create_board(player_id)
Process.monitor(board_pid)

game = game
|> add_player(player_id)
|> add_channel(pid)
game = add_player(game, player_id)

Battleship.Game.Event.player_joined

{:reply, {:ok, self}, game}
end
end

def handle_call(:get_data, _from, game), do: {:reply, %{game | channels: nil}, game}
def handle_call(:get_data, _from, game), do: {:reply, game, game}
def handle_call({:get_data, player_id}, _from, game) do
Logger.debug "Getting Game data for player #{player_id}"

game_data = game
|> Map.delete(:channels)
|> Map.put(:my_board, Board.get_data(player_id))

opponent_id = get_opponents_id(game, player_id)
Expand Down Expand Up @@ -122,9 +116,9 @@ defmodule Battleship.Game do
{:reply, {:ok, game}, game}
end

def get_opponents_id(%Game{attacker: player_id, defender: nil}, player_id), do: nil
def get_opponents_id(%Game{attacker: player_id, defender: defender}, player_id), do: defender
def get_opponents_id(%Game{attacker: attacker, defender: player_id}, player_id), do: attacker
def get_opponents_id(%__MODULE__{attacker: player_id, defender: nil}, player_id), do: nil
def get_opponents_id(%__MODULE__{attacker: player_id, defender: defender}, player_id), do: defender
def get_opponents_id(%__MODULE__{attacker: attacker, defender: player_id}, player_id), do: attacker

@doc """
Handles exit messages from linked game channels processes, destroying boards and
Expand Down Expand Up @@ -165,8 +159,6 @@ defmodule Battleship.Game do
defp add_player(%__MODULE__{attacker: nil} = game, player_id), do: %{game | attacker: player_id}
defp add_player(%__MODULE__{defender: nil} = game, player_id), do: %{game | defender: player_id}

defp add_channel(game, pid), do: %{game | channels: [pid | game.channels]}

defp destroy_board(nil), do: :ok
defp destroy_board(player_id), do: Board.destroy(player_id)

Expand Down
3 changes: 1 addition & 2 deletions web/channels/game_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ defmodule Battleship.GameChannel do

case Game.player_shot(game_id, player_id, x: x, y: y) do
{:ok, %Game{over: true} = game} ->
game = Map.delete(game, :channels)
broadcast(socket, "game:over", %{game: game})
{:noreply, socket}
{:ok, _game} ->
Expand All @@ -112,7 +111,7 @@ defmodule Battleship.GameChannel do

GameSupervisor.stop_game(game_id)

broadcast(socket, "game:over", %{game: %{game | channels: nil}})
broadcast(socket, "game:over", %{game: game})
broadcast(socket, "game:player_left", %{player_id: player_id})

:ok
Expand Down

0 comments on commit cd4177c

Please sign in to comment.