-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save team stats to database for queries/views
- Loading branch information
Showing
21 changed files
with
397 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
db/migrate/20180616130625_add_stats_to_teams_for_query_speed.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.