From 207ad7753c23e8609b1fb6e3b3a17e4d5836314f Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 10 Jan 2020 18:11:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5trustie=E7=BC=96=E7=A8=8B?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/hack_set.rb | 4 +- app/models/program_bank.rb | 34 ++++++++++ app/models/program_bank_test.rb | 2 + lib/tasks/sync_trustie_program_question.rake | 70 ++++++++++++++++++++ spec/models/program_bank_spec.rb | 5 ++ spec/models/program_bank_test_spec.rb | 5 ++ 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/models/program_bank.rb create mode 100644 app/models/program_bank_test.rb create mode 100644 lib/tasks/sync_trustie_program_question.rake create mode 100644 spec/models/program_bank_spec.rb create mode 100644 spec/models/program_bank_test_spec.rb diff --git a/app/models/hack_set.rb b/app/models/hack_set.rb index 4dafd94a7..9b03ee8d0 100644 --- a/app/models/hack_set.rb +++ b/app/models/hack_set.rb @@ -1,6 +1,6 @@ class HackSet < ApplicationRecord - validates_length_of :input, maximum: 500 - validates_length_of :output, maximum: 500 + validates_length_of :input, maximum: 1000 + validates_length_of :output, maximum: 1000 validates :input, presence: { message: "测试集输入不能为空" } validates :output, presence: { message: "测试集输出不能为空" } validates_uniqueness_of :input, scope: [:hack_id, :input], message: "多个测试集的输入不能相同" diff --git a/app/models/program_bank.rb b/app/models/program_bank.rb new file mode 100644 index 000000000..c9be9af9f --- /dev/null +++ b/app/models/program_bank.rb @@ -0,0 +1,34 @@ +class ProgramBank < ApplicationRecord + + def oj_language + result = case language + when '1' + then 'C' + when '2' + then 'C++' + when '3' + then 'Python' + when '4' + then 'Java' + end + result + end + + def strip_description + strip_html description + end + + def oj_sub_discipline_id + result = case language + when '1' + then 3 + when '2' + then 4 + when '3' + then 5 + when '4' + then 2 + end + result + end +end diff --git a/app/models/program_bank_test.rb b/app/models/program_bank_test.rb new file mode 100644 index 000000000..ea336ae91 --- /dev/null +++ b/app/models/program_bank_test.rb @@ -0,0 +1,2 @@ +class ProgramBankTest < ApplicationRecord +end diff --git a/lib/tasks/sync_trustie_program_question.rake b/lib/tasks/sync_trustie_program_question.rake new file mode 100644 index 000000000..679164229 --- /dev/null +++ b/lib/tasks/sync_trustie_program_question.rake @@ -0,0 +1,70 @@ +desc "同步trustie的编程作业" + +namespace :sync_program do + 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) + task data: :environment do + ProgramBank.where(homework_type: 2).each do |program| + unless Hack.where(name: program.name).exists? + strip_des = strip_html(program.description, 5000) + description = strip_des.present? ? strip_des : program.name + hack_params = {name: program.name[0..59], description: description, difficult: 1, open_or_not: 1, score: 200, sub_discipline_id: program.oj_sub_discipline_id} + puts "language::::#{program.language}" + puts "program_bank::::#{program.id}" + hack = Hack.new(hack_params) + hack.user_id = 1 + hack.identifier = generate_identifier Hack, 8 + ActiveRecord::Base.transaction do + hack.save! + # 创建测试集与代码 + position = 1 + ProgramBankTest.where(homework_bank_id: program.id).each do |test_set| + if !test_set.input.blank? && !test_set.output.blank? && !hack.hack_sets.where(input: test_set.input).exists? && test_set.input.length <= 1000 && test_set.output.length <= 1000 + hack.hack_sets.create!(input: test_set.input, output: test_set.output, position: position) + position += 1 + end + end + # 新建知识点 + hack_code_params = {code: program.standard_code, language: program.oj_language} + hack_codes = hack.hack_codes.new(hack_code_params) + hack_codes.modify_time = Time.now + hack_codes.save! + new_item_params = {name: program.name, sub_discipline_id: program.oj_sub_discipline_id, container: hack, item_type: 'PROGRAM', public: 1, difficulty: 1, user_id: 1} + ItemBank.create!(new_item_params) + end + puts hack.id + end + end + end + + # 随机生成字符 + def generate_identifier(container, num) + code = DCODES.sample(num).join + if container == User + while container.exists?(login: code) do + code = DCODES.sample(num).join + end + else + while container.exists?(identifier: code) do + code = DCODES.sample(num).join + end + end + code + end + + def strip_html(text, len=0, endss="...") + ss = "" + if !text.nil? && text.length>0 + ss=text.gsub(/<\/?.*?>/, '').strip + ss = ss.gsub(/ */, '') + ss = ss.gsub(/\r\n/,'') #新增 + ss = ss.gsub(/\n/,'') #新增 + if len > 0 && ss.length > len + ss = ss[0, len-4] + endss + elsif len > 0 && ss.length <= len + ss = ss + #ss = truncate(ss, :length => len) + end + end + ss + end +end \ No newline at end of file diff --git a/spec/models/program_bank_spec.rb b/spec/models/program_bank_spec.rb new file mode 100644 index 000000000..d8a764494 --- /dev/null +++ b/spec/models/program_bank_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ProgramBank, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/program_bank_test_spec.rb b/spec/models/program_bank_test_spec.rb new file mode 100644 index 000000000..22720f254 --- /dev/null +++ b/spec/models/program_bank_test_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ProgramBankTest, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end