diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index cf7e6ee9..77f768d1 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -1,10 +1,10 @@ # encoding: utf-8 class MyshixunsController < ApplicationController layout 'base_myshixun' - skip_before_filter :verify_authenticity_token, :only => [:training_task_status, :close_webssh] - before_filter :require_login, :except => [:training_task_status, :close_webssh] + skip_before_filter :verify_authenticity_token, :only => [:training_task_status, :close_webssh, :code_runinng_message] + before_filter :require_login, :except => [:training_task_status, :close_webssh, :code_runinng_message] before_filter :check_authentication, :except => [:training_task_status, :close_webssh, :mul_test_home, :mul_test_user, - :mul_test_myshixun, :mul_test_shixun, :mul_test_start] + :mul_test_myshixun, :mul_test_shixun, :mul_test_start, :code_runinng_message] before_filter :find_myshixun, :only => [:show, :myshixun_reset, :open_webssh, :sync_reset_time, :destroy, :search_file_list, :vnc] DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) @@ -435,6 +435,39 @@ class MyshixunsController < ApplicationController end + # 代码运行中的信息接口 + def code_runinng_message + begin + logger.info("######################params: #{params}") + jsonTestDetails = JSON.parse(params[:jsonTestDetails]) + game_id = jsonTestDetails['buildID'] + message = jsonTestDetails['textMsg'] + logger.info("##################code_runinng_message:#{jsonTestDetails}") + logger.info("##################buildID: #{game_id}") + logger.info("##################textMsg: #{message}") + if game_id.present? && message.present? + game = Game.find game_id + msg = game.run_code_message + # 只有评测中的game才会创建和更新代码评测中的信息 + logger.info("##################game: #{game.status}") + logger.info("##################retry_status: #{game.retry_status}") + if game.status == 1 || game.status == 2 && game.retry_status == 1 + if msg.blank? + RunCodeMessage.create!(:game_id => game_id, :status => 1, :message => message) + else + msg.update_attributes(:status => (msg.status + 1), :message => message) + end + else + # 评测完成,初始化评测信息的状态 + msg.update_attributes(:status => 0, :message => nil) if msg.present? + end + render :json => {:data => "success"} + end + rescue Exception => e + render :json => {:data => "failed"} + end + end + # taskId 即返回的game id # 返回结果:params [:stauts] 0 表示成功,其它则失败 # msg 错误信息 @@ -540,7 +573,7 @@ class MyshixunsController < ApplicationController logger.info("training_task_status start#4**#{game_id}**** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") render :json => {:data => "success"} rescue Exception => e - render :json => {:data => "failed"} + render :json => {:data => "failed, errer_message:#{e}"} logger.error("training_task_status error: #{e}") raise ActiveRecord::Rollback end diff --git a/app/models/game.rb b/app/models/game.rb index 9f92d109..47674541 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -13,6 +13,7 @@ class Game < ActiveRecord::Base has_many :challenge_samples has_many :game_codes, :dependent => :destroy has_many :evaluate_records, :dependent => :destroy + has_one :run_code_message, :dependent => :destroy include ApplicationHelper scope :min, lambda { select([:id, :status, :myshixun_id, :user_id, :final_score, :challenge_id, :identifier, diff --git a/app/models/run_code_message.rb b/app/models/run_code_message.rb new file mode 100644 index 00000000..191ead34 --- /dev/null +++ b/app/models/run_code_message.rb @@ -0,0 +1,5 @@ +class RunCodeMessage < ActiveRecord::Base + # attr_accessible :title, :body + belongs_to :game + +end diff --git a/app/services/games_service.rb b/app/services/games_service.rb index d7b56838..7f0e6df4 100644 --- a/app/services/games_service.rb +++ b/app/services/games_service.rb @@ -498,7 +498,9 @@ class GamesService # 更新评测次数 game.update_column(:evaluate_count, (game.evaluate_count.to_i + 1)) - + # 清空代码评测信息 + msg = game.run_code_message + msg.update_attributes(:status => 0, :message => nil) if msg.present? # 更新时间是为了TPM端显示的更新,退出实训及访问实训的时候会更新 myshixun.update_column(:updated_at, Time.now) @@ -793,7 +795,10 @@ class GamesService # 如果没有超时并且正在评测中 # 判断评测中的状态有两种:1、如果之前没有通关的,只需判断status为1即可;如果通过关,则判断game的resubmit_identifier是否更新 if (params[:time_out] == "false") && ((params[:resubmit].blank? && game.status==1) || (params[:resubmit].present? && (params[:resubmit] != resubmit_identifier))) - return + # 代码评测的信息 + running_code_status = game.run_code_message.try(:status) + running_code_message = game.run_code_message.try(:message) + return {:running_code_status => running_code_status, :running_code_message => running_code_message} end Rails.logger.info("##### resubmit_identifier is #{resubmit_identifier}") @@ -886,8 +891,10 @@ class GamesService next_game = Game.next_game(shixun.id, game.myshixun_id, game_challenge.position).try(:identifier) output_hash = {:test_sets => test_sets, :had_test_count => test_sets_count, :test_sets_count => test_sets_count, :had_passed_testsests_error_count => had_passed_testsests_error_count} - return {:grade => grade, :gold => score, :experience => experience, :status => game_status, :had_done => had_done, :position => game_challenge.position, - :port => port, :power => power, :record => record, :mirror_name => mirror_name, :picture => picture, :web_route => web_route, :latest_output => latest_output, + + return {:grade => grade, :gold => score, :experience => experience, :status => game_status, :had_done => had_done, + :position => game_challenge.position, :port => port, :power => power, :record => record, + :mirror_name => mirror_name, :picture => picture, :web_route => web_route, :latest_output => latest_output, :star => game.star, :next_game => next_game, :prev_game => prev_game}.merge(output_hash) end diff --git a/config/routes.rb b/config/routes.rb index 0078eb48..051ed500 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -464,6 +464,7 @@ RedmineApp::Application.routes.draw do ## oauth相关 get 'sigle_mul_test' get 'sigle_update_myshixun' match 'training_task_status', :via => [:get, :post] + match 'code_runinng_message', :via => [:get, :post] end resources :games, :path => "stages" do member do diff --git a/db/migrate/20190315093727_create_run_code_messages.rb b/db/migrate/20190315093727_create_run_code_messages.rb new file mode 100644 index 00000000..59aea2cc --- /dev/null +++ b/db/migrate/20190315093727_create_run_code_messages.rb @@ -0,0 +1,10 @@ +class CreateRunCodeMessages < ActiveRecord::Migration + def change + create_table :run_code_messages do |t| + t.integer :status + t.string :message + t.references :game + t.timestamps + end + end +end \ No newline at end of file diff --git a/spec/factories/run_code_messages.rb b/spec/factories/run_code_messages.rb new file mode 100644 index 00000000..b52afbc3 --- /dev/null +++ b/spec/factories/run_code_messages.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :run_code_message do + + end +end diff --git a/spec/models/run_code_message_spec.rb b/spec/models/run_code_message_spec.rb new file mode 100644 index 00000000..6e5cb831 --- /dev/null +++ b/spec/models/run_code_message_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe RunCodeMessage, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end