Skip to content

Commit

Permalink
save team stats to database for queries/views
Browse files Browse the repository at this point in the history
  • Loading branch information
wwahammy committed Jun 16, 2018
1 parent 130b16f commit 0e3619d
Show file tree
Hide file tree
Showing 21 changed files with 397 additions and 83 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ group :development, :test do
gem 'pry-rails'
gem 'better_errors'
gem 'binding_of_caller'
gem 'rubocop'
end

group :doc do
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GEM
archive-zip (0.11.0)
io-like (~> 0.3.0)
arel (6.0.4)
ast (2.4.0)
autoprefixer-rails (8.6.2)
execjs
better_errors (2.4.0)
Expand Down Expand Up @@ -93,6 +94,7 @@ GEM
i18n (0.9.5)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
jaro_winkler (1.5.1)
jbuilder (1.5.3)
activesupport (>= 3.0.0)
multi_json (>= 1.2.0)
Expand Down Expand Up @@ -130,7 +132,11 @@ GEM
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
oj (3.6.2)
parallel (1.12.1)
parser (2.5.1.0)
ast (~> 2.4.0)
pg (0.20.0)
powerpack (0.1.2)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand Down Expand Up @@ -187,13 +193,23 @@ GEM
activesupport (= 4.2.10)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rainbow (3.0.0)
raindrops (0.19.0)
rake (12.3.1)
rdoc (6.0.4)
remotipart (1.4.2)
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rubocop (0.57.2)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
ruby_parser (3.11.0)
sexp_processor (~> 4.9)
rubyzip (1.2.1)
Expand Down Expand Up @@ -225,6 +241,7 @@ GEM
thread_safe (~> 0.1)
uglifier (4.1.11)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.4.0)
unicorn (5.4.0)
kgio (~> 2.6)
raindrops (~> 0.7)
Expand Down Expand Up @@ -256,6 +273,7 @@ DEPENDENCIES
rails_12factor
rails_admin
responders (~> 2.0)
rubocop
sass-rails (~> 5.0.0)
sdoc
uglifier (>= 1.3.0)
Expand Down
Binary file removed app/assets/images/bg1.jpg
Binary file not shown.
Binary file removed app/assets/images/bg2.jpg
Binary file not shown.
Binary file removed app/assets/images/bg3.jpg
Binary file not shown.
Binary file removed app/assets/images/bg4.jpg
Binary file not shown.
Binary file removed app/assets/images/download.jpeg
Binary file not shown.
Binary file removed app/assets/images/wc_logo.jpg
Binary file not shown.
3 changes: 1 addition & 2 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class StaticController < ApplicationController

def index
@teams= Team.all
@teams = Team.all
@matches = Match.all
@today_matches = Match.today
@tomorrow_matches = Match.tomorrow
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class TeamsController < ApplicationController
respond_to :json

def index
@teams=Team.all
@teams = Team.all
render 'team_index.json.rabl', callback: params['callback']
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class Group < ActiveRecord::Base
has_many :teams

def ordered_teams
teams.sort_by { |team| [team.team_points, team.goal_differential] }.reverse
teams.order(:team_points).order(:team_goal_differential)
end
end
10 changes: 9 additions & 1 deletion app/models/match.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
class Match < ActiveRecord::Base
validates_presence_of :home_team, :away_team, :datetime, :status

belongs_to :home_team, :class_name => Team, :foreign_key => "home_team_id"
belongs_to :away_team, :class_name => Team, :foreign_key => "away_team_id"
has_many :events

after_save :update_teams

def self.today
where(datetime: Time.now.beginning_of_day..Time.now.end_of_day).order(:datetime)
end

def self.tomorrow
where(datetime: Time.now.advance(days: 1).beginning_of_day..Time.now.advance(days: 1).end_of_day).order(:datetime)
end

private

