class GamesController < ApplicationController

  def index
    @game = current_user.games.last
  end

  def new
    @game = Game.new
  end

  def create
    @game = current_user.games.create
    SetupNewGame.new(params[:ships], @game).run!
    render js: "window.location = '/games/#{@game.id}'"
  end

  def show
    @opponent_board = Game.find(params[:id]).boards.where(opponent?: true)[0]
    @user_board = Game.find(params[:id]).boards.where(opponent?: false)[0]
    gon.jbuilder
  end

  def move
    game = Game.find(params[:game_id])
    @opponent_board = game.boards.where(opponent?: true)[0]
    MoveLogger.new(params[:move], @opponent_board).log!
    @user_board = game.boards.where(opponent?: false)[0]
    @comp_move = CompAI.new(@user_board).new_move
    MoveLogger.new(@comp_move, @user_board).log!
  end

  def load_game
    # this action exists due to a loading issue with the gon gem
    @game = current_user.games.last
    redirect_to @game
  end

end

class GamesController < ApplicationController
  before_action :set_game, only: [:show, :move, :won, :lost]
  before_action :set_last_game, only: [:index, :load_game]
  before_filter :authenticate_user!

  def index
  end

  def new
    @game = Game.new
  end

  def create
    @game = current_user.games.create
    SetupNewGame.new(params[:ships], @game).run!
    render js: "window.location = '/games/#{@game.id}'"
  end

  def show
    @opponent_board = @game.boards.where(opponent?: true)[0]
    @user_board = @game.boards.where(opponent?: false)[0]
    gon.jbuilder
  end

  def move
    @opponent_board = @game.boards.where(opponent?: true)[0]
    MoveLogger.new(params[:move], @opponent_board).log!
    @user_board = @game.boards.where(opponent?: false)[0]
    @comp_move = CompAI.new(@user_board).new_move
    MoveLogger.new(@comp_move, @user_board).log!
  end

  def load_game # resolves load issue with gon gem
    redirect_to @game
  end

  def won
  end

  def lost
  end

  private

  def set_game
    @game = !!params[:id] ? Game.find(params[:id]) : Game.find(params[:game_id])
  end

  def set_last_game
    @game = current_user.games.last
  end

end

Add before actions and filter; Add won/lost actions
Add before actions and filter; Add won/lost actions
