From 2c7e5d1f38b386ccb2001f88278da1ff59e7f85b Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 14 Sep 2017 17:30:58 +0800 Subject: [PATCH] =?UTF-8?q?TPI=E5=8D=95=E9=80=89=E5=A4=9A=E9=80=89?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/games_controller.rb | 72 ++++++++----------- app/models/challenge.rb | 21 ++++++ app/views/games/_choice_question.html.erb | 33 +++++---- app/views/games/_code_actions.html.erb | 60 ++++------------ app/views/games/_game_choose_results.html.erb | 43 +++++++---- app/views/games/_game_show.html.erb | 7 +- app/views/games/show.js.erb | 12 ++++ ..._modify_correct_type_for_choose_outputs.rb | 8 +++ 8 files changed, 134 insertions(+), 122 deletions(-) create mode 100644 db/migrate/20170914024926_modify_correct_type_for_choose_outputs.rb diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 00e9e52d9..f2fdb4e22 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -296,55 +296,41 @@ class GamesController < ApplicationController end # 选择题评测(选择题不需要重新评测) + # score 获得金币数 tag_count 技能数 right用户是否全对 answer_right全部选择题的正确性 def evaluating_choice - - if false - @game_challenge = @game.challenge - user_answer = params[:answer] - correct = true - score = 0 - tag_count = 0 - if !user_answer.blank? - if user_answer.split("").count != @game_challenge.challenge_questions.where(:right_key => true).count - correct = false - else - user_answer.split("").each do |answer| - question = @game_challenge.challenge_questions.where(:position => answer.to_i).first - unless question.right_key - correct = false - break - end - end - end - else - correct = false + @game_challenge = @game.challenge + @challenges = Challenge.where(:shixun_id => @myshixun.shixun_id) + @shixun = @myshixun.shixun + score = 0 + tag_count = 0 + @right = true + answer_right = [] + @game_challenge.challenge_chooses.each_with_index do |choose, index| + correct = (params[:answer][index] == choose.standard_answer) ? true : false + if choose.choose_outputs.blank? + ChooseOutputs.create(:challenge_choose_id => choose.id, :user_id => User.current.id, :answer => params[:answer][index], :correct => correct) end - @game.update_attributes(:status => 2, :end_time => Time.now) - had_done = @game.had_done - shixun = @myshixun.shixun - if shixun.status > 1 && !@game.answer_open + if @shixun.status > 1 && !@game.answer_open if correct - #User.current.update_attributes(:grade => (User.current.grade + @game.challenge.score), :experience => (User.current.experience + @game.challenge.score)) - reward_grade(@game.user, @game.id, 'Game', @game_challenge.score) - reward_experience(@game.user, @game.id, 'Game', @game_challenge.score) - @game.update_attribute(:final_score, @game_challenge.score) - score = @game_challenge.score - tag_count = @game_challenge.challenge_tags.count - else - score = "+0" - tag_count = 0 + score += choose.score + tag_count += choose.challenge_tags.count end - elsif shixun.status > 1 && @game.answer_open - score = -@game_challenge.score.to_i - tag_count = 0 - else - score = "+0" - tag_count = 0 + elsif @shixun.status > 1 && @game.answer_open + score -= choose.score end - if @game.outputs.blank? - Output.create(:game_id => @game.id, :actual_output => user_answer, :result => correct) + unless correct + @right = false end - render :json => {correct: correct, had_done: had_done, :grade => User.where(:id => User.current.id).first.grade, score: score, tag_count: tag_count} + answer_right << correct + end + @game.update_attributes(:status => 2, :end_time => Time.now) + @had_done = @game.had_done + reward_grade(@game.user, @game.id, 'Game', score) + reward_experience(@game.user, @game.id, 'Game', score) + @game.update_attribute(:final_score, score) + + respond_to do |format| + format.js{redirect_to myshixun_game_path(@game, :myshixun_id => @myshixun, :choose => 1)} end end diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 7efd0c6ba..128a64d8b 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -28,6 +28,27 @@ class Challenge < ActiveRecord::Base str end + def choose_correct_num + num = 0 + self.challenge_chooses.each do |choose| + outputs = ChooseOutputs.where(:challenge_choose_id => choose.id).first + if outputs.nil? + num = nil + else + num += (outputs.correct ? 1 : 0) + end + end + return num + end + + def choose_score + score = 0 + self.challenge_chooses.each do |choose| + score += choose.score + end + return score + end + def next_challenge challenge_count = Challenge.where(:shixun_id => self.shixun_id).count render_404 if self.position ==challenge_count diff --git a/app/views/games/_choice_question.html.erb b/app/views/games/_choice_question.html.erb index badbf1536..cebfd0a05 100644 --- a/app/views/games/_choice_question.html.erb +++ b/app/views/games/_choice_question.html.erb @@ -11,30 +11,30 @@
-
- <% @game_challenge.challenge_chooses.each_with_index do |choose, index| %> -

<%= (index + 1).to_s + "." + (choose.category == 1 ? "单选题" : "多选题") %>

-
- -
-
+
+ <% @game_challenge.challenge_chooses.each_with_index do |choose, index| %> +

<%= (index + 1).to_s + "." + (choose.category == 1 ? "单选题" : "多选题") %>

+
+ +
+
<% choose.challenge_questions.each_with_index do |question, i| %>

<% if choose.category == 1 %> - + name="answer[<%= index + 1 %>]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-radio ml5"> <% else %> - + name="answer[]" category="<%= choose.category %>" value="<%= (question.position + 65).chr %>" id="result_<%= index %>_<%= i %>" class="ml-3 mr5 magic-checkbox ml5"> <% end %> - +

<% end %> -
- <% end %> - -