def update_teams
home_team.save && away_team.save
end
end
104 changes: 65 additions & 39 deletions app/models/team.rb
Original file line number Diff line number Diff line change
@@ -1,99 +1,125 @@
# frozen_string_literal: true

class Team < ActiveRecord::Base
has_many :events
belongs_to :group

has_many :home_matches, :class_name => Match, :foreign_key => "home_team_id" do
before_save :write_iso_code
before_save :write_stats

has_many :home_matches, class_name: Match, foreign_key: 'home_team_id' do
def completed
where("status = ?", 'completed')
where('status = ?', 'completed')
end

def wins
where("status = ? AND home_team_score > away_team_score OR home_team_penalties > away_team_penalties", "completed")
where('status = ? AND home_team_score > away_team_score OR home_team_penalties > away_team_penalties', 'completed')
end

def losses
where("status = ? AND home_team_score < away_team_score OR home_team_penalties < away_team_penalties", "completed")
where('status = ? AND home_team_score < away_team_score OR home_team_penalties < away_team_penalties', 'completed')
end
end

has_many :away_matches, :class_name => Match, :foreign_key => "away_team_id" do
has_many :away_matches, class_name: Match, foreign_key: 'away_team_id' do
def completed
where("status = ?", 'completed')
where('status = ?', 'completed')
end

def wins
where("status = ? AND home_team_score < away_team_score OR home_team_penalties < away_team_penalties", "completed")
where('status = ? AND home_team_score < away_team_score OR home_team_penalties < away_team_penalties', 'completed')
end

def losses
where("status = ? AND home_team_score > away_team_score OR home_team_penalties > away_team_penalties", "completed")
where('status = ? AND home_team_score > away_team_score OR home_team_penalties > away_team_penalties', 'completed')
end
end

has_many :events
belongs_to :group


def matches
Match.where("home_team_id = ? OR away_team_id = ?", self.id, self.id)
Match.where('home_team_id = ? OR away_team_id = ?', id, id)
end

def home_wins
self.home_matches.wins.count
home_matches.wins.count
end

def home_goals_for
self.home_matches.completed.sum(:home_team_score)
home_matches.completed.sum(:home_team_score)
end

def away_goals_for
self.away_matches.completed.sum(:away_team_score)
away_matches.completed.sum(:away_team_score)
end

def home_goals_against
self.home_matches.completed.sum(:away_team_score)
home_matches.completed.sum(:away_team_score)
end

def away_goals_against
self.away_matches.completed.sum(:home_team_score)
away_matches.completed.sum(:home_team_score)
end

def away_wins
self.away_matches.wins.count
away_matches.wins.count
end


def home_losses
self.home_matches.losses.count
home_matches.losses.count
end

def away_losses
self.away_matches.losses.count
away_matches.losses.count
end

def team_draws
self.matches.where("status = ? AND home_team_score = away_team_score AND home_team_penalties IS NULL", "completed").count
def team_draws_count
matches.where('status = ? AND home_team_score = away_team_score AND home_team_penalties IS NULL', 'completed').count
end

def team_wins
self.home_wins + self.away_wins
def team_wins_count
home_wins + away_wins
end

def team_losses
self.home_losses + self.away_losses
def team_losses_count
home_losses + away_losses
end

def team_goals_for
self.home_goals_for + self.away_goals_for
def team_goals_for_count
home_goals_for + away_goals_for
end

def team_goals_against
self.home_goals_against + self.away_goals_against
def team_goals_against_count
home_goals_against + away_goals_against
end

def goal_differential
self.team_goals_for - self.team_goals_against
def goal_differential_count
team_goals_for - team_goals_against
end

def team_points
self.team_wins * 3 + self.team_draws
def team_points_count
(team_wins_count * 3) + team_draws_count
end

def games_played_count
team_wins_count + team_losses_count + team_draws_count
end

private

