Mattias Bodlund 7 months ago
parent
commit
2c17522f4b
6 changed files with 30 additions and 23 deletions
  1. +14
    -10
      app/controllers/questions_controller.rb
  2. +4
    -11
      app/models/player.rb
  3. +2
    -1
      app/views/questions/answer.html.erb
  4. +1
    -0
      config/routes.rb
  5. +6
    -0
      db/migrate/20250522111116_add_score_to_players.rb
  6. +3
    -1
      db/schema.rb

+ 14
- 10
app/controllers/questions_controller.rb View File

@ -16,7 +16,7 @@ class QuestionsController < ApplicationController
end end
def result
def score
not_found if result_node.blank? or current_player.answers.count < questions.size not_found if result_node.blank? or current_player.answers.count < questions.size
@ -31,8 +31,19 @@ class QuestionsController < ApplicationController
end end
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 attachment_index = case
when score_diff >= 2 when score_diff >= 2
@ -43,13 +54,6 @@ class QuestionsController < ApplicationController
2 # Balanced 2 # Balanced
end 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 @result_attachment = result_node.attachments.offset(attachment_index).first
@stats_attachment = result_node.attachments.offset(3).first @stats_attachment = result_node.attachments.offset(3).first


+ 4
- 11
app/models/player.rb View File

@ -11,25 +11,18 @@ class Player < ApplicationRecord
def stats 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 # 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 percentage_string = percentage.round.to_s
else else
percentage_string = "0" percentage_string = "0"
end end
percentage_string percentage_string
end end
end end

+ 2
- 1
app/views/questions/answer.html.erb View File

@ -24,7 +24,8 @@
</div> </div>
<% if question == questions.last %> <% 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 %> <% else %>
<%= link_to t('next_question'), url_for(controller: 'questions', action: 'show', id: question_index.next), class: 'button__base' %> <%= link_to t('next_question'), url_for(controller: 'questions', action: 'show', id: question_index.next), class: 'button__base' %>
<% end %> <% end %>


+ 1
- 0
config/routes.rb View File

@ -60,6 +60,7 @@ Rails.application.routes.draw do
get 'q/:id/answer', to: 'questions#answer' get 'q/:id/answer', to: 'questions#answer'
get 'q/:id', to: 'questions#show' get 'q/:id', to: 'questions#show'
post 'score', to: 'questions#score'
get 'result', to: 'questions#result' get 'result', to: 'questions#result'


+ 6
- 0
db/migrate/20250522111116_add_score_to_players.rb View File

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

+ 3
- 1
db/schema.rb View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql" 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.integer "answer_cache", default: [], array: true
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_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 ["answer_cache"], name: "index_players_on_answer_cache", using: :gin
t.index ["score"], name: "index_players_on_score"
end end
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|


Loading…
Cancel
Save