-
+

+
@@ -61,7 +61,7 @@
- <%= render :partial => 'games/game_choose_results' %> + <%= render :partial => 'games/game_choose_results', :locals => { :game_challenge => @game_challenge} %>
@@ -129,7 +129,6 @@ -
<%= (question.position + 65).chr %>.
diff --git a/app/views/games/_code_actions.html.erb b/app/views/games/_code_actions.html.erb index 9044a7fa0..746ce1313 100644 --- a/app/views/games/_code_actions.html.erb +++ b/app/views/games/_code_actions.html.erb @@ -84,57 +84,25 @@ // 选择题提交评测 // answer 选择的答案 function choice_submmit(){ - var answer = ""; - var nBtn = $(".card-check"); - var choice = nBtn.find("input[name='answer']"); - for(var i = 0; i < choice.length; i++){ - answer += $(choice[i]).val(); - } - if(answer == ""){ - notice_sure_box("答案不能为空!"); - return; + var user_answer = []; + var input_answer = $("input[name='user_answer']"); + var lens = input_answer.length; + for(i = 0; i < lens; i++ ){ + var answer = $(input_answer[i]).val(); + if(answer == ""){ + notice_sure_box("第"+ (i+1) + "题未答,请确认!"); + return; + } + user_answer.push(answer) } + console.log(user_answer); $("#code_test").html("评测中.."); $.ajax({ - type: "post", + type: "POST", url: '<%= evaluating_choice_myshixun_game_path(@game, :myshixun_id => @myshixun) %>', - data: {answer: answer}, - dataType: "json", + data: {answer:user_answer}, + dataType: "script", success: function(data){ - console.log(data); - clearInterval(cm); - var icon = $("#game_status_<%= @game_challenge.id %>"); // 实训列表的icon - icon.find("i").attr("class", "fa fa-unlock fr font-18 mt5 color-light-green w20_center"); - icon.find("a:last").attr("title", "已完成"); - $("#code_test").remove(); - if(data.correct){ - $("#correct_tip").show(); - $("#user_grade").html(data.grade); - $("#shixun_exp_<%=@game_challenge.id %>").html("经验值+"+ data.score + ""); - $("#shixun_grade_<%=@game_challenge.id %>").html("金币+"+ data.score + ""); - $("#shixun_tag_<%=@game_challenge.id %>").html("技能标签+"+ data.tag_count + ""); - if( data.had_done == 0 ) { - $("#code_estimate").html("下一关"); - var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game=> @game, :myshixun => @myshixun, :had_done => 0}) %>"; - }else{ - $("#code_estimate").html("退出实训"); - var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game => @game, :myshixun => @myshixun, :had_done => 1}) %>"; - } - // 传递具体屏幕的宽高,实现不同浏览器与不同分辨率都能撑满屏幕 - pop_box_new2(htmlvalue, window.innerWidth, window.innerHeight); - }else{ - $("#error_tip").show(); - var span_class = parseInt(data.score) < 0 ? "u-color-light-red" : "color-light-green"; - $("#shixun_exp_<%=@game_challenge.id %>").html("经验值+0"); - $("#shixun_grade_<%=@game_challenge.id %>").html("金币"+ data.score + ""); - $("#shixun_tag_<%=@game_challenge.id %>").html("技能标签+0"); - if(0 == data.had_done){ - $("#code_estimate").html("下一关"); - }else if(1 == data.had_done ){ - $("#code_estimate").html("退出实训"); - } - - } }, error: function(){ console.log("错了"); diff --git a/app/views/games/_game_choose_results.html.erb b/app/views/games/_game_choose_results.html.erb index a69854400..061dc8066 100644 --- a/app/views/games/_game_choose_results.html.erb +++ b/app/views/games/_game_choose_results.html.erb @@ -1,6 +1,3 @@ -<%= content_for(:header_tags) do %> - <%= javascript_include_tag 'baiduTemplate', 'jquery.datetimepicker.js' %> -<% end %>
diff --git a/app/views/games/show.js.erb b/app/views/games/show.js.erb index 7e910d2e2..2419d5fec 100644 --- a/app/views/games/show.js.erb +++ b/app/views/games/show.js.erb @@ -10,6 +10,18 @@ $("#all_task_show").html("<%= j (render :partial => "games_list") %>"); $("#all_task_show").hide(); fadein = 0; + // 选择题 + <% if params[:choose] == "1" %> + <% if @had_done == 0 %> + $("#code_estimate").html("下一关"); + var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game=> @game, :myshixun => @myshixun, :had_done => 0}) %>"; + <% else %> + $("#code_estimate").html("退出实训"); + var htmlvalue = "<%= j (render :partial => 'games/pass_game_show', :locals => { :game => @game, :myshixun => @myshixun, :had_done => 1}) %>"; + <% end %> + // 传递具体屏幕的宽高,实现不同浏览器与不同分辨率都能撑满屏幕 + pop_box_new2(htmlvalue, window.innerWidth, window.innerHeight); + <% end %> <% else %> $("#all_task_show").html("<%= j (render :partial => "games_list") %>"); $("#all_task_tab").trigger("click"); // 模拟全部任务点击事件 diff --git a/db/migrate/20170914024926_modify_correct_type_for_choose_outputs.rb b/db/migrate/20170914024926_modify_correct_type_for_choose_outputs.rb new file mode 100644 index 000000000..a186d8842 --- /dev/null +++ b/db/migrate/20170914024926_modify_correct_type_for_choose_outputs.rb @@ -0,0 +1,8 @@ +class ModifyCorrectTypeForChooseOutputs < ActiveRecord::Migration + def up + change_column :choose_outputs, :correct, :boolean + end + + def down + end +end