diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 49bd0da..8eabe2a 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -16,7 +16,7 @@ class QuestionsController < ApplicationController end - def result + def score not_found if result_node.blank? or current_player.answers.count < questions.size @@ -31,8 +31,19 @@ class QuestionsController < ApplicationController end end - people_score = questions.count - planet_score - score_diff = people_score - planet_score + current_player.update(score: planet_score) + + redirect_to url_for(action: 'result') + + end + + + + def result + not_found if result_node.blank? or current_player.score.blank? + + people_score = questions.count - current_player.score + score_diff = people_score - current_player.score attachment_index = case when score_diff >= 2 @@ -43,13 +54,6 @@ class QuestionsController < ApplicationController 2 # Balanced end - - - # logger.info(">>> score_diff #{score_diff}") - # logger.info(">>> people_score #{people_score}") - # logger.info(">>> planet_score #{planet_score}") - # logger.info(">>> attachment_index #{attachment_index}") - @result_attachment = result_node.attachments.offset(attachment_index).first @stats_attachment = result_node.attachments.offset(3).first diff --git a/app/models/player.rb b/app/models/player.rb index fdc65ff..ffd732f 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -11,25 +11,18 @@ class Player < ApplicationRecord def stats - total_with_five = Player.where("array_length(answer_cache, 1) = 5") - .where.not(id: self.id) - .count - - matching_answers = Player.where(answer_cache: self.answer_cache) - .where("array_length(answer_cache, 1) = 5") - .where.not(id: self.id) - .count + total_players_with_score = Player.where.not(score: nil).count + n_players_with_matching_score = Player.where(score: self.score).count # Calculate percentage - if total_with_five > 0 - percentage = (matching_answers.to_f / total_with_five) * 100 + if total_players_with_score > 0 + percentage = (n_players_with_matching_score.to_f / total_players_with_score) * 100 percentage_string = percentage.round.to_s else percentage_string = "0" end percentage_string - end end diff --git a/app/views/questions/answer.html.erb b/app/views/questions/answer.html.erb index ff5098d..556446c 100644 --- a/app/views/questions/answer.html.erb +++ b/app/views/questions/answer.html.erb @@ -24,7 +24,8 @@ <% if question == questions.last %> - <%= link_to t('see_results'), url_for(controller: 'questions', action: 'result'), class: 'button__base' %> + <%= button_to t('see_results'), url_for(controller: 'questions', action: 'score'), class: 'button__base' %> + <%# link_to t('see_results'), url_for(controller: 'questions', action: 'result') %> <% else %> <%= link_to t('next_question'), url_for(controller: 'questions', action: 'show', id: question_index.next), class: 'button__base' %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 5ea2183..84ef6c3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,6 +60,7 @@ Rails.application.routes.draw do get 'q/:id/answer', to: 'questions#answer' get 'q/:id', to: 'questions#show' + post 'score', to: 'questions#score' get 'result', to: 'questions#result' diff --git a/db/migrate/20250522111116_add_score_to_players.rb b/db/migrate/20250522111116_add_score_to_players.rb new file mode 100644 index 0000000..667c667 --- /dev/null +++ b/db/migrate/20250522111116_add_score_to_players.rb @@ -0,0 +1,6 @@ +class AddScoreToPlayers < ActiveRecord::Migration[8.0] + def change + add_column :players, :score, :integer, default: nil + add_index :players, :score + end +end diff --git a/db/schema.rb b/db/schema.rb index a630c3f..2ddfeb7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_04_29_121602) do +ActiveRecord::Schema[8.0].define(version: 2025_05_22_111116) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -120,7 +120,9 @@ ActiveRecord::Schema[8.0].define(version: 2025_04_29_121602) do t.integer "answer_cache", default: [], array: true t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "score" t.index ["answer_cache"], name: "index_players_on_answer_cache", using: :gin + t.index ["score"], name: "index_players_on_score" end create_table "users", force: :cascade do |t|