diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 78f665b3d..1457cafcc 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -290,6 +290,7 @@ class GamesController < ApplicationController answer_deduction = challenge.challenge_answers.where("level <= #{@answer.level}").sum(:score) @game.update_attributes!(:answer_open => answer_open, :answer_deduction => answer_deduction) end + GameAnswer.create!(challenge_answer_id: @answer.id, user_id: current_user.id, game_id: @game.id, view_time: Time.now) rescue Exception => e uid_logger_error("#######金币扣除异常: #{e.message}") @@ -317,6 +318,7 @@ class GamesController < ApplicationController @game.update_attributes!(:answer_open => 1, :answer_deduction => 100) end @challenge_chooses = @challenge.challenge_chooses + GameAnswer.create!(user_id: current_user.id, game_id: @game.id, view_time: Time.now) rescue Exception => e uid_logger_error("#######金币扣除异常: #{e.message}") raise ActiveRecord::Rollback diff --git a/app/helpers/myshixuns_helper.rb b/app/helpers/myshixuns_helper.rb index 2a487e361..e643555e1 100644 --- a/app/helpers/myshixuns_helper.rb +++ b/app/helpers/myshixuns_helper.rb @@ -11,4 +11,8 @@ module MyshixunsHelper nil end end + + def view_answer_time game, user_id + game.game_answers.where(user_id: user_id).last&.view_time + end end diff --git a/app/models/challenge_answer.rb b/app/models/challenge_answer.rb index 93b11be66..0ce757d71 100644 --- a/app/models/challenge_answer.rb +++ b/app/models/challenge_answer.rb @@ -1,4 +1,9 @@ class ChallengeAnswer < ApplicationRecord default_scope { order("challenge_answers.level asc") } belongs_to :challenge + has_many :game_answers, :dependent => :destroy + + def view_answer_time(user_id) + game_answers.where(user_id: user_id).last&.view_time + end end diff --git a/app/models/game.rb b/app/models/game.rb index d7c68dd5f..7aa2e7264 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -15,7 +15,7 @@ class Game < ApplicationRecord belongs_to :user belongs_to :challenge has_one :run_code_message, :dependent => :destroy - + has_many :game_answers, :dependent => :destroy #全部关卡数 scope :ch_games, lambda { |challenge_id| where(challenge_id:challenge_id) } diff --git a/app/models/game_answer.rb b/app/models/game_answer.rb new file mode 100644 index 000000000..c355b0a6c --- /dev/null +++ b/app/models/game_answer.rb @@ -0,0 +1,5 @@ +class GameAnswer < ApplicationRecord + belongs_to :user + belongs_to :game + belongs_to :challenge_answer +end diff --git a/app/views/games/get_answer_info.json.jbuilder b/app/views/games/get_answer_info.json.jbuilder index 0f150d500..29d88933c 100644 --- a/app/views/games/get_answer_info.json.jbuilder +++ b/app/views/games/get_answer_info.json.jbuilder @@ -7,6 +7,7 @@ json.message do json.answer_id answer.id json.answer_name answer.name json.answer_score answer.score + json.view_time answer.view_answer_time(current_user.id) # 高层级不给答案 if @power || @game.answer_open >= index json.answer_contents answer.contents diff --git a/app/views/myshixuns/challenges.json.jbuilder b/app/views/myshixuns/challenges.json.jbuilder index 0be94d9fd..de0864665 100644 --- a/app/views/myshixuns/challenges.json.jbuilder +++ b/app/views/myshixuns/challenges.json.jbuilder @@ -6,4 +6,6 @@ json.array! @games do |game| json.identifier get_game_identifier(@shixun.task_pass, game, @identity) json.get_gold game.user_get_gold_and_experience(@shixun.status, challenge)[0] json.get_experience game.user_get_gold_and_experience(@shixun.status, challenge)[1] + json.view_answer_time view_answer_time(game, user_id) + json.finished_time game.end_time end \ No newline at end of file diff --git a/db/migrate/20191104033349_create_game_answers.rb b/db/migrate/20191104033349_create_game_answers.rb new file mode 100644 index 000000000..211a0c29d --- /dev/null +++ b/db/migrate/20191104033349_create_game_answers.rb @@ -0,0 +1,13 @@ +class CreateGameAnswers < ActiveRecord::Migration[5.2] + def change + create_table :game_answers do |t| + t.references :challenge_answer + t.references :user + t.references :game + t.datetime :view_time + + t.timestamps + end + + end +end diff --git a/db/migrate/20191104055038_add_index_for_game_answers.rb b/db/migrate/20191104055038_add_index_for_game_answers.rb new file mode 100644 index 000000000..7cd59c6ee --- /dev/null +++ b/db/migrate/20191104055038_add_index_for_game_answers.rb @@ -0,0 +1,5 @@ +class AddIndexForGameAnswers < ActiveRecord::Migration[5.2] + def change + add_index :game_answers, [:challenge_answer_id, :user_id], :unique => true + end +end diff --git a/spec/models/game_answer_spec.rb b/spec/models/game_answer_spec.rb new file mode 100644 index 000000000..07e46c052 --- /dev/null +++ b/spec/models/game_answer_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe GameAnswer, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end