Class: Australium::Parser

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

Class Method Summary collapse

Class Method Details

.game_log_digest(game_log) ⇒ String

Calculates a SHA1 digest of a game_log. Used for identifying unique Games.


25
26
27
# File 'lib/australium/parser.rb', line 25

def self.game_log_digest(game_log)
  Digest::SHA1.hexdigest(game_log.join("\n"))
end

.parse_file(logfile) ⇒ Array<String>

Splits a TF2 logfile into an array of individual lines.


11
12
13
# File 'lib/australium/parser.rb', line 11

def self.parse_file(logfile)
  File.read(logfile).encode('UTF-8', :invalid => :replace, :replace => '').split("\n")
end

.parse_file_log(file_log) ⇒ Array<Array<String>>

Splits a TF2 file log into individual game logs, dumping anything extraneous.


18
19
20
# File 'lib/australium/parser.rb', line 18

def self.parse_file_log(file_log)
  file_log.slice_before(MapLoad::LOG_REGEX).select { |l| l.first =~ MapLoad::LOG_REGEX }
end

.parse_game_log(lines, properties = {}) ⇒ Array<Game>

Parses a log of a full TF2 game.


33
34
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 33

def self.parse_game_log(lines, properties = {})
  state = GameState.new
  state.game_id = game_log_digest(lines)
  events = []

  lines.each_with_index do |line, index|
    event = parse_line(line, index, state)
    unless event.nil?
      properties.each_pair { |key, value| event[key] = value }
      events << event
      state = event.state.clone
    end
  end

  if events.empty?
    nil
  else
    events << GameEnd.new(
      :line_number => events.last.line_number + 1,
      :timestamp => events.last.timestamp,
      :server => events.last.server,
      :game_id => events.last.game_id,
      :state => (events.last.state.clone rescue nil)
    )
    Game.new(events)
  end
end

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

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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/australium/parser.rb', line 66

def self.parse_line(line, line_number, state = nil)
  Event::event_classes.each do |event_class|
    next unless defined?(event_class::LOG_REGEX)
    if event_class::LOG_REGEX =~ line
      # Get timestamp data & timestamp GameState if we are being stateful
      timestamp = Time.strptime(Event::TIMESTAMP_REGEX.match(line)[0], Event::TIMESTAMP_FORMAT)
      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.merge!({
        :line_number => line_number,
        :state => state,
        :raw => line,
        :timestamp => timestamp,
        :game_id => (state.nil? ? nil : state.game_id)
      })

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