Class: Australium::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/australium/parser.rb

Class Method Summary collapse

Class Method Details

.parse(lines) ⇒ Array<Game>

Parses a full TF2 log.

Parameters:

  • lines (Array<String>)

    the lines to parse

Returns:

  • (Array<Game>)

    the game(s) data



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/australium/parser.rb', line 10

def self.parse(lines)
  games = [] ; state = GameState.new ; events = []

  lines.each_with_index do |line, index|
    event = parse_line(line, state)

    if event.is_a?(MapLoad) || index == lines.count - 1
      games << Game.new(events) unless events.empty?
      events = []
      event = parse_line(line, GameState.new) # Parse a second time with the correct GameState
    end

    unless event.nil?
      events << event
      state = event.state.deep_clone
    end
  end

  games
end

.parse_line(line, state = nil) ⇒ Event, NilClass

Parses a single line of TF2 log in the context of a game (if a GameState is passed).

Parameters:

  • line (String)

    the line to be parsed

  • the (GameState)

    GameState containing the game context.

Returns:

  • (Event, NilClass)

    event if an event has been recognized; nil otherwise.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/australium/parser.rb', line 35

def self.parse_line(line, state = nil)
  Event::event_classes.each do |event_class|
    if event_class::LOG_REGEX =~ line
      # Get timestamp data & timestamp GameState if we are being stateful
      timestamp = DateTime.strptime(Event::TIMESTAMP_REGEX.match(line)[0], 'L %m/%d/%Y - %H:%M:%S')
      state.timestamp = timestamp unless state.nil?

      # Get the meat of the event data
      data = event_class::LOG_REGEX.match(line).to_h

      # Get supplemental property data, if any exists
      property_data = line.scan(Event::PROPERTY_REGEX).map { |x| [x.first.to_sym, x.last] }.to_h
      data.merge!(property_data)

      # Add other useful data
      data[:state] = state
      data[:raw] = line
      data[:timestamp] = timestamp

      # Construct and return the new Event
      return event_class.new(data)
    end
  end
  nil
end