diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index fdfd0c157..92f9d61cf 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -216,7 +216,10 @@ class ChallengesController < ApplicationController def find_shixun shixun_id = params[:shixun_id] || (params[:challenge] && params[:challenge][:shixun_id]) @shixun = Shixun.find_by_identifier(shixun_id) - render_404 if @shixun.nil? + if @shixun.nil? + render_404 + return + end rescue ActiveRecord::RecordNotFound render_404 end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 3bbfa1705..516468047 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -163,7 +163,7 @@ class GamesController < ApplicationController # 下一关自动开启 if had_done != 1 && @game.status == 2 @game.next_game.update_attribute(:status, 0) - @myshixun.update_attribute(:status => 1) + @myshixun.update_attribute(:status, 1) end test_sets = game_challenge.test_sets unless test_sets.blank? @@ -298,7 +298,12 @@ class GamesController < ApplicationController end def find_game - @game = Game.find(params[:id]) + # myshixun_id = params[:myshixun_id] + @game = Game.find_by_identifier(params[:id]) + if @game.nil? + render_404 + return + end @myshixun = @game.myshixun rescue ActiveRecord::RecordNotFound render_404 diff --git a/app/models/game.rb b/app/models/game.rb index 9bff76d7e..0bed87484 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,7 +1,7 @@ class Game < ActiveRecord::Base # stauts 0: can exe 1:doing 2:successed 3:locked default_scope :order => 'created_at desc' - attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time + attr_accessible :myshixun_id, :user_id, :status, :final_score, :challenge_id, :open_time, :identifier belongs_to :myshixun,:touch=> true belongs_to :user belongs_to :challenge @@ -9,6 +9,12 @@ class Game < ActiveRecord::Base has_many :test_sets, :dependent => :destroy has_many :challenge_samples, :dependent => :destroy + # id 转换成 challenge'position + def to_param + identifier + end + + def last_game challenge = self.challenge last_challenge_id = challenge.last_challenge diff --git a/db/migrate/20170425093133_sync_identifier_to_myshixun.rb b/db/migrate/20170425093133_sync_identifier_to_myshixun.rb deleted file mode 100644 index c8daaf31d..000000000 --- a/db/migrate/20170425093133_sync_identifier_to_myshixun.rb +++ /dev/null @@ -1,9 +0,0 @@ -class SyncIdentifierToMyshixun < ActiveRecord::Migration - def up - myshixuns = Myshixun.all - myshixuns.each do |myshixun| - identifier = myshixun.shixun.try(:identifier) - myshixun.update_attribute(:identifier, identifier) - end - end -end diff --git a/db/migrate/20170426024708_add_identifier_to_games.rb b/db/migrate/20170426024708_add_identifier_to_games.rb new file mode 100644 index 000000000..afe752b22 --- /dev/null +++ b/db/migrate/20170426024708_add_identifier_to_games.rb @@ -0,0 +1,5 @@ +class AddIdentifierToGames < ActiveRecord::Migration + def change + add_column :games, :identifier, :string + end +end diff --git a/db/migrate/20170426024822_sync_game_identifier.rb b/db/migrate/20170426024822_sync_game_identifier.rb new file mode 100644 index 000000000..0590dad3c --- /dev/null +++ b/db/migrate/20170426024822_sync_game_identifier.rb @@ -0,0 +1,14 @@ +class SyncGameIdentifier < ActiveRecord::Migration + CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def up + games = Game.all + games.each do |game| + code = CODES.sample(12).join + code = CODES.sample(12).join if Game.where(identifier: code).present? + game.update_attribute(:identifier, code) + end + end + + def down + end +end diff --git a/db/migrate/20170426060122_sync_identifier_to_myshixun.rb b/db/migrate/20170426060122_sync_identifier_to_myshixun.rb new file mode 100644 index 000000000..eece1f3d8 --- /dev/null +++ b/db/migrate/20170426060122_sync_identifier_to_myshixun.rb @@ -0,0 +1,14 @@ +class SyncIdentifierToMyshixun < ActiveRecord::Migration + CODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def up + myshixuns = Myshixun.all + myshixuns.each do |myshixun| + code = CODES.sample(10).join + code = CODES.sample(10).join if Myshixun.where(identifier: code).present? + myshixun.update_attribute(:identifier, code) + end + end + + def down + end +end