class MatchPlayer

Class to help “play” a match. This class is used to generate seed data and test data.

Constants

FIRST_OPPONENT_WIN

Indicate that the first opponent is to win

SECOND_OPPONENT_WIN

Indicate that the second opponent is to win

Public Class Methods

convert_scores(scores) click to toggle source

Convert an array of numeric scores into an array of character scores. This method is convenient for creating scores when the exact order of wins and losses is not important. Pass the result of this method to play or ::play

  • Args :

    • scores -> Array

      • Array of numeric pairs within an array of sets

  • Returns : Array

    • Array of characters within an array of sets

Scores Example

[[6, 2],[3,2]]

This array of numeric scores result in the following array of character scores:

[['w', 'l', 'w', 'l', 'w', 'w', 'w', 'w'], ['w', 'l', 'w', 'l', 'w']]
# File lib/match_player.rb, line 32
def self.convert_scores(scores)
  scores.map { |pair| convert_set_score(pair[0], pair[1]) }
end
new(match) click to toggle source
# File lib/match_player.rb, line 38
def initialize(match)
  @match = match
end
play(match, scores) click to toggle source

Class method to play a match. Calls play

  • Args :

    • match -> Match

    • scores -> Array

      • Array of characters within an array of sets

# File lib/match_player.rb, line 11
def self.play(match, scores)
  MatchPlayer.new(match).play(scores)
end

Public Instance Methods

play(scores) click to toggle source

Play a match

  • Args :

    • scores -> array of set scores

      • Array of characters within an array of sets

Scores Example

[['l', 'w', 'w', 'w', 'w', 'w', 'w'], ['w', 'l', 'w', 'l', 'w']]

This array indicates that the first opponent is to win the first set 6-1, and be ahead 3-2 in the second.

# File lib/match_player.rb, line 53
def play(scores)
  match.play_match! :start_play
  # apply_first_servers
  scores.each { |v| play_set(v) }
end
start_first_game() click to toggle source

Start the match and start the first game by executing the :start_play and :start_game actions

# File lib/match_player.rb, line 70
def start_first_game
  start_play
  if match.play_match?(:start_game) && no_games?
    if match.doubles
      match.play_match! :start_game, opponent: match.first_team.first_player
    else
      match.play_match! :start_game, opponent: match.first_player
    end
  end
end
start_play() click to toggle source

Start the match by executing the :start_play action

# File lib/match_player.rb, line 61
def start_play
  match.save!
  if match.play_match? :start_play
    match.play_match! :start_play
  end
end
start_set_game() click to toggle source

Start the next set by executing the :start_set action

# File lib/match_player.rb, line 83
def start_set_game
  # Starting set also starts first game
  match.play_match! :start_set
end