Skip to content

Commit

Permalink
👾 now with scores 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiraldo committed Nov 24, 2015
1 parent 2ef5dda commit 07fdcea
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/controllers/general_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ def home
@score_polygonfix = Flag.flags_for_user(current_user.id, "polygonfix")
@score_color = Flag.flags_for_user(current_user.id, "color")
@score_toponym = Flag.flags_for_user(current_user.id, "toponym")
# rankings only happen for logged in users
@rank_geometry = UserScores.rank_for_user_task(current_user.id, "geometry")
@rank_address = UserScores.rank_for_user_task(current_user.id, "address")
@rank_polygonfix = UserScores.rank_for_user_task(current_user.id, "polygonfix")
@rank_color = UserScores.rank_for_user_task(current_user.id, "color")
@rank_toponym = UserScores.rank_for_user_task(current_user.id, "toponym")
else
@score_geometry = Flag.flags_for_session(session, "geometry")
@score_address = Flag.flags_for_session(session, "address")
@score_polygonfix = Flag.flags_for_session(session, "polygonfix")
@score_color = Flag.flags_for_session(session, "color")
@score_toponym = Flag.flags_for_session(session, "toponym")
end

@user_count = User.count

@has_score = false
@has_score = true if @score_geometry > 0 || @score_toponym > 0 || @score_address > 0 || @score_polygonfix > 0
end
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ActiveRecord::Base
:omniauthable, :omniauth_providers => [:google_oauth2, :facebook, :twitter]

has_many :usersessions
has_many :user_scores

# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :provider, :uid, :role
Expand Down
4 changes: 4 additions & 0 deletions app/models/user_rank.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserRank < ActiveRecord::Base
belongs_to :user
attr_accessible :flag_type, :score, :session_id
end
11 changes: 11 additions & 0 deletions app/models/user_scores.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UserScores < ActiveRecord::Base
belongs_to :user
attr_accessible :flag_type, :score, :user_id

