class MatchSet
Model for a set in a match
Overview¶ ↑
-
A set belongs to a Match
-
A set has a state:
-
:in_progress
-
:complete
-
-
A set has a scoring kind
-
:eight_game
-
:ten_point
-
:six_game
-
:ten_point
indicate a match tiebreak set. A tiebreak set has
only one game
-
A set has an ordinal.
The first set in the match has ordinal 1
-
A complete set has a winning team
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
Indicate whether the match has a winner
-
Returns : Boolean
# File app/models/match_set.rb, line 50 def completed? team_winner end
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
Get the last game of the set
-
Returns : Game
# File app/models/match_set.rb, line 98 def last_game set_games.last end
Indicate whether a team can win the set if the team wins one more game
-
Args :
-
team
-> Team
-
-
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
Clear cached scores. For internal use
# File app/models/match_set.rb, line 135 def score_changed @games_won_by_team = nil end
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
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
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
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