Class: Rubygoal::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygoal/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coach_home, coach_away) ⇒ Game



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#ballObject (readonly)

Returns the value of attribute ball.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def ball
  @ball
end

#coach_awayObject (readonly)

Returns the value of attribute coach_away.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def coach_away
  @coach_away
end

#coach_homeObject (readonly)

Returns the value of attribute coach_home.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def coach_home
  @coach_home
end

#goalObject (readonly)

Returns the value of attribute goal.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def goal
  @goal
end

#recorderObject (readonly)

Returns the value of attribute recorder.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def recorder
  @recorder
end

#score_awayObject

Returns the value of attribute score_away.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def score_away
  @score_away
end

#score_homeObject

Returns the value of attribute score_home.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def score_home
  @score_home
end

#team_awayObject (readonly)

Returns the value of attribute team_away.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def team_away
  @team_away
end

#team_homeObject (readonly)

Returns the value of attribute team_home.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def team_home
  @team_home
end

#timeObject

Returns the value of attribute time.



10
11
12
# File 'lib/rubygoal/game.rb', line 10

def time
  @time
end

Instance Method Details

#away_players_positionsObject



73
74
75
# File 'lib/rubygoal/game.rb', line 73

def away_players_positions
  team_away.players_position
end

#ball_positionObject



77
78
79
# File 'lib/rubygoal/game.rb', line 77

def ball_position
  ball.position
end

#celebrating_goal?Boolean



65
66
67
# File 'lib/rubygoal/game.rb', line 65

def celebrating_goal?
  goal.celebrating?
end

#ended?Boolean



85
86
87
# File 'lib/rubygoal/game.rb', line 85

def ended?
  state == :ended
end

#home_players_positionsObject



69
70
71
# File 'lib/rubygoal/game.rb', line 69

def home_players_positions
  team_home.players_position
end

#playersObject



61
62
63
# File 'lib/rubygoal/game.rb', line 61

def players
  teams.map(&:players_list).flatten
end

#recorded_gameObject



81
82
83
# File 'lib/rubygoal/game.rb', line 81

def recorded_game
  recorder.to_hash if record_game?
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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