educoder/app/models/game.rb

47 lines
1.4 KiB
Ruby
Raw Normal View History

2017-04-07 10:16:13 +08:00
class Game < ActiveRecord::Base
# stauts 0: can exe 1doing 2successed 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
2017-04-07 10:16:13 +08:00
belongs_to :myshixun,:touch=> true
belongs_to :user
belongs_to :challenge
has_many :outputs, :dependent => :destroy
2017-05-08 15:28:35 +08:00
has_many :test_sets
has_many :challenge_samples
2017-04-07 10:16:13 +08:00
2017-05-14 15:28:18 +08:00
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
2017-04-26 14:20:07 +08:00
# id 转换成 challenge'position
def to_param
identifier
end
2017-04-14 10:02:53 +08:00
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
2017-04-07 10:16:13 +08:00
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