Permalink
Browse files

calculating match counts

  • Loading branch information...
1 parent ff8b07d commit 4ebb1ef9e027d4655e7b87ecf8b77614e565b6ac @aserafin aserafin committed Jan 3, 2014
Showing with 27 additions and 5 deletions.
  1. +19 −1 app/models/player.rb
  2. +8 −4 app/views/welcome/index.html.slim
View
@@ -1,7 +1,9 @@
class Player < ActiveRecord::Base
validates :email, uniqueness: true
- has_many :ratings, class_name: 'PlayerRating'
+ has_many :ratings, class_name: 'PlayerRating', dependent: :destroy
+ has_many :competitors
+ has_many :matches, through: :competitors
after_create :setup_default_rating
@@ -13,4 +15,20 @@ def avatar_url
def setup_default_rating
self.ratings.create rating_type: 'Ratings::Elo', value: 1000
end
+
+ def count_matches(tournament)
+ matches.where(tournament_id: tournament.id).count
+ end
+
+ def count_wins(tournament)
+ matches.where('tournament_id = ? and winners @> ARRAY[?]', tournament.id, self.id).count
+ end
+
+ def count_losts(tournament)
+ matches.where('tournament_id = ? and not (winners @> ARRAY[?])', tournament.id, self.id).count
+ end
+
+ def count_draws(tournament)
+ matches.where('tournament_id = ? and array_length(winners, 1) = 0', tournament.id).count
+ end
end
@@ -49,10 +49,14 @@ section.table.standings
td.change
em.icon
- th.matches 82
- th.wins 15
- th.losts 27
- th.draws 1
+ th.matches
+ = rating.player.count_matches(Tournament.first)
+ th.wins
+ = rating.player.count_wins(Tournament.first)
+ th.losts
+ = rating.player.count_losts(Tournament.first)
+ th.draws
+ = rating.player.count_draws(Tournament.first)
td.rating= rating.value

0 comments on commit 4ebb1ef

Please sign in to comment.