def self.rank_for_user_task(user_id, task)
score = UserScores.where("user_id = ? AND flag_type = ?", user_id, task).first
return User.count if score == nil # LAST!
count = UserScores.where("score > ? AND flag_type = ?", score, task).count
return count + 1
end
end
15 changes: 10 additions & 5 deletions app/views/general/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,21 @@
<div id="home-scores">
<h3>Your inspections so far:</h3>
<ul>
<li><%= link_to(image_tag("icon-geometry.svg", :class=>"task-icon"), geometry_path, :class=>"link-task") %> <strong><%= @score_geometry %></strong> <%= "footprint".pluralize(@score_geometry) %> checked
<li><%= link_to(image_tag("icon-geometry.svg", :class=>"task-icon"), geometry_path, :class=>"link-task") %> <strong><%= number_with_delimiter @score_geometry %></strong> <%= "footprint".pluralize(@score_geometry) %> checked
<% if (@rank_geometry != nil) %><br />You're #<%= number_with_delimiter(@rank_geometry) %><% end %>
<br /><%= link_to("Inspect more!", geometry_path, :class=>"link-task") %></li>
<li><%= link_to(image_tag("icon-polygonfix.svg", :class=>"task-icon"), polygonfix_path, :class=>"link-task") %> <strong><%= @score_polygonfix %></strong> <%= "footprint".pluralize(@score_polygonfix) %> fixed
<li><%= link_to(image_tag("icon-polygonfix.svg", :class=>"task-icon"), polygonfix_path, :class=>"link-task") %> <strong><%= number_with_delimiter @score_polygonfix %></strong> <%= "footprint".pluralize(@score_polygonfix) %> fixed
<% if (@rank_polygonfix != nil) %><br />You're #<%= number_with_delimiter(@rank_polygonfix) %><% end %>
<br /><%= link_to("Inspect more!", polygonfix_path, :class=>"link-task") %></li>
<li><%= link_to(image_tag("icon-address.svg", :class=>"task-icon"), address_path, :class=>"link-task") %> <strong><%= @score_address %></strong> <%= "building".pluralize(@score_address) %> addressed
<li><%= link_to(image_tag("icon-address.svg", :class=>"task-icon"), address_path, :class=>"link-task") %> <strong><%= number_with_delimiter @score_address %></strong> <%= "building".pluralize(@score_address) %> addressed
<% if (@rank_address != nil) %><br />You're #<%= number_with_delimiter(@rank_address) %><% end %>
<br /><%= link_to("Inspect more!", address_path, :class=>"link-task") %></li>
<li><%= link_to(image_tag("icon-color.svg", :class=>"task-icon"), color_path, :class=>"link-task") %> <strong><%= @score_color %></strong> color <%= "ID".pluralize(@score_color) %>
<li><%= link_to(image_tag("icon-color.svg", :class=>"task-icon"), color_path, :class=>"link-task") %> <strong><%= number_with_delimiter @score_color %></strong> color <%= "ID".pluralize(@score_color) %>
<% if (@rank_color != nil) %><br />You're #<%= number_with_delimiter(@rank_color) %><% end %>
<br /><%= link_to("Inspect more!", color_path, :class=>"link-task") %></li>
<li>&nbsp;</li>
<li><%= link_to(image_tag("icon-toponym.svg", :class=>"task-icon"), toponym_path, :class=>"link-task") %> <strong><%= @score_toponym %></strong> place <%= "name".pluralize(@score_toponym) %>
<li><%= link_to(image_tag("icon-toponym.svg", :class=>"task-icon"), toponym_path, :class=>"link-task") %> <strong><%= number_with_delimiter @score_toponym %></strong> place <%= "name".pluralize(@score_toponym) %>
<% if (@rank_toponym != nil) %><br />You're #<%= number_with_delimiter(@rank_toponym) %><% end %>
<br /><%= link_to("Inspect more!", toponym_path, :class=>"link-task") %></li>
</ul>
</div>
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20151124214518_create_user_scores.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUserScores < ActiveRecord::Migration
def change
create_table :user_scores do |t|
t.integer :user_id
t.string :flag_type
t.integer :score

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20151124214735_add_userscore_task_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddUserscoreTaskIndex < ActiveRecord::Migration
def up
add_index(:user_scores,[:user_id, :flag_type],{unique: true, name: "user_task_index"})
end

def down
remove_index(:user_scores,name: "user_task_index")
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20150506213411) do
ActiveRecord::Schema.define(:version => 20151124214735) do

create_table "consensuspolygons", :force => true do |t|
t.string "task"
Expand Down Expand Up @@ -77,6 +77,16 @@

add_index "sheets", ["layer_id"], :name => "index_sheets_on_layer_id"

create_table "user_scores", :force => true do |t|
t.integer "user_id"
t.string "flag_type"
t.integer "score"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "user_scores", ["user_id", "flag_type"], :name => "user_task_index", :unique => true

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
Expand Down
25 changes: 25 additions & 0 deletions lib/tasks/user.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace :user do

desc "Calculate user scores for ranking/leaderboard"
task :calculate_scores => :environment do
# truncate the table
UserScores.connection.execute("TRUNCATE table user_scores")
UserScores.connection.execute(build_score_query("geometry"))
UserScores.connection.execute(build_score_query("color"))
UserScores.connection.execute(build_score_query("address"))
UserScores.connection.execute(build_score_query("polygonfix"))
UserScores.connection.execute(build_score_query("toponym"))
end

def build_score_query(task)
"INSERT INTO user_scores (user_id, flag_type, score, created_at, updated_at)
SELECT U.id AS user_id, C.flag_type AS flag_type, COUNT(*) AS score, current_timestamp AS created_at, current_timestamp AS updated_at
FROM users U
INNER JOIN usersessions S ON S.user_id=U.id
INNER JOIN flags C ON C.session_id=S.session_id
WHERE C.flag_type = '#{task}'
GROUP BY U.id, C.flag_type
"
end

end

0 comments on commit 07fdcea

Please sign in to comment.