class MatchSet

Model for a set in a match

Overview

:ten_point indicate a match tiebreak set. A tiebreak set has only one game

The first set in the match has ordinal 1

Schema Information

Table name: match_sets

id             :integer          not null, primary key
match_id       :integer          not null
ordinal        :integer          not null
created_at     :datetime         not null
updated_at     :datetime         not null
scoring        :string           not null
team_winner_id :integer

Public Instance Methods

completed?() click to toggle source

Indicate whether the match has a winner

  • Returns : Boolean

# File app/models/match_set.rb, line 50
def completed?
  team_winner
end
compute_team_winner() click to toggle source

Compute the winner of the set, if any, based on the games won and the scoring kind

  • Returns : Team or nil

# File app/models/match_set.rb, line 69
def compute_team_winner
  if completed?
    team_winner
  else
    lookup = lookup_games_won
    # pass games won by each team
    calc_winner_team(lookup[match.first_team_id][0],
                     lookup[match.second_team_id][0])
  end
end
last_game() click to toggle source

Get the last game of the set

  • Returns : Game

# File app/models/match_set.rb, line 98
def last_game
  set_games.last
end
near_team_winner?(team) click to toggle source

Indicate whether a team can win the set if the team wins one more game

  • Args :

  • Returns : Boolean

# File app/models/match_set.rb, line 85
def near_team_winner?(team)
  unless completed?
    lookup = lookup_games_won
    first_won = lookup[match.first_team_id][0]
    second_won = lookup[match.second_team_id][0]
    first_won += 1 if team.id == match.first_team_id
    second_won += 1 if team.id == match.second_team_id
    calc_winner_team(first_won, second_won)
  end
end
score_changed() click to toggle source

Clear cached scores. For internal use

# File app/models/match_set.rb, line 135
def score_changed
  @games_won_by_team = nil
end
state() click to toggle source

Get the state of the match

  • Returns : state

    • :in_progress

    • :complete

# File app/models/match_set.rb, line 58
def state
  if completed?
    :complete
  else
    :in_progress
  end
end
tiebreak?() click to toggle source

Indicate if this set is a match tiebreak

  • Returns : Boolean

# File app/models/match_set.rb, line 119
def tiebreak?
  scoring.to_sym == :ten_point
end
tiebreak_game?(game_ordinal) click to toggle source

Indicate whether a particular game is a tiebreak. A game may be a tiebreak because the set is tied (e.g. 6-6), or because the set is a match tiebreak.

  • Args :

    • game_ordinal -> ordinal

  • Returns : Boolean

# File app/models/match_set.rb, line 129
def tiebreak_game?(game_ordinal)
  tiebreak_ordinal = (win_threshold == 1) ? 1 : win_threshold * 2 + 1
  game_ordinal == tiebreak_ordinal
end
win_threshold() click to toggle source

Get the minimum number of games that an opponent must win in order to win the set, based on the scoring kind.

  • Returns : count

# File app/models/match_set.rb, line 106
def win_threshold
  case scoring.to_sym
  when :eight_game
    8
  when :ten_point
    1
  else # :six_game
    6
  end
end