Class: QuakeLogParser::LineHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/quake-log-parser/line_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeLineHandler

Returns a new instance of LineHandler.



5
6
7
# File 'lib/quake-log-parser/line_handler.rb', line 5

def initialize
  @games = []
end

Instance Method Details

#handle_new_game(_line) ⇒ Object



9
10
11
12
13
# File 'lib/quake-log-parser/line_handler.rb', line 9

def handle_new_game(_line)
  new_id = @games.size + 1
  @games << QuakeLogParser::Game.new(id: new_id)
  QuakeLogParser::Logger.log("Game ##{current_game.id}: New game")
end

#handle_new_kill(line) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quake-log-parser/line_handler.rb', line 21

def handle_new_kill(line)
  killer, killed, means_of_death = extract_kill_infos_from(line)

  means_of_death.chomp!

  current_game.add_kill(QuakeLogParser::Kill.new(killer: killer, killed: killed, mean_of_death: means_of_death))

  previous_total_kills = current_game.total_kills
  current_game.add_total_kill

  previous_means_of_death = current_game.means_of_death[means_of_death] || 0
  current_game.add_means_of_death(means_of_death)

  base_log_message = "Game ##{current_game.id}: New kill. Game total_kills was from #{previous_total_kills} to #{current_game.total_kills} | Game total kills by means_of_death #{means_of_death} was from #{previous_means_of_death} to #{current_game.means_of_death[means_of_death]}"

  if killer == '<world>'
    player = current_game.players[killed]
    player_previous_kills = player.kills
    player.subtract_kill
    QuakeLogParser::Logger.log("#{base_log_message} | Player #{player.name} lost a kill because it was killed by <world> with #{means_of_death} | Player #{player.name} total kills was from #{player_previous_kills} to #{player.kills}")
  else
    player = current_game.players[killer]
    player_previous_kills = player.kills
    player.add_kill
    QuakeLogParser::Logger.log("#{base_log_message} | Player #{player.name} won a kill because killed #{killed} with #{means_of_death} - Player #{player.name} total kills was from #{player_previous_kills} to #{player.kills}")
  end
end

#handle_new_player(line) ⇒ Object



15
16
17
18
19
# File 'lib/quake-log-parser/line_handler.rb', line 15

def handle_new_player(line)
  player = Player.new(name: extract_player_name_from(line))
  current_game.add_player(player)
  QuakeLogParser::Logger.log("Game ##{current_game.id}: New player #{player.name}")
end

#resultsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quake-log-parser/line_handler.rb', line 49

def results
  data = {}

  @games.each do |game|
    data["game_#{game.id}"] = {
      total_kills: game.total_kills,
      players: game.players.keys,
      kills: game.kills.map do |kill|
        {
          killer: kill.killer,
          killed: kill.killed,
          mean_of_death: kill.mean_of_death
        }
      end,
      kills_by_players: Hash[
        game.players
            .sort_by { |_player_name, player| player.kills }
            .reverse
            .collect { |_player_name, player| [player.name, player.kills] }
      ],
      kills_by_means: Hash[
        game.means_of_death
            .sort_by { |_mean, count| count }
            .reverse
            .collect { |mean, count| [mean, count] }
      ]
    }
  end

  data
end