|
|
|
class Challenge < ApplicationRecord
|
|
|
|
# difficulty: 关卡难度: 1.简单 2.中等 3.困难
|
|
|
|
default_scope { order("challenges.position asc") }
|
|
|
|
|
|
|
|
belongs_to :shixun, :touch => true, counter_cache: true
|
|
|
|
belongs_to :user
|
|
|
|
has_many :challenge_samples, :dependent => :destroy
|
|
|
|
has_many :test_sets, :dependent => :destroy
|
|
|
|
has_many :challenge_tags, :dependent => :destroy
|
|
|
|
has_many :games, :dependent => :destroy
|
|
|
|
has_many :challenge_chooses, :dependent => :destroy
|
|
|
|
has_many :homework_challenge_settings, :dependent => :destroy
|
|
|
|
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
|
|
|
|
has_one :praise_tread_cache, as: :object, dependent: :destroy
|
|
|
|
has_many :tidings
|
|
|
|
# 参考答案
|
|
|
|
has_many :challenge_answers, :dependent => :destroy
|
|
|
|
has_many :exercise_bank_shixun_challenges, :dependent => :destroy
|
|
|
|
|
|
|
|
# acts_as_attachable
|
|
|
|
|
|
|
|
scope :base_attrs, -> { select([:id, :subject, :position, :shixun_id, :st, :score, :path, :task_pass, :modify_time,
|
|
|
|
:web_route, :answer, :exec_time]) }
|
|
|
|
scope :choose_type, -> { where(st: 1) }
|
|
|
|
scope :practice_type, -> { where(st: 0) }
|
|
|
|
|
|
|
|
scope :fields_for_list, -> { select([:id, :subject, :st, :score, :position, :shixun_id]) }
|
|
|
|
|
|
|
|
def next_challenge
|
|
|
|
position = self.position + 1
|
|
|
|
Challenge.where(:position => position, :shixun_id => self.shixun).first
|
|
|
|
end
|
|
|
|
|
|
|
|
# 用户关卡是否通关
|
|
|
|
def has_passed?(user_id)
|
|
|
|
self.games.present? && self.games.select{|game| game.user_id == user_id && game.status == 2}.length > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
## 选择题总分
|
|
|
|
def choose_score
|
|
|
|
self.challenge_chooses.pluck(:score).sum
|
|
|
|
end
|
|
|
|
|
|
|
|
# 关卡总分
|
|
|
|
def all_score
|
|
|
|
if self.st == 1
|
|
|
|
self.choose_score
|
|
|
|
else
|
|
|
|
self.score
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 开启挑战
|
|
|
|
def open_game user_id, shixun
|
|
|
|
game = self.games.where(user_id: user_id).first
|
|
|
|
if game.present?
|
|
|
|
shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : ""
|
|
|
|
else
|
|
|
|
"/api/shixuns/#{shixun.identifier}/shixun_exec"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# # 开启挑战
|
|
|
|
# def open_game(user_id, shixun)
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# game = self.games.select([:status, :identifier]).where(user_id: user_id).first
|
|
|
|
# game = self.games.select{|game| game.user_id == user_id}
|
|
|
|
# if game.present?
|
|
|
|
# shixun.task_pass || game.status != 3 ? "/tasks/#{game.identifier}" : ""
|
|
|
|
# else
|
|
|
|
# "/api/shixuns/#{shixun.identifier}/shixun_exec"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成
|
|
|
|
def user_tpi_status user_id
|
|
|
|
# todo: 以前没加索引导致相同关卡,同一用户有多个games
|
|
|
|
# 允许跳关则直接开启
|
|
|
|
return 1 if shixun.task_pass
|
|
|
|
game = games.where(user_id: user_id).take
|
|
|
|
if game.blank?
|
|
|
|
self.position == 1 ? 1 : 0
|
|
|
|
elsif game.status == 2
|
|
|
|
2
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
## 选择题答案
|
|
|
|
def choose_answer
|
|
|
|
result = []
|
|
|
|
self.challenge_chooses.each do |choose|
|
|
|
|
result << {:position => choose.position, :answer => (choose.answer.blank? ? choose.standard_answer : choose.answer)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 关卡用户通关数
|
|
|
|
def user_passed_count
|
|
|
|
games.where(status: 2).count
|
|
|
|
end
|
|
|
|
|
|
|
|
# 关卡用户正在挑战的人数
|
|
|
|
def playing_count
|
|
|
|
games.where(status: [0, 1]).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_challenge
|
|
|
|
Challenge.find_by(position: position - 1, shixun_id: shixun_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 关卡评测文件
|
|
|
|
|
|
|
|
end
|