def write_stats
self.team_wins = team_wins_count
self.team_losses = team_losses_count
self.team_draws = team_draws_count
self.games_played = games_played_count
self.team_points = team_points_count
self.team_goals_for = team_goals_for_count
self.team_goals_against = team_goals_against_count
self.team_goal_differential = goal_differential_count
end

def games_played
self.team_wins + self.team_losses + self.team_draws
def write_iso_code
return if iso_code
json = File.read(Rails.root.join('lib', 'assets', 'country_code.json'))
json = JSON.parse(json)
code = json.select { |_k, v| v.casecmp(country).zero? }
self.iso_code = code.keys.first
end
end
4 changes: 2 additions & 2 deletions app/views/static/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
-# %span{style: "font-size: .7em"}#{match.away_team.country} wins on penalties #{match.away_team_penalties} to #{match.home_team_penalties}
-elsif match.status == "in progress"
.live
-# =image_tag(match.home_team.fifa_code.downcase+".png")
= image_tag("http://www.countryflags.io/#{match.home_team.iso_code}/flat/16.png")
#{match.home_team.country} (#{match.home_team_score}) Vs #{match.away_team.country} (#{match.away_team_score}) (#{match.time})
-# =image_tag(match.away_team.fifa_code.downcase+".png")
= image_tag("http://www.countryflags.io/#{match.away_team.iso_code}/flat/16.png")
-else
#{match.home_team.country} Vs #{match.away_team.country} -- #{(match.datetime).strftime("%H:%M")}

Expand Down
12 changes: 4 additions & 8 deletions app/views/teams/group_results.json.rabl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
collection @groups
attributes :id, :letter
child :ordered_teams do |team|
attributes :id, :country, :fifa_code
node :points do |team|
team.team_points
child :ordered_teams do
attributes :id, :country, :fifa_code
node(:points, &:team_points)
node(:goal_differential, &:team_goal_differential)
end
node :goal_differential do |team|
team.goal_differential
end
end
2 changes: 2 additions & 0 deletions app/views/teams/team_index.json.rabl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

collection @teams, object_root: false
attributes :id, :country, :alternate_name, :fifa_code, :group_id

Expand Down
32 changes: 8 additions & 24 deletions app/views/teams/team_results.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,11 @@ attributes :id, :country, :alternate_name, :fifa_code, :group_id
node :group_letter do |team|
team.group.letter
end
node :wins do |team|
team.team_wins
end
node :draws do |team|
team.team_draws
end
node :losses do |team|
team.team_losses
end
node :games_played do |team|
team.games_played
end
node :points do |team|
team.team_points
end
node :goals_for do |team|
team.team_goals_for
end
node :goals_against do |team|
team.team_goals_against
end
node :goal_differential do |team|
team.goal_differential
end
node :wins, &:team_wins
node :draws, &:team_draws
node :losses, &:team_losses
node :games_played, &:games_played
node :points, &:team_points
node :goals_for, &:team_goals_for
node :goals_against, &:team_goals_against
node :goal_differential, &:team_goal_differential
5 changes: 5 additions & 0 deletions db/migrate/20180616125131_add_iso_code_to_teams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIsoCodeToTeams < ActiveRecord::Migration
def change
add_column :teams, :iso_code, :string
end
end
16 changes: 16 additions & 0 deletions db/migrate/20180616130625_add_stats_to_teams_for_query_speed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AddStatsToTeamsForQuerySpeed < ActiveRecord::Migration
def change
remove_column :teams, :wins, :integer
remove_column :teams, :losses, :integer
remove_column :teams, :goals_for, :integer
remove_column :teams, :goals_against, :integer
add_column :teams, :team_wins, :integer
add_column :teams, :team_losses, :integer
add_column :teams, :team_draws, :integer
add_column :teams, :games_played, :integer
add_column :teams, :team_points, :integer
add_column :teams, :team_goals_for, :integer
add_column :teams, :team_goals_against, :integer
add_column :teams, :team_goal_differential, :integer
end
end
Loading

0 comments on commit 0e3619d

Please sign in to comment.