<commit_before>class Battle
  require 'open4'
  include Mongoid::Document
  include Mongoid::MagicCounterCache
  Mongoid.raise_not_found_error = false 

  field :time,  type: DateTime
  field :result, type: String
  field :defender_id, type: String
  field :attacker_id, type: String
  field :winner_id, type: String

  has_and_belongs_to_many :meches
  has_and_belongs_to_many :users, class_name: 'User', inverse_of: :battles
  # counter_cache :users

  belongs_to :match

  has_and_belongs_to_many :starers, class_name: 'User', inverse_of: :stareds
  # counter_cache :starers

  def battle

    #run battle in sandbox
    pid, stdin, stdout, stderr = Open4.popen4("battle/battle.sh #{self.defender.code_dir}libmyAI.so #{self.attacker.code_dir}libmyAI.so #{self._id}")
    ignored, status = Process::waitpid2 pid
    logger.error(stderr.read)
    #get battle result

    pid, stdin, stdout, stderr = Open4.popen4("battle/result.sh #{self.defender.code_dir}libmyAI.so #{self.attacker.code_dir}libmyAI.so #{self._id}")
    ignored, status = Process::waitpid2 pid

    logger.error(stderr.read)

    # if status.exitstatus
    #   return false
    # end
    x = File.read("battle/result/#{self._id}.xml")
    # if FileTest::exist?("battle/result/#{self._id}.xml")
    #   x = File.read("battle/result/#{self._id}.xml")
    # else
    #   return false
    # end

    winnerID = Hash.from_xml(x)['battleStatistics']['winnerID']

    sa = 0.0
    sb = 0.0

    if winnerID == "0"
      sb = 1.0
      self.winner_id = self.defender_id
      self.defender.update_attributes(:score => self.defender.score + 3,
                                    :win_times => self.defender.win_times + 1)
      self.defender.user.update_attributes(:win_times => self.defender.user.win_times + 1,
                                      :battle_count => self.defender.user.battle_count + 1)

      self.attacker.update_attributes(:score => self.attacker.score - 1,
                                      :fail_times => self.attacker.fail_times + 1,
                                      :protect_begin_time => Time.now.to_i,
                                      :protect_time => 300)

      self.attacker.user.update_attributes(:fail_times => self.attacker.user.fail_times + 1,
                                      :battle_count => self.attacker.user.battle_count + 1)
    elsif winnerID == "1"
      sa = 1.0
      self.winner_id = self.attacker_id
      self.attacker.update_attributes(:score => self.attacker.score + 3,
                                      :win_times => self.attacker.win_times + 1)
      self.attacker.user.update_attributes(:win_times => self.attacker.user.win_times + 1,
                                      :battle_count => self.attacker.user.battle_count + 1)

      self.defender.update_attributes(:score => self.defender.score - 1,
                                      :fail_times => self.defender.fail_times + 1)
      
      self.defender.user.update_attributes(:fail_times => self.defender.user.fail_times + 1,<commit_msg>fix bugs<commit_after>class Battle
  require 'open4'
  include Mongoid::Document
  include Mongoid::MagicCounterCache
  Mongoid.raise_not_found_error = false 

  field :time,  type: DateTime
  field :result, type: String
  field :defender_id, type: String
  field :attacker_id, type: String
  field :winner_id, type: String

  has_and_belongs_to_many :meches
  has_and_belongs_to_many :users, class_name: 'User', inverse_of: :battles
  # counter_cache :users

  belongs_to :match

  has_and_belongs_to_many :starers, class_name: 'User', inverse_of: :stareds
  # counter_cache :starers

  def battle

    #run battle in sandbox
    pid, stdin, stdout, stderr = Open4.popen4("battle/battle.sh #{self.defender.code_dir}libmyAI.so #{self.attacker.code_dir}libmyAI.so #{self._id}")
    ignored, status = Process::waitpid2 pid
    logger.error(stderr.read)
    #get battle result

    pid, stdin, stdout, stderr = Open4.popen4("battle/result.sh #{self.defender.code_dir}libmyAI.so #{self.attacker.code_dir}libmyAI.so #{self._id}")
    ignored, status = Process::waitpid2 pid

    logger.error(stderr.read)

    # if status.exitstatus
    #   return false
    # end
    x = File.read("battle/result/#{self._id}.xml")
    # if FileTest::exist?("battle/result/#{self._id}.xml")
    #   x = File.read("battle/result/#{self._id}.xml")
    # else
    #   return false
    # end

    winnerID = Hash.from_xml(x)['battleStatistics']['winnerID']

    sa = 0.0
    sb = 0.0

    if winnerID == "0"
      sb = 1.0
      self.winner_id = self.defender_id
      self.defender.update_attributes(:score => self.defender.score + 3,
                                    :win_times => self.defender.win_times + 1)
      self.defender.user.update_attributes(:win_times => self.defender.user.win_times + 1,
                                      :battle_count => self.defender.user.battle_count + 1)

      self.attacker.update_attributes(:score => self.attacker.score - 1,
                                      :fail_times => self.attacker.fail_times + 1)

      self.attacker.user.update_attributes(:fail_times => self.attacker.user.fail_times + 1,
                                      :battle_count => self.attacker.user.battle_count + 1)
    elsif winnerID == "1"
      sa = 1.0
      self.winner_id = self.attacker_id
      self.attacker.update_attributes(:score => self.attacker.score + 3,
                                      :win_times => self.attacker.win_times + 1)
      self.attacker.user.update_attributes(:win_times => self.attacker.user.win_times + 1,
                                      :battle_count => self.attacker.user.battle_count + 1)

      self.defender.update_attributes(:score => self.defender.score - 1,
                                      :fail_times => self.defender.fail_times + 1,
                                      :protect_begin_time => Time.now.to_i,
                                      :protect_time => 300)
      
      self.defender.user.update_attributes(:fail_times => self.defender.user.fail_times + 1,
{'fraction_non_alphanumeric': 0.11659785301403798, 'fraction_numerical': 0.011560693641618497, 'mean_word_length': 2.368186874304783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1161086', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1945, 'n_words': 338}