~armagetronad-dev/ladle/ladle-export

« back to all changes in this revision

Viewing changes to lib/ladle/parser.rb

  • Committer: Daniel Lee Harple
  • Date: 2014-04-07 15:42:16 UTC
  • Revision ID: leeharple@gmail.com-20140407154216-u56ptc241m9wuqw9
Record byes and no-shows.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
  
78
78
    # Implements parsing Ladle bracket results
79
79
    module Results
 
80
      BYE_NAME = /^(~bye.*|\s*)$/i
 
81
 
80
82
      # Parses the raw bracket in wiki-text and returns an array of results.
81
83
      # 
82
84
      # Example:
91
93
      #     [...]
92
94
      #   ]      
93
95
      def parse_ladle_results(raw_results)
94
 
        rounds = Hash.new { |h, k| h[k] = Round.new }
95
 
        
96
 
        # Mapping of round_number => [id1, id2, ...]
97
 
        byes = Hash.new { |h, k| h[k] = [] }
98
 
        
 
96
        rounds = Hash.new { |h, k| h[k] = Round.new }        
99
97
        raw_results.each_line do |line|
100
98
          line.chomp!
101
99
          case line
102
100
          when /RD(\d+)-team0*(\d+)=(.*)/
103
101
            round, id = Integer($1), Integer($2)
104
 
            rounds[round].add_team(id, clean_score($3))
 
102
            team = clean_score($3)
 
103
            if team =~ BYE_NAME
 
104
              team = nil
 
105
            end
 
106
            rounds[round].add_team(id, team)
105
107
          when /RD(\d+)-score0*(\d+)=(.*)/
106
108
            round, id = Integer($1), Integer($2)
107
109
            score = Integer(clean_score($3)) rescue nil
108
 
            if score
109
 
              rounds[round].add_score(id, score)
110
 
            else
111
 
              byes[round] << id
112
 
            end
 
110
            rounds[round].add_score(id, score)
113
111
          end
114
112
        end
115
 
        byes.each do |round, ids|
116
 
          ids.each { |id| rounds[round].remove_id(id) }
117
 
        end
118
113
        sorted_rounds = rounds.to_a.sort_by { |(round_num, round)| round_num }
119
114
        return sorted_rounds.map { |(round_num, round)| round.to_a }
120
115
      end