<commit_before>class Game < ActiveRecord::Base
  attr_accessible :players, :name, :max_players, :finished, 
    :deck, :users, :hands

  attr_protected :original_deck_id
  
  has_one :black_card, dependent: :destroy
  has_one :deck, dependent: :destroy
  has_many :hands, dependent: :destroy
  has_many :users, through: :hands

  validates :name, :max_players, :deck, presence: true

  after_create :initialize_game_components!

  def initialize_game_components!
    # Duplicate deck
    self.deck.duplicate_for_game(self.original_deck_id)

    # Create hand (for submitted cards)
    self.hands.new(game: self).save

    self.new_round!
  end

  def new_round!
    if !self.finished
      self.hands.where(user_id: nil).first.white_cards.each { |x| x.destroy }
      self.black_card.destroy if self.black_card
      
      # Assign a black card
      black_card = self.deck.black_cards.to_a.sample(1)[0]
      black_card.deck = nil
      black_card.game = self
      black_card.save

      # Pick a new card czar
      if self.users.where("user_id > #{self.czar_id}").count > 0
        self.czar_id = self.users.where("user_id > #{self.czar_id}").first.id
      elsif self.users.first
        self.czar_id = self.users.first.id
      end

      # Populate all user hands, and set # submissions value
      self.hands.each do |h|
        h.populate_hand!
        h.submissions_left = black_card.num_blanks
        h.save
      end<commit_msg>Fixed czar rotation bug<commit_after>class Game < ActiveRecord::Base
  attr_accessible :players, :name, :max_players, :finished, 
    :deck, :users, :hands

  attr_protected :original_deck_id
  
  has_one :black_card, dependent: :destroy
  has_one :deck, dependent: :destroy
  has_many :hands, dependent: :destroy
  has_many :users, through: :hands

  validates :name, :max_players, :deck, presence: true

  after_create :initialize_game_components!

  def initialize_game_components!
    # Duplicate deck
    self.deck.duplicate_for_game(self.original_deck_id)

    # Create hand (for submitted cards)
    self.hands.new(game: self).save

    self.new_round!
  end

  def new_round!
    if !self.finished
      self.hands.where(user_id: nil).first.white_cards.each { |x| x.destroy }
      self.black_card.destroy if self.black_card
      
      # Assign a black card
      black_card = self.deck.black_cards.to_a.sample(1)[0]
      black_card.deck = nil
      black_card.game = self
      black_card.save

      # Pick a new card czar
      if self.users.where("user_id > #{self.czar_id}").count > 0
        self.czar_id = self.users.where("user_id > #{self.czar_id}").first.id
      elsif self.users.first
        self.czar_id = self.users.order('id ASC').first.id
      end

      # Populate all user hands, and set # submissions value
      self.hands.each do |h|
        h.populate_hand!
        h.submissions_left = black_card.num_blanks
        h.save
      end
{'fraction_non_alphanumeric': 0.10206185567010309, 'fraction_numerical': 0.002061855670103093, 'mean_word_length': 2.976775956284153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1258110', 'n_tokens_mistral': 1009, 'n_tokens_neox': 967, 'n_words': 215}