<commit_before>class GamesController < ApplicationController
  include SessionsHelper
  before_action :authenticate_user!
  before_action :set_game

  def create
    @game = Game.new(game_params)
    @game.creator = session_user
    if @game.save
      @game.participants << @game.creator
      redirect_to @game
    else
      redirect_to root_path, notice: "'#{@game.name}' has already been created!"
    end
  end

  def show
    @game_state = broadcast_game
    respond_to do |format|
      format.html {
        render :live
      }
      format.json {
        render json: @game_state
      }
     end
  end

  def join
    return redirect_to root_path, notice: "'#{params[:name]}' does not exist" unless @game
    return redirect_to game_path(@game) if @game.participants.include?(current_user)
    return redirect_to root_path, notice: "'#{params[:name]}' is in progress" unless @game.teams.empty?
    if !@game.participants.include?(current_user)
      @game.participants << current_user
    end
    redirect_to game_path(@game)
  end

  def start
    return flash[:notice] = "you are NOT the creator of '#{@game.name}'" unless @game.creator == current_user
    CreateRandomTeams.new(@game).call if @game.teams.empty?
    redirect_to @game
  end

  def start_round
    @game.reset_cards
    next_turn
   end

  def pass
    PassCard.new(@game).call
    show
  end

  def win_card
    WinCard.new(@game).call
    if @game.bowl_empty?
      @game.current_round.finish
      return start_round unless @game.is_over?
      return show
    end
    pass
  end

  def next_turn
    StartNextTurn.new(@game).call
    show
  end

  def pause
  end

  private

  def broadcast_game
    state = @game.full_state
    ActionCable.server.broadcast( "game_#{params['name']}", { action: :updateGame, gameState: state })
    if !@game.turns.empty? && !@game.is_over?
      ActionCable.server.broadcast( "game_#{params['name']}", { action: :setTimer, gameName: @game.name })
    end
    state
  end

  def game_params
    params.require(:game).permit(:name)
  end

  def set_game
    @game = Game.find_by(name: params[:name])
  end

end
<commit_msg>broadcast full gamestate for setTimer<commit_after>class GamesController < ApplicationController
  include SessionsHelper
  before_action :authenticate_user!
  before_action :set_game

  def create
    @game = Game.new(game_params)
    @game.creator = session_user
    if @game.save
      @game.participants << @game.creator
      redirect_to @game
    else
      redirect_to root_path, notice: "'#{@game.name}' has already been created!"
    end
  end

  def show
    @game_state = broadcast_game
    respond_to do |format|
      format.html {
        render :live
      }
      format.json {
        render json: @game_state
      }
     end
  end

  def join
    return redirect_to root_path, notice: "'#{params[:name]}' does not exist" unless @game
    return redirect_to game_path(@game) if @game.participants.include?(current_user)
    return redirect_to root_path, notice: "'#{params[:name]}' is in progress" unless @game.teams.empty?
    if !@game.participants.include?(current_user)
      @game.participants << current_user
    end
    redirect_to game_path(@game)
  end

  def start
    return flash[:notice] = "you are NOT the creator of '#{@game.name}'" unless @game.creator == current_user
    CreateRandomTeams.new(@game).call if @game.teams.empty?
    redirect_to @game
  end

  def start_round
    @game.reset_cards
    next_turn
   end

  def pass
    PassCard.new(@game).call
    show
  end

  def win_card
    WinCard.new(@game).call
    if @game.bowl_empty?
      @game.current_round.finish
      return start_round unless @game.is_over?
      return show
    end
    pass
  end

  def next_turn
    StartNextTurn.new(@game).call
    show
  end

  def pause
  end

  private

  def broadcast_game
    state = @game.full_state
    ActionCable.server.broadcast( "game_#{params['name']}", { action: :updateGame, gameState: state })
    if !@game.turns.empty? && !@game.is_over?
      ActionCable.server.broadcast( "game_#{params['name']}", { action: :setTimer, gameState: state })
    end
    state
  end

  def game_params
    params.require(:game).permit(:name)
  end

  def set_game
    @game = Game.find_by(name: params[:name])
  end

end

{'fraction_non_alphanumeric': 0.10511627906976745, 'fraction_numerical': 0.0, 'mean_word_length': 3.0499058380414312, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '3499939', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1399, 'n_words': 344}