|
|
|
|
class Game < ActiveRecord::Base
|
|
|
|
|
# stauts 0: can exe 1:doing 2:successed 3:locked
|
|
|
|
|
# modify_time: 与challenges表的modify_time联合使用,2个字段一致,则表示测试集未修改,反之,被修改
|
|
|
|
|
default_scope :order => 'games.created_at desc'
|
|
|
|
|
attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time, :identifier, :answer_open, :end_time,
|
|
|
|
|
:retry_status, :resubmit_identifier, :test_sets_view, :picture_name, :accuracy, :modify_time, :cost_time, :evaluate_count
|
|
|
|
|
belongs_to :myshixun,:touch=> true
|
|
|
|
|
belongs_to :user
|
|
|
|
|
belongs_to :challenge
|
|
|
|
|
has_many :outputs, :dependent => :destroy
|
|
|
|
|
has_many :choose_output
|
|
|
|
|
# has_many :test_sets, :dependent => :destroy
|
|
|
|
|
has_many :challenge_samples
|
|
|
|
|
has_many :game_codes, :dependent => :destroy
|
|
|
|
|
has_many :evaluate_records, :dependent => :destroy
|
|
|
|
|
|
|
|
|
|
include ApplicationHelper
|
|
|
|
|
scope :min, lambda { select([:id, :status, :myshixun_id, :user_id, :final_score, :challenge_id, :identifier,
|
|
|
|
|
:answer_open, :test_sets_view, :cost_time, :star, :modify_time]) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def had_done
|
|
|
|
|
myshixun = self.myshixun
|
|
|
|
|
challenge_count = myshixun.games.count
|
|
|
|
|
had_done = (myshixun.games.where(:status => 2).count == challenge_count) ? 1 : 0
|
|
|
|
|
return had_done
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def had_passed?
|
|
|
|
|
self.status == 2 ? true : false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def consumes_time
|
|
|
|
|
if self.end_time.blank?
|
|
|
|
|
"--"
|
|
|
|
|
else
|
|
|
|
|
game_spend_time(self.cost_time)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def consumes_time_int
|
|
|
|
|
if self.end_time.blank?
|
|
|
|
|
0
|
|
|
|
|
else
|
|
|
|
|
self.end_time.to_i - self.open_time.to_i
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# id 转换成 challenge'position
|
|
|
|
|
def to_param
|
|
|
|
|
identifier
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 因为outputs是按qurey_index倒序,所有first取的qurey_index的最大值
|
|
|
|
|
# 之所以加1是以为,存的时候要递增,如果仅取的话,则注意:必须减一
|
|
|
|
|
def query_index
|
|
|
|
|
if self.outputs.try(:first).present?
|
|
|
|
|
self.outputs.first.try(:query_index).to_i + 1
|
|
|
|
|
else
|
|
|
|
|
1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def lastest_code
|
|
|
|
|
self.game_codes.first.try(:new_code)
|
|
|
|
|
# game_code = GameCode.where(:game_id => self.id).first
|
|
|
|
|
# game_code.try(:new_code)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def last_game
|
|
|
|
|
challenge = self.challenge
|
|
|
|
|
last_challenge_id = challenge.last_challenge
|
|
|
|
|
game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => last_challenge_id).first
|
|
|
|
|
#render_404 if game.nil?
|
|
|
|
|
return game
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# def next_game
|
|
|
|
|
# challenge = self.challenge
|
|
|
|
|
# next_challenge_id = challenge.next_challenge
|
|
|
|
|
# game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => next_challenge_id).first
|
|
|
|
|
# raise ActiveRecord::RecordNotFound if game.nil?
|
|
|
|
|
# return game
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
def prev_game
|
|
|
|
|
challenge = self.challenge
|
|
|
|
|
prev_challenge_id = challenge.prev_challenge
|
|
|
|
|
game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => prev_challenge_id).first
|
|
|
|
|
raise ActiveRecord::RecordNotFound if game.nil?
|
|
|
|
|
return game
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 针对api形式取下一关identifier
|
|
|
|
|
def self.next_game shixun_id, myshixun_id, challenge_position
|
|
|
|
|
next_game = Game.find_by_sql("select * from games where myshixun_id=#{myshixun_id} and challenge_id=(select id from challenges where
|
|
|
|
|
shixun_id=#{shixun_id} and position=#{challenge_position + 1})").first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 针对api形式取上一关identifier
|
|
|
|
|
def self.prev_identifier shixun_id, myshixun_id, challenge_position
|
|
|
|
|
Game.find_by_sql("select identifier from games where myshixun_id=#{myshixun_id} and challenge_id=(select id from challenges where
|
|
|
|
|
shixun_id=#{shixun_id} and position=#{challenge_position - 1})").first.try(:identifier)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def up_game
|
|
|
|
|
challenge = self.challenge
|
|
|
|
|
if challenge.position == 1
|
|
|
|
|
self
|
|
|
|
|
else
|
|
|
|
|
prev_challenge_id = Challenge.where(:position => (challenge.position - 1), :shixun_id => challenge.shixun).first
|
|
|
|
|
game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => prev_challenge_id).first
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def down_game
|
|
|
|
|
challenge = self.challenge
|
|
|
|
|
challenge_count = Challenge.where(:shixun_id => challenge.shixun_id).count
|
|
|
|
|
if challenge.position == challenge_count
|
|
|
|
|
challenge.position = 0
|
|
|
|
|
end
|
|
|
|
|
next_challenge_id = Challenge.where(:position => (challenge.position + 1), :shixun_id => challenge.shixun).first
|
|
|
|
|
game = Game.where(:myshixun_id => self.myshixun_id, :challenge_id => next_challenge_id).first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def all_outputs
|
|
|
|
|
Output.select([:game_id, :query_index, :out_put, :created_at]).where(:game_id => self.id).group(:query_index)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 获取game最新的一条输出结果
|
|
|
|
|
def latest_output
|
|
|
|
|
outputs = Output.where(:game_id => self.id).order("created_at desc")
|
|
|
|
|
output = outputs.blank? ? nil : outputs.first
|
|
|
|
|
return output
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_final_game?
|
|
|
|
|
challenge = self.challenge
|
|
|
|
|
challenge.try(:position).to_i == challenge.shixun.challenges.count ? true : false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 选择题正确答案个数
|
|
|
|
|
def choose_correct_num
|
|
|
|
|
Output.where(:game_id => self.id, :query_index => (self.query_index - 1), :result => 1).count
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|