diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 9afeb097d..ba7c0679e 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -214,6 +214,7 @@ class GamesController < ApplicationController game_passed_code(@game.id, game_challenge.try(:path), @myshixun.try(:gpid), 1) if !@game.answer_open && shixun.status > 1 reward_grade(@game.user, @game.id, @game.class, game_challenge.score) + reward_experience(@game.user, @game.id, @game.class, game_challenge.score) end end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 35817f97b..7bafaade3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -128,6 +128,14 @@ module ApplicationHelper end end + def reward_experience(user, container_id, container_type, score) + experience = Experience.where(:user_id => user.id, :container_id => container_id, :container_type => container_type).first + if experience.nil? + user.update_column(:experience, (score.to_i + user.experience.to_i)) + Grade.create!(:user_id => user.id, :container_id => container_id, :container_type => container_type, :score => score) + end + end + # codeMirror语言转换 def language_switch language case language diff --git a/app/models/experience.rb b/app/models/experience.rb new file mode 100644 index 000000000..c229f5f35 --- /dev/null +++ b/app/models/experience.rb @@ -0,0 +1,3 @@ +class Experience < ActiveRecord::Base + attr_accessible :container_id, :container_type, :score, :user_id +end diff --git a/db/migrate/20170626031135_add_experience_to_users.rb b/db/migrate/20170626031135_add_experience_to_users.rb new file mode 100644 index 000000000..10387c15e --- /dev/null +++ b/db/migrate/20170626031135_add_experience_to_users.rb @@ -0,0 +1,5 @@ +class AddExperienceToUsers < ActiveRecord::Migration + def change + add_column :users, :experience, :integer, :default => 0 + end +end diff --git a/db/migrate/20170626055814_create_experiences.rb b/db/migrate/20170626055814_create_experiences.rb new file mode 100644 index 000000000..f681d7da6 --- /dev/null +++ b/db/migrate/20170626055814_create_experiences.rb @@ -0,0 +1,12 @@ +class CreateExperiences < ActiveRecord::Migration + def change + create_table :experiences do |t| + t.integer :user_id + t.integer :container_id + t.string :container_type + t.integer :score + + t.timestamps + end + end +end diff --git a/spec/factories/experiences.rb b/spec/factories/experiences.rb new file mode 100644 index 000000000..0862145bc --- /dev/null +++ b/spec/factories/experiences.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :experience do + user_id 1 +container_id 1 +container_type "MyString" +score 1 + end + +end diff --git a/spec/models/experience_spec.rb b/spec/models/experience_spec.rb new file mode 100644 index 000000000..9cbbaf068 --- /dev/null +++ b/spec/models/experience_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Experience, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end