47 lines
1.4 KiB
Ruby
47 lines
1.4 KiB
Ruby
class Game < ActiveRecord::Base
|
||
# stauts 0: can exe 1:doing 2:successed 3:locked
|
||
default_scope :order => '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
|
||
belongs_to :myshixun,:touch=> true
|
||
belongs_to :user
|
||
belongs_to :challenge
|
||
has_many :outputs, :dependent => :destroy
|
||
has_many :test_sets
|
||
has_many :challenge_samples
|
||
|
||
def had_done
|
||
myshixun = self.myshixun
|
||
challenge = self.challenge
|
||
had_done = (myshixun.games.count == challenge.position && self.status == 2) ? 1 : 0
|
||
return had_done
|
||
end
|
||
|
||
# id 转换成 challenge'position
|
||
def to_param
|
||
identifier
|
||
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
|
||
render_404 if game.nil?
|
||
return game
|
||
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
|
||
end
|