From be17994e7a8ef9731826134571b8c28648802333 Mon Sep 17 00:00:00 2001 From: huang Date: Mon, 26 Jun 2017 14:05:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=8F=E9=AA=8C=E5=80=BC?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/games_controller.rb | 1 + app/helpers/application_helper.rb | 8 ++++++++ app/models/experience.rb | 3 +++ db/migrate/20170626031135_add_experience_to_users.rb | 5 +++++ db/migrate/20170626055814_create_experiences.rb | 12 ++++++++++++ spec/factories/experiences.rb | 9 +++++++++ spec/models/experience_spec.rb | 5 +++++ 7 files changed, 43 insertions(+) create mode 100644 app/models/experience.rb create mode 100644 db/migrate/20170626031135_add_experience_to_users.rb create mode 100644 db/migrate/20170626055814_create_experiences.rb create mode 100644 spec/factories/experiences.rb create mode 100644 spec/models/experience_spec.rb 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