Module: FixtureListGenerator

Defined in:
lib/fixture_list_generator.rb,
lib/fixture_list_generator/version.rb

Overview

All code in the gem is namespaced under this module.

Constant Summary collapse

VERSION =

The current version of Fixture List Generator.

"0.1.0"

Class Method Summary collapse

Class Method Details

.build_fixtures_collection(array_of_teams, fixtures) ⇒ Object

builds a skeleton of weekly fixtures and matches to be played



46
47
48
49
50
51
# File 'lib/fixture_list_generator.rb', line 46

def self.build_fixtures_collection(array_of_teams, fixtures)
  fixtures.map.with_index do |week, i|
    week["week"] = i+1
    week["matches"] = Array.new(array_of_teams.length/2) { {:home => nil, :away => nil} }
  end
end

.build_team_matches_for_the_season(array_of_teams) ⇒ Object

builds a list of all possible match combinations



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fixture_list_generator.rb', line 28

def self.build_team_matches_for_the_season(array_of_teams)
  duplicate_array_of_teams = array_of_teams.dup
  matches = []
  while !duplicate_array_of_teams.empty? do
    current_team = duplicate_array_of_teams.pop

    array_of_teams.each do |opponent|
      if opponent != current_team

        match_hash = {:home => current_team, :away => opponent}
        matches.push match_hash
      end
    end
  end
  matches
end

.generate(array_of_teams) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fixture_list_generator.rb', line 5

def FixtureListGenerator.generate array_of_teams
  number_of_fixtures  = (array_of_teams.length-1)*2
  fixtures = Array.new(number_of_fixtures) { {"week" => nil, "matches" => []} }

  build_fixtures_collection(array_of_teams, fixtures)

  matches = build_team_matches_for_the_season(array_of_teams)

  populate_fixtures_from_matches(fixtures, matches)

  fixtures
end

.has_match_been_played(fixtures, current_match) ⇒ Object

determines whether the match combination has already been played this season



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fixture_list_generator.rb', line 72

def self.has_match_been_played(fixtures, current_match)
  match_has_been_played = false
  fixtures.each do | fixture |
    fixture["matches"].each do |match|

      if match == current_match
        puts "#{match == current_match}"
        match_has_been_played = true
      end
    end
  end

  match_has_been_played
end

.has_team_played_this_week(matches, current_match) ⇒ Object

determines whether a team in a match has already played this week



88
89
90
91
92
93
94
95
96
97
# File 'lib/fixture_list_generator.rb', line 88

def self.has_team_played_this_week(matches, current_match)
  team_has_played = false
  matches.each do |match|
    if match[:home] == current_match[:home] || match[:away] == current_match[:away] || match[:home] == current_match[:away] || match[:away] == current_match[:home]
      team_has_played = true
    end
  end

  team_has_played
end

.populate_fixtures_from_matches(fixtures, matches) ⇒ Object

merges the possible fixtures with possible matches



19
20
21
22
23
24
25
# File 'lib/fixture_list_generator.rb', line 19

def self.populate_fixtures_from_matches(fixtures, matches)
  while !matches.empty? do
    current_match = matches.pop

    set_fixture(current_match, fixtures)
  end
end

.set_fixture(current_match, fixtures) ⇒ Object

inserts a match into a slot in a weekly fixture



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fixture_list_generator.rb', line 54

def self.set_fixture(current_match, fixtures)
  unless has_match_been_played fixtures, current_match
    fixtures.each do |fixture|
      unless has_team_played_this_week fixture["matches"], current_match
        fixture["matches"].each do |match|

          if match[:home].nil?
            match[:home] = current_match[:home]
            match[:away] = current_match[:away]
            return
          end
        end
      end
    end
  end
end