class Rubygoal::Game

Attributes

ball[R]
coach_away[R]
coach_home[R]
elapsed_time[RW]
goal[R]
last_time[RW]
recorder[R]
score_away[RW]
score_home[RW]
state[RW]
team_away[R]
team_home[R]
time[RW]

Public Class Methods

new(coach_home, coach_away) click to toggle source
# File lib/rubygoal/game.rb, line 15
def initialize(coach_home, coach_away)
  @coach_home = coach_home
  @coach_away = coach_away

  if debug_output?
    puts "Home coach: #{@coach_home.name}"
    puts "Away coach: #{@coach_away.name}"
  end

  @recorder = Recorder.new(self) if record_game?

  @ball = Ball.new

  @team_home = HomeTeam.new(self, coach_home)
  @team_away = AwayTeam.new(self, coach_away)

  @goal = Goal.new

  @state = :playing

  @time = Rubygoal.configuration.game_time
  @score_home = 0
  @score_away = 0

  reinitialize_players
end

Public Instance Methods

away_players_positions() click to toggle source
# File lib/rubygoal/game.rb, line 73
def away_players_positions
  team_away.players_position
end
ball_position() click to toggle source
# File lib/rubygoal/game.rb, line 77
def ball_position
  ball.position
end
celebrating_goal?() click to toggle source
# File lib/rubygoal/game.rb, line 65
def celebrating_goal?
  goal.celebrating?
end
ended?() click to toggle source
# File lib/rubygoal/game.rb, line 85
def ended?
  state == :ended
end
home_players_positions() click to toggle source
# File lib/rubygoal/game.rb, line 69
def home_players_positions
  team_home.players_position
end
players() click to toggle source
# File lib/rubygoal/game.rb, line 61
def players
  teams.map(&:players_list).flatten
end
recorded_game() click to toggle source
# File lib/rubygoal/game.rb, line 81
def recorded_game
  recorder.to_hash if record_game?
end
update() click to toggle source
# File lib/rubygoal/game.rb, line 42
def update
  return if ended?

  update_elapsed_time

  if celebrating_goal?
    update_goal
  else
    update_remaining_time
    team_home.update(elapsed_time)
    team_away.update(elapsed_time)
    update_ball
  end

  recorder.update if record_game?

  end_match! if time <= 0
end

Private Instance Methods

debug_output?() click to toggle source
# File lib/rubygoal/game.rb, line 154
def debug_output?
  Rubygoal.configuration.debug_output
end
end_match!() click to toggle source
# File lib/rubygoal/game.rb, line 145
def end_match!
  self.state = :ended
  puts_score if debug_output?
end
puts_score() click to toggle source
# File lib/rubygoal/game.rb, line 150
def puts_score
  puts "#{coach_home.name} #{score_home} - #{score_away} #{coach_away.name}"
end
record_game?() click to toggle source
# File lib/rubygoal/game.rb, line 158
def record_game?
  Rubygoal.configuration.record_game
end
reinitialize_ball() click to toggle source
# File lib/rubygoal/game.rb, line 137
def reinitialize_ball
  ball.position = Field.center_position
end
reinitialize_match() click to toggle source
# File lib/rubygoal/game.rb, line 120
def reinitialize_match
  reinitialize_players
  reinitialize_ball
end
reinitialize_players() click to toggle source
# File lib/rubygoal/game.rb, line 133
def reinitialize_players
  teams.each(&:players_to_initial_position)
end
teams() click to toggle source
# File lib/rubygoal/game.rb, line 141
def teams
  [team_home, team_away]
end
update_ball() click to toggle source
# File lib/rubygoal/game.rb, line 107
def update_ball
  ball.update(elapsed_time)
  if ball.goal?
    update_score
    goal.start_celebration
  end
end
update_elapsed_time() click to toggle source
# File lib/rubygoal/game.rb, line 96
def update_elapsed_time
  self.last_time ||= Time.now

  self.elapsed_time = Time.now - last_time
  self.last_time = Time.now
end
update_goal() click to toggle source
# File lib/rubygoal/game.rb, line 115
def update_goal
  goal.update(elapsed_time)
  reinitialize_match unless goal.celebrating?
end
update_remaining_time() click to toggle source
# File lib/rubygoal/game.rb, line 103
def update_remaining_time
  self.time -= elapsed_time
end
update_score() click to toggle source
# File lib/rubygoal/game.rb, line 125
def update_score
  if Field.position_side(ball_position) == :home
    self.score_away += 1
  else
    self.score_home += 1
  end
end