<commit_before>class ActiveHandsController < ApplicationController

  #vars that can be taken out of instance (for heavy class warning)
  # @peg_stack, @ai_played, @peg_card, 4 can_play_(round) vars ???? @sum, @peg_index, ...

  def hand
    return if not retrieve_active_hand?
    gon.watch.rabl template: 'app/views/active_hands/hand.rabl', as: 'hand'
  end

  def throw

    #locate ActiveHand model if it exists
    return if not retrieve_active_hand?

    #read player throw decision (false if form filled out incorrectly)
    unless read_throw_choice?
      redirect_to play_path, notice: 'Invalid choice, please select two cards to pass to the crib.'
      return
    end

    process_throw_choice

  end

  def peg

    #locate ActiveHand model if it exists
    return if not retrieve_active_hand?

    return if not  read_peg_choice?

    process_peg_choice

  end

  private

  def read_peg_choice?

    #make sure form is valid
    @peg_index = params[:pegs].first.to_i

    return false unless @peg_index

    #initialize temporary/view variables
    @sum = @hand.peg_sum
    @peg_stack = @hand.current_peg_stack
    @player_peg, @ai_peg = @hand.peg_hands
    @peg_card = @player_peg[@peg_index]

    #make sure card choice is valid
    if @sum + @peg_card.value > 31 #TODO: Only show Radio Buttons for valid moves?
      return false#Please pick a card to peg such that the sum <= 31.",
    end

    return true

  end

  def process_peg_choice

    process_player_play
    determine_player_peg_states
    check_if_player_ended_round

    process_ai_round

    if !@player_can_play && @ai_can_play
      #AI has already played this round, and since the user cant play anymore, the AI needs to play the rest of his cards
      play_rest_of_ai_hand

    elsif !@player_can_play && !@ai_can_play
      end_pegging
    end

  end

  def check_if_player_ended_round

    if @sum == 31 || (!@ai_can_play_round && !@player_can_play_round)
      end_peg_round
      @ai_can_play_round = @ai_peg.any?
      @player_can_play_round = @player_peg.any?
    end

  end

  def determine_player_peg_states

    @player_can_play, @player_can_play_round = can_play(@sum, @player_peg)
    @ai_can_play,@ai_can_play_round = can_play(@sum, @ai_peg)

  end

  #Will play as once unless player gives a go, then it will play until the end of the round
  def process_ai_round

    @ai_played = 0
    while @ai_can_play_round
      @ai_played += 1
      process_ai_play
      @ai_can_play = @ai_peg.any?
      @player_can_play_round = can_play_round?(@sum, @player_peg) if @player_can_play_round
      if @player_can_play_round || !@ai_can_play
        @ai_can_play_round = false  #Ai can only play once if the player can still play
      else
        @ai_can_play_round = can_play_round?(@sum, @ai_peg)   #Check to see if Ai can continue to go, if so, keep processing his plays

      end
    end<commit_msg>oops, last change broke pegging when ai ends a round<commit_after>class ActiveHandsController < ApplicationController

  #vars that can be taken out of instance (for heavy class warning)
  # @peg_stack, @ai_played, @peg_card, 4 can_play_(round) vars ???? @sum, @peg_index, ...

  def hand
    return if not retrieve_active_hand?
    gon.watch.rabl template: 'app/views/active_hands/hand.rabl', as: 'hand'
  end

  def throw

    #locate ActiveHand model if it exists
    return if not retrieve_active_hand?

    #read player throw decision (false if form filled out incorrectly)
    unless read_throw_choice?
      redirect_to play_path, notice: 'Invalid choice, please select two cards to pass to the crib.'
      return
    end

    process_throw_choice

  end

  def peg

    #locate ActiveHand model if it exists
    return if not retrieve_active_hand?

    return if not  read_peg_choice?

    process_peg_choice

  end

  private

  def read_peg_choice?

    #make sure form is valid
    @peg_index = params[:pegs].first.to_i

    return false unless @peg_index

    #initialize temporary/view variables
    @sum = @hand.peg_sum
    @peg_stack = @hand.current_peg_stack
    @player_peg, @ai_peg = @hand.peg_hands
    @peg_card = @player_peg[@peg_index]

    #make sure card choice is valid
    if @sum + @peg_card.value > 31 #TODO: Only show Radio Buttons for valid moves?
      return false#Please pick a card to peg such that the sum <= 31.",
    end

    return true

  end

  def process_peg_choice

    process_player_play
    determine_player_peg_states
    check_if_player_ended_round

    process_ai_round

    if @player_can_play_round
      return
    end


    if !@player_can_play && @ai_can_play
      #AI has already played this round, and since the user cant play anymore, the AI needs to play the rest of his cards
      play_rest_of_ai_hand

    elsif !@player_can_play && !@ai_can_play
      end_pegging
    end

  end

  def check_if_player_ended_round

    if @sum == 31 || (!@ai_can_play_round && !@player_can_play_round)
      end_peg_round
      @ai_can_play_round = @ai_peg.any?
      @player_can_play_round = @player_peg.any?
    end

  end

  def determine_player_peg_states

    @player_can_play, @player_can_play_round = can_play(@sum, @player_peg)
    @ai_can_play,@ai_can_play_round = can_play(@sum, @ai_peg)

  end

  #Will play as once unless player gives a go, then it will play until the end of the round
  def process_ai_round

    @ai_played = 0
    while @ai_can_play_round
      @ai_played += 1
      process_ai_play
      @ai_can_play = @ai_peg.any?
      @player_can_play_round = can_play_round?(@sum, @player_peg) if @player_can_play_round
      if @player_can_play_round || !@ai_can_play
        @ai_can_play_round = false  #Ai can only play once if the player can still play
      else
        @ai_can_play_round = can_play_round?(@sum, @ai_peg)   #Check to see if Ai can continue to go, if so, keep processing his plays

      end
    end

    #if the AI played and ended a round, then let him score a point and reset the model!
    if @ai_played > 0 && !@player_can_play_round
      end_peg_round
    end
{'fraction_non_alphanumeric': 0.06759213353164766, 'fraction_numerical': 0.0031399768633283754, 'mean_word_length': 3.223307745987439, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '357607', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1960, 'n_words': 616}