Class: CSstats::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/csstats/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Handler

Public: Initialize file.

options - The Hash options:

:path       - The String of csstats.dat file path (required).
:maxplayers - The Integer of how many players to return (optional).

Returns nothing.



12
13
14
15
16
17
# File 'lib/csstats/handler.rb', line 12

def initialize(options = {})
  @file_path = options[:path]
  @max_players = options[:maxplayers] || 0

  raise CSstats::FileNotExist unless File.exist?(file_path.to_s)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



3
4
5
# File 'lib/csstats/handler.rb', line 3

def file_path
  @file_path
end

#max_playersObject (readonly)

Returns the value of attribute max_players.



3
4
5
# File 'lib/csstats/handler.rb', line 3

def max_players
  @max_players
end

Instance Method Details

#player(id) ⇒ Object

Public: Get the player information of specified id.

id - The Integer of player id.

Returns the Mash of player information.



24
25
26
# File 'lib/csstats/handler.rb', line 24

def player(id)
  players[id - 1]
end

#playersObject



44
45
46
47
48
# File 'lib/csstats/handler.rb', line 44

def players
  @players ||= CSstats::Parser::Players.new(
    file_path, max_players: max_players
  ).parse
end

#players_countObject

Public: Get total players count.

Returns the Integer of player count.



31
32
33
# File 'lib/csstats/handler.rb', line 31

def players_count
  players.count
end

#search_by_name(name) ⇒ Object

Public: Get player by specified name.

name - The String of player name.

Returns the Mash of player information.



40
41
42
# File 'lib/csstats/handler.rb', line 40

def search_by_name(name)
  players.find { |player| player.nick == name }
end