添加经验值功能

This commit is contained in:
huang 2017-06-26 14:05:39 +08:00
parent 40765efec5
commit be17994e7a
7 changed files with 43 additions and 0 deletions

View File

@ -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

View File

@ -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

3
app/models/experience.rb Normal file
View File

@ -0,0 +1,3 @@
class Experience < ActiveRecord::Base
attr_accessible :container_id, :container_type, :score, :user_id
end

View File

@ -0,0 +1,5 @@
class AddExperienceToUsers < ActiveRecord::Migration
def change
add_column :users, :experience, :integer, :default => 0
end
end

View File

@ -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

View File

@ -0,0 +1,9 @@
FactoryGirl.define do
factory :experience do
user_id 1
container_id 1
container_type "MyString"
score 1
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Experience, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end