选择题答案获取

dev_course
daiao 6 years ago
parent b6ccd6562c
commit eb5cf9f759

@ -221,12 +221,12 @@ class GamesController < ApplicationController
@result = challenge.st == 0 ? challenge.try(:answer) : challenge.choose_answer
end
# 获取答案
# 获取实践题答案
# GET: /tasks/:identifier/get_answer_info
# 0 直接查看答案, 1 查看答案弹框, 2 答案详情弹框
def get_answer_info
challenge = @game.challenge
@challenge_answers = challenge.challenge_answers
@challenge = @game.challenge
@challenge_answers = @challenge.challenge_answers
# 平台已认证的老师需要控制
@power = (@identity < User::EDU_GAME_MANAGER)
if !@power
@ -243,43 +243,49 @@ class GamesController < ApplicationController
end
end
end
end
# 获取选择题答案
def get_choose_answer
@challenge = @game.challenge
tip_exception("本接口只能获取选择题答案") if @challenge.st != 1
@power = (@identity < User::EDU_GAME_MANAGER)
# 如果没权限,也没看过答案,则需要解锁
if @game.answer_open == 0 && !@power
tip_exception(1, @challenge.choose_score)
else
@challenge_chooses = @challenge.challenge_chooses
end
end
# 解锁答案
# 解锁实践题答案
# GET: /tasks/:identifier/get_answer_info?answer_id=?
def unlock_answer
@challenge = @game.challenge
if @challenge.st == 0
@answer = ChallengeAnswer.find(params[:answer_id])
challenge = @answer.challenge
# 解锁需要本层级的答案是否需要扣分
points = challenge.challenge_answers.where(level: @game.answer_open+1..@answer.level).sum(:score)
deduct_score = ((points / 100.0) * challenge.score).to_i
uid_logger("############金币数目: #{current_user.grade}")
unless current_user.grade.to_i - deduct_score > 0
tip_exception("您没有足够的金币")
end
else
@answer = []
@challenge.challenge_chooses.each do |choose|
@answer << {:position => choose.position, :answer => (choose.answer.blank? ? choose.standard_answer : choose.answer)}
end
@answer = ChallengeAnswer.find(params[:answer_id])
challenge = @answer.challenge
# 解锁需要本层级的答案是否需要扣分
points = challenge.challenge_answers.where(level: @game.answer_open + 1..@answer.level).sum(:score)
deduct_score = ((points / 100.0) * challenge.score).to_i
uid_logger("############金币数目: #{current_user.grade}")
unless current_user.grade.to_i - deduct_score > 0
tip_exception("您没有足够的金币")
end
ActiveRecord::Base.transaction do
begin
# 积分消耗情况记录
score = challenge.st.zero? ? -deduct_score : -challenge.choose_score.to_i
RewardGradeService.call(current_user, container_id: @answer.id, container_type: 'Answer', score: score)
RewardGradeService.call(current_user, container_id: @game.id, container_type: 'Answer', score: score)
# 通关查看答案 不扣 得分
answer_open = @challenge.st == 1 ? 1 : @answer.level
if @game.status == 2
@game.update_attributes!(:answer_open => @answer.level)
@game.update_attributes!(:answer_open =>answer_open)
else
# 扣除总分计算
answer_deduction = challenge.challenge_answers.where("level <= #{@answer.level}").sum(:score)
@game.update_attributes!(:answer_open => @answer.level, :answer_deduction => answer_deduction)
@game.update_attributes!(:answer_open => answer_open, :answer_deduction => answer_deduction)
end
rescue Exception => e
@ -287,7 +293,32 @@ class GamesController < ApplicationController
raise ActiveRecord::Rollback
end
end
end
# 解锁选择题答案
def unlock_choose_answer
@challenge = @game.challenge
score = @challenge.choose_score
unless current_user.grade.to_i - score > 0
tip_exception("您没有足够的金币")
end
ActiveRecord::Base.transaction do
begin
# 积分消耗情况记录
RewardGradeService.call(current_user, container_id: @game.id, container_type: 'Answer', score: -score)
# 通关查看答案 不扣 得分
if @game.status == 2
@game.update_attributes!(:answer_open => 1)
else
# 扣除总分计算
@game.update_attributes!(:answer_open => 1, :answer_deduction => 100)
end
@challenge_answers = @challenge.challenge_chooses
rescue Exception => e
uid_logger_error("#######金币扣除异常: #{e.message}")
raise ActiveRecord::Rollback
end
end
end
# 查看答案需要扣取金币

@ -0,0 +1,6 @@
json.choose_answers do
json.array! @challenge_chooses do |choose|
json.position choose.position
json.answer choose.answer.blank? ? choose.standard_answer : choose.answer
end
end

@ -1,5 +1 @@
if @challenge.st == 0
json.contents @answer.contents
else
json.contents @answer
end
json.contents @answer.contents

@ -0,0 +1,6 @@
json.choose_answers do
json.array! @challenge_chooses do |choose|
json.position choose.position
json.answer choose.answer.blank? ? choose.standard_answer : choose.answer
end
end

@ -124,6 +124,8 @@ Rails.application.routes.draw do
get :get_answer_info
get :unlock_answer
get :check_test_sets
get :unlock_choose_answer
get :get_choose_answer
end
collection do

Loading…
Cancel
Save