Class: L4D2Stats

Inherits:
GameStats show all
Includes:
AbstractL4DStats
Defined in:
lib/steam/community/l4d/l4d2_stats.rb

Overview

This class represents the game statistics for a single user in Left4Dead 2

Author:

  • Sebastian Staudt

Constant Summary collapse

SPECIAL_INFECTED =

The names of the special infected in Left4Dead 2

SPECIAL_INFECTED + %w{charger jockey spitter}

Instance Attribute Summary collapse

Attributes included from AbstractL4DStats

#most_recent_game

Attributes inherited from GameStats

#game, #hours_played, #privacy_state, #user

Instance Method Summary collapse

Methods included from AbstractL4DStats

#favorites, #teamplay_stats, #versus_stats

Methods inherited from GameStats

#achievements, #achievements_done, #achievements_percentage, base_url, #base_url, create_game_stats, #public?

Methods included from XMLData

#parse

Constructor Details

#initialize(steam_id, fetch = true, bypass_cache = false) ⇒ L4D2Stats

Creates a ‘L4D2Stats` object by calling the super constructor with the game name `’l4d2’‘

Parameters:

  • steam_id (String, Fixnum)

    The custom URL or 64bit Steam ID of the user

  • fetch (Boolean)

    if ‘true` the object’s data is fetched after creation

  • bypass_cache (Boolean)

    if ‘true` the object’s data is fetched again even if it has been cached already



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 34

def initialize(steam_id)
  super steam_id, 'l4d2'

  weapons_data = @xml_data['stats']['weapons']

  @damage_percentages = {
    :melee    => weapons_data['meleepctdmg'].to_f,
    :pistols  => weapons_data['pistolspctdmg'].to_f,
    :rifles   => weapons_data['bulletspctdmg'].to_f,
    :shotguns => weapons_data['shellspctdmg'].to_f
  }
end

Instance Attribute Details

#damage_percentagesHash<Symbol, Float> (readonly)

Returns the percentage of damage done by this player with each weapon type

Available weapon types are ‘:melee`, `:pistols`, `:rifles` and `:shotguns`.

Returns:

  • (Hash<Symbol, Float>)

    The percentages of damage done with each weapon type



26
27
28
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 26

def damage_percentages
  @damage_percentages
end

Instance Method Details

#lifetime_statsHash<String, Object>

Returns a hash of lifetime statistics for this user like the time played

If the lifetime statistics haven’t been parsed already, parsing is done now.

There are only a few additional lifetime statistics for Left4Dead 2 which are not generated for Left4Dead, so this calls AbstractL4DStats#lifetime_stats first and adds some additional stats.

Returns:

  • (Hash<String, Object>)

    The lifetime statistics of the player in Left4Dead 2



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 58

def lifetime_stats
  return unless public?

  if @lifetime_stats.nil?
    super

    lifetime_data = @xml_data['stats']['lifetime']
    @lifetime_stats.merge!({
      :avg_adrenaline_shared   => lifetime_data['adrenalineshared'].to_f,
      :avg_adrenaline_used     => lifetime_data['adrenalineused'].to_f,
      :avg_defibrillators_used => lifetime_data['defibrillatorsused'].to_f
    })
  end

  @lifetime_stats
end

#scavenge_statsHash<String, Object>

Returns a hash of Scavenge statistics for this user like the number of Scavenge rounds played

If the Scavenge statistics haven’t been parsed already, parsing is done now.

Returns:

  • (Hash<String, Object>)

    The Scavenge statistics of the player



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 82

def scavenge_stats
  return unless public?

  if @scavenge_stats.nil?
    scavange_data = @xml_data['stats']['scavenge']

    @scavenge_stats = {
      :avg_cans_per_round => scavange_data['avgcansperround'].to_f,
      :perfect_rounds     => scavange_data['perfect16canrounds'].to_i,
      :rounds_lost        => scavange_data['roundslost'].to_i,
      :rounds_played      => scavange_data['roundsplayed'].to_i,
      :rounds_won         => scavange_data['roundswon'].to_i,
      :total_cans         => scavange_data['totalcans'].to_i
    }

    @scavenge_stats[:maps] = {}
    scavange_data['mapstats']['map'].each do |map_data|
      map_stats = {
        :avg_round_score     => map_data['avgscoreperround'].to_i,
        :highest_game_score  => map_data['highgamescore'].to_i,
        :highest_round_score => map_data['highroundscore'].to_i,
        :name                => map_data['fullname'],
        :rounds_played       => map_data['roundsplayed'].to_i,
        :rounds_won          => map_data['roundswon'].to_i
      }
      @scavenge_stats[:maps][map_data['name']] = map_stats

    end

    @scavenge_stats[:infected] = {}
    scavange_data['infectedstats']['special'].each do |infected_data|
      infected_stats = {
        :max_damage_per_life   => infected_data['maxdmg1life'].to_i,
        :max_pours_interrupted => infected_data['maxpoursinterrupted'].to_i,
        :special_attacks       => infected_data['specialattacks'].to_i
      }
      @scavenge_stats[:infected][infected_data['name']] = infected_stats
    end
  end

  @scavenge_stats
end

#survival_statsHash<String, Object>

Returns a hash of Survival statistics for this user like revived teammates

If the Survival statistics haven’t been parsed already, parsing is done now.

The XML layout for the Survival statistics for Left4Dead 2 differs a bit from Left4Dead’s Survival statistics. So we have to use a different way of parsing for the maps and we use a different map class (‘L4D2Map`) which holds the additional information provided in Left4Dead 2’s statistics.

Returns:

  • (Hash<String, Object>)

    The Survival statistics of the player



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 137

def survival_stats
  return unless public?

  if @survival_stats.nil?
    super
    @survival_stats[:maps] = {}
    @xml_data['stats']['survival']['maps']['map'].each do |map_data|
      map = L4D2Map.new(map_data)
      @survival_stats[:maps][map.id] = map
    end
  end

  @survival_stats
end

#weapon_statsHash<String, Object>

Returns a hash of ‘L4D2Weapon` for this user containing all Left4Dead 2 weapons

If the weapons haven’t been parsed already, parsing is done now.

Returns:

  • (Hash<String, Object>)

    The weapon statistics for this player



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/steam/community/l4d/l4d2_stats.rb', line 158

def weapon_stats
  return unless public?

  if @weapon_stats.nil?
    @weapon_stats = {}
    @xml_data['stats']['weapons'].each do |weapon_data|
      next if weapon_data.nil?

      unless %w{bilejars molotov pipes}.include? weapon_data[0]
        weapon = L4D2Weapon.new *weapon_data
      else
        weapon = L4DExplosive.new *weapon_data
      end

      @weapon_stats[weapon_data[0]] = weapon
    end
  end

  @weapon_stats
end