diff --git a/app/assets/javascripts/games.js.coffee b/app/assets/javascripts/games.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/games.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/games.css.scss b/app/assets/stylesheets/games.css.scss new file mode 100644 index 000000000..db1b7bcfa --- /dev/null +++ b/app/assets/stylesheets/games.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the games controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb new file mode 100644 index 000000000..c86cff950 --- /dev/null +++ b/app/controllers/games_controller.rb @@ -0,0 +1,54 @@ +class GamesController < ApplicationController + layout "base_myshixun" + before_filter :find_myshixun, :only => [:index, :game_build] + before_filter :find_game, :only => [:show, :game_build] + before_filter :allowd_manager, :only => [:game_build] + include ApplicationHelper + + def index + + end + + def show + + end + + # REDO:回复状态更新 + def game_build + gitUrl = git_repository_url(@myshixun, "Myshixun") + gitUrl = Base64.encode64(gitUrl) + taskId = params[:id] + jobName = @myshixun.forked_from + step = @game.stage + + if @game.status == 0 + params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"} + uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob") + res = uri_exec uri, params + # @challenge.update_attribute(:status, 1) + end + respond_to do |format| + format.js + end + end + + private + def allowd_manager + render_403 unless User.current.manager_of_myshixun?(@myshixun) + end + + # Find myshixun of id params[:id] + def find_myshixun + myshixun_id = params[:myshixun_id] || (params[:game] && params[:game][:myshixun_id]) + @myshixun = Myshixun.find(myshixun_id) + rescue ActiveRecord::RecordNotFound + render_404 + end + + def find_game + @game = Game.find(params[:id]) + @myshixun = @game.myshixun + rescue ActiveRecord::RecordNotFound + render_404 + end +end diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index aed043f88..f0737dafb 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -1,2 +1,28 @@ class MyshixunsController < ApplicationController + layout 'base_myshixun' + skip_before_filter :verify_authenticity_token, :only => [:training_task_status] + before_filter :require_login, :except => [:training_task_status] + before_filter :find_myshixun, :only => [:show] + + def training_task_status + status = params[:status].to_i + task_id = params[:taskId] + outPut = params[:outPut] + message = Base64.decode64(params[:msg]) unless params[:msg].blank? + GameOutputs.create(:code => status, :msg => message, :game_id => task_id, :out_put => outPut) + end + + def show + respond_to do |format| + format.html{redirect_to myshixun_games_path(@myshixun)} + end + end + + private + # Find myshixun of id params[:id] + def find_myshixun + @myshixun = Myshixun.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 6db99de04..e891383ed 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -2,24 +2,13 @@ # REDO: 创建版本库权限控制 class ShixunsController < ApplicationController layout 'base_shixun' - skip_before_filter :verify_authenticity_token, :only => [:training_task_status] - - before_filter :require_login, :except => [:training_task_status] - before_filter :find_shixun, :except => [ :index, :new, :create, :training_task_status] + before_filter :require_login + before_filter :find_shixun, :except => [ :index, :new, :create] before_filter :shixun_view_allow, :only => [:show] before_filter :require_manager, :only => [ :settings, :add_script] include ApplicationHelper - def training_task_status - status = params[:status].to_i - task_id = params[:taskId] - outPut = params[:outPut] - message = Base64.decode64(params[:msg]) unless params[:msg].blank? - GameOutputs.create(:code => status, :msg => message, :game_id => task_id, :out_put => outPut) - end - - def shixun_monitor monitor_filter if @had_exec @@ -47,11 +36,11 @@ class ShixunsController < ApplicationController s.sync_user(User.current) end gshixun = g.fork(@shixun.gpid, User.current.gid) - new_shixun = copy_shixun(@shixun, gshixun) - publish_challenges(@shixun.id, new_shixun.id, position = 1) + myshixun = copy_myshixun(@shixun, gshixun) + publish_games(@shixun.id, myshixun.id, position = 1) respond_to do |format| - format.html{redirect_to shixun_challenges_path(new_shixun)} + format.html{redirect_to myshixun_games_path(myshixun)} end rescue Exception => e respond_to do |format| @@ -120,20 +109,6 @@ class ShixunsController < ApplicationController end end - def rep_tree_changes - rev = params[:rev] - ent_path = params[:ent_path] - gpid = params[:gpid] - g = Gitlab.client - begin - result = g.rep_last_changes(gpid, :rev => rev, :path => ent_path) - result = {:message => result.message, :author_name => User.find_by_mail(result.author_email).nil? ? result.author_email : User.find_by_mail(result.author_email).show_name, :time => distance_of_time_in_words(result.time, Time.now)} - rescue Exception => e - puts e - end - render :json => result - end - # 添加实训脚本 def add_script if @shixun.update_attribute(:script, params[:shixun_script]) @@ -198,39 +173,40 @@ class ShixunsController < ApplicationController end private - def publish_challenges original_shixun_id, new_shixun_id, position + def publish_games original_shixun_id, new_shixun_id, position original_challenge = Challenge.where(:shixun_id => original_shixun_id, :position => position).first - challenge = Challenge.create(:subject => original_challenge.subject, :description => original_challenge.description, - :position => 1, :shixun_id => new_shixun_id, :user_id => User.current.id) + challenge = Game.create(:subject => original_challenge.subject, :description => original_challenge.description, + :stage => position, :myshixun_id => new_shixun_id, :user_id => User.current.id) end # 复制项目 # gshixun --> gitlab project - def copy_shixun tpm_shixun, gshixun - shixun = Shixun.new - shixun.name = tpm_shixun.name - shixun.description = tpm_shixun.description - shixun.is_public = tpm_shixun.is_public - shixun.parent_id = tpm_shixun.id - shixun.user_id = User.current.id - shixun.gpid = gshixun.id - shixun.forked_form = tpm_shixun.id - shixun.status = 2 - if shixun.save - m = ShixunMember.new(:user_id => User.current.id, :role => 1) - shixun.shixun_members << m - copy_shixun_repository(shixun, gshixun) - return shixun + def copy_myshixun tpm_shixun, gshixun + myshixun = Myshixun.new + myshixun.name = tpm_shixun.name + myshixun.description = tpm_shixun.description + myshixun.is_public = tpm_shixun.is_public + myshixun.parent_id = tpm_shixun.id + myshixun.user_id = User.current.id + myshixun.gpid = gshixun.id + myshixun.forked_from = tpm_shixun.id + + if myshixun.save + m = MyshixunMember.new(:user_id => User.current.id, :role => 1) + myshixun.myshixun_members << m + copy_myshixun_repository(myshixun, gshixun) + return myshixun end end - def copy_shixun_repository(shixun, gshixun) + def copy_myshixun_repository(myshixun, gshixun) repository = Repository.factory('Git') - repository.shixun_id = shixun.id + repository.myshixun_id = myshixun.id repository.type = 'Repository::Gitlab' repository.url = gshixun.name repository.identifier = gshixun.name repository.project_id = -1 + repository.shixun_id = -2 repository = repository.save end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index df6e66c4c..a3882f1ee 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -40,6 +40,8 @@ module ApplicationHelper def git_repository_url project, type if type == "Shixun" rep_identify = Repository.where(:shixun_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) + eleif type == "Myshixun" + rep_identify = Repository.where(:my_shixun_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) else rep_identify = Repository.where(:project_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) end diff --git a/app/helpers/games_helper.rb b/app/helpers/games_helper.rb new file mode 100644 index 000000000..2ef8c1f41 --- /dev/null +++ b/app/helpers/games_helper.rb @@ -0,0 +1,2 @@ +module GamesHelper +end diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 000000000..835aee448 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,6 @@ +class Game < ActiveRecord::Base + attr_accessible :description, :myshixun_id, :stage, :subject, :user_id, :status + belongs_to :myshixun,:touch=> true + belongs_to :user + has_many :game_comments +end diff --git a/app/models/myshixun.rb b/app/models/myshixun.rb index 7544c9b89..e20bd5336 100644 --- a/app/models/myshixun.rb +++ b/app/models/myshixun.rb @@ -1,3 +1,13 @@ class Myshixun < ActiveRecord::Base - attr_accessible :name,:description, :script, :is_public, :parent_id, :user_id, :forked_count + attr_accessible :description, :name, :parent_id, :user_id, :gpid, :forked_from, :visits, :is_public + has_many :users, :through => :myshixun_members + has_many :myshixun_members + has_one :repository + has_many :games, :dependent => :destroy, :order => "games.id ASC" + + def owner + User.find(self.user_id) + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/models/myshixun_member.rb b/app/models/myshixun_member.rb new file mode 100644 index 000000000..9d9320d87 --- /dev/null +++ b/app/models/myshixun_member.rb @@ -0,0 +1,5 @@ +class MyshixunMember < ActiveRecord::Base + attr_accessible :myshixun_id, :role, :user_id + belongs_to :myshixun + belongs_to :user +end diff --git a/app/models/repository.rb b/app/models/repository.rb index a4b9f4e01..06c0510ac 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -26,7 +26,7 @@ class Repository < ActiveRecord::Base belongs_to :project belongs_to :shixun - belongs_to :shixun + belongs_to :myshixun has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" has_many :filechanges, :class_name => 'Change', :through => :changesets diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 5719de1dd..07fc2e0b6 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -22,5 +22,7 @@ class Shixun < ActiveRecord::Base def owner User.find(self.user_id) + rescue ActiveRecord::RecordNotFound + render_404 end end diff --git a/app/models/shixun_member.rb b/app/models/shixun_member.rb index 3dd4b9b80..c39c2521c 100644 --- a/app/models/shixun_member.rb +++ b/app/models/shixun_member.rb @@ -1,7 +1,6 @@ class ShixunMember < ActiveRecord::Base - attr_accessible :shixun_member_id, :user_id, :role + attr_accessible :shixun_id, :user_id, :role belongs_to :shixun belongs_to :user has_many :roles ,:through => :shixun_member_roles, :foreign_key => 'role_id' - has_many :shixun_member_roles,:dependent => :destroy end diff --git a/app/models/user.rb b/app/models/user.rb index e5cff7f99..98d6d7b00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -899,6 +899,12 @@ class User < Principal (!member.blank? || User.current.admin?) ? true : false end + # 超级管理员或者role为1 + def manager_of_myshixun?(myshixun) + member = MyshixunMember.where(:user_id => self.id, :myshixun_id => myshixun.id, :role => 1) + (!member.blank? || User.current.admin?) ? true : false + end + def member_of_course?(course) courses.to_a.include?(course) end diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb new file mode 100644 index 000000000..7893cadbe --- /dev/null +++ b/app/views/games/index.html.erb @@ -0,0 +1 @@ +all tpis \ No newline at end of file diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb new file mode 100644 index 000000000..f13a574f2 --- /dev/null +++ b/app/views/games/show.html.erb @@ -0,0 +1,2 @@ +<%= link_to "提交评测", {:controller => 'games', :action => "game_build", :id => @game, :myshixun_id => @myshixun}, :class => "task-display-span bBlue mt10", :onclick => "training_task_submmit();", :remote => true %> +tpi \ No newline at end of file diff --git a/app/views/layouts/base_myshixun.html.erb b/app/views/layouts/base_myshixun.html.erb new file mode 100644 index 000000000..b4f2129a2 --- /dev/null +++ b/app/views/layouts/base_myshixun.html.erb @@ -0,0 +1,53 @@ + + +
+ +